blob: 12c4b2cdab2732c34ad7f41fc1915377fdb70bfc [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();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004745
Nicolas Geoffray39468442014-09-02 15:17:15 +01004746 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004747 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004748 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00004749 LocationSummary::kCallOnSlowPath :
4750 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004751
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004752 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004753 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4754 if (Primitive::IsFloatingPointType(value_type)) {
4755 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004756 } else {
4757 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4758 }
4759
4760 if (needs_write_barrier) {
4761 // Temporary registers for the write barrier.
Roland Levillain16d9f942016-08-25 17:27:56 +01004762 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004763 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004764 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004765}
4766
4767void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4768 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004769 Location array_loc = locations->InAt(0);
4770 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004771 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004772 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004773 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004774 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004775 bool needs_write_barrier =
4776 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004777 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4778 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4779 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004780
4781 switch (value_type) {
4782 case Primitive::kPrimBoolean:
4783 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004784 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4785 Address address = index.IsConstant()
4786 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4787 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4788 if (value.IsRegister()) {
4789 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004790 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004791 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004792 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004793 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004794 break;
4795 }
4796
4797 case Primitive::kPrimShort:
4798 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004799 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4800 Address address = index.IsConstant()
4801 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4802 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4803 if (value.IsRegister()) {
4804 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004805 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004806 DCHECK(value.IsConstant()) << value;
4807 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004808 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004809 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004810 break;
4811 }
4812
4813 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004814 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4815 Address address = index.IsConstant()
4816 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4817 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004818
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004819 if (!value.IsRegister()) {
4820 // Just setting null.
4821 DCHECK(instruction->InputAt(2)->IsNullConstant());
4822 DCHECK(value.IsConstant()) << value;
4823 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004824 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004825 DCHECK(!needs_write_barrier);
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004826 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004827 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004828 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004829
4830 DCHECK(needs_write_barrier);
4831 CpuRegister register_value = value.AsRegister<CpuRegister>();
Roland Levillain16d9f942016-08-25 17:27:56 +01004832 // We cannot use a NearLabel for `done`, as its range may be too
4833 // short when Baker read barriers are enabled.
4834 Label done;
4835 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004836 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01004837 Location temp_loc = locations->GetTemp(0);
4838 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004839 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004840 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4841 codegen_->AddSlowPath(slow_path);
4842 if (instruction->GetValueCanBeNull()) {
4843 __ testl(register_value, register_value);
4844 __ j(kNotEqual, &not_null);
4845 __ movl(address, Immediate(0));
4846 codegen_->MaybeRecordImplicitNullCheck(instruction);
4847 __ jmp(&done);
4848 __ Bind(&not_null);
4849 }
4850
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004851 // Note that when Baker read barriers are enabled, the type
4852 // checks are performed without read barriers. This is fine,
4853 // even in the case where a class object is in the from-space
4854 // after the flip, as a comparison involving such a type would
4855 // not produce a false positive; it may of course produce a
4856 // false negative, in which case we would take the ArraySet
4857 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01004858
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004859 // /* HeapReference<Class> */ temp = array->klass_
4860 __ movl(temp, Address(array, class_offset));
4861 codegen_->MaybeRecordImplicitNullCheck(instruction);
4862 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01004863
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004864 // /* HeapReference<Class> */ temp = temp->component_type_
4865 __ movl(temp, Address(temp, component_offset));
4866 // If heap poisoning is enabled, no need to unpoison `temp`
4867 // nor the object reference in `register_value->klass`, as
4868 // we are comparing two poisoned references.
4869 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01004870
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004871 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4872 __ j(kEqual, &do_put);
4873 // If heap poisoning is enabled, the `temp` reference has
4874 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004875 __ MaybeUnpoisonHeapReference(temp);
4876
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004877 // If heap poisoning is enabled, no need to unpoison the
4878 // heap reference loaded below, as it is only used for a
4879 // comparison with null.
4880 __ cmpl(Address(temp, super_offset), Immediate(0));
4881 __ j(kNotEqual, slow_path->GetEntryLabel());
4882 __ Bind(&do_put);
4883 } else {
4884 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004885 }
4886 }
4887
4888 if (kPoisonHeapReferences) {
4889 __ movl(temp, register_value);
4890 __ PoisonHeapReference(temp);
4891 __ movl(address, temp);
4892 } else {
4893 __ movl(address, register_value);
4894 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00004895 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004896 codegen_->MaybeRecordImplicitNullCheck(instruction);
4897 }
4898
4899 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4900 codegen_->MarkGCCard(
4901 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4902 __ Bind(&done);
4903
4904 if (slow_path != nullptr) {
4905 __ Bind(slow_path->GetExitLabel());
4906 }
4907
4908 break;
4909 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004910
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004911 case Primitive::kPrimInt: {
4912 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4913 Address address = index.IsConstant()
4914 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4915 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4916 if (value.IsRegister()) {
4917 __ movl(address, value.AsRegister<CpuRegister>());
4918 } else {
4919 DCHECK(value.IsConstant()) << value;
4920 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4921 __ movl(address, Immediate(v));
4922 }
4923 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004924 break;
4925 }
4926
4927 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004928 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4929 Address address = index.IsConstant()
4930 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4931 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4932 if (value.IsRegister()) {
4933 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004934 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004935 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004936 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004937 Address address_high = index.IsConstant()
4938 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4939 offset + sizeof(int32_t))
4940 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4941 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004942 }
4943 break;
4944 }
4945
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004946 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004947 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4948 Address address = index.IsConstant()
4949 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4950 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004951 if (value.IsFpuRegister()) {
4952 __ movss(address, value.AsFpuRegister<XmmRegister>());
4953 } else {
4954 DCHECK(value.IsConstant());
4955 int32_t v =
4956 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4957 __ movl(address, Immediate(v));
4958 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004959 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004960 break;
4961 }
4962
4963 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004964 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4965 Address address = index.IsConstant()
4966 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4967 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004968 if (value.IsFpuRegister()) {
4969 __ movsd(address, value.AsFpuRegister<XmmRegister>());
4970 codegen_->MaybeRecordImplicitNullCheck(instruction);
4971 } else {
4972 int64_t v =
4973 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4974 Address address_high = index.IsConstant()
4975 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4976 offset + sizeof(int32_t))
4977 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4978 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
4979 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004980 break;
4981 }
4982
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004983 case Primitive::kPrimVoid:
4984 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004985 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004986 }
4987}
4988
4989void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004990 LocationSummary* locations =
4991 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004992 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04004993 if (!instruction->IsEmittedAtUseSite()) {
4994 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4995 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004996}
4997
4998void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04004999 if (instruction->IsEmittedAtUseSite()) {
5000 return;
5001 }
5002
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005003 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005004 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005005 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
5006 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005007 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005008 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005009}
5010
5011void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005012 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5013 ? LocationSummary::kCallOnSlowPath
5014 : LocationSummary::kNoCall;
5015 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005016 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005017 HInstruction* length = instruction->InputAt(1);
5018 if (!length->IsEmittedAtUseSite()) {
5019 locations->SetInAt(1, Location::RegisterOrConstant(length));
5020 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005021 if (instruction->HasUses()) {
5022 locations->SetOut(Location::SameAsFirstInput());
5023 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005024}
5025
5026void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
5027 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005028 Location index_loc = locations->InAt(0);
5029 Location length_loc = locations->InAt(1);
Mark Mendellee8d9712016-07-12 11:13:15 -04005030 SlowPathCode* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005031
Mark Mendell99dbd682015-04-22 16:18:52 -04005032 if (length_loc.IsConstant()) {
5033 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5034 if (index_loc.IsConstant()) {
5035 // BCE will remove the bounds check if we are guarenteed to pass.
5036 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5037 if (index < 0 || index >= length) {
5038 codegen_->AddSlowPath(slow_path);
5039 __ jmp(slow_path->GetEntryLabel());
5040 } else {
5041 // Some optimization after BCE may have generated this, and we should not
5042 // generate a bounds check if it is a valid range.
5043 }
5044 return;
5045 }
5046
5047 // We have to reverse the jump condition because the length is the constant.
5048 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
5049 __ cmpl(index_reg, Immediate(length));
5050 codegen_->AddSlowPath(slow_path);
5051 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005052 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005053 HInstruction* array_length = instruction->InputAt(1);
5054 if (array_length->IsEmittedAtUseSite()) {
5055 // Address the length field in the array.
5056 DCHECK(array_length->IsArrayLength());
5057 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5058 Location array_loc = array_length->GetLocations()->InAt(0);
5059 Address array_len(array_loc.AsRegister<CpuRegister>(), len_offset);
5060 if (index_loc.IsConstant()) {
5061 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5062 __ cmpl(array_len, Immediate(value));
5063 } else {
5064 __ cmpl(array_len, index_loc.AsRegister<CpuRegister>());
5065 }
5066 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendell99dbd682015-04-22 16:18:52 -04005067 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005068 CpuRegister length = length_loc.AsRegister<CpuRegister>();
5069 if (index_loc.IsConstant()) {
5070 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5071 __ cmpl(length, Immediate(value));
5072 } else {
5073 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
5074 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005075 }
5076 codegen_->AddSlowPath(slow_path);
5077 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005078 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005079}
5080
5081void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
5082 CpuRegister card,
5083 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005084 CpuRegister value,
5085 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005086 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005087 if (value_can_be_null) {
5088 __ testl(value, value);
5089 __ j(kEqual, &is_null);
5090 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005091 __ gs()->movq(card, Address::Absolute(Thread::CardTableOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005092 /* no_rip */ true));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005093 __ movq(temp, object);
5094 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01005095 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005096 if (value_can_be_null) {
5097 __ Bind(&is_null);
5098 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005099}
5100
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005101void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005102 LOG(FATAL) << "Unimplemented";
5103}
5104
5105void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005106 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5107}
5108
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005109void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
5110 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5111}
5112
5113void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005114 HBasicBlock* block = instruction->GetBlock();
5115 if (block->GetLoopInformation() != nullptr) {
5116 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5117 // The back edge will generate the suspend check.
5118 return;
5119 }
5120 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5121 // The goto will generate the suspend check.
5122 return;
5123 }
5124 GenerateSuspendCheck(instruction, nullptr);
5125}
5126
5127void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
5128 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005129 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005130 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
5131 if (slow_path == nullptr) {
5132 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
5133 instruction->SetSlowPath(slow_path);
5134 codegen_->AddSlowPath(slow_path);
5135 if (successor != nullptr) {
5136 DCHECK(successor->IsLoopHeader());
5137 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5138 }
5139 } else {
5140 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5141 }
5142
Andreas Gampe542451c2016-07-26 09:02:02 -07005143 __ gs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005144 /* no_rip */ true),
5145 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005146 if (successor == nullptr) {
5147 __ j(kNotEqual, slow_path->GetEntryLabel());
5148 __ Bind(slow_path->GetReturnLabel());
5149 } else {
5150 __ j(kEqual, codegen_->GetLabelOf(successor));
5151 __ jmp(slow_path->GetEntryLabel());
5152 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005153}
5154
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005155X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
5156 return codegen_->GetAssembler();
5157}
5158
5159void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005160 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005161 Location source = move->GetSource();
5162 Location destination = move->GetDestination();
5163
5164 if (source.IsRegister()) {
5165 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005166 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005167 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005168 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005169 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005170 } else {
5171 DCHECK(destination.IsDoubleStackSlot());
5172 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005173 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005174 }
5175 } else if (source.IsStackSlot()) {
5176 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005177 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005178 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005179 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005180 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005181 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005182 } else {
5183 DCHECK(destination.IsStackSlot());
5184 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5185 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5186 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005187 } else if (source.IsDoubleStackSlot()) {
5188 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005189 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005190 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005191 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005192 __ movsd(destination.AsFpuRegister<XmmRegister>(),
5193 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005194 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01005195 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005196 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
5197 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
5198 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005199 } else if (source.IsConstant()) {
5200 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005201 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5202 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005203 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005204 if (value == 0) {
5205 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
5206 } else {
5207 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
5208 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005209 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005210 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00005211 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005212 }
5213 } else if (constant->IsLongConstant()) {
5214 int64_t value = constant->AsLongConstant()->GetValue();
5215 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04005216 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005217 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005218 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005219 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005220 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005221 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005222 float fp_value = constant->AsFloatConstant()->GetValue();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005223 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005224 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005225 codegen_->Load32BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005226 } else {
5227 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005228 Immediate imm(bit_cast<int32_t, float>(fp_value));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005229 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
5230 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005231 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005232 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005233 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005234 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005235 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005236 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
Mark Mendell7c0b44f2016-02-01 10:08:35 -05005237 codegen_->Load64BitValue(dest, fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005238 } else {
5239 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04005240 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005241 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005242 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005243 } else if (source.IsFpuRegister()) {
5244 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005245 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005246 } else if (destination.IsStackSlot()) {
5247 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005248 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005249 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005250 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005251 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005252 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005253 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005254 }
5255}
5256
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005257void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005258 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005259 __ movl(Address(CpuRegister(RSP), mem), reg);
5260 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005261}
5262
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005263void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005264 ScratchRegisterScope ensure_scratch(
5265 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5266
5267 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5268 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5269 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5270 Address(CpuRegister(RSP), mem2 + stack_offset));
5271 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5272 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5273 CpuRegister(ensure_scratch.GetRegister()));
5274}
5275
Mark Mendell8a1c7282015-06-29 15:41:28 -04005276void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg1, CpuRegister reg2) {
5277 __ movq(CpuRegister(TMP), reg1);
5278 __ movq(reg1, reg2);
5279 __ movq(reg2, CpuRegister(TMP));
5280}
5281
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005282void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5283 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5284 __ movq(Address(CpuRegister(RSP), mem), reg);
5285 __ movq(reg, CpuRegister(TMP));
5286}
5287
5288void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5289 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005290 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005291
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005292 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5293 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5294 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5295 Address(CpuRegister(RSP), mem2 + stack_offset));
5296 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5297 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5298 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005299}
5300
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005301void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5302 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5303 __ movss(Address(CpuRegister(RSP), mem), reg);
5304 __ movd(reg, CpuRegister(TMP));
5305}
5306
5307void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5308 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5309 __ movsd(Address(CpuRegister(RSP), mem), reg);
5310 __ movd(reg, CpuRegister(TMP));
5311}
5312
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005313void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005314 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005315 Location source = move->GetSource();
5316 Location destination = move->GetDestination();
5317
5318 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell8a1c7282015-06-29 15:41:28 -04005319 Exchange64(source.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005320 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005321 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005322 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005323 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005324 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005325 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5326 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005327 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005328 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005329 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005330 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5331 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005332 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005333 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5334 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5335 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005336 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005337 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005338 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005339 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005340 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005341 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005342 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005343 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005344 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005345 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005346 }
5347}
5348
5349
5350void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5351 __ pushq(CpuRegister(reg));
5352}
5353
5354
5355void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5356 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005357}
5358
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005359void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005360 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005361 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5362 Immediate(mirror::Class::kStatusInitialized));
5363 __ j(kLess, slow_path->GetEntryLabel());
5364 __ Bind(slow_path->GetExitLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005365 // No need for memory fence, thanks to the x86-64 memory model.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005366}
5367
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005368HLoadClass::LoadKind CodeGeneratorX86_64::GetSupportedLoadClassKind(
5369 HLoadClass::LoadKind desired_class_load_kind) {
5370 if (kEmitCompilerReadBarrier) {
5371 switch (desired_class_load_kind) {
5372 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5373 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5374 case HLoadClass::LoadKind::kBootImageAddress:
5375 // TODO: Implement for read barrier.
5376 return HLoadClass::LoadKind::kDexCacheViaMethod;
5377 default:
5378 break;
5379 }
5380 }
5381 switch (desired_class_load_kind) {
5382 case HLoadClass::LoadKind::kReferrersClass:
5383 break;
5384 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5385 DCHECK(!GetCompilerOptions().GetCompilePic());
5386 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5387 return HLoadClass::LoadKind::kBootImageLinkTimePcRelative;
5388 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5389 DCHECK(GetCompilerOptions().GetCompilePic());
5390 break;
5391 case HLoadClass::LoadKind::kBootImageAddress:
5392 break;
5393 case HLoadClass::LoadKind::kDexCacheAddress:
5394 DCHECK(Runtime::Current()->UseJitCompilation());
5395 break;
5396 case HLoadClass::LoadKind::kDexCachePcRelative:
5397 DCHECK(!Runtime::Current()->UseJitCompilation());
5398 break;
5399 case HLoadClass::LoadKind::kDexCacheViaMethod:
5400 break;
5401 }
5402 return desired_class_load_kind;
5403}
5404
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005405void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005406 if (cls->NeedsAccessCheck()) {
5407 InvokeRuntimeCallingConvention calling_convention;
5408 CodeGenerator::CreateLoadClassLocationSummary(
5409 cls,
5410 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5411 Location::RegisterLocation(RAX),
5412 /* code_generator_supports_read_barrier */ true);
5413 return;
5414 }
5415
5416 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5417 ? LocationSummary::kCallOnSlowPath
5418 : LocationSummary::kNoCall;
5419 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
5420 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5421 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5422 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5423 locations->SetInAt(0, Location::RequiresRegister());
5424 }
5425 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005426}
5427
5428void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005429 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005430 if (cls->NeedsAccessCheck()) {
5431 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5432 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5433 cls,
5434 cls->GetDexPc(),
5435 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005436 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005437 return;
5438 }
5439
Roland Levillain0d5a2812015-11-13 10:07:31 +00005440 Location out_loc = locations->Out();
5441 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005442
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005443 bool generate_null_check = false;
5444 switch (cls->GetLoadKind()) {
5445 case HLoadClass::LoadKind::kReferrersClass: {
5446 DCHECK(!cls->CanCallRuntime());
5447 DCHECK(!cls->MustGenerateClinitCheck());
5448 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5449 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5450 GenerateGcRootFieldLoad(
5451 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
5452 break;
5453 }
5454 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5455 DCHECK(!kEmitCompilerReadBarrier);
5456 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5457 codegen_->RecordTypePatch(cls);
5458 break;
5459 case HLoadClass::LoadKind::kBootImageAddress: {
5460 DCHECK(!kEmitCompilerReadBarrier);
5461 DCHECK_NE(cls->GetAddress(), 0u);
5462 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5463 __ movl(out, Immediate(address)); // Zero-extended.
5464 codegen_->RecordSimplePatch();
5465 break;
5466 }
5467 case HLoadClass::LoadKind::kDexCacheAddress: {
5468 DCHECK_NE(cls->GetAddress(), 0u);
5469 // /* GcRoot<mirror::Class> */ out = *address
5470 if (IsUint<32>(cls->GetAddress())) {
5471 Address address = Address::Absolute(cls->GetAddress(), /* no_rip */ true);
5472 GenerateGcRootFieldLoad(cls, out_loc, address);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005473 } else {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005474 // TODO: Consider using opcode A1, i.e. movl eax, moff32 (with 64-bit address).
5475 __ movq(out, Immediate(cls->GetAddress()));
5476 GenerateGcRootFieldLoad(cls, out_loc, Address(out, 0));
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005477 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005478 generate_null_check = !cls->IsInDexCache();
5479 break;
5480 }
5481 case HLoadClass::LoadKind::kDexCachePcRelative: {
5482 uint32_t offset = cls->GetDexCacheElementOffset();
5483 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
5484 Address address = Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset,
5485 /* no_rip */ false);
5486 // /* GcRoot<mirror::Class> */ out = *address /* PC-relative */
5487 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label);
5488 generate_null_check = !cls->IsInDexCache();
5489 break;
5490 }
5491 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5492 // /* GcRoot<mirror::Class>[] */ out =
5493 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5494 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
5495 __ movq(out,
5496 Address(current_method,
5497 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
5498 // /* GcRoot<mirror::Class> */ out = out[type_index]
5499 GenerateGcRootFieldLoad(
5500 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
5501 generate_null_check = !cls->IsInDexCache();
5502 break;
5503 }
5504 default:
5505 LOG(FATAL) << "Unexpected load kind: " << cls->GetLoadKind();
5506 UNREACHABLE();
5507 }
5508
5509 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5510 DCHECK(cls->CanCallRuntime());
5511 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
5512 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5513 codegen_->AddSlowPath(slow_path);
5514 if (generate_null_check) {
5515 __ testl(out, out);
5516 __ j(kEqual, slow_path->GetEntryLabel());
5517 }
5518 if (cls->MustGenerateClinitCheck()) {
5519 GenerateClassInitializationCheck(slow_path, out);
5520 } else {
5521 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005522 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005523 }
5524}
5525
5526void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5527 LocationSummary* locations =
5528 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5529 locations->SetInAt(0, Location::RequiresRegister());
5530 if (check->HasUses()) {
5531 locations->SetOut(Location::SameAsFirstInput());
5532 }
5533}
5534
5535void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005536 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005537 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005538 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005539 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005540 GenerateClassInitializationCheck(slow_path,
5541 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005542}
5543
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005544HLoadString::LoadKind CodeGeneratorX86_64::GetSupportedLoadStringKind(
5545 HLoadString::LoadKind desired_string_load_kind) {
5546 if (kEmitCompilerReadBarrier) {
5547 switch (desired_string_load_kind) {
5548 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5549 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5550 case HLoadString::LoadKind::kBootImageAddress:
5551 // TODO: Implement for read barrier.
5552 return HLoadString::LoadKind::kDexCacheViaMethod;
5553 default:
5554 break;
5555 }
5556 }
5557 switch (desired_string_load_kind) {
5558 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5559 DCHECK(!GetCompilerOptions().GetCompilePic());
5560 // We prefer the always-available RIP-relative address for the x86-64 boot image.
5561 return HLoadString::LoadKind::kBootImageLinkTimePcRelative;
5562 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5563 DCHECK(GetCompilerOptions().GetCompilePic());
5564 break;
5565 case HLoadString::LoadKind::kBootImageAddress:
5566 break;
5567 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005568 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005569 break;
5570 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005571 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005572 break;
5573 case HLoadString::LoadKind::kDexCacheViaMethod:
5574 break;
5575 }
5576 return desired_string_load_kind;
5577}
5578
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005579void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005580 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005581 ? LocationSummary::kCallOnSlowPath
5582 : LocationSummary::kNoCall;
5583 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005584 if (load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod) {
5585 locations->SetInAt(0, Location::RequiresRegister());
5586 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005587 locations->SetOut(Location::RequiresRegister());
5588}
5589
5590void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005591 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005592 Location out_loc = locations->Out();
5593 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005594
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005595 switch (load->GetLoadKind()) {
5596 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
5597 DCHECK(!kEmitCompilerReadBarrier);
5598 __ leal(out, Address::Absolute(CodeGeneratorX86_64::kDummy32BitOffset, /* no_rip */ false));
5599 codegen_->RecordStringPatch(load);
5600 return; // No dex cache slow path.
5601 }
5602 case HLoadString::LoadKind::kBootImageAddress: {
5603 DCHECK(!kEmitCompilerReadBarrier);
5604 DCHECK_NE(load->GetAddress(), 0u);
5605 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5606 __ movl(out, Immediate(address)); // Zero-extended.
5607 codegen_->RecordSimplePatch();
5608 return; // No dex cache slow path.
5609 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005610 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005611 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005612 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005613
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005614 // TODO: Re-add the compiler code to do string dex cache lookup again.
5615 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
5616 codegen_->AddSlowPath(slow_path);
5617 __ jmp(slow_path->GetEntryLabel());
5618 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005619}
5620
David Brazdilcb1c0552015-08-04 16:22:25 +01005621static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005622 return Address::Absolute(Thread::ExceptionOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005623 /* no_rip */ true);
David Brazdilcb1c0552015-08-04 16:22:25 +01005624}
5625
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005626void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5627 LocationSummary* locations =
5628 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5629 locations->SetOut(Location::RequiresRegister());
5630}
5631
5632void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005633 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5634}
5635
5636void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5637 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5638}
5639
5640void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5641 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005642}
5643
5644void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5645 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005646 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005647 InvokeRuntimeCallingConvention calling_convention;
5648 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5649}
5650
5651void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005652 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5653 instruction,
5654 instruction->GetDexPc(),
5655 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005656 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005657}
5658
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005659static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5660 return kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00005661 !kUseBakerReadBarrier &&
5662 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005663 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5664 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5665}
5666
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005667void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005668 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005669 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5670 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005671 case TypeCheckKind::kExactCheck:
5672 case TypeCheckKind::kAbstractClassCheck:
5673 case TypeCheckKind::kClassHierarchyCheck:
5674 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005675 call_kind =
5676 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005677 break;
5678 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005679 case TypeCheckKind::kUnresolvedCheck:
5680 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005681 call_kind = LocationSummary::kCallOnSlowPath;
5682 break;
5683 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005684
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005685 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005686 locations->SetInAt(0, Location::RequiresRegister());
5687 locations->SetInAt(1, Location::Any());
5688 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5689 locations->SetOut(Location::RequiresRegister());
5690 // When read barriers are enabled, we need a temporary register for
5691 // some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005692 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005693 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005694 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005695}
5696
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005697void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005698 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005699 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005700 Location obj_loc = locations->InAt(0);
5701 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005702 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005703 Location out_loc = locations->Out();
5704 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005705 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005706 locations->GetTemp(0) :
5707 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005708 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005709 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5710 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5711 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005712 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005713 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005714
5715 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005716 // Avoid null check if we know obj is not null.
5717 if (instruction->MustDoNullCheck()) {
5718 __ testl(obj, obj);
5719 __ j(kEqual, &zero);
5720 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005721
Roland Levillain0d5a2812015-11-13 10:07:31 +00005722 // /* HeapReference<Class> */ out = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005723 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005724
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005725 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005726 case TypeCheckKind::kExactCheck: {
5727 if (cls.IsRegister()) {
5728 __ cmpl(out, cls.AsRegister<CpuRegister>());
5729 } else {
5730 DCHECK(cls.IsStackSlot()) << cls;
5731 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5732 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005733 if (zero.IsLinked()) {
5734 // Classes must be equal for the instanceof to succeed.
5735 __ j(kNotEqual, &zero);
5736 __ movl(out, Immediate(1));
5737 __ jmp(&done);
5738 } else {
5739 __ setcc(kEqual, out);
5740 // setcc only sets the low byte.
5741 __ andl(out, Immediate(1));
5742 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005743 break;
5744 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005745
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005746 case TypeCheckKind::kAbstractClassCheck: {
5747 // If the class is abstract, we eagerly fetch the super class of the
5748 // object to avoid doing a comparison we know will fail.
5749 NearLabel loop, success;
5750 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005751 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005752 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005753 __ testl(out, out);
5754 // If `out` is null, we use it for the result, and jump to `done`.
5755 __ j(kEqual, &done);
5756 if (cls.IsRegister()) {
5757 __ cmpl(out, cls.AsRegister<CpuRegister>());
5758 } else {
5759 DCHECK(cls.IsStackSlot()) << cls;
5760 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5761 }
5762 __ j(kNotEqual, &loop);
5763 __ movl(out, Immediate(1));
5764 if (zero.IsLinked()) {
5765 __ jmp(&done);
5766 }
5767 break;
5768 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005769
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005770 case TypeCheckKind::kClassHierarchyCheck: {
5771 // Walk over the class hierarchy to find a match.
5772 NearLabel loop, success;
5773 __ Bind(&loop);
5774 if (cls.IsRegister()) {
5775 __ cmpl(out, cls.AsRegister<CpuRegister>());
5776 } else {
5777 DCHECK(cls.IsStackSlot()) << cls;
5778 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5779 }
5780 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005781 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005782 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005783 __ testl(out, out);
5784 __ j(kNotEqual, &loop);
5785 // If `out` is null, we use it for the result, and jump to `done`.
5786 __ jmp(&done);
5787 __ Bind(&success);
5788 __ movl(out, Immediate(1));
5789 if (zero.IsLinked()) {
5790 __ jmp(&done);
5791 }
5792 break;
5793 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005794
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005795 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005796 // Do an exact check.
5797 NearLabel exact_check;
5798 if (cls.IsRegister()) {
5799 __ cmpl(out, cls.AsRegister<CpuRegister>());
5800 } else {
5801 DCHECK(cls.IsStackSlot()) << cls;
5802 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5803 }
5804 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005805 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005806 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005807 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005808 __ testl(out, out);
5809 // If `out` is null, we use it for the result, and jump to `done`.
5810 __ j(kEqual, &done);
5811 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5812 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005813 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005814 __ movl(out, Immediate(1));
5815 __ jmp(&done);
5816 break;
5817 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005818
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005819 case TypeCheckKind::kArrayCheck: {
5820 if (cls.IsRegister()) {
5821 __ cmpl(out, cls.AsRegister<CpuRegister>());
5822 } else {
5823 DCHECK(cls.IsStackSlot()) << cls;
5824 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5825 }
5826 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005827 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5828 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005829 codegen_->AddSlowPath(slow_path);
5830 __ j(kNotEqual, slow_path->GetEntryLabel());
5831 __ movl(out, Immediate(1));
5832 if (zero.IsLinked()) {
5833 __ jmp(&done);
5834 }
5835 break;
5836 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005837
Calin Juravle98893e12015-10-02 21:05:03 +01005838 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005839 case TypeCheckKind::kInterfaceCheck: {
5840 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005841 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00005842 // cases.
5843 //
5844 // We cannot directly call the InstanceofNonTrivial runtime
5845 // entry point without resorting to a type checking slow path
5846 // here (i.e. by calling InvokeRuntime directly), as it would
5847 // require to assign fixed registers for the inputs of this
5848 // HInstanceOf instruction (following the runtime calling
5849 // convention), which might be cluttered by the potential first
5850 // read barrier emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005851 //
5852 // TODO: Introduce a new runtime entry point taking the object
5853 // to test (instead of its class) as argument, and let it deal
5854 // with the read barrier issues. This will let us refactor this
5855 // case of the `switch` code as it was previously (with a direct
5856 // call to the runtime not using a type checking slow path).
5857 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005858 DCHECK(locations->OnlyCallsOnSlowPath());
5859 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5860 /* is_fatal */ false);
5861 codegen_->AddSlowPath(slow_path);
5862 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005863 if (zero.IsLinked()) {
5864 __ jmp(&done);
5865 }
5866 break;
5867 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005868 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005869
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005870 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005871 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005872 __ xorl(out, out);
5873 }
5874
5875 if (done.IsLinked()) {
5876 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005877 }
5878
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005879 if (slow_path != nullptr) {
5880 __ Bind(slow_path->GetExitLabel());
5881 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005882}
5883
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005884void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005885 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5886 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005887 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5888 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005889 case TypeCheckKind::kExactCheck:
5890 case TypeCheckKind::kAbstractClassCheck:
5891 case TypeCheckKind::kClassHierarchyCheck:
5892 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005893 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5894 LocationSummary::kCallOnSlowPath :
5895 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005896 break;
5897 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005898 case TypeCheckKind::kUnresolvedCheck:
5899 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005900 call_kind = LocationSummary::kCallOnSlowPath;
5901 break;
5902 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005903 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5904 locations->SetInAt(0, Location::RequiresRegister());
5905 locations->SetInAt(1, Location::Any());
5906 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
5907 locations->AddTemp(Location::RequiresRegister());
5908 // When read barriers are enabled, we need an additional temporary
5909 // register for some cases.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005910 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005911 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005912 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005913}
5914
5915void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005916 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005917 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005918 Location obj_loc = locations->InAt(0);
5919 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005920 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005921 Location temp_loc = locations->GetTemp(0);
5922 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005923 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain1e7f8db2015-12-15 10:54:19 +00005924 locations->GetTemp(1) :
5925 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005926 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5927 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5928 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5929 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005930
Roland Levillain0d5a2812015-11-13 10:07:31 +00005931 bool is_type_check_slow_path_fatal =
5932 (type_check_kind == TypeCheckKind::kExactCheck ||
5933 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5934 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5935 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5936 !instruction->CanThrowIntoCatchBlock();
5937 SlowPathCode* type_check_slow_path =
5938 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5939 is_type_check_slow_path_fatal);
5940 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005941
Roland Levillain0d5a2812015-11-13 10:07:31 +00005942 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005943 case TypeCheckKind::kExactCheck:
5944 case TypeCheckKind::kArrayCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005945 NearLabel done;
5946 // Avoid null check if we know obj is not null.
5947 if (instruction->MustDoNullCheck()) {
5948 __ testl(obj, obj);
5949 __ j(kEqual, &done);
5950 }
5951
5952 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005953 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00005954
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005955 if (cls.IsRegister()) {
5956 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5957 } else {
5958 DCHECK(cls.IsStackSlot()) << cls;
5959 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5960 }
5961 // Jump to slow path for throwing the exception or doing a
5962 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005963 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00005964 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005965 break;
5966 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005967
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005968 case TypeCheckKind::kAbstractClassCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00005969 NearLabel done;
5970 // Avoid null check if we know obj is not null.
5971 if (instruction->MustDoNullCheck()) {
5972 __ testl(obj, obj);
5973 __ j(kEqual, &done);
5974 }
5975
5976 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005977 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00005978
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005979 // If the class is abstract, we eagerly fetch the super class of the
5980 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005981 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005982 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005983 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005984 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005985
5986 // If the class reference currently in `temp` is not null, jump
5987 // to the `compare_classes` label to compare it with the checked
5988 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005989 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005990 __ j(kNotEqual, &compare_classes);
5991 // Otherwise, jump to the slow path to throw the exception.
5992 //
5993 // But before, move back the object's class into `temp` before
5994 // going into the slow path, as it has been overwritten in the
5995 // meantime.
5996 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00005997 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005998 __ jmp(type_check_slow_path->GetEntryLabel());
5999
6000 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006001 if (cls.IsRegister()) {
6002 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6003 } else {
6004 DCHECK(cls.IsStackSlot()) << cls;
6005 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6006 }
6007 __ j(kNotEqual, &loop);
Roland Levillain86503782016-02-11 19:07:30 +00006008 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006009 break;
6010 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006011
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006012 case TypeCheckKind::kClassHierarchyCheck: {
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 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006024 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006025 __ Bind(&loop);
6026 if (cls.IsRegister()) {
6027 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6028 } else {
6029 DCHECK(cls.IsStackSlot()) << cls;
6030 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6031 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006032 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006033
Roland Levillain0d5a2812015-11-13 10:07:31 +00006034 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006035 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006036
6037 // If the class reference currently in `temp` is not null, jump
6038 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006039 __ testl(temp, temp);
6040 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006041 // Otherwise, jump to the slow path to throw the exception.
6042 //
6043 // But before, move back the object's class into `temp` before
6044 // going into the slow path, as it has been overwritten in the
6045 // meantime.
6046 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006047 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006048 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006049 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006050 break;
6051 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006052
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006053 case TypeCheckKind::kArrayObjectCheck: {
Roland Levillain86503782016-02-11 19:07:30 +00006054 // We cannot use a NearLabel here, as its range might be too
6055 // short in some cases when read barriers are enabled. This has
6056 // been observed for instance when the code emitted for this
6057 // case uses high x86-64 registers (R8-R15).
6058 Label done;
6059 // Avoid null check if we know obj is not null.
6060 if (instruction->MustDoNullCheck()) {
6061 __ testl(obj, obj);
6062 __ j(kEqual, &done);
6063 }
6064
6065 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006066 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00006067
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006068 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006069 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006070 if (cls.IsRegister()) {
6071 __ cmpl(temp, cls.AsRegister<CpuRegister>());
6072 } else {
6073 DCHECK(cls.IsStackSlot()) << cls;
6074 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
6075 }
6076 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006077
6078 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006079 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006080 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006081
6082 // If the component type is not null (i.e. the object is indeed
6083 // an array), jump to label `check_non_primitive_component_type`
6084 // to further check that this component type is not a primitive
6085 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006086 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006087 __ j(kNotEqual, &check_non_primitive_component_type);
6088 // Otherwise, jump to the slow path to throw the exception.
6089 //
6090 // But before, move back the object's class into `temp` before
6091 // going into the slow path, as it has been overwritten in the
6092 // meantime.
6093 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006094 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006095 __ jmp(type_check_slow_path->GetEntryLabel());
6096
6097 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006098 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006099 __ j(kEqual, &done);
6100 // Same comment as above regarding `temp` and the slow path.
6101 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006102 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006103 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006104 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006105 break;
6106 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006107
Calin Juravle98893e12015-10-02 21:05:03 +01006108 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006109 case TypeCheckKind::kInterfaceCheck:
Roland Levillain86503782016-02-11 19:07:30 +00006110 NearLabel done;
6111 // Avoid null check if we know obj is not null.
6112 if (instruction->MustDoNullCheck()) {
6113 __ testl(obj, obj);
6114 __ j(kEqual, &done);
6115 }
6116
6117 // /* HeapReference<Class> */ temp = obj->klass_
Vladimir Marko953437b2016-08-24 08:30:46 +00006118 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset);
Roland Levillain86503782016-02-11 19:07:30 +00006119
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006120 // We always go into the type check slow path for the unresolved
6121 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006122 //
6123 // We cannot directly call the CheckCast runtime entry point
6124 // without resorting to a type checking slow path here (i.e. by
6125 // calling InvokeRuntime directly), as it would require to
6126 // assign fixed registers for the inputs of this HInstanceOf
6127 // instruction (following the runtime calling convention), which
6128 // might be cluttered by the potential first read barrier
6129 // emission at the beginning of this method.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006130 //
6131 // TODO: Introduce a new runtime entry point taking the object
6132 // to test (instead of its class) as argument, and let it deal
6133 // with the read barrier issues. This will let us refactor this
6134 // case of the `switch` code as it was previously (with a direct
6135 // call to the runtime not using a type checking slow path).
6136 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006137 __ jmp(type_check_slow_path->GetEntryLabel());
Roland Levillain86503782016-02-11 19:07:30 +00006138 __ Bind(&done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006139 break;
6140 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006141
Roland Levillain0d5a2812015-11-13 10:07:31 +00006142 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006143}
6144
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006145void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
6146 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006147 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006148 InvokeRuntimeCallingConvention calling_convention;
6149 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6150}
6151
6152void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006153 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6154 : QUICK_ENTRY_POINT(pUnlockObject),
6155 instruction,
6156 instruction->GetDexPc(),
6157 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006158 if (instruction->IsEnter()) {
6159 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6160 } else {
6161 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6162 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006163}
6164
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006165void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6166void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6167void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6168
6169void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6170 LocationSummary* locations =
6171 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6172 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6173 || instruction->GetResultType() == Primitive::kPrimLong);
6174 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04006175 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006176 locations->SetOut(Location::SameAsFirstInput());
6177}
6178
6179void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
6180 HandleBitwiseOperation(instruction);
6181}
6182
6183void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
6184 HandleBitwiseOperation(instruction);
6185}
6186
6187void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
6188 HandleBitwiseOperation(instruction);
6189}
6190
6191void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
6192 LocationSummary* locations = instruction->GetLocations();
6193 Location first = locations->InAt(0);
6194 Location second = locations->InAt(1);
6195 DCHECK(first.Equals(locations->Out()));
6196
6197 if (instruction->GetResultType() == Primitive::kPrimInt) {
6198 if (second.IsRegister()) {
6199 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006200 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006201 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006202 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006203 } else {
6204 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006205 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006206 }
6207 } else if (second.IsConstant()) {
6208 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
6209 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006210 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006211 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006212 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006213 } else {
6214 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006215 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006216 }
6217 } else {
6218 Address address(CpuRegister(RSP), second.GetStackIndex());
6219 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006220 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006221 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006222 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006223 } else {
6224 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006225 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006226 }
6227 }
6228 } else {
6229 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006230 CpuRegister first_reg = first.AsRegister<CpuRegister>();
6231 bool second_is_constant = false;
6232 int64_t value = 0;
6233 if (second.IsConstant()) {
6234 second_is_constant = true;
6235 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006236 }
Mark Mendell40741f32015-04-20 22:10:34 -04006237 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006238
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006239 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006240 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006241 if (is_int32_value) {
6242 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
6243 } else {
6244 __ andq(first_reg, codegen_->LiteralInt64Address(value));
6245 }
6246 } else if (second.IsDoubleStackSlot()) {
6247 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006248 } else {
6249 __ andq(first_reg, second.AsRegister<CpuRegister>());
6250 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006251 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006252 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006253 if (is_int32_value) {
6254 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
6255 } else {
6256 __ orq(first_reg, codegen_->LiteralInt64Address(value));
6257 }
6258 } else if (second.IsDoubleStackSlot()) {
6259 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006260 } else {
6261 __ orq(first_reg, second.AsRegister<CpuRegister>());
6262 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006263 } else {
6264 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006265 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04006266 if (is_int32_value) {
6267 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
6268 } else {
6269 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
6270 }
6271 } else if (second.IsDoubleStackSlot()) {
6272 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006273 } else {
6274 __ xorq(first_reg, second.AsRegister<CpuRegister>());
6275 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006276 }
6277 }
6278}
6279
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006280void InstructionCodeGeneratorX86_64::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6281 Location out,
6282 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006283 Location maybe_temp) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006284 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6285 if (kEmitCompilerReadBarrier) {
6286 if (kUseBakerReadBarrier) {
6287 // Load with fast path based Baker's read barrier.
6288 // /* HeapReference<Object> */ out = *(out + offset)
6289 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006290 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006291 } else {
6292 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006293 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006294 // in the following move operation, as we will need it for the
6295 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00006296 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006297 __ movl(maybe_temp.AsRegister<CpuRegister>(), out_reg);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006298 // /* HeapReference<Object> */ out = *(out + offset)
6299 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006300 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006301 }
6302 } else {
6303 // Plain load with no read barrier.
6304 // /* HeapReference<Object> */ out = *(out + offset)
6305 __ movl(out_reg, Address(out_reg, offset));
6306 __ MaybeUnpoisonHeapReference(out_reg);
6307 }
6308}
6309
6310void InstructionCodeGeneratorX86_64::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6311 Location out,
6312 Location obj,
Vladimir Marko953437b2016-08-24 08:30:46 +00006313 uint32_t offset) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006314 CpuRegister out_reg = out.AsRegister<CpuRegister>();
6315 CpuRegister obj_reg = obj.AsRegister<CpuRegister>();
6316 if (kEmitCompilerReadBarrier) {
6317 if (kUseBakerReadBarrier) {
6318 // Load with fast path based Baker's read barrier.
6319 // /* HeapReference<Object> */ out = *(obj + offset)
6320 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00006321 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006322 } else {
6323 // Load with slow path based read barrier.
6324 // /* HeapReference<Object> */ out = *(obj + offset)
6325 __ movl(out_reg, Address(obj_reg, offset));
6326 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6327 }
6328 } else {
6329 // Plain load with no read barrier.
6330 // /* HeapReference<Object> */ out = *(obj + offset)
6331 __ movl(out_reg, Address(obj_reg, offset));
6332 __ MaybeUnpoisonHeapReference(out_reg);
6333 }
6334}
6335
6336void InstructionCodeGeneratorX86_64::GenerateGcRootFieldLoad(HInstruction* instruction,
6337 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006338 const Address& address,
6339 Label* fixup_label) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006340 CpuRegister root_reg = root.AsRegister<CpuRegister>();
6341 if (kEmitCompilerReadBarrier) {
6342 if (kUseBakerReadBarrier) {
6343 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6344 // Baker's read barrier are used:
6345 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006346 // root = *address;
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006347 // if (Thread::Current()->GetIsGcMarking()) {
6348 // root = ReadBarrier::Mark(root)
6349 // }
6350
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006351 // /* GcRoot<mirror::Object> */ root = *address
6352 __ movl(root_reg, address);
6353 if (fixup_label != nullptr) {
6354 __ Bind(fixup_label);
6355 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006356 static_assert(
6357 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6358 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6359 "have different sizes.");
6360 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6361 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6362 "have different sizes.");
6363
Vladimir Marko953437b2016-08-24 08:30:46 +00006364 // Slow path marking the GC root `root`.
6365 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
6366 instruction, root, /* unpoison */ false);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006367 codegen_->AddSlowPath(slow_path);
6368
Andreas Gampe542451c2016-07-26 09:02:02 -07006369 __ gs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86_64PointerSize>().Int32Value(),
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006370 /* no_rip */ true),
6371 Immediate(0));
6372 __ j(kNotEqual, slow_path->GetEntryLabel());
6373 __ Bind(slow_path->GetExitLabel());
6374 } else {
6375 // GC root loaded through a slow path for read barriers other
6376 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006377 // /* GcRoot<mirror::Object>* */ root = address
6378 __ leaq(root_reg, address);
6379 if (fixup_label != nullptr) {
6380 __ Bind(fixup_label);
6381 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006382 // /* mirror::Object* */ root = root->Read()
6383 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6384 }
6385 } else {
6386 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006387 // /* GcRoot<mirror::Object> */ root = *address
6388 __ movl(root_reg, address);
6389 if (fixup_label != nullptr) {
6390 __ Bind(fixup_label);
6391 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006392 // Note that GC roots are not affected by heap poisoning, thus we
6393 // do not have to unpoison `root_reg` here.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006394 }
6395}
6396
6397void CodeGeneratorX86_64::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6398 Location ref,
6399 CpuRegister obj,
6400 uint32_t offset,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006401 bool needs_null_check) {
6402 DCHECK(kEmitCompilerReadBarrier);
6403 DCHECK(kUseBakerReadBarrier);
6404
6405 // /* HeapReference<Object> */ ref = *(obj + offset)
6406 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006407 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006408}
6409
6410void CodeGeneratorX86_64::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6411 Location ref,
6412 CpuRegister obj,
6413 uint32_t data_offset,
6414 Location index,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006415 bool needs_null_check) {
6416 DCHECK(kEmitCompilerReadBarrier);
6417 DCHECK(kUseBakerReadBarrier);
6418
Roland Levillain3d312422016-06-23 13:53:42 +01006419 static_assert(
6420 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6421 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006422 // /* HeapReference<Object> */ ref =
6423 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6424 Address src = index.IsConstant() ?
6425 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6426 Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00006427 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006428}
6429
6430void CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6431 Location ref,
6432 CpuRegister obj,
6433 const Address& src,
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006434 bool needs_null_check) {
6435 DCHECK(kEmitCompilerReadBarrier);
6436 DCHECK(kUseBakerReadBarrier);
6437
6438 // In slow path based read barriers, the read barrier call is
6439 // inserted after the original load. However, in fast path based
6440 // Baker's read barriers, we need to perform the load of
6441 // mirror::Object::monitor_ *before* the original reference load.
6442 // This load-load ordering is required by the read barrier.
6443 // The fast path/slow path (for Baker's algorithm) should look like:
6444 //
6445 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6446 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6447 // HeapReference<Object> ref = *src; // Original reference load.
6448 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6449 // if (is_gray) {
6450 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6451 // }
6452 //
6453 // Note: the original implementation in ReadBarrier::Barrier is
6454 // slightly more complex as:
6455 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006456 // the high-bits of rb_state, which are expected to be all zeroes
6457 // (we use CodeGeneratorX86_64::GenerateMemoryBarrier instead
6458 // here, which is a no-op thanks to the x86-64 memory model);
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006459 // - it performs additional checks that we do not do here for
6460 // performance reasons.
6461
6462 CpuRegister ref_reg = ref.AsRegister<CpuRegister>();
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006463 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6464
Vladimir Marko953437b2016-08-24 08:30:46 +00006465 // Given the numeric representation, it's enough to check the low bit of the rb_state.
6466 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6467 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6468 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6469 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
6470 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
6471 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
6472
6473 // if (rb_state == ReadBarrier::gray_ptr_)
6474 // ref = ReadBarrier::Mark(ref);
6475 // At this point, just do the "if" and make sure that flags are preserved until the branch.
6476 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006477 if (needs_null_check) {
6478 MaybeRecordImplicitNullCheck(instruction);
6479 }
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006480
6481 // Load fence to prevent load-load reordering.
6482 // Note that this is a no-op, thanks to the x86-64 memory model.
6483 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6484
6485 // The actual reference load.
6486 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00006487 __ movl(ref_reg, src); // Flags are unaffected.
6488
6489 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
6490 // Slow path marking the object `ref` when it is gray.
6491 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86_64(
6492 instruction, ref, /* unpoison */ true);
6493 AddSlowPath(slow_path);
6494
6495 // We have done the "if" of the gray bit check above, now branch based on the flags.
6496 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006497
6498 // Object* ref = ref_addr->AsMirrorPtr()
6499 __ MaybeUnpoisonHeapReference(ref_reg);
6500
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006501 __ Bind(slow_path->GetExitLabel());
6502}
6503
6504void CodeGeneratorX86_64::GenerateReadBarrierSlow(HInstruction* instruction,
6505 Location out,
6506 Location ref,
6507 Location obj,
6508 uint32_t offset,
6509 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006510 DCHECK(kEmitCompilerReadBarrier);
6511
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006512 // Insert a slow path based read barrier *after* the reference load.
6513 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006514 // If heap poisoning is enabled, the unpoisoning of the loaded
6515 // reference will be carried out by the runtime within the slow
6516 // path.
6517 //
6518 // Note that `ref` currently does not get unpoisoned (when heap
6519 // poisoning is enabled), which is alright as the `ref` argument is
6520 // not used by the artReadBarrierSlow entry point.
6521 //
6522 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6523 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6524 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
6525 AddSlowPath(slow_path);
6526
Roland Levillain0d5a2812015-11-13 10:07:31 +00006527 __ jmp(slow_path->GetEntryLabel());
6528 __ Bind(slow_path->GetExitLabel());
6529}
6530
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006531void CodeGeneratorX86_64::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6532 Location out,
6533 Location ref,
6534 Location obj,
6535 uint32_t offset,
6536 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006537 if (kEmitCompilerReadBarrier) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006538 // Baker's read barriers shall be handled by the fast path
6539 // (CodeGeneratorX86_64::GenerateReferenceLoadWithBakerReadBarrier).
6540 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006541 // If heap poisoning is enabled, unpoisoning will be taken care of
6542 // by the runtime within the slow path.
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006543 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006544 } else if (kPoisonHeapReferences) {
6545 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
6546 }
6547}
6548
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006549void CodeGeneratorX86_64::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6550 Location out,
6551 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006552 DCHECK(kEmitCompilerReadBarrier);
6553
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006554 // Insert a slow path based read barrier *after* the GC root load.
6555 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006556 // Note that GC roots are not affected by heap poisoning, so we do
6557 // not need to do anything special for this here.
6558 SlowPathCode* slow_path =
6559 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
6560 AddSlowPath(slow_path);
6561
Roland Levillain0d5a2812015-11-13 10:07:31 +00006562 __ jmp(slow_path->GetEntryLabel());
6563 __ Bind(slow_path->GetExitLabel());
6564}
6565
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006566void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006567 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006568 LOG(FATAL) << "Unreachable";
6569}
6570
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006571void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006572 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006573 LOG(FATAL) << "Unreachable";
6574}
6575
Mark Mendellfe57faa2015-09-18 09:26:15 -04006576// Simple implementation of packed switch - generate cascaded compare/jumps.
6577void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6578 LocationSummary* locations =
6579 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6580 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04006581 locations->AddTemp(Location::RequiresRegister());
6582 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04006583}
6584
6585void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6586 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006587 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006588 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006589 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6590 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6591 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006592 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6593
6594 // Should we generate smaller inline compare/jumps?
6595 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6596 // Figure out the correct compare values and jump conditions.
6597 // Handle the first compare/branch as a special case because it might
6598 // jump to the default case.
6599 DCHECK_GT(num_entries, 2u);
6600 Condition first_condition;
6601 uint32_t index;
6602 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6603 if (lower_bound != 0) {
6604 first_condition = kLess;
6605 __ cmpl(value_reg_in, Immediate(lower_bound));
6606 __ j(first_condition, codegen_->GetLabelOf(default_block));
6607 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
6608
6609 index = 1;
6610 } else {
6611 // Handle all the compare/jumps below.
6612 first_condition = kBelow;
6613 index = 0;
6614 }
6615
6616 // Handle the rest of the compare/jumps.
6617 for (; index + 1 < num_entries; index += 2) {
6618 int32_t compare_to_value = lower_bound + index + 1;
6619 __ cmpl(value_reg_in, Immediate(compare_to_value));
6620 // Jump to successors[index] if value < case_value[index].
6621 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6622 // Jump to successors[index + 1] if value == case_value[index + 1].
6623 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6624 }
6625
6626 if (index != num_entries) {
6627 // There are an odd number of entries. Handle the last one.
6628 DCHECK_EQ(index + 1, num_entries);
Nicolas Geoffray6ce01732015-12-30 14:10:13 +00006629 __ cmpl(value_reg_in, Immediate(static_cast<int32_t>(lower_bound + index)));
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006630 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
6631 }
6632
6633 // And the default for any other value.
6634 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6635 __ jmp(codegen_->GetLabelOf(default_block));
6636 }
6637 return;
6638 }
Mark Mendell9c86b482015-09-18 13:36:07 -04006639
6640 // Remove the bias, if needed.
6641 Register value_reg_out = value_reg_in.AsRegister();
6642 if (lower_bound != 0) {
6643 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6644 value_reg_out = temp_reg.AsRegister();
6645 }
6646 CpuRegister value_reg(value_reg_out);
6647
6648 // Is the value in range?
Mark Mendell9c86b482015-09-18 13:36:07 -04006649 __ cmpl(value_reg, Immediate(num_entries - 1));
6650 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006651
Mark Mendell9c86b482015-09-18 13:36:07 -04006652 // We are in the range of the table.
6653 // Load the address of the jump table in the constant area.
6654 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006655
Mark Mendell9c86b482015-09-18 13:36:07 -04006656 // Load the (signed) offset from the jump table.
6657 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6658
6659 // Add the offset to the address of the table base.
6660 __ addq(temp_reg, base_reg);
6661
6662 // And jump.
6663 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006664}
6665
Aart Bikc5d47542016-01-27 17:00:35 -08006666void CodeGeneratorX86_64::Load32BitValue(CpuRegister dest, int32_t value) {
6667 if (value == 0) {
6668 __ xorl(dest, dest);
6669 } else {
6670 __ movl(dest, Immediate(value));
6671 }
6672}
6673
Mark Mendell92e83bf2015-05-07 11:25:03 -04006674void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6675 if (value == 0) {
Aart Bikc5d47542016-01-27 17:00:35 -08006676 // Clears upper bits too.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006677 __ xorl(dest, dest);
Vladimir Markoed009782016-02-22 16:54:39 +00006678 } else if (IsUint<32>(value)) {
6679 // We can use a 32 bit move, as it will zero-extend and is shorter.
Mark Mendell92e83bf2015-05-07 11:25:03 -04006680 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6681 } else {
6682 __ movq(dest, Immediate(value));
6683 }
6684}
6685
Mark Mendell7c0b44f2016-02-01 10:08:35 -05006686void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, int32_t value) {
6687 if (value == 0) {
6688 __ xorps(dest, dest);
6689 } else {
6690 __ movss(dest, LiteralInt32Address(value));
6691 }
6692}
6693
6694void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, int64_t value) {
6695 if (value == 0) {
6696 __ xorpd(dest, dest);
6697 } else {
6698 __ movsd(dest, LiteralInt64Address(value));
6699 }
6700}
6701
6702void CodeGeneratorX86_64::Load32BitValue(XmmRegister dest, float value) {
6703 Load32BitValue(dest, bit_cast<int32_t, float>(value));
6704}
6705
6706void CodeGeneratorX86_64::Load64BitValue(XmmRegister dest, double value) {
6707 Load64BitValue(dest, bit_cast<int64_t, double>(value));
6708}
6709
Aart Bika19616e2016-02-01 18:57:58 -08006710void CodeGeneratorX86_64::Compare32BitValue(CpuRegister dest, int32_t value) {
6711 if (value == 0) {
6712 __ testl(dest, dest);
6713 } else {
6714 __ cmpl(dest, Immediate(value));
6715 }
6716}
6717
6718void CodeGeneratorX86_64::Compare64BitValue(CpuRegister dest, int64_t value) {
6719 if (IsInt<32>(value)) {
6720 if (value == 0) {
6721 __ testq(dest, dest);
6722 } else {
6723 __ cmpq(dest, Immediate(static_cast<int32_t>(value)));
6724 }
6725 } else {
6726 // Value won't fit in an int.
6727 __ cmpq(dest, LiteralInt64Address(value));
6728 }
6729}
6730
Mark Mendellcfa410b2015-05-25 16:02:44 -04006731void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6732 DCHECK(dest.IsDoubleStackSlot());
6733 if (IsInt<32>(value)) {
6734 // Can move directly as an int32 constant.
6735 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6736 Immediate(static_cast<int32_t>(value)));
6737 } else {
6738 Load64BitValue(CpuRegister(TMP), value);
6739 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6740 }
6741}
6742
Mark Mendell9c86b482015-09-18 13:36:07 -04006743/**
6744 * Class to handle late fixup of offsets into constant area.
6745 */
6746class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6747 public:
6748 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6749 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6750
6751 protected:
6752 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6753
6754 CodeGeneratorX86_64* codegen_;
6755
6756 private:
6757 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6758 // Patch the correct offset for the instruction. We use the address of the
6759 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6760 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6761 int32_t relative_position = constant_offset - pos;
6762
6763 // Patch in the right value.
6764 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6765 }
6766
6767 // Location in constant area that the fixup refers to.
6768 size_t offset_into_constant_area_;
6769};
6770
6771/**
6772 t * Class to handle late fixup of offsets to a jump table that will be created in the
6773 * constant area.
6774 */
6775class JumpTableRIPFixup : public RIPFixup {
6776 public:
6777 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6778 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6779
6780 void CreateJumpTable() {
6781 X86_64Assembler* assembler = codegen_->GetAssembler();
6782
6783 // Ensure that the reference to the jump table has the correct offset.
6784 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6785 SetOffset(offset_in_constant_table);
6786
6787 // Compute the offset from the start of the function to this jump table.
6788 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6789
6790 // Populate the jump table with the correct values for the jump table.
6791 int32_t num_entries = switch_instr_->GetNumEntries();
6792 HBasicBlock* block = switch_instr_->GetBlock();
6793 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6794 // The value that we want is the target offset - the position of the table.
6795 for (int32_t i = 0; i < num_entries; i++) {
6796 HBasicBlock* b = successors[i];
6797 Label* l = codegen_->GetLabelOf(b);
6798 DCHECK(l->IsBound());
6799 int32_t offset_to_block = l->Position() - current_table_offset;
6800 assembler->AppendInt32(offset_to_block);
6801 }
6802 }
6803
6804 private:
6805 const HPackedSwitch* switch_instr_;
6806};
6807
Mark Mendellf55c3e02015-03-26 21:07:46 -04006808void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6809 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006810 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006811 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6812 // 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 -04006813 assembler->Align(4, 0);
6814 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006815
6816 // Populate any jump tables.
6817 for (auto jump_table : fixups_to_jump_tables_) {
6818 jump_table->CreateJumpTable();
6819 }
6820
6821 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006822 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006823 }
6824
6825 // And finish up.
6826 CodeGenerator::Finalize(allocator);
6827}
6828
Mark Mendellf55c3e02015-03-26 21:07:46 -04006829Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
6830 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6831 return Address::RIP(fixup);
6832}
6833
6834Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
6835 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6836 return Address::RIP(fixup);
6837}
6838
6839Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
6840 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6841 return Address::RIP(fixup);
6842}
6843
6844Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
6845 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6846 return Address::RIP(fixup);
6847}
6848
Andreas Gampe85b62f22015-09-09 13:15:38 -07006849// TODO: trg as memory.
6850void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6851 if (!trg.IsValid()) {
Roland Levillain1e7f8db2015-12-15 10:54:19 +00006852 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006853 return;
6854 }
6855
6856 DCHECK_NE(type, Primitive::kPrimVoid);
6857
6858 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
6859 if (trg.Equals(return_loc)) {
6860 return;
6861 }
6862
6863 // Let the parallel move resolver take care of all of this.
6864 HParallelMove parallel_move(GetGraph()->GetArena());
6865 parallel_move.AddMove(return_loc, trg, type, nullptr);
6866 GetMoveResolver()->EmitNativeCode(&parallel_move);
6867}
6868
Mark Mendell9c86b482015-09-18 13:36:07 -04006869Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
6870 // Create a fixup to be used to create and address the jump table.
6871 JumpTableRIPFixup* table_fixup =
6872 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6873
6874 // We have to populate the jump tables.
6875 fixups_to_jump_tables_.push_back(table_fixup);
6876 return Address::RIP(table_fixup);
6877}
6878
Mark Mendellea5af682015-10-22 17:35:49 -04006879void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
6880 const Address& addr_high,
6881 int64_t v,
6882 HInstruction* instruction) {
6883 if (IsInt<32>(v)) {
6884 int32_t v_32 = v;
6885 __ movq(addr_low, Immediate(v_32));
6886 MaybeRecordImplicitNullCheck(instruction);
6887 } else {
6888 // Didn't fit in a register. Do it in pieces.
6889 int32_t low_v = Low32Bits(v);
6890 int32_t high_v = High32Bits(v);
6891 __ movl(addr_low, Immediate(low_v));
6892 MaybeRecordImplicitNullCheck(instruction);
6893 __ movl(addr_high, Immediate(high_v));
6894 }
6895}
6896
Roland Levillain4d027112015-07-01 15:41:14 +01006897#undef __
6898
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006899} // namespace x86_64
6900} // namespace art