blob: 4088160b3fb0b2458a65a7991d82762665edd9f8 [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;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010044
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +000045static constexpr Register kCoreCalleeSaves[] = { RBX, RBP, R12, R13, R14, R15 };
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000046static constexpr FloatRegister kFpuCalleeSaves[] = { XMM12, XMM13, XMM14, XMM15 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010047
Mark Mendell24f2dfa2015-01-14 19:51:45 -050048static constexpr int kC2ConditionMask = 0x400;
49
Roland Levillain62a46b22015-06-01 18:24:13 +010050#define __ down_cast<X86_64Assembler*>(codegen->GetAssembler())->
Calin Juravle175dc732015-08-25 15:42:32 +010051#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86_64WordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010052
Andreas Gampe85b62f22015-09-09 13:15:38 -070053class NullCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010054 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010055 explicit NullCheckSlowPathX86_64(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010056
Alexandre Rames2ed20af2015-03-06 13:55:35 +000057 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +000058 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010059 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000060 if (instruction_->CanThrowIntoCatchBlock()) {
61 // Live registers will be restored in the catch block if caught.
62 SaveLiveRegisters(codegen, instruction_->GetLocations());
63 }
Roland Levillain0d5a2812015-11-13 10:07:31 +000064 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
65 instruction_,
66 instruction_->GetDexPc(),
67 this);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010068 }
69
Alexandre Rames8158f282015-08-07 10:26:17 +010070 bool IsFatal() const OVERRIDE { return true; }
71
Alexandre Rames9931f312015-06-19 14:47:01 +010072 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86_64"; }
73
Nicolas Geoffraye5038322014-07-04 09:41:32 +010074 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010075 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86_64);
77};
78
Andreas Gampe85b62f22015-09-09 13:15:38 -070079class DivZeroCheckSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000080 public:
81 explicit DivZeroCheckSlowPathX86_64(HDivZeroCheck* instruction) : instruction_(instruction) {}
82
Alexandre Rames2ed20af2015-03-06 13:55:35 +000083 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +000084 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000085 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000086 if (instruction_->CanThrowIntoCatchBlock()) {
87 // Live registers will be restored in the catch block if caught.
88 SaveLiveRegisters(codegen, instruction_->GetLocations());
89 }
Roland Levillain0d5a2812015-11-13 10:07:31 +000090 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
91 instruction_,
92 instruction_->GetDexPc(),
93 this);
Calin Juravled0d48522014-11-04 16:40:20 +000094 }
95
Alexandre Rames8158f282015-08-07 10:26:17 +010096 bool IsFatal() const OVERRIDE { return true; }
97
Alexandre Rames9931f312015-06-19 14:47:01 +010098 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86_64"; }
99
Calin Juravled0d48522014-11-04 16:40:20 +0000100 private:
101 HDivZeroCheck* const instruction_;
102 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86_64);
103};
104
Andreas Gampe85b62f22015-09-09 13:15:38 -0700105class DivRemMinusOneSlowPathX86_64 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000106 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100107 DivRemMinusOneSlowPathX86_64(Register reg, Primitive::Type type, bool is_div)
Calin Juravlebacfec32014-11-14 15:54:36 +0000108 : cpu_reg_(CpuRegister(reg)), type_(type), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000109
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000110 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000111 __ Bind(GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000112 if (type_ == Primitive::kPrimInt) {
Calin Juravlebacfec32014-11-14 15:54:36 +0000113 if (is_div_) {
114 __ negl(cpu_reg_);
115 } else {
Mark Mendellcfa410b2015-05-25 16:02:44 -0400116 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000117 }
118
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000119 } else {
120 DCHECK_EQ(Primitive::kPrimLong, type_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000121 if (is_div_) {
122 __ negq(cpu_reg_);
123 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -0400124 __ xorl(cpu_reg_, cpu_reg_);
Calin Juravlebacfec32014-11-14 15:54:36 +0000125 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000126 }
Calin Juravled0d48522014-11-04 16:40:20 +0000127 __ jmp(GetExitLabel());
128 }
129
Alexandre Rames9931f312015-06-19 14:47:01 +0100130 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86_64"; }
131
Calin Juravled0d48522014-11-04 16:40:20 +0000132 private:
Calin Juravlebacfec32014-11-14 15:54:36 +0000133 const CpuRegister cpu_reg_;
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000134 const Primitive::Type type_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000135 const bool is_div_;
136 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86_64);
Calin Juravled0d48522014-11-04 16:40:20 +0000137};
138
Andreas Gampe85b62f22015-09-09 13:15:38 -0700139class SuspendCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000140 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100141 SuspendCheckSlowPathX86_64(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100142 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000143
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000144 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000145 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000146 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000147 SaveLiveRegisters(codegen, instruction_->GetLocations());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000148 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
149 instruction_,
150 instruction_->GetDexPc(),
151 this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000152 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100153 if (successor_ == nullptr) {
154 __ jmp(GetReturnLabel());
155 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000156 __ jmp(x86_64_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100157 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000158 }
159
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100160 Label* GetReturnLabel() {
161 DCHECK(successor_ == nullptr);
162 return &return_label_;
163 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000164
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100165 HBasicBlock* GetSuccessor() const {
166 return successor_;
167 }
168
Alexandre Rames9931f312015-06-19 14:47:01 +0100169 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86_64"; }
170
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000171 private:
172 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100173 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000174 Label return_label_;
175
176 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86_64);
177};
178
Andreas Gampe85b62f22015-09-09 13:15:38 -0700179class BoundsCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100180 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100181 explicit BoundsCheckSlowPathX86_64(HBoundsCheck* instruction)
182 : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100183
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000184 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100185 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000186 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100187 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000188 if (instruction_->CanThrowIntoCatchBlock()) {
189 // Live registers will be restored in the catch block if caught.
190 SaveLiveRegisters(codegen, instruction_->GetLocations());
191 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000192 // We're moving two locations to locations that could overlap, so we need a parallel
193 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100194 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000195 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100196 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000197 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100198 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100199 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100200 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
201 Primitive::kPrimInt);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000202 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
203 instruction_,
204 instruction_->GetDexPc(),
205 this);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100206 }
207
Alexandre Rames8158f282015-08-07 10:26:17 +0100208 bool IsFatal() const OVERRIDE { return true; }
209
Alexandre Rames9931f312015-06-19 14:47:01 +0100210 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86_64"; }
211
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100212 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100213 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100214
215 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86_64);
216};
217
Andreas Gampe85b62f22015-09-09 13:15:38 -0700218class LoadClassSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100219 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000220 LoadClassSlowPathX86_64(HLoadClass* cls,
221 HInstruction* at,
222 uint32_t dex_pc,
223 bool do_clinit)
224 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
225 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
226 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100227
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000228 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000229 LocationSummary* locations = at_->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000230 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100231 __ Bind(GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100232
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000233 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000234
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100235 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000236 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)), Immediate(cls_->GetTypeIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000237 x86_64_codegen->InvokeRuntime(do_clinit_ ?
238 QUICK_ENTRY_POINT(pInitializeStaticStorage) :
239 QUICK_ENTRY_POINT(pInitializeType),
240 at_,
241 dex_pc_,
242 this);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100243
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000244 Location out = locations->Out();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000245 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000246 if (out.IsValid()) {
247 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000248 x86_64_codegen->Move(out, Location::RegisterLocation(RAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000249 }
250
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000251 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100252 __ jmp(GetExitLabel());
253 }
254
Alexandre Rames9931f312015-06-19 14:47:01 +0100255 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86_64"; }
256
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100257 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000258 // The class this slow path will load.
259 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100260
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000261 // The instruction where this slow path is happening.
262 // (Might be the load class or an initialization check).
263 HInstruction* const at_;
264
265 // The dex PC of `at_`.
266 const uint32_t dex_pc_;
267
268 // Whether to initialize the class.
269 const bool do_clinit_;
270
271 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86_64);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100272};
273
Andreas Gampe85b62f22015-09-09 13:15:38 -0700274class LoadStringSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000275 public:
276 explicit LoadStringSlowPathX86_64(HLoadString* instruction) : instruction_(instruction) {}
277
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000278 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000279 LocationSummary* locations = instruction_->GetLocations();
280 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
281
Roland Levillain0d5a2812015-11-13 10:07:31 +0000282 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000283 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000284 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000285
286 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800287 __ movl(CpuRegister(calling_convention.GetRegisterAt(0)),
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000288 Immediate(instruction_->GetStringIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +0000289 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
290 instruction_,
291 instruction_->GetDexPc(),
292 this);
293 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000294 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000295 __ jmp(GetExitLabel());
296 }
297
Alexandre Rames9931f312015-06-19 14:47:01 +0100298 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86_64"; }
299
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000300 private:
301 HLoadString* const instruction_;
302
303 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86_64);
304};
305
Andreas Gampe85b62f22015-09-09 13:15:38 -0700306class TypeCheckSlowPathX86_64 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000307 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000308 TypeCheckSlowPathX86_64(HInstruction* instruction, bool is_fatal)
309 : instruction_(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000310
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000311 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000312 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100313 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
314 : locations->Out();
315 uint32_t dex_pc = instruction_->GetDexPc();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000316 DCHECK(instruction_->IsCheckCast()
317 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000318
Roland Levillain0d5a2812015-11-13 10:07:31 +0000319 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000320 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000321
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000322 if (!is_fatal_) {
323 SaveLiveRegisters(codegen, locations);
324 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000325
326 // We're moving two locations to locations that could overlap, so we need a parallel
327 // move resolver.
328 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000329 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100330 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000331 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100332 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100333 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100334 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
335 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000336
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000337 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000338 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
339 instruction_,
340 dex_pc,
341 this);
342 CheckEntrypointTypes<
343 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000344 } else {
345 DCHECK(instruction_->IsCheckCast());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000346 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
347 instruction_,
348 dex_pc,
349 this);
350 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000351 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000352
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000353 if (!is_fatal_) {
354 if (instruction_->IsInstanceOf()) {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000355 x86_64_codegen->Move(locations->Out(), Location::RegisterLocation(RAX));
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000356 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000357
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000358 RestoreLiveRegisters(codegen, locations);
359 __ jmp(GetExitLabel());
360 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000361 }
362
Alexandre Rames9931f312015-06-19 14:47:01 +0100363 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86_64"; }
364
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000365 bool IsFatal() const OVERRIDE { return is_fatal_; }
366
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000367 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000368 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000369 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000370
371 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86_64);
372};
373
Andreas Gampe85b62f22015-09-09 13:15:38 -0700374class DeoptimizationSlowPathX86_64 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700375 public:
376 explicit DeoptimizationSlowPathX86_64(HInstruction* instruction)
377 : instruction_(instruction) {}
378
379 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000380 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700381 __ Bind(GetEntryLabel());
382 SaveLiveRegisters(codegen, instruction_->GetLocations());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700383 DCHECK(instruction_->IsDeoptimize());
384 HDeoptimize* deoptimize = instruction_->AsDeoptimize();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000385 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
386 deoptimize,
387 deoptimize->GetDexPc(),
388 this);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700389 }
390
Alexandre Rames9931f312015-06-19 14:47:01 +0100391 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86_64"; }
392
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700393 private:
394 HInstruction* const instruction_;
395 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86_64);
396};
397
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100398class ArraySetSlowPathX86_64 : public SlowPathCode {
399 public:
400 explicit ArraySetSlowPathX86_64(HInstruction* instruction) : instruction_(instruction) {}
401
402 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
403 LocationSummary* locations = instruction_->GetLocations();
404 __ Bind(GetEntryLabel());
405 SaveLiveRegisters(codegen, locations);
406
407 InvokeRuntimeCallingConvention calling_convention;
408 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
409 parallel_move.AddMove(
410 locations->InAt(0),
411 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
412 Primitive::kPrimNot,
413 nullptr);
414 parallel_move.AddMove(
415 locations->InAt(1),
416 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
417 Primitive::kPrimInt,
418 nullptr);
419 parallel_move.AddMove(
420 locations->InAt(2),
421 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
422 Primitive::kPrimNot,
423 nullptr);
424 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
425
Roland Levillain0d5a2812015-11-13 10:07:31 +0000426 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
427 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
428 instruction_,
429 instruction_->GetDexPc(),
430 this);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100431 RestoreLiveRegisters(codegen, locations);
432 __ jmp(GetExitLabel());
433 }
434
435 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86_64"; }
436
437 private:
438 HInstruction* const instruction_;
439
440 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86_64);
441};
442
Roland Levillain0d5a2812015-11-13 10:07:31 +0000443// Slow path generating a read barrier for a heap reference.
444class ReadBarrierForHeapReferenceSlowPathX86_64 : public SlowPathCode {
445 public:
446 ReadBarrierForHeapReferenceSlowPathX86_64(HInstruction* instruction,
447 Location out,
448 Location ref,
449 Location obj,
450 uint32_t offset,
451 Location index)
452 : instruction_(instruction),
453 out_(out),
454 ref_(ref),
455 obj_(obj),
456 offset_(offset),
457 index_(index) {
458 DCHECK(kEmitCompilerReadBarrier);
459 // If `obj` is equal to `out` or `ref`, it means the initial
460 // object has been overwritten by (or after) the heap object
461 // reference load to be instrumented, e.g.:
462 //
463 // __ movl(out, Address(out, offset));
464 // codegen_->GenerateReadBarrier(instruction, out_loc, out_loc, out_loc, offset);
465 //
466 // In that case, we have lost the information about the original
467 // object, and the emitted read barrier cannot work properly.
468 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
469 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
470}
471
472 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
473 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
474 LocationSummary* locations = instruction_->GetLocations();
475 CpuRegister reg_out = out_.AsRegister<CpuRegister>();
476 DCHECK(locations->CanCall());
477 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out.AsRegister())) << out_;
478 DCHECK(!instruction_->IsInvoke() ||
479 (instruction_->IsInvokeStaticOrDirect() &&
480 instruction_->GetLocations()->Intrinsified()));
481
482 __ Bind(GetEntryLabel());
483 SaveLiveRegisters(codegen, locations);
484
485 // We may have to change the index's value, but as `index_` is a
486 // constant member (like other "inputs" of this slow path),
487 // introduce a copy of it, `index`.
488 Location index = index_;
489 if (index_.IsValid()) {
490 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
491 if (instruction_->IsArrayGet()) {
492 // Compute real offset and store it in index_.
493 Register index_reg = index_.AsRegister<CpuRegister>().AsRegister();
494 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
495 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
496 // We are about to change the value of `index_reg` (see the
497 // calls to art::x86_64::X86_64Assembler::shll and
498 // art::x86_64::X86_64Assembler::AddImmediate below), but it
499 // has not been saved by the previous call to
500 // art::SlowPathCode::SaveLiveRegisters, as it is a
501 // callee-save register --
502 // art::SlowPathCode::SaveLiveRegisters does not consider
503 // callee-save registers, as it has been designed with the
504 // assumption that callee-save registers are supposed to be
505 // handled by the called function. So, as a callee-save
506 // register, `index_reg` _would_ eventually be saved onto
507 // the stack, but it would be too late: we would have
508 // changed its value earlier. Therefore, we manually save
509 // it here into another freely available register,
510 // `free_reg`, chosen of course among the caller-save
511 // registers (as a callee-save `free_reg` register would
512 // exhibit the same problem).
513 //
514 // Note we could have requested a temporary register from
515 // the register allocator instead; but we prefer not to, as
516 // this is a slow path, and we know we can find a
517 // caller-save register that is available.
518 Register free_reg = FindAvailableCallerSaveRegister(codegen).AsRegister();
519 __ movl(CpuRegister(free_reg), CpuRegister(index_reg));
520 index_reg = free_reg;
521 index = Location::RegisterLocation(index_reg);
522 } else {
523 // The initial register stored in `index_` has already been
524 // saved in the call to art::SlowPathCode::SaveLiveRegisters
525 // (as it is not a callee-save register), so we can freely
526 // use it.
527 }
528 // Shifting the index value contained in `index_reg` by the
529 // scale factor (2) cannot overflow in practice, as the
530 // runtime is unable to allocate object arrays with a size
531 // larger than 2^26 - 1 (that is, 2^28 - 4 bytes).
532 __ shll(CpuRegister(index_reg), Immediate(TIMES_4));
533 static_assert(
534 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
535 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
536 __ AddImmediate(CpuRegister(index_reg), Immediate(offset_));
537 } else {
538 DCHECK(instruction_->IsInvoke());
539 DCHECK(instruction_->GetLocations()->Intrinsified());
540 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
541 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
542 << instruction_->AsInvoke()->GetIntrinsic();
543 DCHECK_EQ(offset_, 0U);
544 DCHECK(index_.IsRegister());
545 }
546 }
547
548 // We're moving two or three locations to locations that could
549 // overlap, so we need a parallel move resolver.
550 InvokeRuntimeCallingConvention calling_convention;
551 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
552 parallel_move.AddMove(ref_,
553 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
554 Primitive::kPrimNot,
555 nullptr);
556 parallel_move.AddMove(obj_,
557 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
558 Primitive::kPrimNot,
559 nullptr);
560 if (index.IsValid()) {
561 parallel_move.AddMove(index,
562 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
563 Primitive::kPrimInt,
564 nullptr);
565 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
566 } else {
567 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
568 __ movl(CpuRegister(calling_convention.GetRegisterAt(2)), Immediate(offset_));
569 }
570 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
571 instruction_,
572 instruction_->GetDexPc(),
573 this);
574 CheckEntrypointTypes<
575 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
576 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
577
578 RestoreLiveRegisters(codegen, locations);
579 __ jmp(GetExitLabel());
580 }
581
582 const char* GetDescription() const OVERRIDE {
583 return "ReadBarrierForHeapReferenceSlowPathX86_64";
584 }
585
586 private:
587 CpuRegister FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
588 size_t ref = static_cast<int>(ref_.AsRegister<CpuRegister>().AsRegister());
589 size_t obj = static_cast<int>(obj_.AsRegister<CpuRegister>().AsRegister());
590 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
591 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
592 return static_cast<CpuRegister>(i);
593 }
594 }
595 // We shall never fail to find a free caller-save register, as
596 // there are more than two core caller-save registers on x86-64
597 // (meaning it is possible to find one which is different from
598 // `ref` and `obj`).
599 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
600 LOG(FATAL) << "Could not find a free caller-save register";
601 UNREACHABLE();
602 }
603
604 HInstruction* const instruction_;
605 const Location out_;
606 const Location ref_;
607 const Location obj_;
608 const uint32_t offset_;
609 // An additional location containing an index to an array.
610 // Only used for HArrayGet and the UnsafeGetObject &
611 // UnsafeGetObjectVolatile intrinsics.
612 const Location index_;
613
614 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86_64);
615};
616
617// Slow path generating a read barrier for a GC root.
618class ReadBarrierForRootSlowPathX86_64 : public SlowPathCode {
619 public:
620 ReadBarrierForRootSlowPathX86_64(HInstruction* instruction, Location out, Location root)
621 : instruction_(instruction), out_(out), root_(root) {}
622
623 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
624 LocationSummary* locations = instruction_->GetLocations();
625 DCHECK(locations->CanCall());
626 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(out_.reg()));
627 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString());
628
629 __ Bind(GetEntryLabel());
630 SaveLiveRegisters(codegen, locations);
631
632 InvokeRuntimeCallingConvention calling_convention;
633 CodeGeneratorX86_64* x86_64_codegen = down_cast<CodeGeneratorX86_64*>(codegen);
634 x86_64_codegen->Move(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
635 x86_64_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
636 instruction_,
637 instruction_->GetDexPc(),
638 this);
639 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
640 x86_64_codegen->Move(out_, Location::RegisterLocation(RAX));
641
642 RestoreLiveRegisters(codegen, locations);
643 __ jmp(GetExitLabel());
644 }
645
646 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86_64"; }
647
648 private:
649 HInstruction* const instruction_;
650 const Location out_;
651 const Location root_;
652
653 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86_64);
654};
655
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100656#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100657#define __ down_cast<X86_64Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100658
Roland Levillain4fa13f62015-07-06 18:11:54 +0100659inline Condition X86_64IntegerCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700660 switch (cond) {
661 case kCondEQ: return kEqual;
662 case kCondNE: return kNotEqual;
663 case kCondLT: return kLess;
664 case kCondLE: return kLessEqual;
665 case kCondGT: return kGreater;
666 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700667 case kCondB: return kBelow;
668 case kCondBE: return kBelowEqual;
669 case kCondA: return kAbove;
670 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700671 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100672 LOG(FATAL) << "Unreachable";
673 UNREACHABLE();
674}
675
Aart Bike9f37602015-10-09 11:15:55 -0700676// Maps FP condition to x86_64 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100677inline Condition X86_64FPCondition(IfCondition cond) {
678 switch (cond) {
679 case kCondEQ: return kEqual;
680 case kCondNE: return kNotEqual;
681 case kCondLT: return kBelow;
682 case kCondLE: return kBelowEqual;
683 case kCondGT: return kAbove;
684 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700685 default: break; // should not happen
Roland Levillain4fa13f62015-07-06 18:11:54 +0100686 };
687 LOG(FATAL) << "Unreachable";
688 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700689}
690
Vladimir Markodc151b22015-10-15 18:02:30 +0100691HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86_64::GetSupportedInvokeStaticOrDirectDispatch(
692 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
693 MethodReference target_method ATTRIBUTE_UNUSED) {
694 switch (desired_dispatch_info.code_ptr_location) {
695 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
696 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
697 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
698 return HInvokeStaticOrDirect::DispatchInfo {
699 desired_dispatch_info.method_load_kind,
700 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
701 desired_dispatch_info.method_load_data,
702 0u
703 };
704 default:
705 return desired_dispatch_info;
706 }
707}
708
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800709void CodeGeneratorX86_64::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
Nicolas Geoffray38207af2015-06-01 15:46:22 +0100710 Location temp) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800711 // All registers are assumed to be correctly set up.
712
Vladimir Marko58155012015-08-19 12:49:41 +0000713 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
714 switch (invoke->GetMethodLoadKind()) {
715 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
716 // temp = thread->string_init_entrypoint
717 __ gs()->movl(temp.AsRegister<CpuRegister>(),
718 Address::Absolute(invoke->GetStringInitOffset(), true));
719 break;
720 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
721 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
722 break;
723 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
724 __ movq(temp.AsRegister<CpuRegister>(), Immediate(invoke->GetMethodAddress()));
725 break;
726 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
727 __ movl(temp.AsRegister<CpuRegister>(), Immediate(0)); // Placeholder.
728 method_patches_.emplace_back(invoke->GetTargetMethod());
729 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
730 break;
731 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000732 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
733 invoke->GetDexCacheArrayOffset());
Vladimir Marko58155012015-08-19 12:49:41 +0000734 __ movq(temp.AsRegister<CpuRegister>(),
735 Address::Absolute(kDummy32BitOffset, false /* no_rip */));
736 // Bind the label at the end of the "movl" insn.
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000737 __ Bind(&pc_relative_dex_cache_patches_.back().label);
Vladimir Marko58155012015-08-19 12:49:41 +0000738 break;
739 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
740 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
741 Register method_reg;
742 CpuRegister reg = temp.AsRegister<CpuRegister>();
743 if (current_method.IsRegister()) {
744 method_reg = current_method.AsRegister<Register>();
745 } else {
746 DCHECK(invoke->GetLocations()->Intrinsified());
747 DCHECK(!current_method.IsValid());
748 method_reg = reg.AsRegister();
749 __ movq(reg, Address(CpuRegister(RSP), kCurrentMethodStackOffset));
750 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000751 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +0100752 __ movq(reg,
753 Address(CpuRegister(method_reg),
754 ArtMethod::DexCacheResolvedMethodsOffset(kX86_64PointerSize).SizeValue()));
Vladimir Marko58155012015-08-19 12:49:41 +0000755 // temp = temp[index_in_cache]
756 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
757 __ movq(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
758 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +0100759 }
Vladimir Marko58155012015-08-19 12:49:41 +0000760 }
761
762 switch (invoke->GetCodePtrLocation()) {
763 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
764 __ call(&frame_entry_label_);
765 break;
766 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
767 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
768 Label* label = &relative_call_patches_.back().label;
769 __ call(label); // Bind to the patch label, override at link time.
770 __ Bind(label); // Bind the label at the end of the "call" insn.
771 break;
772 }
773 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
774 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +0100775 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
776 LOG(FATAL) << "Unsupported";
777 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +0000778 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
779 // (callee_method + offset_of_quick_compiled_code)()
780 __ call(Address(callee_method.AsRegister<CpuRegister>(),
781 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
782 kX86_64WordSize).SizeValue()));
783 break;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000784 }
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800785
786 DCHECK(!IsLeafMethod());
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800787}
788
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000789void CodeGeneratorX86_64::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
790 CpuRegister temp = temp_in.AsRegister<CpuRegister>();
791 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
792 invoke->GetVTableIndex(), kX86_64PointerSize).SizeValue();
793 LocationSummary* locations = invoke->GetLocations();
794 Location receiver = locations->InAt(0);
795 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000796 DCHECK(receiver.IsRegister());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000797 // /* HeapReference<Class> */ temp = receiver->klass_
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000798 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
799 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000800 // Instead of simply (possibly) unpoisoning `temp` here, we should
801 // emit a read barrier for the previous class reference load.
802 // However this is not required in practice, as this is an
803 // intermediate/temporary reference and because the current
804 // concurrent copying collector keeps the from-space memory
805 // intact/accessible until the end of the marking phase (the
806 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +0000807 __ MaybeUnpoisonHeapReference(temp);
808 // temp = temp->GetMethodAt(method_offset);
809 __ movq(temp, Address(temp, method_offset));
810 // call temp->GetEntryPoint();
811 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
812 kX86_64WordSize).SizeValue()));
813}
814
Vladimir Marko58155012015-08-19 12:49:41 +0000815void CodeGeneratorX86_64::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
816 DCHECK(linker_patches->empty());
817 size_t size =
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000818 method_patches_.size() +
819 relative_call_patches_.size() +
820 pc_relative_dex_cache_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +0000821 linker_patches->reserve(size);
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000822 // The label points to the end of the "movl" insn but the literal offset for method
823 // patch needs to point to the embedded constant which occupies the last 4 bytes.
824 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +0000825 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000826 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000827 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
828 info.target_method.dex_file,
829 info.target_method.dex_method_index));
830 }
831 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000832 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000833 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
834 info.target_method.dex_file,
835 info.target_method.dex_method_index));
836 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000837 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
838 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +0000839 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
840 &info.target_dex_file,
841 info.label.Position(),
842 info.element_offset));
843 }
844}
845
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100846void CodeGeneratorX86_64::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100847 stream << Register(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100848}
849
850void CodeGeneratorX86_64::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100851 stream << FloatRegister(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100852}
853
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100854size_t CodeGeneratorX86_64::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
855 __ movq(Address(CpuRegister(RSP), stack_index), CpuRegister(reg_id));
856 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100857}
858
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100859size_t CodeGeneratorX86_64::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
860 __ movq(CpuRegister(reg_id), Address(CpuRegister(RSP), stack_index));
861 return kX86_64WordSize;
862}
863
864size_t CodeGeneratorX86_64::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
865 __ movsd(Address(CpuRegister(RSP), stack_index), XmmRegister(reg_id));
866 return kX86_64WordSize;
867}
868
869size_t CodeGeneratorX86_64::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
870 __ movsd(XmmRegister(reg_id), Address(CpuRegister(RSP), stack_index));
871 return kX86_64WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100872}
873
Calin Juravle175dc732015-08-25 15:42:32 +0100874void CodeGeneratorX86_64::InvokeRuntime(QuickEntrypointEnum entrypoint,
875 HInstruction* instruction,
876 uint32_t dex_pc,
877 SlowPathCode* slow_path) {
878 InvokeRuntime(GetThreadOffset<kX86_64WordSize>(entrypoint).Int32Value(),
879 instruction,
880 dex_pc,
881 slow_path);
882}
883
884void CodeGeneratorX86_64::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100885 HInstruction* instruction,
886 uint32_t dex_pc,
887 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100888 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100889 __ gs()->call(Address::Absolute(entry_point_offset, true));
Alexandre Rames8158f282015-08-07 10:26:17 +0100890 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100891}
892
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000893static constexpr int kNumberOfCpuRegisterPairs = 0;
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000894// Use a fake return address register to mimic Quick.
895static constexpr Register kFakeReturnRegister = Register(kLastCpuRegister + 1);
Mark Mendellfb8d2792015-03-31 22:16:59 -0400896CodeGeneratorX86_64::CodeGeneratorX86_64(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000897 const X86_64InstructionSetFeatures& isa_features,
898 const CompilerOptions& compiler_options,
899 OptimizingCompilerStats* stats)
Nicolas Geoffray98893962015-01-21 12:32:32 +0000900 : CodeGenerator(graph,
901 kNumberOfCpuRegisters,
902 kNumberOfFloatRegisters,
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000903 kNumberOfCpuRegisterPairs,
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000904 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
905 arraysize(kCoreCalleeSaves))
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +0000906 | (1 << kFakeReturnRegister),
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000907 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
908 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100909 compiler_options,
910 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100911 block_labels_(nullptr),
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100912 location_builder_(graph, this),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000913 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400914 move_resolver_(graph->GetArena(), this),
Mark Mendellf55c3e02015-03-26 21:07:46 -0400915 isa_features_(isa_features),
Vladimir Marko58155012015-08-19 12:49:41 +0000916 constant_area_start_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +0100917 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
918 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000919 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell9c86b482015-09-18 13:36:07 -0400920 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000921 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
922}
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100923
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100924InstructionCodeGeneratorX86_64::InstructionCodeGeneratorX86_64(HGraph* graph,
925 CodeGeneratorX86_64* codegen)
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100926 : HGraphVisitor(graph),
927 assembler_(codegen->GetAssembler()),
928 codegen_(codegen) {}
929
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100930Location CodeGeneratorX86_64::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100931 switch (type) {
932 case Primitive::kPrimLong:
933 case Primitive::kPrimByte:
934 case Primitive::kPrimBoolean:
935 case Primitive::kPrimChar:
936 case Primitive::kPrimShort:
937 case Primitive::kPrimInt:
938 case Primitive::kPrimNot: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100939 size_t reg = FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100940 return Location::RegisterLocation(reg);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100941 }
942
943 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100944 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100945 size_t reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfFloatRegisters);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100946 return Location::FpuRegisterLocation(reg);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100947 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100948
949 case Primitive::kPrimVoid:
950 LOG(FATAL) << "Unreachable type " << type;
951 }
952
Roland Levillain0d5a2812015-11-13 10:07:31 +0000953 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100954}
955
Nicolas Geoffray98893962015-01-21 12:32:32 +0000956void CodeGeneratorX86_64::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100957 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100958 blocked_core_registers_[RSP] = true;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100959
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000960 // Block the register used as TMP.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100961 blocked_core_registers_[TMP] = true;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000962
Nicolas Geoffray98893962015-01-21 12:32:32 +0000963 if (is_baseline) {
964 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
965 blocked_core_registers_[kCoreCalleeSaves[i]] = true;
966 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000967 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
968 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
969 }
Nicolas Geoffray98893962015-01-21 12:32:32 +0000970 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100971}
972
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100973static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100974 return dwarf::Reg::X86_64Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100975}
David Srbecky9d8606d2015-04-12 09:35:32 +0100976
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100977static dwarf::Reg DWARFReg(FloatRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100978 return dwarf::Reg::X86_64Fp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100979}
980
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100981void CodeGeneratorX86_64::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100982 __ cfi().SetCurrentCFAOffset(kX86_64WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000983 __ Bind(&frame_entry_label_);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100984 bool skip_overflow_check = IsLeafMethod()
Dave Allison648d7112014-07-25 16:15:27 -0700985 && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86_64);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000986 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100987
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000988 if (!skip_overflow_check) {
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100989 __ testq(CpuRegister(RAX), Address(
990 CpuRegister(RSP), -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86_64))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100991 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +0100992 }
Nicolas Geoffraya26369a2015-01-22 08:46:05 +0000993
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000994 if (HasEmptyFrame()) {
995 return;
996 }
997
Nicolas Geoffray98893962015-01-21 12:32:32 +0000998 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000999 Register reg = kCoreCalleeSaves[i];
Nicolas Geoffray4597b5b2015-01-23 21:51:55 +00001000 if (allocated_registers_.ContainsCoreRegister(reg)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001001 __ pushq(CpuRegister(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001002 __ cfi().AdjustCFAOffset(kX86_64WordSize);
1003 __ cfi().RelOffset(DWARFReg(reg), 0);
Nicolas Geoffray98893962015-01-21 12:32:32 +00001004 }
1005 }
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +01001006
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001007 int adjust = GetFrameSize() - GetCoreSpillSize();
1008 __ subq(CpuRegister(RSP), Immediate(adjust));
1009 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001010 uint32_t xmm_spill_location = GetFpuSpillStart();
1011 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001012
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001013 for (int i = arraysize(kFpuCalleeSaves) - 1; i >= 0; --i) {
1014 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001015 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1016 __ movsd(Address(CpuRegister(RSP), offset), XmmRegister(kFpuCalleeSaves[i]));
1017 __ cfi().RelOffset(DWARFReg(kFpuCalleeSaves[i]), offset);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001018 }
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001019 }
1020
Mathieu Chartiere401d142015-04-22 13:56:20 -07001021 __ movq(Address(CpuRegister(RSP), kCurrentMethodStackOffset),
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001022 CpuRegister(kMethodRegisterArgument));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001023}
1024
1025void CodeGeneratorX86_64::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001026 __ cfi().RememberState();
1027 if (!HasEmptyFrame()) {
1028 uint32_t xmm_spill_location = GetFpuSpillStart();
1029 size_t xmm_spill_slot_size = GetFloatingPointSpillSlotSize();
1030 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1031 if (allocated_registers_.ContainsFloatingPointRegister(kFpuCalleeSaves[i])) {
1032 int offset = xmm_spill_location + (xmm_spill_slot_size * i);
1033 __ movsd(XmmRegister(kFpuCalleeSaves[i]), Address(CpuRegister(RSP), offset));
1034 __ cfi().Restore(DWARFReg(kFpuCalleeSaves[i]));
1035 }
1036 }
1037
1038 int adjust = GetFrameSize() - GetCoreSpillSize();
1039 __ addq(CpuRegister(RSP), Immediate(adjust));
1040 __ cfi().AdjustCFAOffset(-adjust);
1041
1042 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1043 Register reg = kCoreCalleeSaves[i];
1044 if (allocated_registers_.ContainsCoreRegister(reg)) {
1045 __ popq(CpuRegister(reg));
1046 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86_64WordSize));
1047 __ cfi().Restore(DWARFReg(reg));
1048 }
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001049 }
1050 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001051 __ ret();
1052 __ cfi().RestoreState();
1053 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001054}
1055
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001056void CodeGeneratorX86_64::Bind(HBasicBlock* block) {
1057 __ Bind(GetLabelOf(block));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001058}
1059
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001060Location CodeGeneratorX86_64::GetStackLocation(HLoadLocal* load) const {
1061 switch (load->GetType()) {
1062 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001063 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001064 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001065
1066 case Primitive::kPrimInt:
1067 case Primitive::kPrimNot:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001068 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001069 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001070
1071 case Primitive::kPrimBoolean:
1072 case Primitive::kPrimByte:
1073 case Primitive::kPrimChar:
1074 case Primitive::kPrimShort:
1075 case Primitive::kPrimVoid:
1076 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -07001077 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001078 }
1079
1080 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -07001081 UNREACHABLE();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001082}
1083
1084void CodeGeneratorX86_64::Move(Location destination, Location source) {
1085 if (source.Equals(destination)) {
1086 return;
1087 }
1088 if (destination.IsRegister()) {
1089 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001090 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001091 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001092 __ movd(destination.AsRegister<CpuRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001093 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001094 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001095 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001096 } else {
1097 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001098 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001099 Address(CpuRegister(RSP), source.GetStackIndex()));
1100 }
1101 } else if (destination.IsFpuRegister()) {
1102 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001103 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001104 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001105 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001106 } else if (source.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001107 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001108 Address(CpuRegister(RSP), source.GetStackIndex()));
1109 } else {
1110 DCHECK(source.IsDoubleStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001111 __ movsd(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001112 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001113 }
1114 } else if (destination.IsStackSlot()) {
1115 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001116 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001117 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001118 } else if (source.IsFpuRegister()) {
1119 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001120 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001121 } else if (source.IsConstant()) {
1122 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001123 int32_t value = GetInt32ValueOf(constant);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001124 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001125 } else {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001126 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001127 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1128 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001129 }
1130 } else {
1131 DCHECK(destination.IsDoubleStackSlot());
1132 if (source.IsRegister()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001133 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001134 source.AsRegister<CpuRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001135 } else if (source.IsFpuRegister()) {
1136 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00001137 source.AsFpuRegister<XmmRegister>());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001138 } else if (source.IsConstant()) {
1139 HConstant* constant = source.GetConstant();
Zheng Xu12bca972015-03-30 19:35:50 +08001140 int64_t value;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001141 if (constant->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001142 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Mark Mendell24f2dfa2015-01-14 19:51:45 -05001143 } else {
1144 DCHECK(constant->IsLongConstant());
1145 value = constant->AsLongConstant()->GetValue();
1146 }
Mark Mendellcfa410b2015-05-25 16:02:44 -04001147 Store64BitValueToStack(destination, value);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001148 } else {
1149 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00001150 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
1151 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001152 }
1153 }
1154}
1155
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001156void CodeGeneratorX86_64::Move(HInstruction* instruction,
1157 Location location,
1158 HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001159 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001160 if (instruction->IsCurrentMethod()) {
Mathieu Chartiere3b034a2015-05-31 14:29:23 -07001161 Move(location, Location::DoubleStackSlot(kCurrentMethodStackOffset));
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001162 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001163 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001164 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001165 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001166 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
1167 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +00001168 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001169 __ movl(location.AsRegister<CpuRegister>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +00001170 } else if (location.IsStackSlot()) {
1171 __ movl(Address(CpuRegister(RSP), location.GetStackIndex()), imm);
1172 } else {
1173 DCHECK(location.IsConstant());
1174 DCHECK_EQ(location.GetConstant(), const_to_move);
1175 }
1176 } else if (const_to_move->IsLongConstant()) {
1177 int64_t value = const_to_move->AsLongConstant()->GetValue();
1178 if (location.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04001179 Load64BitValue(location.AsRegister<CpuRegister>(), value);
Calin Juravlea21f5982014-11-13 15:53:04 +00001180 } else if (location.IsDoubleStackSlot()) {
Mark Mendellcfa410b2015-05-25 16:02:44 -04001181 Store64BitValueToStack(location, value);
Calin Juravlea21f5982014-11-13 15:53:04 +00001182 } else {
1183 DCHECK(location.IsConstant());
1184 DCHECK_EQ(location.GetConstant(), const_to_move);
1185 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001186 }
Roland Levillain476df552014-10-09 17:51:36 +01001187 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001188 switch (instruction->GetType()) {
1189 case Primitive::kPrimBoolean:
1190 case Primitive::kPrimByte:
1191 case Primitive::kPrimChar:
1192 case Primitive::kPrimShort:
1193 case Primitive::kPrimInt:
1194 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001195 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001196 Move(location, Location::StackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
1197 break;
1198
1199 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001200 case Primitive::kPrimDouble:
Roland Levillain199f3362014-11-27 17:15:16 +00001201 Move(location,
1202 Location::DoubleStackSlot(GetStackSlot(instruction->AsLoadLocal()->GetLocal())));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001203 break;
1204
1205 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001206 LOG(FATAL) << "Unexpected local type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001207 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001208 } else if (instruction->IsTemporary()) {
1209 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
1210 Move(location, temp_location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001211 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001212 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001213 switch (instruction->GetType()) {
1214 case Primitive::kPrimBoolean:
1215 case Primitive::kPrimByte:
1216 case Primitive::kPrimChar:
1217 case Primitive::kPrimShort:
1218 case Primitive::kPrimInt:
1219 case Primitive::kPrimNot:
1220 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001221 case Primitive::kPrimFloat:
1222 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +00001223 Move(location, locations->Out());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001224 break;
1225
1226 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001227 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001228 }
1229 }
1230}
1231
Calin Juravle175dc732015-08-25 15:42:32 +01001232void CodeGeneratorX86_64::MoveConstant(Location location, int32_t value) {
1233 DCHECK(location.IsRegister());
1234 Load64BitValue(location.AsRegister<CpuRegister>(), static_cast<int64_t>(value));
1235}
1236
Calin Juravlee460d1d2015-09-29 04:52:17 +01001237void CodeGeneratorX86_64::MoveLocation(
1238 Location dst, Location src, Primitive::Type dst_type ATTRIBUTE_UNUSED) {
1239 Move(dst, src);
1240}
1241
1242void CodeGeneratorX86_64::AddLocationAsTemp(Location location, LocationSummary* locations) {
1243 if (location.IsRegister()) {
1244 locations->AddTemp(location);
1245 } else {
1246 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1247 }
1248}
1249
David Brazdilfc6a86a2015-06-26 10:33:45 +00001250void InstructionCodeGeneratorX86_64::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001251 DCHECK(!successor->IsExitBlock());
1252
1253 HBasicBlock* block = got->GetBlock();
1254 HInstruction* previous = got->GetPrevious();
1255
1256 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001257 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001258 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1259 return;
1260 }
1261
1262 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1263 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1264 }
1265 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001266 __ jmp(codegen_->GetLabelOf(successor));
1267 }
1268}
1269
David Brazdilfc6a86a2015-06-26 10:33:45 +00001270void LocationsBuilderX86_64::VisitGoto(HGoto* got) {
1271 got->SetLocations(nullptr);
1272}
1273
1274void InstructionCodeGeneratorX86_64::VisitGoto(HGoto* got) {
1275 HandleGoto(got, got->GetSuccessor());
1276}
1277
1278void LocationsBuilderX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1279 try_boundary->SetLocations(nullptr);
1280}
1281
1282void InstructionCodeGeneratorX86_64::VisitTryBoundary(HTryBoundary* try_boundary) {
1283 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1284 if (!successor->IsExitBlock()) {
1285 HandleGoto(try_boundary, successor);
1286 }
1287}
1288
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001289void LocationsBuilderX86_64::VisitExit(HExit* exit) {
1290 exit->SetLocations(nullptr);
1291}
1292
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001293void InstructionCodeGeneratorX86_64::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001294}
1295
Mark Mendellc4701932015-04-10 13:18:51 -04001296void InstructionCodeGeneratorX86_64::GenerateFPJumps(HCondition* cond,
1297 Label* true_label,
1298 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001299 if (cond->IsFPConditionTrueIfNaN()) {
1300 __ j(kUnordered, true_label);
1301 } else if (cond->IsFPConditionFalseIfNaN()) {
1302 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001303 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001304 __ j(X86_64FPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001305}
1306
David Brazdil0debae72015-11-12 18:37:00 +00001307void InstructionCodeGeneratorX86_64::GenerateCompareTestAndBranch(HCondition* condition,
1308 Label* true_target_in,
1309 Label* false_target_in) {
1310 // Generated branching requires both targets to be explicit. If either of the
1311 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1312 Label fallthrough_target;
1313 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1314 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1315
Mark Mendellc4701932015-04-10 13:18:51 -04001316 LocationSummary* locations = condition->GetLocations();
1317 Location left = locations->InAt(0);
1318 Location right = locations->InAt(1);
1319
Mark Mendellc4701932015-04-10 13:18:51 -04001320 Primitive::Type type = condition->InputAt(0)->GetType();
1321 switch (type) {
1322 case Primitive::kPrimLong: {
1323 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1324 if (right.IsConstant()) {
1325 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1326 if (IsInt<32>(value)) {
1327 if (value == 0) {
1328 __ testq(left_reg, left_reg);
1329 } else {
1330 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1331 }
1332 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001333 // Value won't fit in a 32-bit integer.
Mark Mendellc4701932015-04-10 13:18:51 -04001334 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
1335 }
1336 } else if (right.IsDoubleStackSlot()) {
1337 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1338 } else {
1339 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1340 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001341 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Mark Mendellc4701932015-04-10 13:18:51 -04001342 break;
1343 }
1344 case Primitive::kPrimFloat: {
1345 if (right.IsFpuRegister()) {
1346 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1347 } else if (right.IsConstant()) {
1348 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1349 codegen_->LiteralFloatAddress(
1350 right.GetConstant()->AsFloatConstant()->GetValue()));
1351 } else {
1352 DCHECK(right.IsStackSlot());
1353 __ ucomiss(left.AsFpuRegister<XmmRegister>(),
1354 Address(CpuRegister(RSP), right.GetStackIndex()));
1355 }
1356 GenerateFPJumps(condition, true_target, false_target);
1357 break;
1358 }
1359 case Primitive::kPrimDouble: {
1360 if (right.IsFpuRegister()) {
1361 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1362 } else if (right.IsConstant()) {
1363 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1364 codegen_->LiteralDoubleAddress(
1365 right.GetConstant()->AsDoubleConstant()->GetValue()));
1366 } else {
1367 DCHECK(right.IsDoubleStackSlot());
1368 __ ucomisd(left.AsFpuRegister<XmmRegister>(),
1369 Address(CpuRegister(RSP), right.GetStackIndex()));
1370 }
1371 GenerateFPJumps(condition, true_target, false_target);
1372 break;
1373 }
1374 default:
1375 LOG(FATAL) << "Unexpected condition type " << type;
1376 }
1377
David Brazdil0debae72015-11-12 18:37:00 +00001378 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001379 __ jmp(false_target);
1380 }
David Brazdil0debae72015-11-12 18:37:00 +00001381
1382 if (fallthrough_target.IsLinked()) {
1383 __ Bind(&fallthrough_target);
1384 }
Mark Mendellc4701932015-04-10 13:18:51 -04001385}
1386
David Brazdil0debae72015-11-12 18:37:00 +00001387static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1388 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1389 // are set only strictly before `branch`. We can't use the eflags on long
1390 // conditions if they are materialized due to the complex branching.
1391 return cond->IsCondition() &&
1392 cond->GetNext() == branch &&
1393 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1394}
1395
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001396void InstructionCodeGeneratorX86_64::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001397 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001398 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001399 Label* false_target) {
1400 HInstruction* cond = instruction->InputAt(condition_input_index);
1401
1402 if (true_target == nullptr && false_target == nullptr) {
1403 // Nothing to do. The code always falls through.
1404 return;
1405 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001406 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001407 if (cond->AsIntConstant()->IsOne()) {
1408 if (true_target != nullptr) {
1409 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001410 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001411 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001412 DCHECK(cond->AsIntConstant()->IsZero());
1413 if (false_target != nullptr) {
1414 __ jmp(false_target);
1415 }
1416 }
1417 return;
1418 }
1419
1420 // The following code generates these patterns:
1421 // (1) true_target == nullptr && false_target != nullptr
1422 // - opposite condition true => branch to false_target
1423 // (2) true_target != nullptr && false_target == nullptr
1424 // - condition true => branch to true_target
1425 // (3) true_target != nullptr && false_target != nullptr
1426 // - condition true => branch to true_target
1427 // - branch to false_target
1428 if (IsBooleanValueOrMaterializedCondition(cond)) {
1429 if (AreEflagsSetFrom(cond, instruction)) {
1430 if (true_target == nullptr) {
1431 __ j(X86_64IntegerCondition(cond->AsCondition()->GetOppositeCondition()), false_target);
1432 } else {
1433 __ j(X86_64IntegerCondition(cond->AsCondition()->GetCondition()), true_target);
1434 }
1435 } else {
1436 // Materialized condition, compare against 0.
1437 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1438 if (lhs.IsRegister()) {
1439 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1440 } else {
1441 __ cmpl(Address(CpuRegister(RSP), lhs.GetStackIndex()), Immediate(0));
1442 }
1443 if (true_target == nullptr) {
1444 __ j(kEqual, false_target);
1445 } else {
1446 __ j(kNotEqual, true_target);
1447 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001448 }
1449 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001450 // Condition has not been materialized, use its inputs as the
1451 // comparison and its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001452 HCondition* condition = cond->AsCondition();
Mark Mendellc4701932015-04-10 13:18:51 -04001453
David Brazdil0debae72015-11-12 18:37:00 +00001454 // If this is a long or FP comparison that has been folded into
1455 // the HCondition, generate the comparison directly.
1456 Primitive::Type type = condition->InputAt(0)->GetType();
1457 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1458 GenerateCompareTestAndBranch(condition, true_target, false_target);
1459 return;
1460 }
1461
1462 Location lhs = condition->GetLocations()->InAt(0);
1463 Location rhs = condition->GetLocations()->InAt(1);
1464 if (rhs.IsRegister()) {
1465 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1466 } else if (rhs.IsConstant()) {
1467 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1468 if (constant == 0) {
1469 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001470 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001471 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001472 }
1473 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001474 __ cmpl(lhs.AsRegister<CpuRegister>(),
1475 Address(CpuRegister(RSP), rhs.GetStackIndex()));
1476 }
1477 if (true_target == nullptr) {
1478 __ j(X86_64IntegerCondition(condition->GetOppositeCondition()), false_target);
1479 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001480 __ j(X86_64IntegerCondition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001481 }
Dave Allison20dfc792014-06-16 20:44:29 -07001482 }
David Brazdil0debae72015-11-12 18:37:00 +00001483
1484 // If neither branch falls through (case 3), the conditional branch to `true_target`
1485 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1486 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001487 __ jmp(false_target);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001488 }
1489}
1490
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001491void LocationsBuilderX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001492 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1493 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001494 locations->SetInAt(0, Location::Any());
1495 }
1496}
1497
1498void InstructionCodeGeneratorX86_64::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001499 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1500 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1501 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1502 nullptr : codegen_->GetLabelOf(true_successor);
1503 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1504 nullptr : codegen_->GetLabelOf(false_successor);
1505 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001506}
1507
1508void LocationsBuilderX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
1509 LocationSummary* locations = new (GetGraph()->GetArena())
1510 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001511 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001512 locations->SetInAt(0, Location::Any());
1513 }
1514}
1515
1516void InstructionCodeGeneratorX86_64::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001517 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001518 DeoptimizationSlowPathX86_64(deoptimize);
1519 codegen_->AddSlowPath(slow_path);
David Brazdil0debae72015-11-12 18:37:00 +00001520 GenerateTestAndBranch(deoptimize,
1521 /* condition_input_index */ 0,
1522 slow_path->GetEntryLabel(),
1523 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001524}
1525
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001526void LocationsBuilderX86_64::VisitLocal(HLocal* local) {
1527 local->SetLocations(nullptr);
1528}
1529
1530void InstructionCodeGeneratorX86_64::VisitLocal(HLocal* local) {
1531 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
1532}
1533
1534void LocationsBuilderX86_64::VisitLoadLocal(HLoadLocal* local) {
1535 local->SetLocations(nullptr);
1536}
1537
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001538void InstructionCodeGeneratorX86_64::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001539 // Nothing to do, this is driven by the code generator.
1540}
1541
1542void LocationsBuilderX86_64::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001543 LocationSummary* locations =
1544 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001545 switch (store->InputAt(1)->GetType()) {
1546 case Primitive::kPrimBoolean:
1547 case Primitive::kPrimByte:
1548 case Primitive::kPrimChar:
1549 case Primitive::kPrimShort:
1550 case Primitive::kPrimInt:
1551 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001552 case Primitive::kPrimFloat:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001553 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1554 break;
1555
1556 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001557 case Primitive::kPrimDouble:
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001558 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1559 break;
1560
1561 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001562 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001563 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001564}
1565
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001566void InstructionCodeGeneratorX86_64::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001567}
1568
Roland Levillain0d37cd02015-05-27 16:39:19 +01001569void LocationsBuilderX86_64::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001570 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001571 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001572 // Handle the long/FP comparisons made in instruction simplification.
1573 switch (cond->InputAt(0)->GetType()) {
1574 case Primitive::kPrimLong:
1575 locations->SetInAt(0, Location::RequiresRegister());
1576 locations->SetInAt(1, Location::Any());
1577 break;
1578 case Primitive::kPrimFloat:
1579 case Primitive::kPrimDouble:
1580 locations->SetInAt(0, Location::RequiresFpuRegister());
1581 locations->SetInAt(1, Location::Any());
1582 break;
1583 default:
1584 locations->SetInAt(0, Location::RequiresRegister());
1585 locations->SetInAt(1, Location::Any());
1586 break;
1587 }
Roland Levillain0d37cd02015-05-27 16:39:19 +01001588 if (cond->NeedsMaterialization()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001589 locations->SetOut(Location::RequiresRegister());
1590 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001591}
1592
Roland Levillain0d37cd02015-05-27 16:39:19 +01001593void InstructionCodeGeneratorX86_64::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001594 if (!cond->NeedsMaterialization()) {
1595 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001596 }
Mark Mendellc4701932015-04-10 13:18:51 -04001597
1598 LocationSummary* locations = cond->GetLocations();
1599 Location lhs = locations->InAt(0);
1600 Location rhs = locations->InAt(1);
1601 CpuRegister reg = locations->Out().AsRegister<CpuRegister>();
1602 Label true_label, false_label;
1603
1604 switch (cond->InputAt(0)->GetType()) {
1605 default:
1606 // Integer case.
1607
1608 // Clear output register: setcc only sets the low byte.
1609 __ xorl(reg, reg);
1610
1611 if (rhs.IsRegister()) {
1612 __ cmpl(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1613 } else if (rhs.IsConstant()) {
1614 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1615 if (constant == 0) {
1616 __ testl(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1617 } else {
1618 __ cmpl(lhs.AsRegister<CpuRegister>(), Immediate(constant));
1619 }
1620 } else {
1621 __ cmpl(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1622 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001623 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001624 return;
1625 case Primitive::kPrimLong:
1626 // Clear output register: setcc only sets the low byte.
1627 __ xorl(reg, reg);
1628
1629 if (rhs.IsRegister()) {
1630 __ cmpq(lhs.AsRegister<CpuRegister>(), rhs.AsRegister<CpuRegister>());
1631 } else if (rhs.IsConstant()) {
1632 int64_t value = rhs.GetConstant()->AsLongConstant()->GetValue();
1633 if (IsInt<32>(value)) {
1634 if (value == 0) {
1635 __ testq(lhs.AsRegister<CpuRegister>(), lhs.AsRegister<CpuRegister>());
1636 } else {
1637 __ cmpq(lhs.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
1638 }
1639 } else {
1640 // Value won't fit in an int.
1641 __ cmpq(lhs.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
1642 }
1643 } else {
1644 __ cmpq(lhs.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), rhs.GetStackIndex()));
1645 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001646 __ setcc(X86_64IntegerCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001647 return;
1648 case Primitive::kPrimFloat: {
1649 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1650 if (rhs.IsConstant()) {
1651 float value = rhs.GetConstant()->AsFloatConstant()->GetValue();
1652 __ ucomiss(lhs_reg, codegen_->LiteralFloatAddress(value));
1653 } else if (rhs.IsStackSlot()) {
1654 __ ucomiss(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1655 } else {
1656 __ ucomiss(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1657 }
1658 GenerateFPJumps(cond, &true_label, &false_label);
1659 break;
1660 }
1661 case Primitive::kPrimDouble: {
1662 XmmRegister lhs_reg = lhs.AsFpuRegister<XmmRegister>();
1663 if (rhs.IsConstant()) {
1664 double value = rhs.GetConstant()->AsDoubleConstant()->GetValue();
1665 __ ucomisd(lhs_reg, codegen_->LiteralDoubleAddress(value));
1666 } else if (rhs.IsDoubleStackSlot()) {
1667 __ ucomisd(lhs_reg, Address(CpuRegister(RSP), rhs.GetStackIndex()));
1668 } else {
1669 __ ucomisd(lhs_reg, rhs.AsFpuRegister<XmmRegister>());
1670 }
1671 GenerateFPJumps(cond, &true_label, &false_label);
1672 break;
1673 }
1674 }
1675
1676 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001677 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001678
Roland Levillain4fa13f62015-07-06 18:11:54 +01001679 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001680 __ Bind(&false_label);
1681 __ xorl(reg, reg);
1682 __ jmp(&done_label);
1683
Roland Levillain4fa13f62015-07-06 18:11:54 +01001684 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001685 __ Bind(&true_label);
1686 __ movl(reg, Immediate(1));
1687 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001688}
1689
1690void LocationsBuilderX86_64::VisitEqual(HEqual* comp) {
1691 VisitCondition(comp);
1692}
1693
1694void InstructionCodeGeneratorX86_64::VisitEqual(HEqual* comp) {
1695 VisitCondition(comp);
1696}
1697
1698void LocationsBuilderX86_64::VisitNotEqual(HNotEqual* comp) {
1699 VisitCondition(comp);
1700}
1701
1702void InstructionCodeGeneratorX86_64::VisitNotEqual(HNotEqual* comp) {
1703 VisitCondition(comp);
1704}
1705
1706void LocationsBuilderX86_64::VisitLessThan(HLessThan* comp) {
1707 VisitCondition(comp);
1708}
1709
1710void InstructionCodeGeneratorX86_64::VisitLessThan(HLessThan* comp) {
1711 VisitCondition(comp);
1712}
1713
1714void LocationsBuilderX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1715 VisitCondition(comp);
1716}
1717
1718void InstructionCodeGeneratorX86_64::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1719 VisitCondition(comp);
1720}
1721
1722void LocationsBuilderX86_64::VisitGreaterThan(HGreaterThan* comp) {
1723 VisitCondition(comp);
1724}
1725
1726void InstructionCodeGeneratorX86_64::VisitGreaterThan(HGreaterThan* comp) {
1727 VisitCondition(comp);
1728}
1729
1730void LocationsBuilderX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1731 VisitCondition(comp);
1732}
1733
1734void InstructionCodeGeneratorX86_64::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1735 VisitCondition(comp);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001736}
1737
Aart Bike9f37602015-10-09 11:15:55 -07001738void LocationsBuilderX86_64::VisitBelow(HBelow* comp) {
1739 VisitCondition(comp);
1740}
1741
1742void InstructionCodeGeneratorX86_64::VisitBelow(HBelow* comp) {
1743 VisitCondition(comp);
1744}
1745
1746void LocationsBuilderX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
1747 VisitCondition(comp);
1748}
1749
1750void InstructionCodeGeneratorX86_64::VisitBelowOrEqual(HBelowOrEqual* comp) {
1751 VisitCondition(comp);
1752}
1753
1754void LocationsBuilderX86_64::VisitAbove(HAbove* comp) {
1755 VisitCondition(comp);
1756}
1757
1758void InstructionCodeGeneratorX86_64::VisitAbove(HAbove* comp) {
1759 VisitCondition(comp);
1760}
1761
1762void LocationsBuilderX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
1763 VisitCondition(comp);
1764}
1765
1766void InstructionCodeGeneratorX86_64::VisitAboveOrEqual(HAboveOrEqual* comp) {
1767 VisitCondition(comp);
1768}
1769
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001770void LocationsBuilderX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001771 LocationSummary* locations =
1772 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00001773 switch (compare->InputAt(0)->GetType()) {
1774 case Primitive::kPrimLong: {
1775 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001776 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001777 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1778 break;
1779 }
1780 case Primitive::kPrimFloat:
1781 case Primitive::kPrimDouble: {
1782 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04001783 locations->SetInAt(1, Location::Any());
Calin Juravleddb7df22014-11-25 20:56:51 +00001784 locations->SetOut(Location::RequiresRegister());
1785 break;
1786 }
1787 default:
1788 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
1789 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001790}
1791
1792void InstructionCodeGeneratorX86_64::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001793 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00001794 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Calin Juravleddb7df22014-11-25 20:56:51 +00001795 Location left = locations->InAt(0);
1796 Location right = locations->InAt(1);
1797
Mark Mendell0c9497d2015-08-21 09:30:05 -04001798 NearLabel less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00001799 Primitive::Type type = compare->InputAt(0)->GetType();
1800 switch (type) {
1801 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001802 CpuRegister left_reg = left.AsRegister<CpuRegister>();
1803 if (right.IsConstant()) {
1804 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell40741f32015-04-20 22:10:34 -04001805 if (IsInt<32>(value)) {
1806 if (value == 0) {
1807 __ testq(left_reg, left_reg);
1808 } else {
1809 __ cmpq(left_reg, Immediate(static_cast<int32_t>(value)));
1810 }
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001811 } else {
Mark Mendell40741f32015-04-20 22:10:34 -04001812 // Value won't fit in an int.
1813 __ cmpq(left_reg, codegen_->LiteralInt64Address(value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001814 }
Mark Mendell40741f32015-04-20 22:10:34 -04001815 } else if (right.IsDoubleStackSlot()) {
1816 __ cmpq(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04001817 } else {
1818 __ cmpq(left_reg, right.AsRegister<CpuRegister>());
1819 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001820 break;
Calin Juravleddb7df22014-11-25 20:56:51 +00001821 }
1822 case Primitive::kPrimFloat: {
Mark Mendell40741f32015-04-20 22:10:34 -04001823 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1824 if (right.IsConstant()) {
1825 float value = right.GetConstant()->AsFloatConstant()->GetValue();
1826 __ ucomiss(left_reg, codegen_->LiteralFloatAddress(value));
1827 } else if (right.IsStackSlot()) {
1828 __ ucomiss(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1829 } else {
1830 __ ucomiss(left_reg, right.AsFpuRegister<XmmRegister>());
1831 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001832 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1833 break;
1834 }
1835 case Primitive::kPrimDouble: {
Mark Mendell40741f32015-04-20 22:10:34 -04001836 XmmRegister left_reg = left.AsFpuRegister<XmmRegister>();
1837 if (right.IsConstant()) {
1838 double value = right.GetConstant()->AsDoubleConstant()->GetValue();
1839 __ ucomisd(left_reg, codegen_->LiteralDoubleAddress(value));
1840 } else if (right.IsDoubleStackSlot()) {
1841 __ ucomisd(left_reg, Address(CpuRegister(RSP), right.GetStackIndex()));
1842 } else {
1843 __ ucomisd(left_reg, right.AsFpuRegister<XmmRegister>());
1844 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001845 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
1846 break;
1847 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001848 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00001849 LOG(FATAL) << "Unexpected compare type " << type;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001850 }
Calin Juravleddb7df22014-11-25 20:56:51 +00001851 __ movl(out, Immediate(0));
Calin Juravle91debbc2014-11-26 19:01:09 +00001852 __ j(kEqual, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00001853 __ j(type == Primitive::kPrimLong ? kLess : kBelow, &less); // ucomis{s,d} sets CF (kBelow)
Calin Juravlefd861242014-11-25 20:56:51 +00001854
Calin Juravle91debbc2014-11-26 19:01:09 +00001855 __ Bind(&greater);
Calin Juravleddb7df22014-11-25 20:56:51 +00001856 __ movl(out, Immediate(1));
1857 __ jmp(&done);
1858
1859 __ Bind(&less);
1860 __ movl(out, Immediate(-1));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01001861
1862 __ Bind(&done);
1863}
1864
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001865void LocationsBuilderX86_64::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001866 LocationSummary* locations =
1867 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001868 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001869}
1870
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001871void InstructionCodeGeneratorX86_64::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001872 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001873}
1874
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001875void LocationsBuilderX86_64::VisitNullConstant(HNullConstant* constant) {
1876 LocationSummary* locations =
1877 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1878 locations->SetOut(Location::ConstantLocation(constant));
1879}
1880
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001881void InstructionCodeGeneratorX86_64::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001882 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001883}
1884
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001885void LocationsBuilderX86_64::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001886 LocationSummary* locations =
1887 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001888 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001889}
1890
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001891void InstructionCodeGeneratorX86_64::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001892 // Will be generated at use site.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001893}
1894
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001895void LocationsBuilderX86_64::VisitFloatConstant(HFloatConstant* constant) {
1896 LocationSummary* locations =
1897 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1898 locations->SetOut(Location::ConstantLocation(constant));
1899}
1900
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001901void InstructionCodeGeneratorX86_64::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001902 // Will be generated at use site.
1903}
1904
1905void LocationsBuilderX86_64::VisitDoubleConstant(HDoubleConstant* constant) {
1906 LocationSummary* locations =
1907 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1908 locations->SetOut(Location::ConstantLocation(constant));
1909}
1910
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001911void InstructionCodeGeneratorX86_64::VisitDoubleConstant(
1912 HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001913 // Will be generated at use site.
1914}
1915
Calin Juravle27df7582015-04-17 19:12:31 +01001916void LocationsBuilderX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1917 memory_barrier->SetLocations(nullptr);
1918}
1919
1920void InstructionCodeGeneratorX86_64::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1921 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1922}
1923
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001924void LocationsBuilderX86_64::VisitReturnVoid(HReturnVoid* ret) {
1925 ret->SetLocations(nullptr);
1926}
1927
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001928void InstructionCodeGeneratorX86_64::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001929 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001930}
1931
1932void LocationsBuilderX86_64::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001933 LocationSummary* locations =
1934 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001935 switch (ret->InputAt(0)->GetType()) {
1936 case Primitive::kPrimBoolean:
1937 case Primitive::kPrimByte:
1938 case Primitive::kPrimChar:
1939 case Primitive::kPrimShort:
1940 case Primitive::kPrimInt:
1941 case Primitive::kPrimNot:
1942 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001943 locations->SetInAt(0, Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001944 break;
1945
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001946 case Primitive::kPrimFloat:
1947 case Primitive::kPrimDouble:
Mark Mendell40741f32015-04-20 22:10:34 -04001948 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001949 break;
1950
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001951 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001952 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001953 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001954}
1955
1956void InstructionCodeGeneratorX86_64::VisitReturn(HReturn* ret) {
1957 if (kIsDebugBuild) {
1958 switch (ret->InputAt(0)->GetType()) {
1959 case Primitive::kPrimBoolean:
1960 case Primitive::kPrimByte:
1961 case Primitive::kPrimChar:
1962 case Primitive::kPrimShort:
1963 case Primitive::kPrimInt:
1964 case Primitive::kPrimNot:
1965 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001966 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<CpuRegister>().AsRegister(), RAX);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001967 break;
1968
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001969 case Primitive::kPrimFloat:
1970 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001971 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>().AsFloatRegister(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001972 XMM0);
1973 break;
1974
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001975 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001976 LOG(FATAL) << "Unexpected return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001977 }
1978 }
1979 codegen_->GenerateFrameExit();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001980}
1981
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001982Location InvokeDexCallingConventionVisitorX86_64::GetReturnLocation(Primitive::Type type) const {
1983 switch (type) {
1984 case Primitive::kPrimBoolean:
1985 case Primitive::kPrimByte:
1986 case Primitive::kPrimChar:
1987 case Primitive::kPrimShort:
1988 case Primitive::kPrimInt:
1989 case Primitive::kPrimNot:
1990 case Primitive::kPrimLong:
1991 return Location::RegisterLocation(RAX);
1992
1993 case Primitive::kPrimVoid:
1994 return Location::NoLocation();
1995
1996 case Primitive::kPrimDouble:
1997 case Primitive::kPrimFloat:
1998 return Location::FpuRegisterLocation(XMM0);
1999 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002000
2001 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002002}
2003
2004Location InvokeDexCallingConventionVisitorX86_64::GetMethodLocation() const {
2005 return Location::RegisterLocation(kMethodRegisterArgument);
2006}
2007
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002008Location InvokeDexCallingConventionVisitorX86_64::GetNextLocation(Primitive::Type type) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002009 switch (type) {
2010 case Primitive::kPrimBoolean:
2011 case Primitive::kPrimByte:
2012 case Primitive::kPrimChar:
2013 case Primitive::kPrimShort:
2014 case Primitive::kPrimInt:
2015 case Primitive::kPrimNot: {
2016 uint32_t index = gp_index_++;
2017 stack_index_++;
2018 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002019 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002020 } else {
2021 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2022 }
2023 }
2024
2025 case Primitive::kPrimLong: {
2026 uint32_t index = gp_index_;
2027 stack_index_ += 2;
2028 if (index < calling_convention.GetNumberOfRegisters()) {
2029 gp_index_ += 1;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002030 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002031 } else {
2032 gp_index_ += 2;
2033 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2034 }
2035 }
2036
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002037 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002038 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002039 stack_index_++;
2040 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002041 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002042 } else {
2043 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
2044 }
2045 }
2046
2047 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002048 uint32_t index = float_index_++;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002049 stack_index_ += 2;
2050 if (index < calling_convention.GetNumberOfFpuRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002051 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002052 } else {
2053 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
2054 }
2055 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002056
2057 case Primitive::kPrimVoid:
2058 LOG(FATAL) << "Unexpected parameter type " << type;
2059 break;
2060 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00002061 return Location::NoLocation();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002062}
2063
Calin Juravle175dc732015-08-25 15:42:32 +01002064void LocationsBuilderX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2065 // The trampoline uses the same calling convention as dex calling conventions,
2066 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2067 // the method_idx.
2068 HandleInvoke(invoke);
2069}
2070
2071void InstructionCodeGeneratorX86_64::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2072 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2073}
2074
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002075void LocationsBuilderX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002076 // When we do not run baseline, explicit clinit checks triggered by static
2077 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2078 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002079
Mark Mendellfb8d2792015-03-31 22:16:59 -04002080 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002081 if (intrinsic.TryDispatch(invoke)) {
2082 return;
2083 }
2084
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002085 HandleInvoke(invoke);
2086}
2087
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002088static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86_64* codegen) {
2089 if (invoke->GetLocations()->Intrinsified()) {
2090 IntrinsicCodeGeneratorX86_64 intrinsic(codegen);
2091 intrinsic.Dispatch(invoke);
2092 return true;
2093 }
2094 return false;
2095}
2096
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002097void InstructionCodeGeneratorX86_64::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002098 // When we do not run baseline, explicit clinit checks triggered by static
2099 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2100 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002101
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002102 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2103 return;
2104 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002105
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002106 LocationSummary* locations = invoke->GetLocations();
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002107 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002108 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002109 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002110}
2111
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002112void LocationsBuilderX86_64::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002113 InvokeDexCallingConventionVisitorX86_64 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002114 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002115}
2116
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002117void LocationsBuilderX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Mark Mendellfb8d2792015-03-31 22:16:59 -04002118 IntrinsicLocationsBuilderX86_64 intrinsic(codegen_);
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002119 if (intrinsic.TryDispatch(invoke)) {
2120 return;
2121 }
2122
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002123 HandleInvoke(invoke);
2124}
2125
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002126void InstructionCodeGeneratorX86_64::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -08002127 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2128 return;
2129 }
2130
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002131 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002132 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002133 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002134}
2135
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002136void LocationsBuilderX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2137 HandleInvoke(invoke);
2138 // Add the hidden argument.
2139 invoke->GetLocations()->AddTemp(Location::RegisterLocation(RAX));
2140}
2141
2142void InstructionCodeGeneratorX86_64::VisitInvokeInterface(HInvokeInterface* invoke) {
2143 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002144 LocationSummary* locations = invoke->GetLocations();
2145 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
2146 CpuRegister hidden_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002147 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2148 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86_64PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002149 Location receiver = locations->InAt(0);
2150 size_t class_offset = mirror::Object::ClassOffset().SizeValue();
2151
Roland Levillain0d5a2812015-11-13 10:07:31 +00002152 // Set the hidden argument. This is safe to do this here, as RAX
2153 // won't be modified thereafter, before the `call` instruction.
2154 DCHECK_EQ(RAX, hidden_reg.AsRegister());
Mark Mendell92e83bf2015-05-07 11:25:03 -04002155 codegen_->Load64BitValue(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002156
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002157 if (receiver.IsStackSlot()) {
2158 __ movl(temp, Address(CpuRegister(RSP), receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002159 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002160 __ movl(temp, Address(temp, class_offset));
2161 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002162 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002163 __ movl(temp, Address(receiver.AsRegister<CpuRegister>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002164 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002165 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002166 // Instead of simply (possibly) unpoisoning `temp` here, we should
2167 // emit a read barrier for the previous class reference load.
2168 // However this is not required in practice, as this is an
2169 // intermediate/temporary reference and because the current
2170 // concurrent copying collector keeps the from-space memory
2171 // intact/accessible until the end of the marking phase (the
2172 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002173 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002174 // temp = temp->GetImtEntryAt(method_offset);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002175 __ movq(temp, Address(temp, method_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002176 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002177 __ call(Address(temp,
2178 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86_64WordSize).SizeValue()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002179
2180 DCHECK(!codegen_->IsLeafMethod());
2181 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2182}
2183
Roland Levillain88cb1752014-10-20 16:36:47 +01002184void LocationsBuilderX86_64::VisitNeg(HNeg* neg) {
2185 LocationSummary* locations =
2186 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2187 switch (neg->GetResultType()) {
2188 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002189 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002190 locations->SetInAt(0, Location::RequiresRegister());
2191 locations->SetOut(Location::SameAsFirstInput());
2192 break;
2193
Roland Levillain88cb1752014-10-20 16:36:47 +01002194 case Primitive::kPrimFloat:
2195 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002196 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002197 locations->SetOut(Location::SameAsFirstInput());
Roland Levillain5368c212014-11-27 15:03:41 +00002198 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002199 break;
2200
2201 default:
2202 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2203 }
2204}
2205
2206void InstructionCodeGeneratorX86_64::VisitNeg(HNeg* neg) {
2207 LocationSummary* locations = neg->GetLocations();
2208 Location out = locations->Out();
2209 Location in = locations->InAt(0);
2210 switch (neg->GetResultType()) {
2211 case Primitive::kPrimInt:
2212 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002213 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002214 __ negl(out.AsRegister<CpuRegister>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002215 break;
2216
2217 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002218 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002219 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002220 __ negq(out.AsRegister<CpuRegister>());
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002221 break;
2222
Roland Levillain5368c212014-11-27 15:03:41 +00002223 case Primitive::kPrimFloat: {
2224 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002225 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002226 // Implement float negation with an exclusive or with value
2227 // 0x80000000 (mask for bit 31, representing the sign of a
2228 // single-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002229 __ movss(mask, codegen_->LiteralInt32Address(0x80000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002230 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002231 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002232 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002233
Roland Levillain5368c212014-11-27 15:03:41 +00002234 case Primitive::kPrimDouble: {
2235 DCHECK(in.Equals(out));
Mark Mendell40741f32015-04-20 22:10:34 -04002236 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002237 // Implement double negation with an exclusive or with value
Roland Levillain3dbcb382014-10-28 17:30:07 +00002238 // 0x8000000000000000 (mask for bit 63, representing the sign of
Roland Levillain5368c212014-11-27 15:03:41 +00002239 // a double-precision floating-point number).
Mark Mendell40741f32015-04-20 22:10:34 -04002240 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000)));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002241 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002242 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002243 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002244
2245 default:
2246 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2247 }
2248}
2249
Roland Levillaindff1f282014-11-05 14:15:05 +00002250void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2251 LocationSummary* locations =
2252 new (GetGraph()->GetArena()) LocationSummary(conversion, LocationSummary::kNoCall);
2253 Primitive::Type result_type = conversion->GetResultType();
2254 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002255 DCHECK_NE(result_type, input_type);
David Brazdil46e2a392015-03-16 17:31:52 +00002256
David Brazdilb2bd1c52015-03-25 11:17:37 +00002257 // The Java language does not allow treating boolean as an integral type but
2258 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002259
Roland Levillaindff1f282014-11-05 14:15:05 +00002260 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002261 case Primitive::kPrimByte:
2262 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002263 case Primitive::kPrimBoolean:
2264 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002265 case Primitive::kPrimShort:
2266 case Primitive::kPrimInt:
2267 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002268 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002269 locations->SetInAt(0, Location::Any());
2270 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2271 break;
2272
2273 default:
2274 LOG(FATAL) << "Unexpected type conversion from " << input_type
2275 << " to " << result_type;
2276 }
2277 break;
2278
Roland Levillain01a8d712014-11-14 16:27:39 +00002279 case Primitive::kPrimShort:
2280 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002281 case Primitive::kPrimBoolean:
2282 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002283 case Primitive::kPrimByte:
2284 case Primitive::kPrimInt:
2285 case Primitive::kPrimChar:
2286 // Processing a Dex `int-to-short' instruction.
2287 locations->SetInAt(0, Location::Any());
2288 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2289 break;
2290
2291 default:
2292 LOG(FATAL) << "Unexpected type conversion from " << input_type
2293 << " to " << result_type;
2294 }
2295 break;
2296
Roland Levillain946e1432014-11-11 17:35:19 +00002297 case Primitive::kPrimInt:
2298 switch (input_type) {
2299 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002300 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002301 locations->SetInAt(0, Location::Any());
2302 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2303 break;
2304
2305 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002306 // Processing a Dex `float-to-int' instruction.
2307 locations->SetInAt(0, Location::RequiresFpuRegister());
2308 locations->SetOut(Location::RequiresRegister());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002309 break;
2310
Roland Levillain946e1432014-11-11 17:35:19 +00002311 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002312 // Processing a Dex `double-to-int' instruction.
2313 locations->SetInAt(0, Location::RequiresFpuRegister());
2314 locations->SetOut(Location::RequiresRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002315 break;
2316
2317 default:
2318 LOG(FATAL) << "Unexpected type conversion from " << input_type
2319 << " to " << result_type;
2320 }
2321 break;
2322
Roland Levillaindff1f282014-11-05 14:15:05 +00002323 case Primitive::kPrimLong:
2324 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002325 case Primitive::kPrimBoolean:
2326 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002327 case Primitive::kPrimByte:
2328 case Primitive::kPrimShort:
2329 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002330 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002331 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002332 // TODO: We would benefit from a (to-be-implemented)
2333 // Location::RegisterOrStackSlot requirement for this input.
2334 locations->SetInAt(0, Location::RequiresRegister());
2335 locations->SetOut(Location::RequiresRegister());
2336 break;
2337
2338 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002339 // Processing a Dex `float-to-long' instruction.
2340 locations->SetInAt(0, Location::RequiresFpuRegister());
2341 locations->SetOut(Location::RequiresRegister());
Roland Levillain624279f2014-12-04 11:54:28 +00002342 break;
2343
Roland Levillaindff1f282014-11-05 14:15:05 +00002344 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002345 // Processing a Dex `double-to-long' instruction.
2346 locations->SetInAt(0, Location::RequiresFpuRegister());
2347 locations->SetOut(Location::RequiresRegister());
Roland Levillaindff1f282014-11-05 14:15:05 +00002348 break;
2349
2350 default:
2351 LOG(FATAL) << "Unexpected type conversion from " << input_type
2352 << " to " << result_type;
2353 }
2354 break;
2355
Roland Levillain981e4542014-11-14 11:47:14 +00002356 case Primitive::kPrimChar:
2357 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002358 case Primitive::kPrimBoolean:
2359 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002360 case Primitive::kPrimByte:
2361 case Primitive::kPrimShort:
2362 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002363 // Processing a Dex `int-to-char' instruction.
2364 locations->SetInAt(0, Location::Any());
2365 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2366 break;
2367
2368 default:
2369 LOG(FATAL) << "Unexpected type conversion from " << input_type
2370 << " to " << result_type;
2371 }
2372 break;
2373
Roland Levillaindff1f282014-11-05 14:15:05 +00002374 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002375 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002376 case Primitive::kPrimBoolean:
2377 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002378 case Primitive::kPrimByte:
2379 case Primitive::kPrimShort:
2380 case Primitive::kPrimInt:
2381 case Primitive::kPrimChar:
2382 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002383 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002384 locations->SetOut(Location::RequiresFpuRegister());
2385 break;
2386
2387 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002388 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002389 locations->SetInAt(0, Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002390 locations->SetOut(Location::RequiresFpuRegister());
2391 break;
2392
Roland Levillaincff13742014-11-17 14:32:17 +00002393 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002394 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002395 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002396 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002397 break;
2398
2399 default:
2400 LOG(FATAL) << "Unexpected type conversion from " << input_type
2401 << " to " << result_type;
2402 };
2403 break;
2404
Roland Levillaindff1f282014-11-05 14:15:05 +00002405 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002406 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002407 case Primitive::kPrimBoolean:
2408 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002409 case Primitive::kPrimByte:
2410 case Primitive::kPrimShort:
2411 case Primitive::kPrimInt:
2412 case Primitive::kPrimChar:
2413 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002414 locations->SetInAt(0, Location::Any());
Roland Levillaincff13742014-11-17 14:32:17 +00002415 locations->SetOut(Location::RequiresFpuRegister());
2416 break;
2417
2418 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002419 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002420 locations->SetInAt(0, Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002421 locations->SetOut(Location::RequiresFpuRegister());
2422 break;
2423
Roland Levillaincff13742014-11-17 14:32:17 +00002424 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002425 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002426 locations->SetInAt(0, Location::Any());
Roland Levillain8964e2b2014-12-04 12:10:50 +00002427 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002428 break;
2429
2430 default:
2431 LOG(FATAL) << "Unexpected type conversion from " << input_type
2432 << " to " << result_type;
2433 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002434 break;
2435
2436 default:
2437 LOG(FATAL) << "Unexpected type conversion from " << input_type
2438 << " to " << result_type;
2439 }
2440}
2441
2442void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conversion) {
2443 LocationSummary* locations = conversion->GetLocations();
2444 Location out = locations->Out();
2445 Location in = locations->InAt(0);
2446 Primitive::Type result_type = conversion->GetResultType();
2447 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002448 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002449 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002450 case Primitive::kPrimByte:
2451 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002452 case Primitive::kPrimBoolean:
2453 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002454 case Primitive::kPrimShort:
2455 case Primitive::kPrimInt:
2456 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002457 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002458 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002459 __ movsxb(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain51d3fc42014-11-13 14:11:42 +00002460 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002461 __ movsxb(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002462 Address(CpuRegister(RSP), in.GetStackIndex()));
2463 } else {
2464 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002465 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain51d3fc42014-11-13 14:11:42 +00002466 Immediate(static_cast<int8_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2467 }
2468 break;
2469
2470 default:
2471 LOG(FATAL) << "Unexpected type conversion from " << input_type
2472 << " to " << result_type;
2473 }
2474 break;
2475
Roland Levillain01a8d712014-11-14 16:27:39 +00002476 case Primitive::kPrimShort:
2477 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002478 case Primitive::kPrimBoolean:
2479 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002480 case Primitive::kPrimByte:
2481 case Primitive::kPrimInt:
2482 case Primitive::kPrimChar:
2483 // Processing a Dex `int-to-short' instruction.
2484 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002485 __ movsxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002486 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002487 __ movsxw(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002488 Address(CpuRegister(RSP), in.GetStackIndex()));
2489 } else {
2490 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002491 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain01a8d712014-11-14 16:27:39 +00002492 Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2493 }
2494 break;
2495
2496 default:
2497 LOG(FATAL) << "Unexpected type conversion from " << input_type
2498 << " to " << result_type;
2499 }
2500 break;
2501
Roland Levillain946e1432014-11-11 17:35:19 +00002502 case Primitive::kPrimInt:
2503 switch (input_type) {
2504 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002505 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002506 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002507 __ movl(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain946e1432014-11-11 17:35:19 +00002508 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002509 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain946e1432014-11-11 17:35:19 +00002510 Address(CpuRegister(RSP), in.GetStackIndex()));
2511 } else {
2512 DCHECK(in.IsConstant());
2513 DCHECK(in.GetConstant()->IsLongConstant());
2514 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002515 __ movl(out.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002516 }
2517 break;
2518
Roland Levillain3f8f9362014-12-02 17:45:01 +00002519 case Primitive::kPrimFloat: {
2520 // Processing a Dex `float-to-int' instruction.
2521 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2522 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002523 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002524
2525 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002526 // if input >= (float)INT_MAX goto done
2527 __ comiss(input, codegen_->LiteralFloatAddress(kPrimIntMax));
Roland Levillain3f8f9362014-12-02 17:45:01 +00002528 __ j(kAboveEqual, &done);
2529 // if input == NaN goto nan
2530 __ j(kUnordered, &nan);
2531 // output = float-to-int-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002532 __ cvttss2si(output, input, false);
Roland Levillain3f8f9362014-12-02 17:45:01 +00002533 __ jmp(&done);
2534 __ Bind(&nan);
2535 // output = 0
2536 __ xorl(output, output);
2537 __ Bind(&done);
2538 break;
2539 }
2540
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002541 case Primitive::kPrimDouble: {
2542 // Processing a Dex `double-to-int' instruction.
2543 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2544 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002545 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002546
2547 __ movl(output, Immediate(kPrimIntMax));
Mark Mendellcfa410b2015-05-25 16:02:44 -04002548 // if input >= (double)INT_MAX goto done
2549 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimIntMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002550 __ j(kAboveEqual, &done);
2551 // if input == NaN goto nan
2552 __ j(kUnordered, &nan);
2553 // output = double-to-int-truncate(input)
2554 __ cvttsd2si(output, input);
2555 __ jmp(&done);
2556 __ Bind(&nan);
2557 // output = 0
2558 __ xorl(output, output);
2559 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002560 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002561 }
Roland Levillain946e1432014-11-11 17:35:19 +00002562
2563 default:
2564 LOG(FATAL) << "Unexpected type conversion from " << input_type
2565 << " to " << result_type;
2566 }
2567 break;
2568
Roland Levillaindff1f282014-11-05 14:15:05 +00002569 case Primitive::kPrimLong:
2570 switch (input_type) {
2571 DCHECK(out.IsRegister());
David Brazdil46e2a392015-03-16 17:31:52 +00002572 case Primitive::kPrimBoolean:
2573 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002574 case Primitive::kPrimByte:
2575 case Primitive::kPrimShort:
2576 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002577 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002578 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002579 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002580 __ movsxd(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002581 break;
2582
Roland Levillain624279f2014-12-04 11:54:28 +00002583 case Primitive::kPrimFloat: {
2584 // Processing a Dex `float-to-long' instruction.
2585 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2586 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002587 NearLabel done, nan;
Roland Levillain624279f2014-12-04 11:54:28 +00002588
Mark Mendell92e83bf2015-05-07 11:25:03 -04002589 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002590 // if input >= (float)LONG_MAX goto done
2591 __ comiss(input, codegen_->LiteralFloatAddress(kPrimLongMax));
Roland Levillain624279f2014-12-04 11:54:28 +00002592 __ j(kAboveEqual, &done);
2593 // if input == NaN goto nan
2594 __ j(kUnordered, &nan);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002595 // output = float-to-long-truncate(input)
Roland Levillain624279f2014-12-04 11:54:28 +00002596 __ cvttss2si(output, input, true);
2597 __ jmp(&done);
2598 __ Bind(&nan);
2599 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002600 __ xorl(output, output);
Roland Levillain624279f2014-12-04 11:54:28 +00002601 __ Bind(&done);
2602 break;
2603 }
2604
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002605 case Primitive::kPrimDouble: {
2606 // Processing a Dex `double-to-long' instruction.
2607 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2608 CpuRegister output = out.AsRegister<CpuRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002609 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002610
Mark Mendell92e83bf2015-05-07 11:25:03 -04002611 codegen_->Load64BitValue(output, kPrimLongMax);
Mark Mendellcfa410b2015-05-25 16:02:44 -04002612 // if input >= (double)LONG_MAX goto done
2613 __ comisd(input, codegen_->LiteralDoubleAddress(kPrimLongMax));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002614 __ j(kAboveEqual, &done);
2615 // if input == NaN goto nan
2616 __ j(kUnordered, &nan);
2617 // output = double-to-long-truncate(input)
2618 __ cvttsd2si(output, input, true);
2619 __ jmp(&done);
2620 __ Bind(&nan);
2621 // output = 0
Mark Mendell92e83bf2015-05-07 11:25:03 -04002622 __ xorl(output, output);
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002623 __ Bind(&done);
Roland Levillaindff1f282014-11-05 14:15:05 +00002624 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002625 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002626
2627 default:
2628 LOG(FATAL) << "Unexpected type conversion from " << input_type
2629 << " to " << result_type;
2630 }
2631 break;
2632
Roland Levillain981e4542014-11-14 11:47:14 +00002633 case Primitive::kPrimChar:
2634 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002635 case Primitive::kPrimBoolean:
2636 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002637 case Primitive::kPrimByte:
2638 case Primitive::kPrimShort:
2639 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002640 // Processing a Dex `int-to-char' instruction.
2641 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002642 __ movzxw(out.AsRegister<CpuRegister>(), in.AsRegister<CpuRegister>());
Roland Levillain981e4542014-11-14 11:47:14 +00002643 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002644 __ movzxw(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002645 Address(CpuRegister(RSP), in.GetStackIndex()));
2646 } else {
2647 DCHECK(in.GetConstant()->IsIntConstant());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002648 __ movl(out.AsRegister<CpuRegister>(),
Roland Levillain981e4542014-11-14 11:47:14 +00002649 Immediate(static_cast<uint16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
2650 }
2651 break;
2652
2653 default:
2654 LOG(FATAL) << "Unexpected type conversion from " << input_type
2655 << " to " << result_type;
2656 }
2657 break;
2658
Roland Levillaindff1f282014-11-05 14:15:05 +00002659 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002660 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002661 case Primitive::kPrimBoolean:
2662 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002663 case Primitive::kPrimByte:
2664 case Primitive::kPrimShort:
2665 case Primitive::kPrimInt:
2666 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002667 // Processing a Dex `int-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002668 if (in.IsRegister()) {
2669 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2670 } else if (in.IsConstant()) {
2671 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2672 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2673 if (v == 0) {
2674 __ xorps(dest, dest);
2675 } else {
2676 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2677 }
2678 } else {
2679 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2680 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2681 }
Roland Levillaincff13742014-11-17 14:32:17 +00002682 break;
2683
2684 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002685 // Processing a Dex `long-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002686 if (in.IsRegister()) {
2687 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2688 } else if (in.IsConstant()) {
2689 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2690 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2691 if (v == 0) {
2692 __ xorps(dest, dest);
2693 } else {
2694 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2695 }
2696 } else {
2697 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(),
2698 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2699 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002700 break;
2701
Roland Levillaincff13742014-11-17 14:32:17 +00002702 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002703 // Processing a Dex `double-to-float' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002704 if (in.IsFpuRegister()) {
2705 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2706 } else if (in.IsConstant()) {
2707 double v = in.GetConstant()->AsDoubleConstant()->GetValue();
2708 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2709 if (bit_cast<int64_t, double>(v) == 0) {
2710 __ xorps(dest, dest);
2711 } else {
2712 __ movss(dest, codegen_->LiteralFloatAddress(static_cast<float>(v)));
2713 }
2714 } else {
2715 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(),
2716 Address(CpuRegister(RSP), in.GetStackIndex()));
2717 }
Roland Levillaincff13742014-11-17 14:32:17 +00002718 break;
2719
2720 default:
2721 LOG(FATAL) << "Unexpected type conversion from " << input_type
2722 << " to " << result_type;
2723 };
2724 break;
2725
Roland Levillaindff1f282014-11-05 14:15:05 +00002726 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002727 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002728 case Primitive::kPrimBoolean:
2729 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002730 case Primitive::kPrimByte:
2731 case Primitive::kPrimShort:
2732 case Primitive::kPrimInt:
2733 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002734 // Processing a Dex `int-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002735 if (in.IsRegister()) {
2736 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), false);
2737 } else if (in.IsConstant()) {
2738 int32_t v = in.GetConstant()->AsIntConstant()->GetValue();
2739 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2740 if (v == 0) {
2741 __ xorpd(dest, dest);
2742 } else {
2743 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2744 }
2745 } else {
2746 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2747 Address(CpuRegister(RSP), in.GetStackIndex()), false);
2748 }
Roland Levillaincff13742014-11-17 14:32:17 +00002749 break;
2750
2751 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002752 // Processing a Dex `long-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002753 if (in.IsRegister()) {
2754 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<CpuRegister>(), true);
2755 } else if (in.IsConstant()) {
2756 int64_t v = in.GetConstant()->AsLongConstant()->GetValue();
2757 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2758 if (v == 0) {
2759 __ xorpd(dest, dest);
2760 } else {
2761 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2762 }
2763 } else {
2764 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(),
2765 Address(CpuRegister(RSP), in.GetStackIndex()), true);
2766 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002767 break;
2768
Roland Levillaincff13742014-11-17 14:32:17 +00002769 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002770 // Processing a Dex `float-to-double' instruction.
Mark Mendell40741f32015-04-20 22:10:34 -04002771 if (in.IsFpuRegister()) {
2772 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
2773 } else if (in.IsConstant()) {
2774 float v = in.GetConstant()->AsFloatConstant()->GetValue();
2775 XmmRegister dest = out.AsFpuRegister<XmmRegister>();
2776 if (bit_cast<int32_t, float>(v) == 0) {
2777 __ xorpd(dest, dest);
2778 } else {
2779 __ movsd(dest, codegen_->LiteralDoubleAddress(static_cast<double>(v)));
2780 }
2781 } else {
2782 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(),
2783 Address(CpuRegister(RSP), in.GetStackIndex()));
2784 }
Roland Levillaincff13742014-11-17 14:32:17 +00002785 break;
2786
2787 default:
2788 LOG(FATAL) << "Unexpected type conversion from " << input_type
2789 << " to " << result_type;
2790 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002791 break;
2792
2793 default:
2794 LOG(FATAL) << "Unexpected type conversion from " << input_type
2795 << " to " << result_type;
2796 }
2797}
2798
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002799void LocationsBuilderX86_64::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002800 LocationSummary* locations =
2801 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002802 switch (add->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002803 case Primitive::kPrimInt: {
2804 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002805 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2806 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002807 break;
2808 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002809
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002810 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002811 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell09b84632015-02-13 17:48:38 -05002812 // We can use a leaq or addq if the constant can fit in an immediate.
Mark Mendellea5af682015-10-22 17:35:49 -04002813 locations->SetInAt(1, Location::RegisterOrInt32Constant(add->InputAt(1)));
Mark Mendell09b84632015-02-13 17:48:38 -05002814 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002815 break;
2816 }
2817
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002818 case Primitive::kPrimDouble:
2819 case Primitive::kPrimFloat: {
2820 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002821 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002822 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002823 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002824 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002825
2826 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002827 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002828 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002829}
2830
2831void InstructionCodeGeneratorX86_64::VisitAdd(HAdd* add) {
2832 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002833 Location first = locations->InAt(0);
2834 Location second = locations->InAt(1);
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002835 Location out = locations->Out();
Calin Juravle11351682014-10-23 15:38:15 +01002836
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002837 switch (add->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002838 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002839 if (second.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002840 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2841 __ addl(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002842 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2843 __ addl(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002844 } else {
2845 __ leal(out.AsRegister<CpuRegister>(), Address(
2846 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2847 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002848 } else if (second.IsConstant()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002849 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2850 __ addl(out.AsRegister<CpuRegister>(),
2851 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
2852 } else {
2853 __ leal(out.AsRegister<CpuRegister>(), Address(
2854 first.AsRegister<CpuRegister>(), second.GetConstant()->AsIntConstant()->GetValue()));
2855 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002856 } else {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00002857 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002858 __ addl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002859 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002860 break;
2861 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002862
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002863 case Primitive::kPrimLong: {
Mark Mendell09b84632015-02-13 17:48:38 -05002864 if (second.IsRegister()) {
2865 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2866 __ addq(out.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002867 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2868 __ addq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>());
Mark Mendell09b84632015-02-13 17:48:38 -05002869 } else {
2870 __ leaq(out.AsRegister<CpuRegister>(), Address(
2871 first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>(), TIMES_1, 0));
2872 }
2873 } else {
2874 DCHECK(second.IsConstant());
2875 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2876 int32_t int32_value = Low32Bits(value);
2877 DCHECK_EQ(int32_value, value);
2878 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2879 __ addq(out.AsRegister<CpuRegister>(), Immediate(int32_value));
2880 } else {
2881 __ leaq(out.AsRegister<CpuRegister>(), Address(
2882 first.AsRegister<CpuRegister>(), int32_value));
2883 }
2884 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002885 break;
2886 }
2887
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002888 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002889 if (second.IsFpuRegister()) {
2890 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2891 } else if (second.IsConstant()) {
2892 __ addss(first.AsFpuRegister<XmmRegister>(),
2893 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2894 } else {
2895 DCHECK(second.IsStackSlot());
2896 __ addss(first.AsFpuRegister<XmmRegister>(),
2897 Address(CpuRegister(RSP), second.GetStackIndex()));
2898 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002899 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002900 }
2901
2902 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002903 if (second.IsFpuRegister()) {
2904 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2905 } else if (second.IsConstant()) {
2906 __ addsd(first.AsFpuRegister<XmmRegister>(),
2907 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2908 } else {
2909 DCHECK(second.IsDoubleStackSlot());
2910 __ addsd(first.AsFpuRegister<XmmRegister>(),
2911 Address(CpuRegister(RSP), second.GetStackIndex()));
2912 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002913 break;
2914 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002915
2916 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002917 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002918 }
2919}
2920
2921void LocationsBuilderX86_64::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002922 LocationSummary* locations =
2923 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002924 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002925 case Primitive::kPrimInt: {
2926 locations->SetInAt(0, Location::RequiresRegister());
2927 locations->SetInAt(1, Location::Any());
2928 locations->SetOut(Location::SameAsFirstInput());
2929 break;
2930 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002931 case Primitive::kPrimLong: {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002932 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04002933 locations->SetInAt(1, Location::RegisterOrInt32Constant(sub->InputAt(1)));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002934 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002935 break;
2936 }
Calin Juravle11351682014-10-23 15:38:15 +01002937 case Primitive::kPrimFloat:
2938 case Primitive::kPrimDouble: {
2939 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04002940 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002941 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002942 break;
Calin Juravle11351682014-10-23 15:38:15 +01002943 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002944 default:
Calin Juravle11351682014-10-23 15:38:15 +01002945 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002946 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002947}
2948
2949void InstructionCodeGeneratorX86_64::VisitSub(HSub* sub) {
2950 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002951 Location first = locations->InAt(0);
2952 Location second = locations->InAt(1);
2953 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002954 switch (sub->GetResultType()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002955 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002956 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002957 __ subl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle11351682014-10-23 15:38:15 +01002958 } else if (second.IsConstant()) {
2959 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002960 __ subl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002961 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002962 __ subl(first.AsRegister<CpuRegister>(), Address(CpuRegister(RSP), second.GetStackIndex()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002963 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00002964 break;
2965 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002966 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04002967 if (second.IsConstant()) {
2968 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2969 DCHECK(IsInt<32>(value));
2970 __ subq(first.AsRegister<CpuRegister>(), Immediate(static_cast<int32_t>(value)));
2971 } else {
2972 __ subq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
2973 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002974 break;
2975 }
2976
Calin Juravle11351682014-10-23 15:38:15 +01002977 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002978 if (second.IsFpuRegister()) {
2979 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2980 } else if (second.IsConstant()) {
2981 __ subss(first.AsFpuRegister<XmmRegister>(),
2982 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
2983 } else {
2984 DCHECK(second.IsStackSlot());
2985 __ subss(first.AsFpuRegister<XmmRegister>(),
2986 Address(CpuRegister(RSP), second.GetStackIndex()));
2987 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01002988 break;
Calin Juravle11351682014-10-23 15:38:15 +01002989 }
2990
2991 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04002992 if (second.IsFpuRegister()) {
2993 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2994 } else if (second.IsConstant()) {
2995 __ subsd(first.AsFpuRegister<XmmRegister>(),
2996 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
2997 } else {
2998 DCHECK(second.IsDoubleStackSlot());
2999 __ subsd(first.AsFpuRegister<XmmRegister>(),
3000 Address(CpuRegister(RSP), second.GetStackIndex()));
3001 }
Calin Juravle11351682014-10-23 15:38:15 +01003002 break;
3003 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003004
3005 default:
Calin Juravle11351682014-10-23 15:38:15 +01003006 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003007 }
3008}
3009
Calin Juravle34bacdf2014-10-07 20:23:36 +01003010void LocationsBuilderX86_64::VisitMul(HMul* mul) {
3011 LocationSummary* locations =
3012 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3013 switch (mul->GetResultType()) {
3014 case Primitive::kPrimInt: {
3015 locations->SetInAt(0, Location::RequiresRegister());
3016 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003017 if (mul->InputAt(1)->IsIntConstant()) {
3018 // Can use 3 operand multiply.
3019 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3020 } else {
3021 locations->SetOut(Location::SameAsFirstInput());
3022 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003023 break;
3024 }
3025 case Primitive::kPrimLong: {
3026 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003027 locations->SetInAt(1, Location::Any());
3028 if (mul->InputAt(1)->IsLongConstant() &&
3029 IsInt<32>(mul->InputAt(1)->AsLongConstant()->GetValue())) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003030 // Can use 3 operand multiply.
3031 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3032 } else {
3033 locations->SetOut(Location::SameAsFirstInput());
3034 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003035 break;
3036 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003037 case Primitive::kPrimFloat:
3038 case Primitive::kPrimDouble: {
3039 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003040 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01003041 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003042 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003043 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003044
3045 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003046 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003047 }
3048}
3049
3050void InstructionCodeGeneratorX86_64::VisitMul(HMul* mul) {
3051 LocationSummary* locations = mul->GetLocations();
3052 Location first = locations->InAt(0);
3053 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003054 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003055 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003056 case Primitive::kPrimInt:
3057 // The constant may have ended up in a register, so test explicitly to avoid
3058 // problems where the output may not be the same as the first operand.
3059 if (mul->InputAt(1)->IsIntConstant()) {
3060 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3061 __ imull(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(), imm);
3062 } else if (second.IsRegister()) {
3063 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003064 __ imull(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003065 } else {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003066 DCHECK(first.Equals(out));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003067 DCHECK(second.IsStackSlot());
Roland Levillain199f3362014-11-27 17:15:16 +00003068 __ imull(first.AsRegister<CpuRegister>(),
3069 Address(CpuRegister(RSP), second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003070 }
3071 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003072 case Primitive::kPrimLong: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003073 // The constant may have ended up in a register, so test explicitly to avoid
3074 // problems where the output may not be the same as the first operand.
3075 if (mul->InputAt(1)->IsLongConstant()) {
3076 int64_t value = mul->InputAt(1)->AsLongConstant()->GetValue();
3077 if (IsInt<32>(value)) {
3078 __ imulq(out.AsRegister<CpuRegister>(), first.AsRegister<CpuRegister>(),
3079 Immediate(static_cast<int32_t>(value)));
3080 } else {
3081 // Have to use the constant area.
3082 DCHECK(first.Equals(out));
3083 __ imulq(first.AsRegister<CpuRegister>(), codegen_->LiteralInt64Address(value));
3084 }
3085 } else if (second.IsRegister()) {
3086 DCHECK(first.Equals(out));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003087 __ imulq(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003088 } else {
3089 DCHECK(second.IsDoubleStackSlot());
3090 DCHECK(first.Equals(out));
3091 __ imulq(first.AsRegister<CpuRegister>(),
3092 Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003093 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003094 break;
3095 }
3096
Calin Juravleb5bfa962014-10-21 18:02:24 +01003097 case Primitive::kPrimFloat: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003098 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003099 if (second.IsFpuRegister()) {
3100 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3101 } else if (second.IsConstant()) {
3102 __ mulss(first.AsFpuRegister<XmmRegister>(),
3103 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
3104 } else {
3105 DCHECK(second.IsStackSlot());
3106 __ mulss(first.AsFpuRegister<XmmRegister>(),
3107 Address(CpuRegister(RSP), second.GetStackIndex()));
3108 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003109 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003110 }
3111
3112 case Primitive::kPrimDouble: {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003113 DCHECK(first.Equals(out));
Mark Mendellf55c3e02015-03-26 21:07:46 -04003114 if (second.IsFpuRegister()) {
3115 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3116 } else if (second.IsConstant()) {
3117 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3118 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
3119 } else {
3120 DCHECK(second.IsDoubleStackSlot());
3121 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3122 Address(CpuRegister(RSP), second.GetStackIndex()));
3123 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003124 break;
3125 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003126
3127 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003128 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003129 }
3130}
3131
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003132void InstructionCodeGeneratorX86_64::PushOntoFPStack(Location source, uint32_t temp_offset,
3133 uint32_t stack_adjustment, bool is_float) {
3134 if (source.IsStackSlot()) {
3135 DCHECK(is_float);
3136 __ flds(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3137 } else if (source.IsDoubleStackSlot()) {
3138 DCHECK(!is_float);
3139 __ fldl(Address(CpuRegister(RSP), source.GetStackIndex() + stack_adjustment));
3140 } else {
3141 // Write the value to the temporary location on the stack and load to FP stack.
3142 if (is_float) {
3143 Location stack_temp = Location::StackSlot(temp_offset);
3144 codegen_->Move(stack_temp, source);
3145 __ flds(Address(CpuRegister(RSP), temp_offset));
3146 } else {
3147 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3148 codegen_->Move(stack_temp, source);
3149 __ fldl(Address(CpuRegister(RSP), temp_offset));
3150 }
3151 }
3152}
3153
3154void InstructionCodeGeneratorX86_64::GenerateRemFP(HRem *rem) {
3155 Primitive::Type type = rem->GetResultType();
3156 bool is_float = type == Primitive::kPrimFloat;
3157 size_t elem_size = Primitive::ComponentSize(type);
3158 LocationSummary* locations = rem->GetLocations();
3159 Location first = locations->InAt(0);
3160 Location second = locations->InAt(1);
3161 Location out = locations->Out();
3162
3163 // Create stack space for 2 elements.
3164 // TODO: enhance register allocator to ask for stack temporaries.
3165 __ subq(CpuRegister(RSP), Immediate(2 * elem_size));
3166
3167 // Load the values to the FP stack in reverse order, using temporaries if needed.
3168 PushOntoFPStack(second, elem_size, 2 * elem_size, is_float);
3169 PushOntoFPStack(first, 0, 2 * elem_size, is_float);
3170
3171 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003172 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003173 __ Bind(&retry);
3174 __ fprem();
3175
3176 // Move FP status to AX.
3177 __ fstsw();
3178
3179 // And see if the argument reduction is complete. This is signaled by the
3180 // C2 FPU flag bit set to 0.
3181 __ andl(CpuRegister(RAX), Immediate(kC2ConditionMask));
3182 __ j(kNotEqual, &retry);
3183
3184 // We have settled on the final value. Retrieve it into an XMM register.
3185 // Store FP top of stack to real stack.
3186 if (is_float) {
3187 __ fsts(Address(CpuRegister(RSP), 0));
3188 } else {
3189 __ fstl(Address(CpuRegister(RSP), 0));
3190 }
3191
3192 // Pop the 2 items from the FP stack.
3193 __ fucompp();
3194
3195 // Load the value from the stack into an XMM register.
3196 DCHECK(out.IsFpuRegister()) << out;
3197 if (is_float) {
3198 __ movss(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3199 } else {
3200 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(CpuRegister(RSP), 0));
3201 }
3202
3203 // And remove the temporary stack space we allocated.
3204 __ addq(CpuRegister(RSP), Immediate(2 * elem_size));
3205}
3206
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003207void InstructionCodeGeneratorX86_64::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3208 DCHECK(instruction->IsDiv() || instruction->IsRem());
3209
3210 LocationSummary* locations = instruction->GetLocations();
3211 Location second = locations->InAt(1);
3212 DCHECK(second.IsConstant());
3213
3214 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3215 CpuRegister input_register = locations->InAt(0).AsRegister<CpuRegister>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003216 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003217
3218 DCHECK(imm == 1 || imm == -1);
3219
3220 switch (instruction->GetResultType()) {
3221 case Primitive::kPrimInt: {
3222 if (instruction->IsRem()) {
3223 __ xorl(output_register, output_register);
3224 } else {
3225 __ movl(output_register, input_register);
3226 if (imm == -1) {
3227 __ negl(output_register);
3228 }
3229 }
3230 break;
3231 }
3232
3233 case Primitive::kPrimLong: {
3234 if (instruction->IsRem()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003235 __ xorl(output_register, output_register);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003236 } else {
3237 __ movq(output_register, input_register);
3238 if (imm == -1) {
3239 __ negq(output_register);
3240 }
3241 }
3242 break;
3243 }
3244
3245 default:
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003246 LOG(FATAL) << "Unexpected type for div by (-)1 " << instruction->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003247 }
3248}
3249
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003250void InstructionCodeGeneratorX86_64::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003251 LocationSummary* locations = instruction->GetLocations();
3252 Location second = locations->InAt(1);
3253
3254 CpuRegister output_register = locations->Out().AsRegister<CpuRegister>();
3255 CpuRegister numerator = locations->InAt(0).AsRegister<CpuRegister>();
3256
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003257 int64_t imm = Int64FromConstant(second.GetConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003258
3259 DCHECK(IsPowerOfTwo(std::abs(imm)));
3260
3261 CpuRegister tmp = locations->GetTemp(0).AsRegister<CpuRegister>();
3262
3263 if (instruction->GetResultType() == Primitive::kPrimInt) {
3264 __ leal(tmp, Address(numerator, std::abs(imm) - 1));
3265 __ testl(numerator, numerator);
3266 __ cmov(kGreaterEqual, tmp, numerator);
3267 int shift = CTZ(imm);
3268 __ sarl(tmp, Immediate(shift));
3269
3270 if (imm < 0) {
3271 __ negl(tmp);
3272 }
3273
3274 __ movl(output_register, tmp);
3275 } else {
3276 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3277 CpuRegister rdx = locations->GetTemp(0).AsRegister<CpuRegister>();
3278
Mark Mendell92e83bf2015-05-07 11:25:03 -04003279 codegen_->Load64BitValue(rdx, std::abs(imm) - 1);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003280 __ addq(rdx, numerator);
3281 __ testq(numerator, numerator);
3282 __ cmov(kGreaterEqual, rdx, numerator);
3283 int shift = CTZ(imm);
3284 __ sarq(rdx, Immediate(shift));
3285
3286 if (imm < 0) {
3287 __ negq(rdx);
3288 }
3289
3290 __ movq(output_register, rdx);
3291 }
3292}
3293
3294void InstructionCodeGeneratorX86_64::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3295 DCHECK(instruction->IsDiv() || instruction->IsRem());
3296
3297 LocationSummary* locations = instruction->GetLocations();
3298 Location second = locations->InAt(1);
3299
3300 CpuRegister numerator = instruction->IsDiv() ? locations->GetTemp(1).AsRegister<CpuRegister>()
3301 : locations->GetTemp(0).AsRegister<CpuRegister>();
3302 CpuRegister eax = locations->InAt(0).AsRegister<CpuRegister>();
3303 CpuRegister edx = instruction->IsDiv() ? locations->GetTemp(0).AsRegister<CpuRegister>()
3304 : locations->Out().AsRegister<CpuRegister>();
3305 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3306
3307 DCHECK_EQ(RAX, eax.AsRegister());
3308 DCHECK_EQ(RDX, edx.AsRegister());
3309 if (instruction->IsDiv()) {
3310 DCHECK_EQ(RAX, out.AsRegister());
3311 } else {
3312 DCHECK_EQ(RDX, out.AsRegister());
3313 }
3314
3315 int64_t magic;
3316 int shift;
3317
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003318 // TODO: can these branches be written as one?
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003319 if (instruction->GetResultType() == Primitive::kPrimInt) {
3320 int imm = second.GetConstant()->AsIntConstant()->GetValue();
3321
3322 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3323
3324 __ movl(numerator, eax);
3325
Mark Mendell0c9497d2015-08-21 09:30:05 -04003326 NearLabel no_div;
3327 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003328 __ testl(eax, eax);
3329 __ j(kNotEqual, &no_div);
3330
3331 __ xorl(out, out);
3332 __ jmp(&end);
3333
3334 __ Bind(&no_div);
3335
3336 __ movl(eax, Immediate(magic));
3337 __ imull(numerator);
3338
3339 if (imm > 0 && magic < 0) {
3340 __ addl(edx, numerator);
3341 } else if (imm < 0 && magic > 0) {
3342 __ subl(edx, numerator);
3343 }
3344
3345 if (shift != 0) {
3346 __ sarl(edx, Immediate(shift));
3347 }
3348
3349 __ movl(eax, edx);
3350 __ shrl(edx, Immediate(31));
3351 __ addl(edx, eax);
3352
3353 if (instruction->IsRem()) {
3354 __ movl(eax, numerator);
3355 __ imull(edx, Immediate(imm));
3356 __ subl(eax, edx);
3357 __ movl(edx, eax);
3358 } else {
3359 __ movl(eax, edx);
3360 }
3361 __ Bind(&end);
3362 } else {
3363 int64_t imm = second.GetConstant()->AsLongConstant()->GetValue();
3364
3365 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
3366
3367 CpuRegister rax = eax;
3368 CpuRegister rdx = edx;
3369
3370 CalculateMagicAndShiftForDivRem(imm, true /* is_long */, &magic, &shift);
3371
3372 // Save the numerator.
3373 __ movq(numerator, rax);
3374
3375 // RAX = magic
Mark Mendell92e83bf2015-05-07 11:25:03 -04003376 codegen_->Load64BitValue(rax, magic);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003377
3378 // RDX:RAX = magic * numerator
3379 __ imulq(numerator);
3380
3381 if (imm > 0 && magic < 0) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003382 // RDX += numerator
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003383 __ addq(rdx, numerator);
3384 } else if (imm < 0 && magic > 0) {
3385 // RDX -= numerator
3386 __ subq(rdx, numerator);
3387 }
3388
3389 // Shift if needed.
3390 if (shift != 0) {
3391 __ sarq(rdx, Immediate(shift));
3392 }
3393
3394 // RDX += 1 if RDX < 0
3395 __ movq(rax, rdx);
3396 __ shrq(rdx, Immediate(63));
3397 __ addq(rdx, rax);
3398
3399 if (instruction->IsRem()) {
3400 __ movq(rax, numerator);
3401
3402 if (IsInt<32>(imm)) {
3403 __ imulq(rdx, Immediate(static_cast<int32_t>(imm)));
3404 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04003405 __ imulq(rdx, codegen_->LiteralInt64Address(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003406 }
3407
3408 __ subq(rax, rdx);
3409 __ movq(rdx, rax);
3410 } else {
3411 __ movq(rax, rdx);
3412 }
3413 }
3414}
3415
Calin Juravlebacfec32014-11-14 15:54:36 +00003416void InstructionCodeGeneratorX86_64::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3417 DCHECK(instruction->IsDiv() || instruction->IsRem());
3418 Primitive::Type type = instruction->GetResultType();
3419 DCHECK(type == Primitive::kPrimInt || Primitive::kPrimLong);
3420
3421 bool is_div = instruction->IsDiv();
3422 LocationSummary* locations = instruction->GetLocations();
3423
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003424 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
3425 Location second = locations->InAt(1);
Calin Juravlebacfec32014-11-14 15:54:36 +00003426
Roland Levillain271ab9c2014-11-27 15:23:57 +00003427 DCHECK_EQ(RAX, locations->InAt(0).AsRegister<CpuRegister>().AsRegister());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003428 DCHECK_EQ(is_div ? RAX : RDX, out.AsRegister());
Calin Juravlebacfec32014-11-14 15:54:36 +00003429
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003430 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003431 int64_t imm = Int64FromConstant(second.GetConstant());
Calin Juravlebacfec32014-11-14 15:54:36 +00003432
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003433 if (imm == 0) {
3434 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3435 } else if (imm == 1 || imm == -1) {
3436 DivRemOneOrMinusOne(instruction);
3437 } else if (instruction->IsDiv() && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003438 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003439 } else {
3440 DCHECK(imm <= -2 || imm >= 2);
3441 GenerateDivRemWithAnyConstant(instruction);
3442 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003443 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003444 SlowPathCode* slow_path =
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003445 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86_64(
3446 out.AsRegister(), type, is_div);
3447 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003448
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003449 CpuRegister second_reg = second.AsRegister<CpuRegister>();
3450 // 0x80000000(00000000)/-1 triggers an arithmetic exception!
3451 // Dividing by -1 is actually negation and -0x800000000(00000000) = 0x80000000(00000000)
3452 // so it's safe to just use negl instead of more complex comparisons.
3453 if (type == Primitive::kPrimInt) {
3454 __ cmpl(second_reg, Immediate(-1));
3455 __ j(kEqual, slow_path->GetEntryLabel());
3456 // edx:eax <- sign-extended of eax
3457 __ cdq();
3458 // eax = quotient, edx = remainder
3459 __ idivl(second_reg);
3460 } else {
3461 __ cmpq(second_reg, Immediate(-1));
3462 __ j(kEqual, slow_path->GetEntryLabel());
3463 // rdx:rax <- sign-extended of rax
3464 __ cqo();
3465 // rax = quotient, rdx = remainder
3466 __ idivq(second_reg);
3467 }
3468 __ Bind(slow_path->GetExitLabel());
3469 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003470}
3471
Calin Juravle7c4954d2014-10-28 16:57:40 +00003472void LocationsBuilderX86_64::VisitDiv(HDiv* div) {
3473 LocationSummary* locations =
3474 new (GetGraph()->GetArena()) LocationSummary(div, LocationSummary::kNoCall);
3475 switch (div->GetResultType()) {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003476 case Primitive::kPrimInt:
3477 case Primitive::kPrimLong: {
Calin Juravled0d48522014-11-04 16:40:20 +00003478 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003479 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003480 locations->SetOut(Location::SameAsFirstInput());
3481 // Intel uses edx:eax as the dividend.
3482 locations->AddTemp(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003483 // We need to save the numerator while we tweak rax and rdx. As we are using imul in a way
3484 // which enforces results to be in RAX and RDX, things are simpler if we use RDX also as
3485 // output and request another temp.
3486 if (div->InputAt(1)->IsConstant()) {
3487 locations->AddTemp(Location::RequiresRegister());
3488 }
Calin Juravled0d48522014-11-04 16:40:20 +00003489 break;
3490 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003491
Calin Juravle7c4954d2014-10-28 16:57:40 +00003492 case Primitive::kPrimFloat:
3493 case Primitive::kPrimDouble: {
3494 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendellf55c3e02015-03-26 21:07:46 -04003495 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003496 locations->SetOut(Location::SameAsFirstInput());
3497 break;
3498 }
3499
3500 default:
3501 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3502 }
3503}
3504
3505void InstructionCodeGeneratorX86_64::VisitDiv(HDiv* div) {
3506 LocationSummary* locations = div->GetLocations();
3507 Location first = locations->InAt(0);
3508 Location second = locations->InAt(1);
3509 DCHECK(first.Equals(locations->Out()));
3510
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003511 Primitive::Type type = div->GetResultType();
3512 switch (type) {
3513 case Primitive::kPrimInt:
3514 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003515 GenerateDivRemIntegral(div);
Calin Juravled0d48522014-11-04 16:40:20 +00003516 break;
3517 }
3518
Calin Juravle7c4954d2014-10-28 16:57:40 +00003519 case Primitive::kPrimFloat: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003520 if (second.IsFpuRegister()) {
3521 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3522 } else if (second.IsConstant()) {
3523 __ divss(first.AsFpuRegister<XmmRegister>(),
3524 codegen_->LiteralFloatAddress(second.GetConstant()->AsFloatConstant()->GetValue()));
3525 } else {
3526 DCHECK(second.IsStackSlot());
3527 __ divss(first.AsFpuRegister<XmmRegister>(),
3528 Address(CpuRegister(RSP), second.GetStackIndex()));
3529 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003530 break;
3531 }
3532
3533 case Primitive::kPrimDouble: {
Mark Mendellf55c3e02015-03-26 21:07:46 -04003534 if (second.IsFpuRegister()) {
3535 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3536 } else if (second.IsConstant()) {
3537 __ divsd(first.AsFpuRegister<XmmRegister>(),
3538 codegen_->LiteralDoubleAddress(second.GetConstant()->AsDoubleConstant()->GetValue()));
3539 } else {
3540 DCHECK(second.IsDoubleStackSlot());
3541 __ divsd(first.AsFpuRegister<XmmRegister>(),
3542 Address(CpuRegister(RSP), second.GetStackIndex()));
3543 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003544 break;
3545 }
3546
3547 default:
3548 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3549 }
3550}
3551
Calin Juravlebacfec32014-11-14 15:54:36 +00003552void LocationsBuilderX86_64::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003553 Primitive::Type type = rem->GetResultType();
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003554 LocationSummary* locations =
3555 new (GetGraph()->GetArena()) LocationSummary(rem, LocationSummary::kNoCall);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003556
3557 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003558 case Primitive::kPrimInt:
3559 case Primitive::kPrimLong: {
3560 locations->SetInAt(0, Location::RegisterLocation(RAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003561 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003562 // Intel uses rdx:rax as the dividend and puts the remainder in rdx
3563 locations->SetOut(Location::RegisterLocation(RDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003564 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3565 // which enforces results to be in RAX and RDX, things are simpler if we use EAX also as
3566 // output and request another temp.
3567 if (rem->InputAt(1)->IsConstant()) {
3568 locations->AddTemp(Location::RequiresRegister());
3569 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003570 break;
3571 }
3572
3573 case Primitive::kPrimFloat:
3574 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003575 locations->SetInAt(0, Location::Any());
3576 locations->SetInAt(1, Location::Any());
3577 locations->SetOut(Location::RequiresFpuRegister());
3578 locations->AddTemp(Location::RegisterLocation(RAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003579 break;
3580 }
3581
3582 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003583 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003584 }
3585}
3586
3587void InstructionCodeGeneratorX86_64::VisitRem(HRem* rem) {
3588 Primitive::Type type = rem->GetResultType();
3589 switch (type) {
3590 case Primitive::kPrimInt:
3591 case Primitive::kPrimLong: {
3592 GenerateDivRemIntegral(rem);
3593 break;
3594 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003595 case Primitive::kPrimFloat:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003596 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003597 GenerateRemFP(rem);
Calin Juravled2ec87d2014-12-08 14:24:46 +00003598 break;
3599 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003600 default:
3601 LOG(FATAL) << "Unexpected rem type " << rem->GetResultType();
3602 }
3603}
3604
Calin Juravled0d48522014-11-04 16:40:20 +00003605void LocationsBuilderX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003606 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3607 ? LocationSummary::kCallOnSlowPath
3608 : LocationSummary::kNoCall;
3609 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled0d48522014-11-04 16:40:20 +00003610 locations->SetInAt(0, Location::Any());
3611 if (instruction->HasUses()) {
3612 locations->SetOut(Location::SameAsFirstInput());
3613 }
3614}
3615
3616void InstructionCodeGeneratorX86_64::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003617 SlowPathCode* slow_path =
Calin Juravled0d48522014-11-04 16:40:20 +00003618 new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86_64(instruction);
3619 codegen_->AddSlowPath(slow_path);
3620
3621 LocationSummary* locations = instruction->GetLocations();
3622 Location value = locations->InAt(0);
3623
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003624 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003625 case Primitive::kPrimByte:
3626 case Primitive::kPrimChar:
3627 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003628 case Primitive::kPrimInt: {
3629 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003630 __ testl(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003631 __ j(kEqual, slow_path->GetEntryLabel());
3632 } else if (value.IsStackSlot()) {
3633 __ cmpl(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3634 __ j(kEqual, slow_path->GetEntryLabel());
3635 } else {
3636 DCHECK(value.IsConstant()) << value;
3637 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3638 __ jmp(slow_path->GetEntryLabel());
3639 }
3640 }
3641 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003642 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003643 case Primitive::kPrimLong: {
3644 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003645 __ testq(value.AsRegister<CpuRegister>(), value.AsRegister<CpuRegister>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003646 __ j(kEqual, slow_path->GetEntryLabel());
3647 } else if (value.IsDoubleStackSlot()) {
3648 __ cmpq(Address(CpuRegister(RSP), value.GetStackIndex()), Immediate(0));
3649 __ j(kEqual, slow_path->GetEntryLabel());
3650 } else {
3651 DCHECK(value.IsConstant()) << value;
3652 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3653 __ jmp(slow_path->GetEntryLabel());
3654 }
3655 }
3656 break;
3657 }
3658 default:
3659 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003660 }
Calin Juravled0d48522014-11-04 16:40:20 +00003661}
3662
Calin Juravle9aec02f2014-11-18 23:06:35 +00003663void LocationsBuilderX86_64::HandleShift(HBinaryOperation* op) {
3664 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3665
3666 LocationSummary* locations =
3667 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3668
3669 switch (op->GetResultType()) {
3670 case Primitive::kPrimInt:
3671 case Primitive::kPrimLong: {
3672 locations->SetInAt(0, Location::RequiresRegister());
3673 // The shift count needs to be in CL.
3674 locations->SetInAt(1, Location::ByteRegisterOrConstant(RCX, op->InputAt(1)));
3675 locations->SetOut(Location::SameAsFirstInput());
3676 break;
3677 }
3678 default:
3679 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3680 }
3681}
3682
3683void InstructionCodeGeneratorX86_64::HandleShift(HBinaryOperation* op) {
3684 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3685
3686 LocationSummary* locations = op->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003687 CpuRegister first_reg = locations->InAt(0).AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003688 Location second = locations->InAt(1);
3689
3690 switch (op->GetResultType()) {
3691 case Primitive::kPrimInt: {
3692 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003693 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003694 if (op->IsShl()) {
3695 __ shll(first_reg, second_reg);
3696 } else if (op->IsShr()) {
3697 __ sarl(first_reg, second_reg);
3698 } else {
3699 __ shrl(first_reg, second_reg);
3700 }
3701 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003702 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003703 if (op->IsShl()) {
3704 __ shll(first_reg, imm);
3705 } else if (op->IsShr()) {
3706 __ sarl(first_reg, imm);
3707 } else {
3708 __ shrl(first_reg, imm);
3709 }
3710 }
3711 break;
3712 }
3713 case Primitive::kPrimLong: {
3714 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003715 CpuRegister second_reg = second.AsRegister<CpuRegister>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003716 if (op->IsShl()) {
3717 __ shlq(first_reg, second_reg);
3718 } else if (op->IsShr()) {
3719 __ sarq(first_reg, second_reg);
3720 } else {
3721 __ shrq(first_reg, second_reg);
3722 }
3723 } else {
Nicolas Geoffray486cc192014-12-08 18:00:55 +00003724 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003725 if (op->IsShl()) {
3726 __ shlq(first_reg, imm);
3727 } else if (op->IsShr()) {
3728 __ sarq(first_reg, imm);
3729 } else {
3730 __ shrq(first_reg, imm);
3731 }
3732 }
3733 break;
3734 }
3735 default:
3736 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3737 }
3738}
3739
3740void LocationsBuilderX86_64::VisitShl(HShl* shl) {
3741 HandleShift(shl);
3742}
3743
3744void InstructionCodeGeneratorX86_64::VisitShl(HShl* shl) {
3745 HandleShift(shl);
3746}
3747
3748void LocationsBuilderX86_64::VisitShr(HShr* shr) {
3749 HandleShift(shr);
3750}
3751
3752void InstructionCodeGeneratorX86_64::VisitShr(HShr* shr) {
3753 HandleShift(shr);
3754}
3755
3756void LocationsBuilderX86_64::VisitUShr(HUShr* ushr) {
3757 HandleShift(ushr);
3758}
3759
3760void InstructionCodeGeneratorX86_64::VisitUShr(HUShr* ushr) {
3761 HandleShift(ushr);
3762}
3763
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003764void LocationsBuilderX86_64::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003765 LocationSummary* locations =
3766 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01003767 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003768 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003769 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003770 locations->SetOut(Location::RegisterLocation(RAX));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003771}
3772
3773void InstructionCodeGeneratorX86_64::VisitNewInstance(HNewInstance* instruction) {
3774 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003775 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3776 instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003777 // Note: if heap poisoning is enabled, the entry point takes cares
3778 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01003779
Calin Juravle175dc732015-08-25 15:42:32 +01003780 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3781 instruction,
3782 instruction->GetDexPc(),
3783 nullptr);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003784
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003785 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003786}
3787
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003788void LocationsBuilderX86_64::VisitNewArray(HNewArray* instruction) {
3789 LocationSummary* locations =
3790 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3791 InvokeRuntimeCallingConvention calling_convention;
3792 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003793 locations->SetOut(Location::RegisterLocation(RAX));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003794 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003795 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003796}
3797
3798void InstructionCodeGeneratorX86_64::VisitNewArray(HNewArray* instruction) {
3799 InvokeRuntimeCallingConvention calling_convention;
Mark Mendell92e83bf2015-05-07 11:25:03 -04003800 codegen_->Load64BitValue(CpuRegister(calling_convention.GetRegisterAt(0)),
3801 instruction->GetTypeIndex());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003802
Roland Levillain4d027112015-07-01 15:41:14 +01003803 // Note: if heap poisoning is enabled, the entry point takes cares
3804 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003805 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3806 instruction,
3807 instruction->GetDexPc(),
3808 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003809
3810 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003811}
3812
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003813void LocationsBuilderX86_64::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003814 LocationSummary* locations =
3815 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003816 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3817 if (location.IsStackSlot()) {
3818 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3819 } else if (location.IsDoubleStackSlot()) {
3820 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3821 }
3822 locations->SetOut(location);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003823}
3824
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003825void InstructionCodeGeneratorX86_64::VisitParameterValue(
3826 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003827 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003828}
3829
3830void LocationsBuilderX86_64::VisitCurrentMethod(HCurrentMethod* instruction) {
3831 LocationSummary* locations =
3832 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3833 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3834}
3835
3836void InstructionCodeGeneratorX86_64::VisitCurrentMethod(
3837 HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3838 // Nothing to do, the method is already at its location.
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003839}
3840
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003841void LocationsBuilderX86_64::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003842 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003843 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00003844 locations->SetInAt(0, Location::RequiresRegister());
3845 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003846}
3847
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003848void InstructionCodeGeneratorX86_64::VisitNot(HNot* not_) {
3849 LocationSummary* locations = not_->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003850 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3851 locations->Out().AsRegister<CpuRegister>().AsRegister());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003852 Location out = locations->Out();
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003853 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003854 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003855 __ notl(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003856 break;
3857
3858 case Primitive::kPrimLong:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003859 __ notq(out.AsRegister<CpuRegister>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003860 break;
3861
3862 default:
3863 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3864 }
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003865}
3866
David Brazdil66d126e2015-04-03 16:02:44 +01003867void LocationsBuilderX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
3868 LocationSummary* locations =
3869 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3870 locations->SetInAt(0, Location::RequiresRegister());
3871 locations->SetOut(Location::SameAsFirstInput());
3872}
3873
3874void InstructionCodeGeneratorX86_64::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003875 LocationSummary* locations = bool_not->GetLocations();
3876 DCHECK_EQ(locations->InAt(0).AsRegister<CpuRegister>().AsRegister(),
3877 locations->Out().AsRegister<CpuRegister>().AsRegister());
3878 Location out = locations->Out();
3879 __ xorl(out.AsRegister<CpuRegister>(), Immediate(1));
3880}
3881
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003882void LocationsBuilderX86_64::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003883 LocationSummary* locations =
3884 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003885 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3886 locations->SetInAt(i, Location::Any());
3887 }
3888 locations->SetOut(Location::Any());
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003889}
3890
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003891void InstructionCodeGeneratorX86_64::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01003892 LOG(FATAL) << "Unimplemented";
3893}
3894
Calin Juravle52c48962014-12-16 17:02:57 +00003895void InstructionCodeGeneratorX86_64::GenerateMemoryBarrier(MemBarrierKind kind) {
3896 /*
3897 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3898 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3899 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3900 */
3901 switch (kind) {
3902 case MemBarrierKind::kAnyAny: {
3903 __ mfence();
3904 break;
3905 }
3906 case MemBarrierKind::kAnyStore:
3907 case MemBarrierKind::kLoadAny:
3908 case MemBarrierKind::kStoreStore: {
3909 // nop
3910 break;
3911 }
3912 default:
3913 LOG(FATAL) << "Unexpected memory barier " << kind;
3914 }
3915}
3916
3917void LocationsBuilderX86_64::HandleFieldGet(HInstruction* instruction) {
3918 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3919
Roland Levillain0d5a2812015-11-13 10:07:31 +00003920 bool object_field_get_with_read_barrier =
3921 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01003922 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00003923 new (GetGraph()->GetArena()) LocationSummary(instruction,
3924 object_field_get_with_read_barrier ?
3925 LocationSummary::kCallOnSlowPath :
3926 LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00003927 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003928 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3929 locations->SetOut(Location::RequiresFpuRegister());
3930 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00003931 // The output overlaps for an object field get when read barriers
3932 // are enabled: we do not want the move to overwrite the object's
3933 // location, as we need it to emit the read barrier.
3934 locations->SetOut(
3935 Location::RequiresRegister(),
3936 object_field_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003937 }
Calin Juravle52c48962014-12-16 17:02:57 +00003938}
3939
3940void InstructionCodeGeneratorX86_64::HandleFieldGet(HInstruction* instruction,
3941 const FieldInfo& field_info) {
3942 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
3943
3944 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00003945 Location base_loc = locations->InAt(0);
3946 CpuRegister base = base_loc.AsRegister<CpuRegister>();
Calin Juravle52c48962014-12-16 17:02:57 +00003947 Location out = locations->Out();
3948 bool is_volatile = field_info.IsVolatile();
3949 Primitive::Type field_type = field_info.GetFieldType();
3950 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3951
3952 switch (field_type) {
3953 case Primitive::kPrimBoolean: {
3954 __ movzxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3955 break;
3956 }
3957
3958 case Primitive::kPrimByte: {
3959 __ movsxb(out.AsRegister<CpuRegister>(), Address(base, offset));
3960 break;
3961 }
3962
3963 case Primitive::kPrimShort: {
3964 __ movsxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3965 break;
3966 }
3967
3968 case Primitive::kPrimChar: {
3969 __ movzxw(out.AsRegister<CpuRegister>(), Address(base, offset));
3970 break;
3971 }
3972
3973 case Primitive::kPrimInt:
3974 case Primitive::kPrimNot: {
3975 __ movl(out.AsRegister<CpuRegister>(), Address(base, offset));
3976 break;
3977 }
3978
3979 case Primitive::kPrimLong: {
3980 __ movq(out.AsRegister<CpuRegister>(), Address(base, offset));
3981 break;
3982 }
3983
3984 case Primitive::kPrimFloat: {
3985 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3986 break;
3987 }
3988
3989 case Primitive::kPrimDouble: {
3990 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
3991 break;
3992 }
3993
3994 case Primitive::kPrimVoid:
3995 LOG(FATAL) << "Unreachable type " << field_type;
3996 UNREACHABLE();
3997 }
3998
Calin Juravle77520bc2015-01-12 18:45:46 +00003999 codegen_->MaybeRecordImplicitNullCheck(instruction);
4000
Calin Juravle52c48962014-12-16 17:02:57 +00004001 if (is_volatile) {
4002 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4003 }
Roland Levillain4d027112015-07-01 15:41:14 +01004004
4005 if (field_type == Primitive::kPrimNot) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004006 codegen_->MaybeGenerateReadBarrier(instruction, out, out, base_loc, offset);
Roland Levillain4d027112015-07-01 15:41:14 +01004007 }
Calin Juravle52c48962014-12-16 17:02:57 +00004008}
4009
4010void LocationsBuilderX86_64::HandleFieldSet(HInstruction* instruction,
4011 const FieldInfo& field_info) {
4012 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4013
4014 LocationSummary* locations =
4015 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain4d027112015-07-01 15:41:14 +01004016 Primitive::Type field_type = field_info.GetFieldType();
Mark Mendellea5af682015-10-22 17:35:49 -04004017 bool is_volatile = field_info.IsVolatile();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004018 bool needs_write_barrier =
Roland Levillain4d027112015-07-01 15:41:14 +01004019 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004020
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004021 locations->SetInAt(0, Location::RequiresRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004022 if (Primitive::IsFloatingPointType(instruction->InputAt(1)->GetType())) {
Mark Mendellea5af682015-10-22 17:35:49 -04004023 if (is_volatile) {
4024 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4025 locations->SetInAt(1, Location::FpuRegisterOrInt32Constant(instruction->InputAt(1)));
4026 } else {
4027 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4028 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004029 } else {
Mark Mendellea5af682015-10-22 17:35:49 -04004030 if (is_volatile) {
4031 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4032 locations->SetInAt(1, Location::RegisterOrInt32Constant(instruction->InputAt(1)));
4033 } else {
4034 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4035 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004036 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004037 if (needs_write_barrier) {
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004038 // Temporary registers for the write barrier.
Roland Levillain4d027112015-07-01 15:41:14 +01004039 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004040 locations->AddTemp(Location::RequiresRegister());
Roland Levillain4d027112015-07-01 15:41:14 +01004041 } else if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4042 // Temporary register for the reference poisoning.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004043 locations->AddTemp(Location::RequiresRegister());
4044 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004045}
4046
Calin Juravle52c48962014-12-16 17:02:57 +00004047void InstructionCodeGeneratorX86_64::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004048 const FieldInfo& field_info,
4049 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004050 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4051
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004052 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004053 CpuRegister base = locations->InAt(0).AsRegister<CpuRegister>();
4054 Location value = locations->InAt(1);
4055 bool is_volatile = field_info.IsVolatile();
4056 Primitive::Type field_type = field_info.GetFieldType();
4057 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4058
4059 if (is_volatile) {
4060 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4061 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004062
Mark Mendellea5af682015-10-22 17:35:49 -04004063 bool maybe_record_implicit_null_check_done = false;
4064
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004065 switch (field_type) {
4066 case Primitive::kPrimBoolean:
4067 case Primitive::kPrimByte: {
Mark Mendell40741f32015-04-20 22:10:34 -04004068 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004069 int8_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004070 __ movb(Address(base, offset), Immediate(v));
4071 } else {
4072 __ movb(Address(base, offset), value.AsRegister<CpuRegister>());
4073 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004074 break;
4075 }
4076
4077 case Primitive::kPrimShort:
4078 case Primitive::kPrimChar: {
Mark Mendell40741f32015-04-20 22:10:34 -04004079 if (value.IsConstant()) {
Mark Mendellea5af682015-10-22 17:35:49 -04004080 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Mark Mendell40741f32015-04-20 22:10:34 -04004081 __ movw(Address(base, offset), Immediate(v));
4082 } else {
4083 __ movw(Address(base, offset), value.AsRegister<CpuRegister>());
4084 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004085 break;
4086 }
4087
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004088 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004089 case Primitive::kPrimNot: {
Mark Mendell40741f32015-04-20 22:10:34 -04004090 if (value.IsConstant()) {
4091 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Roland Levillain4d027112015-07-01 15:41:14 +01004092 // `field_type == Primitive::kPrimNot` implies `v == 0`.
4093 DCHECK((field_type != Primitive::kPrimNot) || (v == 0));
4094 // Note: if heap poisoning is enabled, no need to poison
4095 // (negate) `v` if it is a reference, as it would be null.
Roland Levillain06b66d02015-07-01 12:47:25 +01004096 __ movl(Address(base, offset), Immediate(v));
Mark Mendell40741f32015-04-20 22:10:34 -04004097 } else {
Roland Levillain4d027112015-07-01 15:41:14 +01004098 if (kPoisonHeapReferences && field_type == Primitive::kPrimNot) {
4099 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4100 __ movl(temp, value.AsRegister<CpuRegister>());
4101 __ PoisonHeapReference(temp);
4102 __ movl(Address(base, offset), temp);
4103 } else {
4104 __ movl(Address(base, offset), value.AsRegister<CpuRegister>());
4105 }
Mark Mendell40741f32015-04-20 22:10:34 -04004106 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004107 break;
4108 }
4109
4110 case Primitive::kPrimLong: {
Mark Mendell40741f32015-04-20 22:10:34 -04004111 if (value.IsConstant()) {
4112 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004113 codegen_->MoveInt64ToAddress(Address(base, offset),
4114 Address(base, offset + sizeof(int32_t)),
4115 v,
4116 instruction);
4117 maybe_record_implicit_null_check_done = true;
Mark Mendell40741f32015-04-20 22:10:34 -04004118 } else {
4119 __ movq(Address(base, offset), value.AsRegister<CpuRegister>());
4120 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004121 break;
4122 }
4123
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004124 case Primitive::kPrimFloat: {
Mark Mendellea5af682015-10-22 17:35:49 -04004125 if (value.IsConstant()) {
4126 int32_t v =
4127 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4128 __ movl(Address(base, offset), Immediate(v));
4129 } else {
4130 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4131 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004132 break;
4133 }
4134
4135 case Primitive::kPrimDouble: {
Mark Mendellea5af682015-10-22 17:35:49 -04004136 if (value.IsConstant()) {
4137 int64_t v =
4138 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4139 codegen_->MoveInt64ToAddress(Address(base, offset),
4140 Address(base, offset + sizeof(int32_t)),
4141 v,
4142 instruction);
4143 maybe_record_implicit_null_check_done = true;
4144 } else {
4145 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4146 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004147 break;
4148 }
4149
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004150 case Primitive::kPrimVoid:
4151 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004152 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004153 }
Calin Juravle52c48962014-12-16 17:02:57 +00004154
Mark Mendellea5af682015-10-22 17:35:49 -04004155 if (!maybe_record_implicit_null_check_done) {
4156 codegen_->MaybeRecordImplicitNullCheck(instruction);
4157 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004158
4159 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4160 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4161 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004162 codegen_->MarkGCCard(temp, card, base, value.AsRegister<CpuRegister>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004163 }
4164
Calin Juravle52c48962014-12-16 17:02:57 +00004165 if (is_volatile) {
4166 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4167 }
4168}
4169
4170void LocationsBuilderX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4171 HandleFieldSet(instruction, instruction->GetFieldInfo());
4172}
4173
4174void InstructionCodeGeneratorX86_64::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004175 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004176}
4177
4178void LocationsBuilderX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004179 HandleFieldGet(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004180}
4181
4182void InstructionCodeGeneratorX86_64::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
Calin Juravle52c48962014-12-16 17:02:57 +00004183 HandleFieldGet(instruction, instruction->GetFieldInfo());
4184}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004185
Calin Juravle52c48962014-12-16 17:02:57 +00004186void LocationsBuilderX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4187 HandleFieldGet(instruction);
4188}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004189
Calin Juravle52c48962014-12-16 17:02:57 +00004190void InstructionCodeGeneratorX86_64::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4191 HandleFieldGet(instruction, instruction->GetFieldInfo());
4192}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004193
Calin Juravle52c48962014-12-16 17:02:57 +00004194void LocationsBuilderX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4195 HandleFieldSet(instruction, instruction->GetFieldInfo());
4196}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004197
Calin Juravle52c48962014-12-16 17:02:57 +00004198void InstructionCodeGeneratorX86_64::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004199 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004200}
4201
Calin Juravlee460d1d2015-09-29 04:52:17 +01004202void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldGet(
4203 HUnresolvedInstanceFieldGet* instruction) {
4204 FieldAccessCallingConventionX86_64 calling_convention;
4205 codegen_->CreateUnresolvedFieldLocationSummary(
4206 instruction, instruction->GetFieldType(), calling_convention);
4207}
4208
4209void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldGet(
4210 HUnresolvedInstanceFieldGet* instruction) {
4211 FieldAccessCallingConventionX86_64 calling_convention;
4212 codegen_->GenerateUnresolvedFieldAccess(instruction,
4213 instruction->GetFieldType(),
4214 instruction->GetFieldIndex(),
4215 instruction->GetDexPc(),
4216 calling_convention);
4217}
4218
4219void LocationsBuilderX86_64::VisitUnresolvedInstanceFieldSet(
4220 HUnresolvedInstanceFieldSet* instruction) {
4221 FieldAccessCallingConventionX86_64 calling_convention;
4222 codegen_->CreateUnresolvedFieldLocationSummary(
4223 instruction, instruction->GetFieldType(), calling_convention);
4224}
4225
4226void InstructionCodeGeneratorX86_64::VisitUnresolvedInstanceFieldSet(
4227 HUnresolvedInstanceFieldSet* instruction) {
4228 FieldAccessCallingConventionX86_64 calling_convention;
4229 codegen_->GenerateUnresolvedFieldAccess(instruction,
4230 instruction->GetFieldType(),
4231 instruction->GetFieldIndex(),
4232 instruction->GetDexPc(),
4233 calling_convention);
4234}
4235
4236void LocationsBuilderX86_64::VisitUnresolvedStaticFieldGet(
4237 HUnresolvedStaticFieldGet* instruction) {
4238 FieldAccessCallingConventionX86_64 calling_convention;
4239 codegen_->CreateUnresolvedFieldLocationSummary(
4240 instruction, instruction->GetFieldType(), calling_convention);
4241}
4242
4243void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldGet(
4244 HUnresolvedStaticFieldGet* instruction) {
4245 FieldAccessCallingConventionX86_64 calling_convention;
4246 codegen_->GenerateUnresolvedFieldAccess(instruction,
4247 instruction->GetFieldType(),
4248 instruction->GetFieldIndex(),
4249 instruction->GetDexPc(),
4250 calling_convention);
4251}
4252
4253void LocationsBuilderX86_64::VisitUnresolvedStaticFieldSet(
4254 HUnresolvedStaticFieldSet* instruction) {
4255 FieldAccessCallingConventionX86_64 calling_convention;
4256 codegen_->CreateUnresolvedFieldLocationSummary(
4257 instruction, instruction->GetFieldType(), calling_convention);
4258}
4259
4260void InstructionCodeGeneratorX86_64::VisitUnresolvedStaticFieldSet(
4261 HUnresolvedStaticFieldSet* instruction) {
4262 FieldAccessCallingConventionX86_64 calling_convention;
4263 codegen_->GenerateUnresolvedFieldAccess(instruction,
4264 instruction->GetFieldType(),
4265 instruction->GetFieldIndex(),
4266 instruction->GetDexPc(),
4267 calling_convention);
4268}
4269
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004270void LocationsBuilderX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004271 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4272 ? LocationSummary::kCallOnSlowPath
4273 : LocationSummary::kNoCall;
4274 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4275 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004276 ? Location::RequiresRegister()
4277 : Location::Any();
4278 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004279 if (instruction->HasUses()) {
4280 locations->SetOut(Location::SameAsFirstInput());
4281 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004282}
4283
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004284void InstructionCodeGeneratorX86_64::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004285 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4286 return;
4287 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004288 LocationSummary* locations = instruction->GetLocations();
4289 Location obj = locations->InAt(0);
4290
4291 __ testl(CpuRegister(RAX), Address(obj.AsRegister<CpuRegister>(), 0));
4292 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4293}
4294
4295void InstructionCodeGeneratorX86_64::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004296 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86_64(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004297 codegen_->AddSlowPath(slow_path);
4298
4299 LocationSummary* locations = instruction->GetLocations();
4300 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004301
4302 if (obj.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004303 __ testl(obj.AsRegister<CpuRegister>(), obj.AsRegister<CpuRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004304 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004305 __ cmpl(Address(CpuRegister(RSP), obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004306 } else {
4307 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004308 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004309 __ jmp(slow_path->GetEntryLabel());
4310 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004311 }
4312 __ j(kEqual, slow_path->GetEntryLabel());
4313}
4314
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004315void InstructionCodeGeneratorX86_64::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004316 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004317 GenerateImplicitNullCheck(instruction);
4318 } else {
4319 GenerateExplicitNullCheck(instruction);
4320 }
4321}
4322
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004323void LocationsBuilderX86_64::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004324 bool object_array_get_with_read_barrier =
4325 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004326 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004327 new (GetGraph()->GetArena()) LocationSummary(instruction,
4328 object_array_get_with_read_barrier ?
4329 LocationSummary::kCallOnSlowPath :
4330 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004331 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04004332 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004333 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4334 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4335 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004336 // The output overlaps for an object array get when read barriers
4337 // are enabled: we do not want the move to overwrite the array's
4338 // location, as we need it to emit the read barrier.
4339 locations->SetOut(
4340 Location::RequiresRegister(),
4341 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004342 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004343}
4344
4345void InstructionCodeGeneratorX86_64::VisitArrayGet(HArrayGet* instruction) {
4346 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004347 Location obj_loc = locations->InAt(0);
4348 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004349 Location index = locations->InAt(1);
Roland Levillain4d027112015-07-01 15:41:14 +01004350 Primitive::Type type = instruction->GetType();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004351
Roland Levillain4d027112015-07-01 15:41:14 +01004352 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004353 case Primitive::kPrimBoolean: {
4354 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004355 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004356 if (index.IsConstant()) {
4357 __ movzxb(out, Address(obj,
4358 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4359 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004360 __ movzxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004361 }
4362 break;
4363 }
4364
4365 case Primitive::kPrimByte: {
4366 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004367 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004368 if (index.IsConstant()) {
4369 __ movsxb(out, Address(obj,
4370 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4371 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004372 __ movsxb(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004373 }
4374 break;
4375 }
4376
4377 case Primitive::kPrimShort: {
4378 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004379 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004380 if (index.IsConstant()) {
4381 __ movsxw(out, Address(obj,
4382 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4383 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004384 __ movsxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004385 }
4386 break;
4387 }
4388
4389 case Primitive::kPrimChar: {
4390 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004391 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004392 if (index.IsConstant()) {
4393 __ movzxw(out, Address(obj,
4394 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4395 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004396 __ movzxw(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004397 }
4398 break;
4399 }
4400
4401 case Primitive::kPrimInt:
4402 case Primitive::kPrimNot: {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004403 static_assert(
4404 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4405 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004406 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004407 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004408 if (index.IsConstant()) {
4409 __ movl(out, Address(obj,
4410 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4411 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004412 __ movl(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004413 }
4414 break;
4415 }
4416
4417 case Primitive::kPrimLong: {
4418 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004419 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004420 if (index.IsConstant()) {
4421 __ movq(out, Address(obj,
4422 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4423 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004424 __ movq(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004425 }
4426 break;
4427 }
4428
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004429 case Primitive::kPrimFloat: {
4430 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004431 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004432 if (index.IsConstant()) {
4433 __ movss(out, Address(obj,
4434 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4435 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004436 __ movss(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_4, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004437 }
4438 break;
4439 }
4440
4441 case Primitive::kPrimDouble: {
4442 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004443 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004444 if (index.IsConstant()) {
4445 __ movsd(out, Address(obj,
4446 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4447 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004448 __ movsd(out, Address(obj, index.AsRegister<CpuRegister>(), TIMES_8, data_offset));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004449 }
4450 break;
4451 }
4452
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004453 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004454 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004455 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004456 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004457 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004458
4459 if (type == Primitive::kPrimNot) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004460 static_assert(
4461 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4462 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4463 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4464 Location out = locations->Out();
4465 if (index.IsConstant()) {
4466 uint32_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4467 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, offset);
4468 } else {
4469 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, data_offset, index);
4470 }
Roland Levillain4d027112015-07-01 15:41:14 +01004471 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004472}
4473
4474void LocationsBuilderX86_64::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004475 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004476
4477 bool needs_write_barrier =
4478 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004479 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004480 bool object_array_set_with_read_barrier =
4481 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004482
Nicolas Geoffray39468442014-09-02 15:17:15 +01004483 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004484 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00004485 (may_need_runtime_call || object_array_set_with_read_barrier) ?
4486 LocationSummary::kCallOnSlowPath :
4487 LocationSummary::kNoCall);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004488
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004489 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellea5af682015-10-22 17:35:49 -04004490 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4491 if (Primitive::IsFloatingPointType(value_type)) {
4492 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004493 } else {
4494 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4495 }
4496
4497 if (needs_write_barrier) {
4498 // Temporary registers for the write barrier.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004499
4500 // This first temporary register is possibly used for heap
4501 // reference poisoning and/or read barrier emission too.
4502 locations->AddTemp(Location::RequiresRegister());
4503 // This second temporary register is possibly used for read
4504 // barrier emission too.
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004505 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004506 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004507}
4508
4509void InstructionCodeGeneratorX86_64::VisitArraySet(HArraySet* instruction) {
4510 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004511 Location array_loc = locations->InAt(0);
4512 CpuRegister array = array_loc.AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004513 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004514 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004515 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004516 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004517 bool needs_write_barrier =
4518 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004519 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4520 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4521 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004522
4523 switch (value_type) {
4524 case Primitive::kPrimBoolean:
4525 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004526 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4527 Address address = index.IsConstant()
4528 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4529 : Address(array, index.AsRegister<CpuRegister>(), TIMES_1, offset);
4530 if (value.IsRegister()) {
4531 __ movb(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004532 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004533 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004534 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004535 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004536 break;
4537 }
4538
4539 case Primitive::kPrimShort:
4540 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004541 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4542 Address address = index.IsConstant()
4543 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4544 : Address(array, index.AsRegister<CpuRegister>(), TIMES_2, offset);
4545 if (value.IsRegister()) {
4546 __ movw(address, value.AsRegister<CpuRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004547 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004548 DCHECK(value.IsConstant()) << value;
4549 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004550 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004551 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004552 break;
4553 }
4554
4555 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004556 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4557 Address address = index.IsConstant()
4558 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4559 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004560
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004561 if (!value.IsRegister()) {
4562 // Just setting null.
4563 DCHECK(instruction->InputAt(2)->IsNullConstant());
4564 DCHECK(value.IsConstant()) << value;
4565 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004566 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004567 DCHECK(!needs_write_barrier);
4568 DCHECK(!may_need_runtime_call);
4569 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004570 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004571
4572 DCHECK(needs_write_barrier);
4573 CpuRegister register_value = value.AsRegister<CpuRegister>();
4574 NearLabel done, not_null, do_put;
4575 SlowPathCode* slow_path = nullptr;
4576 CpuRegister temp = locations->GetTemp(0).AsRegister<CpuRegister>();
4577 if (may_need_runtime_call) {
4578 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86_64(instruction);
4579 codegen_->AddSlowPath(slow_path);
4580 if (instruction->GetValueCanBeNull()) {
4581 __ testl(register_value, register_value);
4582 __ j(kNotEqual, &not_null);
4583 __ movl(address, Immediate(0));
4584 codegen_->MaybeRecordImplicitNullCheck(instruction);
4585 __ jmp(&done);
4586 __ Bind(&not_null);
4587 }
4588
Roland Levillain0d5a2812015-11-13 10:07:31 +00004589 if (kEmitCompilerReadBarrier) {
4590 // When read barriers are enabled, the type checking
4591 // instrumentation requires two read barriers:
4592 //
4593 // __ movl(temp2, temp);
4594 // // /* HeapReference<Class> */ temp = temp->component_type_
4595 // __ movl(temp, Address(temp, component_offset));
4596 // codegen_->GenerateReadBarrier(
4597 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
4598 //
4599 // // /* HeapReference<Class> */ temp2 = register_value->klass_
4600 // __ movl(temp2, Address(register_value, class_offset));
4601 // codegen_->GenerateReadBarrier(
4602 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
4603 //
4604 // __ cmpl(temp, temp2);
4605 //
4606 // However, the second read barrier may trash `temp`, as it
4607 // is a temporary register, and as such would not be saved
4608 // along with live registers before calling the runtime (nor
4609 // restored afterwards). So in this case, we bail out and
4610 // delegate the work to the array set slow path.
4611 //
4612 // TODO: Extend the register allocator to support a new
4613 // "(locally) live temp" location so as to avoid always
4614 // going into the slow path when read barriers are enabled.
4615 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004616 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004617 // /* HeapReference<Class> */ temp = array->klass_
4618 __ movl(temp, Address(array, class_offset));
4619 codegen_->MaybeRecordImplicitNullCheck(instruction);
4620 __ MaybeUnpoisonHeapReference(temp);
4621
4622 // /* HeapReference<Class> */ temp = temp->component_type_
4623 __ movl(temp, Address(temp, component_offset));
4624 // If heap poisoning is enabled, no need to unpoison `temp`
4625 // nor the object reference in `register_value->klass`, as
4626 // we are comparing two poisoned references.
4627 __ cmpl(temp, Address(register_value, class_offset));
4628
4629 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4630 __ j(kEqual, &do_put);
4631 // If heap poisoning is enabled, the `temp` reference has
4632 // not been unpoisoned yet; unpoison it now.
4633 __ MaybeUnpoisonHeapReference(temp);
4634
4635 // /* HeapReference<Class> */ temp = temp->super_class_
4636 __ movl(temp, Address(temp, super_offset));
4637 // If heap poisoning is enabled, no need to unpoison
4638 // `temp`, as we are comparing against null below.
4639 __ testl(temp, temp);
4640 __ j(kNotEqual, slow_path->GetEntryLabel());
4641 __ Bind(&do_put);
4642 } else {
4643 __ j(kNotEqual, slow_path->GetEntryLabel());
4644 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004645 }
4646 }
4647
4648 if (kPoisonHeapReferences) {
4649 __ movl(temp, register_value);
4650 __ PoisonHeapReference(temp);
4651 __ movl(address, temp);
4652 } else {
4653 __ movl(address, register_value);
4654 }
4655 if (!may_need_runtime_call) {
4656 codegen_->MaybeRecordImplicitNullCheck(instruction);
4657 }
4658
4659 CpuRegister card = locations->GetTemp(1).AsRegister<CpuRegister>();
4660 codegen_->MarkGCCard(
4661 temp, card, array, value.AsRegister<CpuRegister>(), instruction->GetValueCanBeNull());
4662 __ Bind(&done);
4663
4664 if (slow_path != nullptr) {
4665 __ Bind(slow_path->GetExitLabel());
4666 }
4667
4668 break;
4669 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004670
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004671 case Primitive::kPrimInt: {
4672 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4673 Address address = index.IsConstant()
4674 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4675 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
4676 if (value.IsRegister()) {
4677 __ movl(address, value.AsRegister<CpuRegister>());
4678 } else {
4679 DCHECK(value.IsConstant()) << value;
4680 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4681 __ movl(address, Immediate(v));
4682 }
4683 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004684 break;
4685 }
4686
4687 case Primitive::kPrimLong: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004688 uint32_t offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
4689 Address address = index.IsConstant()
4690 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4691 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
4692 if (value.IsRegister()) {
4693 __ movq(address, value.AsRegister<CpuRegister>());
Mark Mendellea5af682015-10-22 17:35:49 -04004694 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004695 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004696 int64_t v = value.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellea5af682015-10-22 17:35:49 -04004697 Address address_high = index.IsConstant()
4698 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4699 offset + sizeof(int32_t))
4700 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4701 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004702 }
4703 break;
4704 }
4705
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004706 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004707 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4708 Address address = index.IsConstant()
4709 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4710 : Address(array, index.AsRegister<CpuRegister>(), TIMES_4, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004711 if (value.IsFpuRegister()) {
4712 __ movss(address, value.AsFpuRegister<XmmRegister>());
4713 } else {
4714 DCHECK(value.IsConstant());
4715 int32_t v =
4716 bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4717 __ movl(address, Immediate(v));
4718 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004719 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004720 break;
4721 }
4722
4723 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004724 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4725 Address address = index.IsConstant()
4726 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4727 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset);
Mark Mendellea5af682015-10-22 17:35:49 -04004728 if (value.IsFpuRegister()) {
4729 __ movsd(address, value.AsFpuRegister<XmmRegister>());
4730 codegen_->MaybeRecordImplicitNullCheck(instruction);
4731 } else {
4732 int64_t v =
4733 bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4734 Address address_high = index.IsConstant()
4735 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4736 offset + sizeof(int32_t))
4737 : Address(array, index.AsRegister<CpuRegister>(), TIMES_8, offset + sizeof(int32_t));
4738 codegen_->MoveInt64ToAddress(address, address_high, v, instruction);
4739 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004740 break;
4741 }
4742
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004743 case Primitive::kPrimVoid:
4744 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004745 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004746 }
4747}
4748
4749void LocationsBuilderX86_64::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004750 LocationSummary* locations =
4751 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004752 locations->SetInAt(0, Location::RequiresRegister());
4753 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004754}
4755
4756void InstructionCodeGeneratorX86_64::VisitArrayLength(HArrayLength* instruction) {
4757 LocationSummary* locations = instruction->GetLocations();
4758 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004759 CpuRegister obj = locations->InAt(0).AsRegister<CpuRegister>();
4760 CpuRegister out = locations->Out().AsRegister<CpuRegister>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004761 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004762 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004763}
4764
4765void LocationsBuilderX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004766 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4767 ? LocationSummary::kCallOnSlowPath
4768 : LocationSummary::kNoCall;
4769 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004770 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004771 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004772 if (instruction->HasUses()) {
4773 locations->SetOut(Location::SameAsFirstInput());
4774 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004775}
4776
4777void InstructionCodeGeneratorX86_64::VisitBoundsCheck(HBoundsCheck* instruction) {
4778 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004779 Location index_loc = locations->InAt(0);
4780 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07004781 SlowPathCode* slow_path =
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004782 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86_64(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004783
Mark Mendell99dbd682015-04-22 16:18:52 -04004784 if (length_loc.IsConstant()) {
4785 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4786 if (index_loc.IsConstant()) {
4787 // BCE will remove the bounds check if we are guarenteed to pass.
4788 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4789 if (index < 0 || index >= length) {
4790 codegen_->AddSlowPath(slow_path);
4791 __ jmp(slow_path->GetEntryLabel());
4792 } else {
4793 // Some optimization after BCE may have generated this, and we should not
4794 // generate a bounds check if it is a valid range.
4795 }
4796 return;
4797 }
4798
4799 // We have to reverse the jump condition because the length is the constant.
4800 CpuRegister index_reg = index_loc.AsRegister<CpuRegister>();
4801 __ cmpl(index_reg, Immediate(length));
4802 codegen_->AddSlowPath(slow_path);
4803 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004804 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004805 CpuRegister length = length_loc.AsRegister<CpuRegister>();
4806 if (index_loc.IsConstant()) {
4807 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4808 __ cmpl(length, Immediate(value));
4809 } else {
4810 __ cmpl(length, index_loc.AsRegister<CpuRegister>());
4811 }
4812 codegen_->AddSlowPath(slow_path);
4813 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004814 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004815}
4816
4817void CodeGeneratorX86_64::MarkGCCard(CpuRegister temp,
4818 CpuRegister card,
4819 CpuRegister object,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004820 CpuRegister value,
4821 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004822 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004823 if (value_can_be_null) {
4824 __ testl(value, value);
4825 __ j(kEqual, &is_null);
4826 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004827 __ gs()->movq(card, Address::Absolute(
4828 Thread::CardTableOffset<kX86_64WordSize>().Int32Value(), true));
4829 __ movq(temp, object);
4830 __ shrq(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillain4d027112015-07-01 15:41:14 +01004831 __ movb(Address(temp, card, TIMES_1, 0), card);
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004832 if (value_can_be_null) {
4833 __ Bind(&is_null);
4834 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004835}
4836
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004837void LocationsBuilderX86_64::VisitTemporary(HTemporary* temp) {
4838 temp->SetLocations(nullptr);
4839}
4840
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004841void InstructionCodeGeneratorX86_64::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004842 // Nothing to do, this is driven by the code generator.
4843}
4844
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004845void LocationsBuilderX86_64::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01004846 LOG(FATAL) << "Unimplemented";
4847}
4848
4849void InstructionCodeGeneratorX86_64::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004850 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4851}
4852
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004853void LocationsBuilderX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
4854 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4855}
4856
4857void InstructionCodeGeneratorX86_64::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004858 HBasicBlock* block = instruction->GetBlock();
4859 if (block->GetLoopInformation() != nullptr) {
4860 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4861 // The back edge will generate the suspend check.
4862 return;
4863 }
4864 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4865 // The goto will generate the suspend check.
4866 return;
4867 }
4868 GenerateSuspendCheck(instruction, nullptr);
4869}
4870
4871void InstructionCodeGeneratorX86_64::GenerateSuspendCheck(HSuspendCheck* instruction,
4872 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004873 SuspendCheckSlowPathX86_64* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004874 down_cast<SuspendCheckSlowPathX86_64*>(instruction->GetSlowPath());
4875 if (slow_path == nullptr) {
4876 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86_64(instruction, successor);
4877 instruction->SetSlowPath(slow_path);
4878 codegen_->AddSlowPath(slow_path);
4879 if (successor != nullptr) {
4880 DCHECK(successor->IsLoopHeader());
4881 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4882 }
4883 } else {
4884 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4885 }
4886
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004887 __ gs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004888 Thread::ThreadFlagsOffset<kX86_64WordSize>().Int32Value(), true), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004889 if (successor == nullptr) {
4890 __ j(kNotEqual, slow_path->GetEntryLabel());
4891 __ Bind(slow_path->GetReturnLabel());
4892 } else {
4893 __ j(kEqual, codegen_->GetLabelOf(successor));
4894 __ jmp(slow_path->GetEntryLabel());
4895 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004896}
4897
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004898X86_64Assembler* ParallelMoveResolverX86_64::GetAssembler() const {
4899 return codegen_->GetAssembler();
4900}
4901
4902void ParallelMoveResolverX86_64::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004903 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004904 Location source = move->GetSource();
4905 Location destination = move->GetDestination();
4906
4907 if (source.IsRegister()) {
4908 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004909 __ movq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004910 } else if (destination.IsStackSlot()) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004911 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004912 source.AsRegister<CpuRegister>());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004913 } else {
4914 DCHECK(destination.IsDoubleStackSlot());
4915 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004916 source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004917 }
4918 } else if (source.IsStackSlot()) {
4919 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004920 __ movl(destination.AsRegister<CpuRegister>(),
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004921 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004922 } else if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004923 __ movss(destination.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004924 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00004925 } else {
4926 DCHECK(destination.IsStackSlot());
4927 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4928 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4929 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004930 } else if (source.IsDoubleStackSlot()) {
4931 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004932 __ movq(destination.AsRegister<CpuRegister>(),
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004933 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004934 } else if (destination.IsFpuRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004935 __ movsd(destination.AsFpuRegister<XmmRegister>(),
4936 Address(CpuRegister(RSP), source.GetStackIndex()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004937 } else {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +01004938 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004939 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), source.GetStackIndex()));
4940 __ movq(Address(CpuRegister(RSP), destination.GetStackIndex()), CpuRegister(TMP));
4941 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004942 } else if (source.IsConstant()) {
4943 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004944 if (constant->IsIntConstant() || constant->IsNullConstant()) {
4945 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004946 if (destination.IsRegister()) {
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004947 if (value == 0) {
4948 __ xorl(destination.AsRegister<CpuRegister>(), destination.AsRegister<CpuRegister>());
4949 } else {
4950 __ movl(destination.AsRegister<CpuRegister>(), Immediate(value));
4951 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004952 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004953 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray748f1402015-01-27 08:17:54 +00004954 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004955 }
4956 } else if (constant->IsLongConstant()) {
4957 int64_t value = constant->AsLongConstant()->GetValue();
4958 if (destination.IsRegister()) {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004959 codegen_->Load64BitValue(destination.AsRegister<CpuRegister>(), value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004960 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004961 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004962 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004963 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004964 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004965 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004966 int32_t value = bit_cast<int32_t, float>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004967 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004968 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4969 if (value == 0) {
4970 // easy FP 0.0.
4971 __ xorps(dest, dest);
4972 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004973 __ movss(dest, codegen_->LiteralFloatAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004974 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004975 } else {
4976 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell92e83bf2015-05-07 11:25:03 -04004977 Immediate imm(value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004978 __ movl(Address(CpuRegister(RSP), destination.GetStackIndex()), imm);
4979 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004980 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004981 DCHECK(constant->IsDoubleConstant()) << constant->DebugName();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004982 double fp_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004983 int64_t value = bit_cast<int64_t, double>(fp_value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004984 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004985 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4986 if (value == 0) {
4987 __ xorpd(dest, dest);
4988 } else {
Mark Mendell92e83bf2015-05-07 11:25:03 -04004989 __ movsd(dest, codegen_->LiteralDoubleAddress(fp_value));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004990 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004991 } else {
4992 DCHECK(destination.IsDoubleStackSlot()) << destination;
Mark Mendellcfa410b2015-05-25 16:02:44 -04004993 codegen_->Store64BitValueToStack(destination, value);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004994 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004995 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004996 } else if (source.IsFpuRegister()) {
4997 if (destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004998 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01004999 } else if (destination.IsStackSlot()) {
5000 __ movss(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005001 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005002 } else {
Nicolas Geoffray31596742014-11-24 15:28:45 +00005003 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005004 __ movsd(Address(CpuRegister(RSP), destination.GetStackIndex()),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005005 source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005006 }
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005007 }
5008}
5009
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005010void ParallelMoveResolverX86_64::Exchange32(CpuRegister reg, int mem) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005011 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005012 __ movl(Address(CpuRegister(RSP), mem), reg);
5013 __ movl(reg, CpuRegister(TMP));
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005014}
5015
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005016void ParallelMoveResolverX86_64::Exchange32(int mem1, int mem2) {
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005017 ScratchRegisterScope ensure_scratch(
5018 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
5019
5020 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5021 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5022 __ movl(CpuRegister(ensure_scratch.GetRegister()),
5023 Address(CpuRegister(RSP), mem2 + stack_offset));
5024 __ movl(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5025 __ movl(Address(CpuRegister(RSP), mem1 + stack_offset),
5026 CpuRegister(ensure_scratch.GetRegister()));
5027}
5028
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005029void ParallelMoveResolverX86_64::Exchange64(CpuRegister reg, int mem) {
5030 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5031 __ movq(Address(CpuRegister(RSP), mem), reg);
5032 __ movq(reg, CpuRegister(TMP));
5033}
5034
5035void ParallelMoveResolverX86_64::Exchange64(int mem1, int mem2) {
5036 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005037 this, TMP, RAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005038
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005039 int stack_offset = ensure_scratch.IsSpilled() ? kX86_64WordSize : 0;
5040 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem1 + stack_offset));
5041 __ movq(CpuRegister(ensure_scratch.GetRegister()),
5042 Address(CpuRegister(RSP), mem2 + stack_offset));
5043 __ movq(Address(CpuRegister(RSP), mem2 + stack_offset), CpuRegister(TMP));
5044 __ movq(Address(CpuRegister(RSP), mem1 + stack_offset),
5045 CpuRegister(ensure_scratch.GetRegister()));
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005046}
5047
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005048void ParallelMoveResolverX86_64::Exchange32(XmmRegister reg, int mem) {
5049 __ movl(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5050 __ movss(Address(CpuRegister(RSP), mem), reg);
5051 __ movd(reg, CpuRegister(TMP));
5052}
5053
5054void ParallelMoveResolverX86_64::Exchange64(XmmRegister reg, int mem) {
5055 __ movq(CpuRegister(TMP), Address(CpuRegister(RSP), mem));
5056 __ movsd(Address(CpuRegister(RSP), mem), reg);
5057 __ movd(reg, CpuRegister(TMP));
5058}
5059
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005060void ParallelMoveResolverX86_64::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005061 MoveOperands* move = moves_[index];
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005062 Location source = move->GetSource();
5063 Location destination = move->GetDestination();
5064
5065 if (source.IsRegister() && destination.IsRegister()) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005066 __ xchgq(destination.AsRegister<CpuRegister>(), source.AsRegister<CpuRegister>());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005067 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005068 Exchange32(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005069 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005070 Exchange32(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005071 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005072 Exchange32(destination.GetStackIndex(), source.GetStackIndex());
5073 } else if (source.IsRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005074 Exchange64(source.AsRegister<CpuRegister>(), destination.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005075 } else if (source.IsDoubleStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005076 Exchange64(destination.AsRegister<CpuRegister>(), source.GetStackIndex());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005077 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
5078 Exchange64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005079 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005080 __ movd(CpuRegister(TMP), source.AsFpuRegister<XmmRegister>());
5081 __ movaps(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5082 __ movd(destination.AsFpuRegister<XmmRegister>(), CpuRegister(TMP));
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005083 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005084 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005085 } else if (source.IsStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005086 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005087 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005088 Exchange64(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005089 } else if (source.IsDoubleStackSlot() && destination.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005090 Exchange64(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005091 } else {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01005092 LOG(FATAL) << "Unimplemented swap between " << source << " and " << destination;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +00005093 }
5094}
5095
5096
5097void ParallelMoveResolverX86_64::SpillScratch(int reg) {
5098 __ pushq(CpuRegister(reg));
5099}
5100
5101
5102void ParallelMoveResolverX86_64::RestoreScratch(int reg) {
5103 __ popq(CpuRegister(reg));
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01005104}
5105
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005106void InstructionCodeGeneratorX86_64::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005107 SlowPathCode* slow_path, CpuRegister class_reg) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005108 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5109 Immediate(mirror::Class::kStatusInitialized));
5110 __ j(kLess, slow_path->GetEntryLabel());
5111 __ Bind(slow_path->GetExitLabel());
5112 // No need for memory fence, thanks to the X86_64 memory model.
5113}
5114
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005115void LocationsBuilderX86_64::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005116 InvokeRuntimeCallingConvention calling_convention;
5117 CodeGenerator::CreateLoadClassLocationSummary(
5118 cls,
5119 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005120 Location::RegisterLocation(RAX),
5121 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005122}
5123
5124void InstructionCodeGeneratorX86_64::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005125 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005126 if (cls->NeedsAccessCheck()) {
5127 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5128 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5129 cls,
5130 cls->GetDexPc(),
5131 nullptr);
Calin Juravle580b6092015-10-06 17:35:58 +01005132 return;
5133 }
5134
Roland Levillain0d5a2812015-11-13 10:07:31 +00005135 Location out_loc = locations->Out();
5136 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Calin Juravle580b6092015-10-06 17:35:58 +01005137 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005138
Calin Juravle580b6092015-10-06 17:35:58 +01005139 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005140 DCHECK(!cls->CanCallRuntime());
5141 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005142 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
5143 if (kEmitCompilerReadBarrier) {
5144 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
5145 __ leaq(out, Address(current_method, declaring_class_offset));
5146 // /* mirror::Class* */ out = out->Read()
5147 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
5148 } else {
5149 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5150 __ movl(out, Address(current_method, declaring_class_offset));
5151 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005152 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005153 DCHECK(cls->CanCallRuntime());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005154 // /* GcRoot<mirror::Class>[] */ out =
5155 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5156 __ movq(out, Address(current_method,
5157 ArtMethod::DexCacheResolvedTypesOffset(kX86_64PointerSize).Int32Value()));
5158
5159 size_t cache_offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
5160 if (kEmitCompilerReadBarrier) {
5161 // /* GcRoot<mirror::Class>* */ out = &out[type_index]
5162 __ leaq(out, Address(out, cache_offset));
5163 // /* mirror::Class* */ out = out->Read()
5164 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
5165 } else {
5166 // /* GcRoot<mirror::Class> */ out = out[type_index]
5167 __ movl(out, Address(out, cache_offset));
5168 }
Roland Levillain4d027112015-07-01 15:41:14 +01005169
Andreas Gampe85b62f22015-09-09 13:15:38 -07005170 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005171 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5172 codegen_->AddSlowPath(slow_path);
5173 __ testl(out, out);
5174 __ j(kEqual, slow_path->GetEntryLabel());
5175 if (cls->MustGenerateClinitCheck()) {
5176 GenerateClassInitializationCheck(slow_path, out);
5177 } else {
5178 __ Bind(slow_path->GetExitLabel());
5179 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005180 }
5181}
5182
5183void LocationsBuilderX86_64::VisitClinitCheck(HClinitCheck* check) {
5184 LocationSummary* locations =
5185 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5186 locations->SetInAt(0, Location::RequiresRegister());
5187 if (check->HasUses()) {
5188 locations->SetOut(Location::SameAsFirstInput());
5189 }
5190}
5191
5192void InstructionCodeGeneratorX86_64::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005193 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005194 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86_64(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005195 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005196 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005197 GenerateClassInitializationCheck(slow_path,
5198 check->GetLocations()->InAt(0).AsRegister<CpuRegister>());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005199}
5200
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005201void LocationsBuilderX86_64::VisitLoadString(HLoadString* load) {
5202 LocationSummary* locations =
5203 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005204 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005205 locations->SetOut(Location::RequiresRegister());
5206}
5207
5208void InstructionCodeGeneratorX86_64::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005209 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86_64(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005210 codegen_->AddSlowPath(slow_path);
5211
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005212 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005213 Location out_loc = locations->Out();
5214 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005215 CpuRegister current_method = locations->InAt(0).AsRegister<CpuRegister>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005216
5217 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
5218 if (kEmitCompilerReadBarrier) {
5219 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
5220 __ leaq(out, Address(current_method, declaring_class_offset));
5221 // /* mirror::Class* */ out = out->Read()
5222 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
5223 } else {
5224 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5225 __ movl(out, Address(current_method, declaring_class_offset));
5226 }
5227
5228 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
5229 __ movq(out, Address(out, mirror::Class::DexCacheStringsOffset().Uint32Value()));
5230
5231 size_t cache_offset = CodeGenerator::GetCacheOffset(load->GetStringIndex());
5232 if (kEmitCompilerReadBarrier) {
5233 // /* GcRoot<mirror::String>* */ out = &out[string_index]
5234 __ leaq(out, Address(out, cache_offset));
5235 // /* mirror::String* */ out = out->Read()
5236 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
5237 } else {
5238 // /* GcRoot<mirror::String> */ out = out[string_index]
5239 __ movl(out, Address(out, cache_offset));
5240 }
5241
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005242 __ testl(out, out);
5243 __ j(kEqual, slow_path->GetEntryLabel());
5244 __ Bind(slow_path->GetExitLabel());
5245}
5246
David Brazdilcb1c0552015-08-04 16:22:25 +01005247static Address GetExceptionTlsAddress() {
5248 return Address::Absolute(Thread::ExceptionOffset<kX86_64WordSize>().Int32Value(), true);
5249}
5250
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005251void LocationsBuilderX86_64::VisitLoadException(HLoadException* load) {
5252 LocationSummary* locations =
5253 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5254 locations->SetOut(Location::RequiresRegister());
5255}
5256
5257void InstructionCodeGeneratorX86_64::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005258 __ gs()->movl(load->GetLocations()->Out().AsRegister<CpuRegister>(), GetExceptionTlsAddress());
5259}
5260
5261void LocationsBuilderX86_64::VisitClearException(HClearException* clear) {
5262 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5263}
5264
5265void InstructionCodeGeneratorX86_64::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5266 __ gs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005267}
5268
5269void LocationsBuilderX86_64::VisitThrow(HThrow* instruction) {
5270 LocationSummary* locations =
5271 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5272 InvokeRuntimeCallingConvention calling_convention;
5273 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5274}
5275
5276void InstructionCodeGeneratorX86_64::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005277 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5278 instruction,
5279 instruction->GetDexPc(),
5280 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005281}
5282
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005283void LocationsBuilderX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005284 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005285 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5286 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005287 case TypeCheckKind::kExactCheck:
5288 case TypeCheckKind::kAbstractClassCheck:
5289 case TypeCheckKind::kClassHierarchyCheck:
5290 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005291 call_kind =
5292 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005293 break;
5294 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005295 case TypeCheckKind::kUnresolvedCheck:
5296 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005297 call_kind = LocationSummary::kCallOnSlowPath;
5298 break;
5299 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005300
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005301 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005302 locations->SetInAt(0, Location::RequiresRegister());
5303 locations->SetInAt(1, Location::Any());
5304 // Note that TypeCheckSlowPathX86_64 uses this "out" register too.
5305 locations->SetOut(Location::RequiresRegister());
5306 // When read barriers are enabled, we need a temporary register for
5307 // some cases.
5308 if (kEmitCompilerReadBarrier &&
5309 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5310 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5311 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
5312 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005313 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005314}
5315
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005316void InstructionCodeGeneratorX86_64::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005317 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005318 Location obj_loc = locations->InAt(0);
5319 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005320 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005321 Location out_loc = locations->Out();
5322 CpuRegister out = out_loc.AsRegister<CpuRegister>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005323 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005324 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5325 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5326 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005327 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005328 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005329
5330 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005331 // Avoid null check if we know obj is not null.
5332 if (instruction->MustDoNullCheck()) {
5333 __ testl(obj, obj);
5334 __ j(kEqual, &zero);
5335 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005336
Roland Levillain0d5a2812015-11-13 10:07:31 +00005337 // /* HeapReference<Class> */ out = obj->klass_
5338 __ movl(out, Address(obj, class_offset));
5339 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005340
5341 switch (instruction->GetTypeCheckKind()) {
5342 case TypeCheckKind::kExactCheck: {
5343 if (cls.IsRegister()) {
5344 __ cmpl(out, cls.AsRegister<CpuRegister>());
5345 } else {
5346 DCHECK(cls.IsStackSlot()) << cls;
5347 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5348 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005349 if (zero.IsLinked()) {
5350 // Classes must be equal for the instanceof to succeed.
5351 __ j(kNotEqual, &zero);
5352 __ movl(out, Immediate(1));
5353 __ jmp(&done);
5354 } else {
5355 __ setcc(kEqual, out);
5356 // setcc only sets the low byte.
5357 __ andl(out, Immediate(1));
5358 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005359 break;
5360 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005361
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005362 case TypeCheckKind::kAbstractClassCheck: {
5363 // If the class is abstract, we eagerly fetch the super class of the
5364 // object to avoid doing a comparison we know will fail.
5365 NearLabel loop, success;
5366 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005367 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5368 if (kEmitCompilerReadBarrier) {
5369 // Save the value of `out` into `temp` before overwriting it
5370 // in the following move operation, as we will need it for the
5371 // read barrier below.
5372 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
5373 __ movl(temp, out);
5374 }
5375 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005376 __ movl(out, Address(out, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005377 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005378 __ testl(out, out);
5379 // If `out` is null, we use it for the result, and jump to `done`.
5380 __ j(kEqual, &done);
5381 if (cls.IsRegister()) {
5382 __ cmpl(out, cls.AsRegister<CpuRegister>());
5383 } else {
5384 DCHECK(cls.IsStackSlot()) << cls;
5385 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5386 }
5387 __ j(kNotEqual, &loop);
5388 __ movl(out, Immediate(1));
5389 if (zero.IsLinked()) {
5390 __ jmp(&done);
5391 }
5392 break;
5393 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005394
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005395 case TypeCheckKind::kClassHierarchyCheck: {
5396 // Walk over the class hierarchy to find a match.
5397 NearLabel loop, success;
5398 __ Bind(&loop);
5399 if (cls.IsRegister()) {
5400 __ cmpl(out, cls.AsRegister<CpuRegister>());
5401 } else {
5402 DCHECK(cls.IsStackSlot()) << cls;
5403 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5404 }
5405 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005406 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5407 if (kEmitCompilerReadBarrier) {
5408 // Save the value of `out` into `temp` before overwriting it
5409 // in the following move operation, as we will need it for the
5410 // read barrier below.
5411 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
5412 __ movl(temp, out);
5413 }
5414 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005415 __ movl(out, Address(out, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005416 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005417 __ testl(out, out);
5418 __ j(kNotEqual, &loop);
5419 // If `out` is null, we use it for the result, and jump to `done`.
5420 __ jmp(&done);
5421 __ Bind(&success);
5422 __ movl(out, Immediate(1));
5423 if (zero.IsLinked()) {
5424 __ jmp(&done);
5425 }
5426 break;
5427 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005428
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005429 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005430 // Do an exact check.
5431 NearLabel exact_check;
5432 if (cls.IsRegister()) {
5433 __ cmpl(out, cls.AsRegister<CpuRegister>());
5434 } else {
5435 DCHECK(cls.IsStackSlot()) << cls;
5436 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5437 }
5438 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005439 // Otherwise, we need to check that the object's class is a non-primitive array.
5440 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5441 if (kEmitCompilerReadBarrier) {
5442 // Save the value of `out` into `temp` before overwriting it
5443 // in the following move operation, as we will need it for the
5444 // read barrier below.
5445 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
5446 __ movl(temp, out);
5447 }
5448 // /* HeapReference<Class> */ out = out->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005449 __ movl(out, Address(out, component_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005450 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, component_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005451 __ testl(out, out);
5452 // If `out` is null, we use it for the result, and jump to `done`.
5453 __ j(kEqual, &done);
5454 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5455 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005456 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005457 __ movl(out, Immediate(1));
5458 __ jmp(&done);
5459 break;
5460 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005461
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005462 case TypeCheckKind::kArrayCheck: {
5463 if (cls.IsRegister()) {
5464 __ cmpl(out, cls.AsRegister<CpuRegister>());
5465 } else {
5466 DCHECK(cls.IsStackSlot()) << cls;
5467 __ cmpl(out, Address(CpuRegister(RSP), cls.GetStackIndex()));
5468 }
5469 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005470 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5471 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005472 codegen_->AddSlowPath(slow_path);
5473 __ j(kNotEqual, slow_path->GetEntryLabel());
5474 __ movl(out, Immediate(1));
5475 if (zero.IsLinked()) {
5476 __ jmp(&done);
5477 }
5478 break;
5479 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005480
Calin Juravle98893e12015-10-02 21:05:03 +01005481 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005482 case TypeCheckKind::kInterfaceCheck: {
5483 // Note that we indeed only call on slow path, but we always go
5484 // into the slow path for the unresolved & interface check
5485 // cases.
5486 //
5487 // We cannot directly call the InstanceofNonTrivial runtime
5488 // entry point without resorting to a type checking slow path
5489 // here (i.e. by calling InvokeRuntime directly), as it would
5490 // require to assign fixed registers for the inputs of this
5491 // HInstanceOf instruction (following the runtime calling
5492 // convention), which might be cluttered by the potential first
5493 // read barrier emission at the beginning of this method.
5494 DCHECK(locations->OnlyCallsOnSlowPath());
5495 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5496 /* is_fatal */ false);
5497 codegen_->AddSlowPath(slow_path);
5498 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005499 if (zero.IsLinked()) {
5500 __ jmp(&done);
5501 }
5502 break;
5503 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005504 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005505
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005506 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005507 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005508 __ xorl(out, out);
5509 }
5510
5511 if (done.IsLinked()) {
5512 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005513 }
5514
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005515 if (slow_path != nullptr) {
5516 __ Bind(slow_path->GetExitLabel());
5517 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005518}
5519
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005520void LocationsBuilderX86_64::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005521 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5522 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005523 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5524 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005525 case TypeCheckKind::kExactCheck:
5526 case TypeCheckKind::kAbstractClassCheck:
5527 case TypeCheckKind::kClassHierarchyCheck:
5528 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005529 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5530 LocationSummary::kCallOnSlowPath :
5531 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005532 break;
5533 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005534 case TypeCheckKind::kUnresolvedCheck:
5535 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005536 call_kind = LocationSummary::kCallOnSlowPath;
5537 break;
5538 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005539 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5540 locations->SetInAt(0, Location::RequiresRegister());
5541 locations->SetInAt(1, Location::Any());
5542 // Note that TypeCheckSlowPathX86_64 uses this "temp" register too.
5543 locations->AddTemp(Location::RequiresRegister());
5544 // When read barriers are enabled, we need an additional temporary
5545 // register for some cases.
5546 if (kEmitCompilerReadBarrier &&
5547 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5548 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5549 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005550 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005551 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005552}
5553
5554void InstructionCodeGeneratorX86_64::VisitCheckCast(HCheckCast* instruction) {
5555 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005556 Location obj_loc = locations->InAt(0);
5557 CpuRegister obj = obj_loc.AsRegister<CpuRegister>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005558 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005559 Location temp_loc = locations->GetTemp(0);
5560 CpuRegister temp = temp_loc.AsRegister<CpuRegister>();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005561 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5562 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5563 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5564 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005565
Roland Levillain0d5a2812015-11-13 10:07:31 +00005566 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5567 bool is_type_check_slow_path_fatal =
5568 (type_check_kind == TypeCheckKind::kExactCheck ||
5569 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5570 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5571 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5572 !instruction->CanThrowIntoCatchBlock();
5573 SlowPathCode* type_check_slow_path =
5574 new (GetGraph()->GetArena()) TypeCheckSlowPathX86_64(instruction,
5575 is_type_check_slow_path_fatal);
5576 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005577
5578 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005579 // Avoid null check if we know obj is not null.
5580 if (instruction->MustDoNullCheck()) {
5581 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005582 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005583 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005584
Roland Levillain0d5a2812015-11-13 10:07:31 +00005585 // /* HeapReference<Class> */ temp = obj->klass_
5586 __ movl(temp, Address(obj, class_offset));
5587 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005588
Roland Levillain0d5a2812015-11-13 10:07:31 +00005589 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005590 case TypeCheckKind::kExactCheck:
5591 case TypeCheckKind::kArrayCheck: {
5592 if (cls.IsRegister()) {
5593 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5594 } else {
5595 DCHECK(cls.IsStackSlot()) << cls;
5596 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5597 }
5598 // Jump to slow path for throwing the exception or doing a
5599 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005600 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005601 break;
5602 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005603
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005604 case TypeCheckKind::kAbstractClassCheck: {
5605 // If the class is abstract, we eagerly fetch the super class of the
5606 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005607 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005608 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005609 Location temp2_loc =
5610 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
5611 if (kEmitCompilerReadBarrier) {
5612 // Save the value of `temp` into `temp2` before overwriting it
5613 // in the following move operation, as we will need it for the
5614 // read barrier below.
5615 CpuRegister temp2 = temp2_loc.AsRegister<CpuRegister>();
5616 __ movl(temp2, temp);
5617 }
5618 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005619 __ movl(temp, Address(temp, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005620 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
5621
5622 // If the class reference currently in `temp` is not null, jump
5623 // to the `compare_classes` label to compare it with the checked
5624 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005625 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005626 __ j(kNotEqual, &compare_classes);
5627 // Otherwise, jump to the slow path to throw the exception.
5628 //
5629 // But before, move back the object's class into `temp` before
5630 // going into the slow path, as it has been overwritten in the
5631 // meantime.
5632 // /* HeapReference<Class> */ temp = obj->klass_
5633 __ movl(temp, Address(obj, class_offset));
5634 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
5635 __ jmp(type_check_slow_path->GetEntryLabel());
5636
5637 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005638 if (cls.IsRegister()) {
5639 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5640 } else {
5641 DCHECK(cls.IsStackSlot()) << cls;
5642 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5643 }
5644 __ j(kNotEqual, &loop);
5645 break;
5646 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005647
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005648 case TypeCheckKind::kClassHierarchyCheck: {
5649 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005650 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005651 __ Bind(&loop);
5652 if (cls.IsRegister()) {
5653 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5654 } else {
5655 DCHECK(cls.IsStackSlot()) << cls;
5656 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5657 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005658 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005659
5660 Location temp2_loc =
5661 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
5662 if (kEmitCompilerReadBarrier) {
5663 // Save the value of `temp` into `temp2` before overwriting it
5664 // in the following move operation, as we will need it for the
5665 // read barrier below.
5666 CpuRegister temp2 = temp2_loc.AsRegister<CpuRegister>();
5667 __ movl(temp2, temp);
5668 }
5669 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005670 __ movl(temp, Address(temp, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005671 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
5672
5673 // If the class reference currently in `temp` is not null, jump
5674 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005675 __ testl(temp, temp);
5676 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005677 // Otherwise, jump to the slow path to throw the exception.
5678 //
5679 // But before, move back the object's class into `temp` before
5680 // going into the slow path, as it has been overwritten in the
5681 // meantime.
5682 // /* HeapReference<Class> */ temp = obj->klass_
5683 __ movl(temp, Address(obj, class_offset));
5684 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
5685 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005686 break;
5687 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005688
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005689 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005690 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005691 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005692 if (cls.IsRegister()) {
5693 __ cmpl(temp, cls.AsRegister<CpuRegister>());
5694 } else {
5695 DCHECK(cls.IsStackSlot()) << cls;
5696 __ cmpl(temp, Address(CpuRegister(RSP), cls.GetStackIndex()));
5697 }
5698 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005699
5700 // Otherwise, we need to check that the object's class is a non-primitive array.
5701 Location temp2_loc =
5702 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
5703 if (kEmitCompilerReadBarrier) {
5704 // Save the value of `temp` into `temp2` before overwriting it
5705 // in the following move operation, as we will need it for the
5706 // read barrier below.
5707 CpuRegister temp2 = temp2_loc.AsRegister<CpuRegister>();
5708 __ movl(temp2, temp);
5709 }
5710 // /* HeapReference<Class> */ temp = temp->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005711 __ movl(temp, Address(temp, component_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005712 codegen_->MaybeGenerateReadBarrier(
5713 instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5714
5715 // If the component type is not null (i.e. the object is indeed
5716 // an array), jump to label `check_non_primitive_component_type`
5717 // to further check that this component type is not a primitive
5718 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005719 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005720 __ j(kNotEqual, &check_non_primitive_component_type);
5721 // Otherwise, jump to the slow path to throw the exception.
5722 //
5723 // But before, move back the object's class into `temp` before
5724 // going into the slow path, as it has been overwritten in the
5725 // meantime.
5726 // /* HeapReference<Class> */ temp = obj->klass_
5727 __ movl(temp, Address(obj, class_offset));
5728 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
5729 __ jmp(type_check_slow_path->GetEntryLabel());
5730
5731 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005732 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005733 __ j(kEqual, &done);
5734 // Same comment as above regarding `temp` and the slow path.
5735 // /* HeapReference<Class> */ temp = obj->klass_
5736 __ movl(temp, Address(obj, class_offset));
5737 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
5738 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005739 break;
5740 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005741
Calin Juravle98893e12015-10-02 21:05:03 +01005742 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005743 case TypeCheckKind::kInterfaceCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005744 // We always go into the type check slow path for the unresolved &
5745 // interface check cases.
5746 //
5747 // We cannot directly call the CheckCast runtime entry point
5748 // without resorting to a type checking slow path here (i.e. by
5749 // calling InvokeRuntime directly), as it would require to
5750 // assign fixed registers for the inputs of this HInstanceOf
5751 // instruction (following the runtime calling convention), which
5752 // might be cluttered by the potential first read barrier
5753 // emission at the beginning of this method.
5754 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005755 break;
5756 }
5757 __ Bind(&done);
5758
Roland Levillain0d5a2812015-11-13 10:07:31 +00005759 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005760}
5761
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005762void LocationsBuilderX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
5763 LocationSummary* locations =
5764 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5765 InvokeRuntimeCallingConvention calling_convention;
5766 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5767}
5768
5769void InstructionCodeGeneratorX86_64::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005770 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
5771 : QUICK_ENTRY_POINT(pUnlockObject),
5772 instruction,
5773 instruction->GetDexPc(),
5774 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005775}
5776
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005777void LocationsBuilderX86_64::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
5778void LocationsBuilderX86_64::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
5779void LocationsBuilderX86_64::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
5780
5781void LocationsBuilderX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5782 LocationSummary* locations =
5783 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5784 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5785 || instruction->GetResultType() == Primitive::kPrimLong);
5786 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell40741f32015-04-20 22:10:34 -04005787 locations->SetInAt(1, Location::Any());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005788 locations->SetOut(Location::SameAsFirstInput());
5789}
5790
5791void InstructionCodeGeneratorX86_64::VisitAnd(HAnd* instruction) {
5792 HandleBitwiseOperation(instruction);
5793}
5794
5795void InstructionCodeGeneratorX86_64::VisitOr(HOr* instruction) {
5796 HandleBitwiseOperation(instruction);
5797}
5798
5799void InstructionCodeGeneratorX86_64::VisitXor(HXor* instruction) {
5800 HandleBitwiseOperation(instruction);
5801}
5802
5803void InstructionCodeGeneratorX86_64::HandleBitwiseOperation(HBinaryOperation* instruction) {
5804 LocationSummary* locations = instruction->GetLocations();
5805 Location first = locations->InAt(0);
5806 Location second = locations->InAt(1);
5807 DCHECK(first.Equals(locations->Out()));
5808
5809 if (instruction->GetResultType() == Primitive::kPrimInt) {
5810 if (second.IsRegister()) {
5811 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005812 __ andl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005813 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005814 __ orl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005815 } else {
5816 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005817 __ xorl(first.AsRegister<CpuRegister>(), second.AsRegister<CpuRegister>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005818 }
5819 } else if (second.IsConstant()) {
5820 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue());
5821 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005822 __ andl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005823 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005824 __ orl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005825 } else {
5826 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005827 __ xorl(first.AsRegister<CpuRegister>(), imm);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005828 }
5829 } else {
5830 Address address(CpuRegister(RSP), second.GetStackIndex());
5831 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005832 __ andl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005833 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005834 __ orl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005835 } else {
5836 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005837 __ xorl(first.AsRegister<CpuRegister>(), address);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005838 }
5839 }
5840 } else {
5841 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005842 CpuRegister first_reg = first.AsRegister<CpuRegister>();
5843 bool second_is_constant = false;
5844 int64_t value = 0;
5845 if (second.IsConstant()) {
5846 second_is_constant = true;
5847 value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005848 }
Mark Mendell40741f32015-04-20 22:10:34 -04005849 bool is_int32_value = IsInt<32>(value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005850
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005851 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005852 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005853 if (is_int32_value) {
5854 __ andq(first_reg, Immediate(static_cast<int32_t>(value)));
5855 } else {
5856 __ andq(first_reg, codegen_->LiteralInt64Address(value));
5857 }
5858 } else if (second.IsDoubleStackSlot()) {
5859 __ andq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005860 } else {
5861 __ andq(first_reg, second.AsRegister<CpuRegister>());
5862 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005863 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005864 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005865 if (is_int32_value) {
5866 __ orq(first_reg, Immediate(static_cast<int32_t>(value)));
5867 } else {
5868 __ orq(first_reg, codegen_->LiteralInt64Address(value));
5869 }
5870 } else if (second.IsDoubleStackSlot()) {
5871 __ orq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005872 } else {
5873 __ orq(first_reg, second.AsRegister<CpuRegister>());
5874 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005875 } else {
5876 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005877 if (second_is_constant) {
Mark Mendell40741f32015-04-20 22:10:34 -04005878 if (is_int32_value) {
5879 __ xorq(first_reg, Immediate(static_cast<int32_t>(value)));
5880 } else {
5881 __ xorq(first_reg, codegen_->LiteralInt64Address(value));
5882 }
5883 } else if (second.IsDoubleStackSlot()) {
5884 __ xorq(first_reg, Address(CpuRegister(RSP), second.GetStackIndex()));
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005885 } else {
5886 __ xorq(first_reg, second.AsRegister<CpuRegister>());
5887 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005888 }
5889 }
5890}
5891
Roland Levillain0d5a2812015-11-13 10:07:31 +00005892void CodeGeneratorX86_64::GenerateReadBarrier(HInstruction* instruction,
5893 Location out,
5894 Location ref,
5895 Location obj,
5896 uint32_t offset,
5897 Location index) {
5898 DCHECK(kEmitCompilerReadBarrier);
5899
5900 // If heap poisoning is enabled, the unpoisoning of the loaded
5901 // reference will be carried out by the runtime within the slow
5902 // path.
5903 //
5904 // Note that `ref` currently does not get unpoisoned (when heap
5905 // poisoning is enabled), which is alright as the `ref` argument is
5906 // not used by the artReadBarrierSlow entry point.
5907 //
5908 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
5909 SlowPathCode* slow_path = new (GetGraph()->GetArena())
5910 ReadBarrierForHeapReferenceSlowPathX86_64(instruction, out, ref, obj, offset, index);
5911 AddSlowPath(slow_path);
5912
5913 // TODO: When read barrier has a fast path, add it here.
5914 /* Currently the read barrier call is inserted after the original load.
5915 * However, if we have a fast path, we need to perform the load of obj.LockWord *before* the
5916 * original load. This load-load ordering is required by the read barrier.
5917 * The fast path/slow path (for Baker's algorithm) should look like:
5918 *
5919 * bool isGray = obj.LockWord & kReadBarrierMask;
5920 * lfence; // load fence or artificial data dependence to prevent load-load reordering
5921 * ref = obj.field; // this is the original load
5922 * if (isGray) {
5923 * ref = Mark(ref); // ideally the slow path just does Mark(ref)
5924 * }
5925 */
5926
5927 __ jmp(slow_path->GetEntryLabel());
5928 __ Bind(slow_path->GetExitLabel());
5929}
5930
5931void CodeGeneratorX86_64::MaybeGenerateReadBarrier(HInstruction* instruction,
5932 Location out,
5933 Location ref,
5934 Location obj,
5935 uint32_t offset,
5936 Location index) {
5937 if (kEmitCompilerReadBarrier) {
5938 // If heap poisoning is enabled, unpoisoning will be taken care of
5939 // by the runtime within the slow path.
5940 GenerateReadBarrier(instruction, out, ref, obj, offset, index);
5941 } else if (kPoisonHeapReferences) {
5942 __ UnpoisonHeapReference(out.AsRegister<CpuRegister>());
5943 }
5944}
5945
5946void CodeGeneratorX86_64::GenerateReadBarrierForRoot(HInstruction* instruction,
5947 Location out,
5948 Location root) {
5949 DCHECK(kEmitCompilerReadBarrier);
5950
5951 // Note that GC roots are not affected by heap poisoning, so we do
5952 // not need to do anything special for this here.
5953 SlowPathCode* slow_path =
5954 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86_64(instruction, out, root);
5955 AddSlowPath(slow_path);
5956
5957 // TODO: Implement a fast path for ReadBarrierForRoot, performing
5958 // the following operation (for Baker's algorithm):
5959 //
5960 // if (thread.tls32_.is_gc_marking) {
5961 // root = Mark(root);
5962 // }
5963
5964 __ jmp(slow_path->GetEntryLabel());
5965 __ Bind(slow_path->GetExitLabel());
5966}
5967
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005968void LocationsBuilderX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005969 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005970 LOG(FATAL) << "Unreachable";
5971}
5972
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005973void InstructionCodeGeneratorX86_64::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005974 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005975 LOG(FATAL) << "Unreachable";
5976}
5977
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005978void LocationsBuilderX86_64::VisitFakeString(HFakeString* instruction) {
5979 DCHECK(codegen_->IsBaseline());
5980 LocationSummary* locations =
5981 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5982 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5983}
5984
5985void InstructionCodeGeneratorX86_64::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5986 DCHECK(codegen_->IsBaseline());
5987 // Will be generated at use site.
5988}
5989
Mark Mendellfe57faa2015-09-18 09:26:15 -04005990// Simple implementation of packed switch - generate cascaded compare/jumps.
5991void LocationsBuilderX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5992 LocationSummary* locations =
5993 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5994 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell9c86b482015-09-18 13:36:07 -04005995 locations->AddTemp(Location::RequiresRegister());
5996 locations->AddTemp(Location::RequiresRegister());
Mark Mendellfe57faa2015-09-18 09:26:15 -04005997}
5998
5999void InstructionCodeGeneratorX86_64::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6000 int32_t lower_bound = switch_instr->GetStartValue();
6001 int32_t num_entries = switch_instr->GetNumEntries();
6002 LocationSummary* locations = switch_instr->GetLocations();
Mark Mendell9c86b482015-09-18 13:36:07 -04006003 CpuRegister value_reg_in = locations->InAt(0).AsRegister<CpuRegister>();
6004 CpuRegister temp_reg = locations->GetTemp(0).AsRegister<CpuRegister>();
6005 CpuRegister base_reg = locations->GetTemp(1).AsRegister<CpuRegister>();
6006
6007 // Remove the bias, if needed.
6008 Register value_reg_out = value_reg_in.AsRegister();
6009 if (lower_bound != 0) {
6010 __ leal(temp_reg, Address(value_reg_in, -lower_bound));
6011 value_reg_out = temp_reg.AsRegister();
6012 }
6013 CpuRegister value_reg(value_reg_out);
6014
6015 // Is the value in range?
Mark Mendellfe57faa2015-09-18 09:26:15 -04006016 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
Mark Mendell9c86b482015-09-18 13:36:07 -04006017 __ cmpl(value_reg, Immediate(num_entries - 1));
6018 __ j(kAbove, codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006019
Mark Mendell9c86b482015-09-18 13:36:07 -04006020 // We are in the range of the table.
6021 // Load the address of the jump table in the constant area.
6022 __ leaq(base_reg, codegen_->LiteralCaseTable(switch_instr));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006023
Mark Mendell9c86b482015-09-18 13:36:07 -04006024 // Load the (signed) offset from the jump table.
6025 __ movsxd(temp_reg, Address(base_reg, value_reg, TIMES_4, 0));
6026
6027 // Add the offset to the address of the table base.
6028 __ addq(temp_reg, base_reg);
6029
6030 // And jump.
6031 __ jmp(temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04006032}
6033
Mark Mendell92e83bf2015-05-07 11:25:03 -04006034void CodeGeneratorX86_64::Load64BitValue(CpuRegister dest, int64_t value) {
6035 if (value == 0) {
6036 __ xorl(dest, dest);
6037 } else if (value > 0 && IsInt<32>(value)) {
6038 // We can use a 32 bit move, as it will zero-extend and is one byte shorter.
6039 __ movl(dest, Immediate(static_cast<int32_t>(value)));
6040 } else {
6041 __ movq(dest, Immediate(value));
6042 }
6043}
6044
Mark Mendellcfa410b2015-05-25 16:02:44 -04006045void CodeGeneratorX86_64::Store64BitValueToStack(Location dest, int64_t value) {
6046 DCHECK(dest.IsDoubleStackSlot());
6047 if (IsInt<32>(value)) {
6048 // Can move directly as an int32 constant.
6049 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()),
6050 Immediate(static_cast<int32_t>(value)));
6051 } else {
6052 Load64BitValue(CpuRegister(TMP), value);
6053 __ movq(Address(CpuRegister(RSP), dest.GetStackIndex()), CpuRegister(TMP));
6054 }
6055}
6056
Mark Mendell9c86b482015-09-18 13:36:07 -04006057/**
6058 * Class to handle late fixup of offsets into constant area.
6059 */
6060class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
6061 public:
6062 RIPFixup(CodeGeneratorX86_64& codegen, size_t offset)
6063 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6064
6065 protected:
6066 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6067
6068 CodeGeneratorX86_64* codegen_;
6069
6070 private:
6071 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6072 // Patch the correct offset for the instruction. We use the address of the
6073 // 'next' instruction, which is 'pos' (patch the 4 bytes before).
6074 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6075 int32_t relative_position = constant_offset - pos;
6076
6077 // Patch in the right value.
6078 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6079 }
6080
6081 // Location in constant area that the fixup refers to.
6082 size_t offset_into_constant_area_;
6083};
6084
6085/**
6086 t * Class to handle late fixup of offsets to a jump table that will be created in the
6087 * constant area.
6088 */
6089class JumpTableRIPFixup : public RIPFixup {
6090 public:
6091 JumpTableRIPFixup(CodeGeneratorX86_64& codegen, HPackedSwitch* switch_instr)
6092 : RIPFixup(codegen, -1), switch_instr_(switch_instr) {}
6093
6094 void CreateJumpTable() {
6095 X86_64Assembler* assembler = codegen_->GetAssembler();
6096
6097 // Ensure that the reference to the jump table has the correct offset.
6098 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6099 SetOffset(offset_in_constant_table);
6100
6101 // Compute the offset from the start of the function to this jump table.
6102 const int32_t current_table_offset = assembler->CodeSize() + offset_in_constant_table;
6103
6104 // Populate the jump table with the correct values for the jump table.
6105 int32_t num_entries = switch_instr_->GetNumEntries();
6106 HBasicBlock* block = switch_instr_->GetBlock();
6107 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6108 // The value that we want is the target offset - the position of the table.
6109 for (int32_t i = 0; i < num_entries; i++) {
6110 HBasicBlock* b = successors[i];
6111 Label* l = codegen_->GetLabelOf(b);
6112 DCHECK(l->IsBound());
6113 int32_t offset_to_block = l->Position() - current_table_offset;
6114 assembler->AppendInt32(offset_to_block);
6115 }
6116 }
6117
6118 private:
6119 const HPackedSwitch* switch_instr_;
6120};
6121
Mark Mendellf55c3e02015-03-26 21:07:46 -04006122void CodeGeneratorX86_64::Finalize(CodeAllocator* allocator) {
6123 // Generate the constant area if needed.
Mark Mendell39dcf552015-04-09 20:42:42 -04006124 X86_64Assembler* assembler = GetAssembler();
Mark Mendell9c86b482015-09-18 13:36:07 -04006125 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6126 // 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 -04006127 assembler->Align(4, 0);
6128 constant_area_start_ = assembler->CodeSize();
Mark Mendell9c86b482015-09-18 13:36:07 -04006129
6130 // Populate any jump tables.
6131 for (auto jump_table : fixups_to_jump_tables_) {
6132 jump_table->CreateJumpTable();
6133 }
6134
6135 // And now add the constant area to the generated code.
Mark Mendell39dcf552015-04-09 20:42:42 -04006136 assembler->AddConstantArea();
Mark Mendellf55c3e02015-03-26 21:07:46 -04006137 }
6138
6139 // And finish up.
6140 CodeGenerator::Finalize(allocator);
6141}
6142
Mark Mendellf55c3e02015-03-26 21:07:46 -04006143Address CodeGeneratorX86_64::LiteralDoubleAddress(double v) {
6144 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6145 return Address::RIP(fixup);
6146}
6147
6148Address CodeGeneratorX86_64::LiteralFloatAddress(float v) {
6149 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6150 return Address::RIP(fixup);
6151}
6152
6153Address CodeGeneratorX86_64::LiteralInt32Address(int32_t v) {
6154 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6155 return Address::RIP(fixup);
6156}
6157
6158Address CodeGeneratorX86_64::LiteralInt64Address(int64_t v) {
6159 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6160 return Address::RIP(fixup);
6161}
6162
Andreas Gampe85b62f22015-09-09 13:15:38 -07006163// TODO: trg as memory.
6164void CodeGeneratorX86_64::MoveFromReturnRegister(Location trg, Primitive::Type type) {
6165 if (!trg.IsValid()) {
6166 DCHECK(type == Primitive::kPrimVoid);
6167 return;
6168 }
6169
6170 DCHECK_NE(type, Primitive::kPrimVoid);
6171
6172 Location return_loc = InvokeDexCallingConventionVisitorX86_64().GetReturnLocation(type);
6173 if (trg.Equals(return_loc)) {
6174 return;
6175 }
6176
6177 // Let the parallel move resolver take care of all of this.
6178 HParallelMove parallel_move(GetGraph()->GetArena());
6179 parallel_move.AddMove(return_loc, trg, type, nullptr);
6180 GetMoveResolver()->EmitNativeCode(&parallel_move);
6181}
6182
Mark Mendell9c86b482015-09-18 13:36:07 -04006183Address CodeGeneratorX86_64::LiteralCaseTable(HPackedSwitch* switch_instr) {
6184 // Create a fixup to be used to create and address the jump table.
6185 JumpTableRIPFixup* table_fixup =
6186 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6187
6188 // We have to populate the jump tables.
6189 fixups_to_jump_tables_.push_back(table_fixup);
6190 return Address::RIP(table_fixup);
6191}
6192
Mark Mendellea5af682015-10-22 17:35:49 -04006193void CodeGeneratorX86_64::MoveInt64ToAddress(const Address& addr_low,
6194 const Address& addr_high,
6195 int64_t v,
6196 HInstruction* instruction) {
6197 if (IsInt<32>(v)) {
6198 int32_t v_32 = v;
6199 __ movq(addr_low, Immediate(v_32));
6200 MaybeRecordImplicitNullCheck(instruction);
6201 } else {
6202 // Didn't fit in a register. Do it in pieces.
6203 int32_t low_v = Low32Bits(v);
6204 int32_t high_v = High32Bits(v);
6205 __ movl(addr_low, Immediate(low_v));
6206 MaybeRecordImplicitNullCheck(instruction);
6207 __ movl(addr_high, Immediate(high_v));
6208 }
6209}
6210
Roland Levillain4d027112015-07-01 15:41:14 +01006211#undef __
6212
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01006213} // namespace x86_64
6214} // namespace art