blob: ed1a53623904dfa976c6eff5fc14552452fea04b [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
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.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000019#include "art_method-inl.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010020#include "class_table.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000024#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010025#include "gc/accounting/card_table.h"
Vladimir Markoeebb8212018-06-05 14:57:24 +010026#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070027#include "heap_poisoning.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040028#include "intrinsics.h"
29#include "intrinsics_x86.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000030#include "jit/profiling_info.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010031#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070032#include "lock_word.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070033#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070034#include "mirror/class-inl.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000035#include "scoped_thread_state_change-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010036#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000037#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010038#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000039#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010040#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000041
Vladimir Marko0a516052019-10-14 13:00:44 +000042namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010043
Roland Levillain0d5a2812015-11-13 10:07:31 +000044template<class MirrorType>
45class GcRoot;
46
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000047namespace x86 {
48
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010049static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010050static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050051static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010052
Mark Mendell24f2dfa2015-01-14 19:51:45 -050053static constexpr int kC2ConditionMask = 0x400;
54
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000055static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000056
Aart Bik1f8d51b2018-02-15 10:42:37 -080057static constexpr int64_t kDoubleNaN = INT64_C(0x7FF8000000000000);
58static constexpr int32_t kFloatNaN = INT32_C(0x7FC00000);
59
Vladimir Marko3232dbb2018-07-25 15:42:46 +010060static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
61 InvokeRuntimeCallingConvention calling_convention;
62 RegisterSet caller_saves = RegisterSet::Empty();
63 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
64 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
65 // that the the kPrimNot result register is the same as the first argument register.
66 return caller_saves;
67}
68
Roland Levillain7cbd27f2016-08-11 23:53:33 +010069// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
70#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070071#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010072
Andreas Gampe85b62f22015-09-09 13:15:38 -070073class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010074 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000075 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010077 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +010078 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000080 if (instruction_->CanThrowIntoCatchBlock()) {
81 // Live registers will be restored in the catch block if caught.
82 SaveLiveRegisters(codegen, instruction_->GetLocations());
83 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010084 x86_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexandre Rames8158f282015-08-07 10:26:17 +010085 instruction_,
86 instruction_->GetDexPc(),
87 this);
Roland Levillain888d0672015-11-23 18:53:50 +000088 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010089 }
90
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010091 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +010092
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010093 const char* GetDescription() const override { return "NullCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +010094
Nicolas Geoffraye5038322014-07-04 09:41:32 +010095 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010096 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
97};
98
Andreas Gampe85b62f22015-09-09 13:15:38 -070099class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000100 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000101 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000102
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100103 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +0100104 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +0000105 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100106 x86_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000107 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000108 }
109
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100110 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100111
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100112 const char* GetDescription() const override { return "DivZeroCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100113
Calin Juravled0d48522014-11-04 16:40:20 +0000114 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000115 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
116};
117
Andreas Gampe85b62f22015-09-09 13:15:38 -0700118class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000119 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000120 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
121 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000122
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100123 void EmitNativeCode(CodeGenerator* codegen) override {
Calin Juravled0d48522014-11-04 16:40:20 +0000124 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000125 if (is_div_) {
126 __ negl(reg_);
127 } else {
128 __ movl(reg_, Immediate(0));
129 }
Calin Juravled0d48522014-11-04 16:40:20 +0000130 __ jmp(GetExitLabel());
131 }
132
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100133 const char* GetDescription() const override { return "DivRemMinusOneSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100134
Calin Juravled0d48522014-11-04 16:40:20 +0000135 private:
136 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000137 bool is_div_;
138 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000139};
140
Andreas Gampe85b62f22015-09-09 13:15:38 -0700141class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100142 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000143 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100144
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100145 void EmitNativeCode(CodeGenerator* codegen) override {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100146 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100147 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100148 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000149 // We're moving two locations to locations that could overlap, so we need a parallel
150 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000151 if (instruction_->CanThrowIntoCatchBlock()) {
152 // Live registers will be restored in the catch block if caught.
153 SaveLiveRegisters(codegen, instruction_->GetLocations());
154 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400155
156 // Are we using an array length from memory?
157 HInstruction* array_length = instruction_->InputAt(1);
158 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100159 InvokeRuntimeCallingConvention calling_convention;
Mark Mendellee8d9712016-07-12 11:13:15 -0400160 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
161 // Load the array length into our temporary.
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100162 HArrayLength* length = array_length->AsArrayLength();
163 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(length);
Mark Mendellee8d9712016-07-12 11:13:15 -0400164 Location array_loc = array_length->GetLocations()->InAt(0);
165 Address array_len(array_loc.AsRegister<Register>(), len_offset);
166 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
167 // Check for conflicts with index.
168 if (length_loc.Equals(locations->InAt(0))) {
169 // We know we aren't using parameter 2.
170 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
171 }
172 __ movl(length_loc.AsRegister<Register>(), array_len);
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100173 if (mirror::kUseStringCompression && length->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100174 __ shrl(length_loc.AsRegister<Register>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700175 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400176 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000177 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100178 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000179 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100180 DataType::Type::kInt32,
Mark Mendellee8d9712016-07-12 11:13:15 -0400181 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100182 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100183 DataType::Type::kInt32);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100184 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
185 ? kQuickThrowStringBounds
186 : kQuickThrowArrayBounds;
187 x86_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100188 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000189 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100190 }
191
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100192 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100193
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100194 const char* GetDescription() const override { return "BoundsCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100195
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100196 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100197 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
198};
199
Andreas Gampe85b62f22015-09-09 13:15:38 -0700200class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000201 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000202 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000203 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000204
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100205 void EmitNativeCode(CodeGenerator* codegen) override {
Aart Bikb13c65b2017-03-21 20:14:07 -0700206 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100207 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000208 __ Bind(GetEntryLabel());
Aart Bik24b905f2017-04-06 09:59:06 -0700209 SaveLiveRegisters(codegen, locations); // Only saves full width XMM for SIMD.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100210 x86_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000211 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Aart Bik24b905f2017-04-06 09:59:06 -0700212 RestoreLiveRegisters(codegen, locations); // Only restores full width XMM for SIMD.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100213 if (successor_ == nullptr) {
214 __ jmp(GetReturnLabel());
215 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100216 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100217 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000218 }
219
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100220 Label* GetReturnLabel() {
221 DCHECK(successor_ == nullptr);
222 return &return_label_;
223 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000224
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100225 HBasicBlock* GetSuccessor() const {
226 return successor_;
227 }
228
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100229 const char* GetDescription() const override { return "SuspendCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100230
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000231 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100232 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000233 Label return_label_;
234
235 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
236};
237
Vladimir Markoaad75c62016-10-03 08:46:48 +0000238class LoadStringSlowPathX86 : public SlowPathCode {
239 public:
240 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
241
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100242 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000243 LocationSummary* locations = instruction_->GetLocations();
244 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
245
246 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
247 __ Bind(GetEntryLabel());
248 SaveLiveRegisters(codegen, locations);
249
250 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000251 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
252 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index.index_));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000253 x86_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
254 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
255 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
256 RestoreLiveRegisters(codegen, locations);
257
Vladimir Markoaad75c62016-10-03 08:46:48 +0000258 __ jmp(GetExitLabel());
259 }
260
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100261 const char* GetDescription() const override { return "LoadStringSlowPathX86"; }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000262
263 private:
264 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
265};
266
Andreas Gampe85b62f22015-09-09 13:15:38 -0700267class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000268 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100269 LoadClassSlowPathX86(HLoadClass* cls, HInstruction* at)
270 : SlowPathCode(at), cls_(cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000271 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100272 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000273 }
274
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100275 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000276 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100277 Location out = locations->Out();
278 const uint32_t dex_pc = instruction_->GetDexPc();
279 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
280 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
281
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000282 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
283 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000284 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000285
286 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100287 if (must_resolve_type) {
288 DCHECK(IsSameDexFile(cls_->GetDexFile(), x86_codegen->GetGraph()->GetDexFile()));
289 dex::TypeIndex type_index = cls_->GetTypeIndex();
290 __ movl(calling_convention.GetRegisterAt(0), Immediate(type_index.index_));
Vladimir Marko9d479252018-07-24 11:35:20 +0100291 x86_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
292 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100293 // If we also must_do_clinit, the resolved type is now in the correct register.
294 } else {
295 DCHECK(must_do_clinit);
296 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
297 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), source);
298 }
299 if (must_do_clinit) {
300 x86_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
301 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Roland Levillain888d0672015-11-23 18:53:50 +0000302 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000303
304 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000305 if (out.IsValid()) {
306 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
307 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000308 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000309 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000310 __ jmp(GetExitLabel());
311 }
312
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100313 const char* GetDescription() const override { return "LoadClassSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100314
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000315 private:
316 // The class this slow path will load.
317 HLoadClass* const cls_;
318
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000319 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
320};
321
Andreas Gampe85b62f22015-09-09 13:15:38 -0700322class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000323 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000324 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000325 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000326
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100327 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000328 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000329 DCHECK(instruction_->IsCheckCast()
330 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000331
332 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
333 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000334
Vladimir Markoe619f6c2017-12-12 16:00:01 +0000335 if (kPoisonHeapReferences &&
336 instruction_->IsCheckCast() &&
337 instruction_->AsCheckCast()->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) {
338 // First, unpoison the `cls` reference that was poisoned for direct memory comparison.
339 __ UnpoisonHeapReference(locations->InAt(1).AsRegister<Register>());
340 }
341
Vladimir Marko87584542017-12-12 17:47:52 +0000342 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000343 SaveLiveRegisters(codegen, locations);
344 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000345
346 // We're moving two locations to locations that could overlap, so we need a parallel
347 // move resolver.
348 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800349 x86_codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800350 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100351 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800352 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800353 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100354 DataType::Type::kReference);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000355 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100356 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100357 instruction_,
358 instruction_->GetDexPc(),
359 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800360 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000361 } else {
362 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800363 x86_codegen->InvokeRuntime(kQuickCheckInstanceOf,
364 instruction_,
365 instruction_->GetDexPc(),
366 this);
367 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000368 }
369
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000370 if (!is_fatal_) {
371 if (instruction_->IsInstanceOf()) {
372 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
373 }
374 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000375
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000376 __ jmp(GetExitLabel());
377 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000378 }
379
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100380 const char* GetDescription() const override { return "TypeCheckSlowPathX86"; }
381 bool IsFatal() const override { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100382
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000383 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000384 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000385
386 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
387};
388
Andreas Gampe85b62f22015-09-09 13:15:38 -0700389class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700390 public:
Aart Bik42249c32016-01-07 15:33:50 -0800391 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000392 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700393
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100394 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +0100395 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700396 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100397 LocationSummary* locations = instruction_->GetLocations();
398 SaveLiveRegisters(codegen, locations);
399 InvokeRuntimeCallingConvention calling_convention;
400 x86_codegen->Load32BitValue(
401 calling_convention.GetRegisterAt(0),
402 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100403 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100404 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700405 }
406
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100407 const char* GetDescription() const override { return "DeoptimizationSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100408
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700409 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700410 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
411};
412
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100413class ArraySetSlowPathX86 : public SlowPathCode {
414 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000415 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100416
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100417 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100418 LocationSummary* locations = instruction_->GetLocations();
419 __ Bind(GetEntryLabel());
420 SaveLiveRegisters(codegen, locations);
421
422 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100423 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100424 parallel_move.AddMove(
425 locations->InAt(0),
426 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100427 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100428 nullptr);
429 parallel_move.AddMove(
430 locations->InAt(1),
431 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100432 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100433 nullptr);
434 parallel_move.AddMove(
435 locations->InAt(2),
436 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100437 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100438 nullptr);
439 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
440
441 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100442 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000443 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100444 RestoreLiveRegisters(codegen, locations);
445 __ jmp(GetExitLabel());
446 }
447
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100448 const char* GetDescription() const override { return "ArraySetSlowPathX86"; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100449
450 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100451 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
452};
453
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100454// Slow path marking an object reference `ref` during a read
455// barrier. The field `obj.field` in the object `obj` holding this
456// reference does not get updated by this slow path after marking (see
457// ReadBarrierMarkAndUpdateFieldSlowPathX86 below for that).
458//
459// This means that after the execution of this slow path, `ref` will
460// always be up-to-date, but `obj.field` may not; i.e., after the
461// flip, `ref` will be a to-space reference, but `obj.field` will
462// probably still be a from-space reference (unless it gets updated by
463// another thread, or if another thread installed another object
464// reference (different from `ref`) in `obj.field`).
Roland Levillain7c1559a2015-12-15 10:55:36 +0000465class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
466 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100467 ReadBarrierMarkSlowPathX86(HInstruction* instruction,
468 Location ref,
469 bool unpoison_ref_before_marking)
470 : SlowPathCode(instruction),
471 ref_(ref),
472 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000473 DCHECK(kEmitCompilerReadBarrier);
474 }
475
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100476 const char* GetDescription() const override { return "ReadBarrierMarkSlowPathX86"; }
Roland Levillain7c1559a2015-12-15 10:55:36 +0000477
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100478 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000479 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100480 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000481 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100482 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000483 DCHECK(instruction_->IsInstanceFieldGet() ||
484 instruction_->IsStaticFieldGet() ||
485 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100486 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000487 instruction_->IsLoadClass() ||
488 instruction_->IsLoadString() ||
489 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100490 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100491 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
492 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000493 << "Unexpected instruction in read barrier marking slow path: "
494 << instruction_->DebugName();
495
496 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100497 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000498 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100499 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000500 }
Roland Levillain4359e612016-07-20 11:32:19 +0100501 // No need to save live registers; it's taken care of by the
502 // entrypoint. Also, there is no need to update the stack mask,
503 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000504 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100505 DCHECK_NE(ref_reg, ESP);
506 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100507 // "Compact" slow path, saving two moves.
508 //
509 // Instead of using the standard runtime calling convention (input
510 // and output in EAX):
511 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100512 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100513 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100514 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100515 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100516 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100517 // of a dedicated entrypoint:
518 //
519 // rX <- ReadBarrierMarkRegX(rX)
520 //
Roland Levillain97c46462017-05-11 14:04:03 +0100521 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100522 // This runtime call does not require a stack map.
523 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000524 __ jmp(GetExitLabel());
525 }
526
527 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100528 // The location (register) of the marked object reference.
529 const Location ref_;
530 // Should the reference in `ref_` be unpoisoned prior to marking it?
531 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000532
533 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
534};
535
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100536// Slow path marking an object reference `ref` during a read barrier,
537// and if needed, atomically updating the field `obj.field` in the
538// object `obj` holding this reference after marking (contrary to
539// ReadBarrierMarkSlowPathX86 above, which never tries to update
540// `obj.field`).
541//
542// This means that after the execution of this slow path, both `ref`
543// and `obj.field` will be up-to-date; i.e., after the flip, both will
544// hold the same to-space reference (unless another thread installed
545// another object reference (different from `ref`) in `obj.field`).
546class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
547 public:
548 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
549 Location ref,
550 Register obj,
551 const Address& field_addr,
552 bool unpoison_ref_before_marking,
553 Register temp)
554 : SlowPathCode(instruction),
555 ref_(ref),
556 obj_(obj),
557 field_addr_(field_addr),
558 unpoison_ref_before_marking_(unpoison_ref_before_marking),
559 temp_(temp) {
560 DCHECK(kEmitCompilerReadBarrier);
561 }
562
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100563 const char* GetDescription() const override { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100564
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100565 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100566 LocationSummary* locations = instruction_->GetLocations();
567 Register ref_reg = ref_.AsRegister<Register>();
568 DCHECK(locations->CanCall());
569 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
570 // This slow path is only used by the UnsafeCASObject intrinsic.
571 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
572 << "Unexpected instruction in read barrier marking and field updating slow path: "
573 << instruction_->DebugName();
574 DCHECK(instruction_->GetLocations()->Intrinsified());
575 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
576
577 __ Bind(GetEntryLabel());
578 if (unpoison_ref_before_marking_) {
579 // Object* ref = ref_addr->AsMirrorPtr()
580 __ MaybeUnpoisonHeapReference(ref_reg);
581 }
582
583 // Save the old (unpoisoned) reference.
584 __ movl(temp_, ref_reg);
585
586 // No need to save live registers; it's taken care of by the
587 // entrypoint. Also, there is no need to update the stack mask,
588 // as this runtime call will not trigger a garbage collection.
589 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
590 DCHECK_NE(ref_reg, ESP);
591 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
592 // "Compact" slow path, saving two moves.
593 //
594 // Instead of using the standard runtime calling convention (input
595 // and output in EAX):
596 //
597 // EAX <- ref
598 // EAX <- ReadBarrierMark(EAX)
599 // ref <- EAX
600 //
601 // we just use rX (the register containing `ref`) as input and output
602 // of a dedicated entrypoint:
603 //
604 // rX <- ReadBarrierMarkRegX(rX)
605 //
Roland Levillain97c46462017-05-11 14:04:03 +0100606 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100607 // This runtime call does not require a stack map.
608 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
609
610 // If the new reference is different from the old reference,
611 // update the field in the holder (`*field_addr`).
612 //
613 // Note that this field could also hold a different object, if
614 // another thread had concurrently changed it. In that case, the
615 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
616 // operation below would abort the CAS, leaving the field as-is.
617 NearLabel done;
618 __ cmpl(temp_, ref_reg);
619 __ j(kEqual, &done);
620
621 // Update the the holder's field atomically. This may fail if
622 // mutator updates before us, but it's OK. This is achieved
623 // using a strong compare-and-set (CAS) operation with relaxed
624 // memory synchronization ordering, where the expected value is
625 // the old reference and the desired value is the new reference.
626 // This operation is implemented with a 32-bit LOCK CMPXLCHG
627 // instruction, which requires the expected value (the old
628 // reference) to be in EAX. Save EAX beforehand, and move the
629 // expected value (stored in `temp_`) into EAX.
630 __ pushl(EAX);
631 __ movl(EAX, temp_);
632
633 // Convenience aliases.
634 Register base = obj_;
635 Register expected = EAX;
636 Register value = ref_reg;
637
638 bool base_equals_value = (base == value);
639 if (kPoisonHeapReferences) {
640 if (base_equals_value) {
641 // If `base` and `value` are the same register location, move
642 // `value` to a temporary register. This way, poisoning
643 // `value` won't invalidate `base`.
644 value = temp_;
645 __ movl(value, base);
646 }
647
648 // Check that the register allocator did not assign the location
649 // of `expected` (EAX) to `value` nor to `base`, so that heap
650 // poisoning (when enabled) works as intended below.
651 // - If `value` were equal to `expected`, both references would
652 // be poisoned twice, meaning they would not be poisoned at
653 // all, as heap poisoning uses address negation.
654 // - If `base` were equal to `expected`, poisoning `expected`
655 // would invalidate `base`.
656 DCHECK_NE(value, expected);
657 DCHECK_NE(base, expected);
658
659 __ PoisonHeapReference(expected);
660 __ PoisonHeapReference(value);
661 }
662
663 __ LockCmpxchgl(field_addr_, value);
664
665 // If heap poisoning is enabled, we need to unpoison the values
666 // that were poisoned earlier.
667 if (kPoisonHeapReferences) {
668 if (base_equals_value) {
669 // `value` has been moved to a temporary register, no need
670 // to unpoison it.
671 } else {
672 __ UnpoisonHeapReference(value);
673 }
674 // No need to unpoison `expected` (EAX), as it is be overwritten below.
675 }
676
677 // Restore EAX.
678 __ popl(EAX);
679
680 __ Bind(&done);
681 __ jmp(GetExitLabel());
682 }
683
684 private:
685 // The location (register) of the marked object reference.
686 const Location ref_;
687 // The register containing the object holding the marked object reference field.
688 const Register obj_;
689 // The address of the marked reference field. The base of this address must be `obj_`.
690 const Address field_addr_;
691
692 // Should the reference in `ref_` be unpoisoned prior to marking it?
693 const bool unpoison_ref_before_marking_;
694
695 const Register temp_;
696
697 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
698};
699
Roland Levillain0d5a2812015-11-13 10:07:31 +0000700// Slow path generating a read barrier for a heap reference.
701class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
702 public:
703 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
704 Location out,
705 Location ref,
706 Location obj,
707 uint32_t offset,
708 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000709 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000710 out_(out),
711 ref_(ref),
712 obj_(obj),
713 offset_(offset),
714 index_(index) {
715 DCHECK(kEmitCompilerReadBarrier);
716 // If `obj` is equal to `out` or `ref`, it means the initial object
717 // has been overwritten by (or after) the heap object reference load
718 // to be instrumented, e.g.:
719 //
720 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000721 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000722 //
723 // In that case, we have lost the information about the original
724 // object, and the emitted read barrier cannot work properly.
725 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
726 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
727 }
728
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100729 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000730 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
731 LocationSummary* locations = instruction_->GetLocations();
732 Register reg_out = out_.AsRegister<Register>();
733 DCHECK(locations->CanCall());
734 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100735 DCHECK(instruction_->IsInstanceFieldGet() ||
736 instruction_->IsStaticFieldGet() ||
737 instruction_->IsArrayGet() ||
738 instruction_->IsInstanceOf() ||
739 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700740 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000741 << "Unexpected instruction in read barrier for heap reference slow path: "
742 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000743
744 __ Bind(GetEntryLabel());
745 SaveLiveRegisters(codegen, locations);
746
747 // We may have to change the index's value, but as `index_` is a
748 // constant member (like other "inputs" of this slow path),
749 // introduce a copy of it, `index`.
750 Location index = index_;
751 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100752 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000753 if (instruction_->IsArrayGet()) {
754 // Compute the actual memory offset and store it in `index`.
755 Register index_reg = index_.AsRegister<Register>();
756 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
757 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
758 // We are about to change the value of `index_reg` (see the
759 // calls to art::x86::X86Assembler::shll and
760 // art::x86::X86Assembler::AddImmediate below), but it has
761 // not been saved by the previous call to
762 // art::SlowPathCode::SaveLiveRegisters, as it is a
763 // callee-save register --
764 // art::SlowPathCode::SaveLiveRegisters does not consider
765 // callee-save registers, as it has been designed with the
766 // assumption that callee-save registers are supposed to be
767 // handled by the called function. So, as a callee-save
768 // register, `index_reg` _would_ eventually be saved onto
769 // the stack, but it would be too late: we would have
770 // changed its value earlier. Therefore, we manually save
771 // it here into another freely available register,
772 // `free_reg`, chosen of course among the caller-save
773 // registers (as a callee-save `free_reg` register would
774 // exhibit the same problem).
775 //
776 // Note we could have requested a temporary register from
777 // the register allocator instead; but we prefer not to, as
778 // this is a slow path, and we know we can find a
779 // caller-save register that is available.
780 Register free_reg = FindAvailableCallerSaveRegister(codegen);
781 __ movl(free_reg, index_reg);
782 index_reg = free_reg;
783 index = Location::RegisterLocation(index_reg);
784 } else {
785 // The initial register stored in `index_` has already been
786 // saved in the call to art::SlowPathCode::SaveLiveRegisters
787 // (as it is not a callee-save register), so we can freely
788 // use it.
789 }
790 // Shifting the index value contained in `index_reg` by the scale
791 // factor (2) cannot overflow in practice, as the runtime is
792 // unable to allocate object arrays with a size larger than
793 // 2^26 - 1 (that is, 2^28 - 4 bytes).
794 __ shll(index_reg, Immediate(TIMES_4));
795 static_assert(
796 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
797 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
798 __ AddImmediate(index_reg, Immediate(offset_));
799 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100800 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
801 // intrinsics, `index_` is not shifted by a scale factor of 2
802 // (as in the case of ArrayGet), as it is actually an offset
803 // to an object field within an object.
804 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000805 DCHECK(instruction_->GetLocations()->Intrinsified());
806 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
807 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
808 << instruction_->AsInvoke()->GetIntrinsic();
809 DCHECK_EQ(offset_, 0U);
810 DCHECK(index_.IsRegisterPair());
811 // UnsafeGet's offset location is a register pair, the low
812 // part contains the correct offset.
813 index = index_.ToLow();
814 }
815 }
816
817 // We're moving two or three locations to locations that could
818 // overlap, so we need a parallel move resolver.
819 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100820 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000821 parallel_move.AddMove(ref_,
822 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100823 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000824 nullptr);
825 parallel_move.AddMove(obj_,
826 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100827 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000828 nullptr);
829 if (index.IsValid()) {
830 parallel_move.AddMove(index,
831 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100832 DataType::Type::kInt32,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000833 nullptr);
834 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
835 } else {
836 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
837 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
838 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100839 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000840 CheckEntrypointTypes<
841 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
842 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
843
844 RestoreLiveRegisters(codegen, locations);
845 __ jmp(GetExitLabel());
846 }
847
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100848 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathX86"; }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000849
850 private:
851 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
852 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
853 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
854 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
855 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
856 return static_cast<Register>(i);
857 }
858 }
859 // We shall never fail to find a free caller-save register, as
860 // there are more than two core caller-save registers on x86
861 // (meaning it is possible to find one which is different from
862 // `ref` and `obj`).
863 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
864 LOG(FATAL) << "Could not find a free caller-save register";
865 UNREACHABLE();
866 }
867
Roland Levillain0d5a2812015-11-13 10:07:31 +0000868 const Location out_;
869 const Location ref_;
870 const Location obj_;
871 const uint32_t offset_;
872 // An additional location containing an index to an array.
873 // Only used for HArrayGet and the UnsafeGetObject &
874 // UnsafeGetObjectVolatile intrinsics.
875 const Location index_;
876
877 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
878};
879
880// Slow path generating a read barrier for a GC root.
881class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
882 public:
883 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000884 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000885 DCHECK(kEmitCompilerReadBarrier);
886 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000887
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100888 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000889 LocationSummary* locations = instruction_->GetLocations();
890 Register reg_out = out_.AsRegister<Register>();
891 DCHECK(locations->CanCall());
892 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000893 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
894 << "Unexpected instruction in read barrier for GC root slow path: "
895 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000896
897 __ Bind(GetEntryLabel());
898 SaveLiveRegisters(codegen, locations);
899
900 InvokeRuntimeCallingConvention calling_convention;
901 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
902 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100903 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000904 instruction_,
905 instruction_->GetDexPc(),
906 this);
907 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
908 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
909
910 RestoreLiveRegisters(codegen, locations);
911 __ jmp(GetExitLabel());
912 }
913
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100914 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathX86"; }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000915
916 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000917 const Location out_;
918 const Location root_;
919
920 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
921};
922
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100923#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100924// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
925#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100926
Aart Bike9f37602015-10-09 11:15:55 -0700927inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700928 switch (cond) {
929 case kCondEQ: return kEqual;
930 case kCondNE: return kNotEqual;
931 case kCondLT: return kLess;
932 case kCondLE: return kLessEqual;
933 case kCondGT: return kGreater;
934 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700935 case kCondB: return kBelow;
936 case kCondBE: return kBelowEqual;
937 case kCondA: return kAbove;
938 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700939 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100940 LOG(FATAL) << "Unreachable";
941 UNREACHABLE();
942}
943
Aart Bike9f37602015-10-09 11:15:55 -0700944// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100945inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
946 switch (cond) {
947 case kCondEQ: return kEqual;
948 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700949 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100950 case kCondLT: return kBelow;
951 case kCondLE: return kBelowEqual;
952 case kCondGT: return kAbove;
953 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700954 // Unsigned remain unchanged.
955 case kCondB: return kBelow;
956 case kCondBE: return kBelowEqual;
957 case kCondA: return kAbove;
958 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100959 }
960 LOG(FATAL) << "Unreachable";
961 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700962}
963
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100964void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100965 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100966}
967
968void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100969 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100970}
971
Vladimir Markoa0431112018-06-25 09:32:54 +0100972const X86InstructionSetFeatures& CodeGeneratorX86::GetInstructionSetFeatures() const {
973 return *GetCompilerOptions().GetInstructionSetFeatures()->AsX86InstructionSetFeatures();
974}
975
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100976size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
977 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
978 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100979}
980
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100981size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
982 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
983 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100984}
985
Mark Mendell7c8d0092015-01-26 11:21:33 -0500986size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700987 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700988 __ movups(Address(ESP, stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -0700989 } else {
990 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
991 }
Artem Serov6a0b6572019-07-26 20:38:37 +0100992 return GetSlowPathFPWidth();
Mark Mendell7c8d0092015-01-26 11:21:33 -0500993}
994
995size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700996 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700997 __ movups(XmmRegister(reg_id), Address(ESP, stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -0700998 } else {
999 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
1000 }
Artem Serov6a0b6572019-07-26 20:38:37 +01001001 return GetSlowPathFPWidth();
Mark Mendell7c8d0092015-01-26 11:21:33 -05001002}
1003
Calin Juravle175dc732015-08-25 15:42:32 +01001004void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
1005 HInstruction* instruction,
1006 uint32_t dex_pc,
1007 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001008 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001009 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
1010 if (EntrypointRequiresStackMap(entrypoint)) {
1011 RecordPcInfo(instruction, dex_pc, slow_path);
1012 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001013}
1014
Roland Levillaindec8f632016-07-22 17:10:06 +01001015void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1016 HInstruction* instruction,
1017 SlowPathCode* slow_path) {
1018 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001019 GenerateInvokeRuntime(entry_point_offset);
1020}
1021
1022void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001023 __ fs()->call(Address::Absolute(entry_point_offset));
1024}
1025
Mark Mendellfb8d2792015-03-31 22:16:59 -04001026CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001027 const CompilerOptions& compiler_options,
1028 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001029 : CodeGenerator(graph,
1030 kNumberOfCpuRegisters,
1031 kNumberOfXmmRegisters,
1032 kNumberOfRegisterPairs,
1033 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1034 arraysize(kCoreCalleeSaves))
1035 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001036 0,
1037 compiler_options,
1038 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001039 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001040 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001041 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001042 move_resolver_(graph->GetAllocator(), this),
1043 assembler_(graph->GetAllocator()),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001044 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1045 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1046 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1047 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001048 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001049 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +01001050 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001051 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1052 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001053 constant_area_start_(-1),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001054 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001055 method_address_offset_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001056 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001057 // Use a fake return address register to mimic Quick.
1058 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001059}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001060
David Brazdil58282f42016-01-14 12:45:10 +00001061void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001062 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001063 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001064}
1065
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001066InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001067 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001068 assembler_(codegen->GetAssembler()),
1069 codegen_(codegen) {}
1070
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001071static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001072 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001073}
1074
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001075void CodeGeneratorX86::MaybeIncrementHotness(bool is_frame_entry) {
1076 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1077 Register reg = EAX;
1078 if (is_frame_entry) {
1079 reg = kMethodRegisterArgument;
1080 } else {
1081 __ pushl(EAX);
1082 __ movl(EAX, Address(ESP, kX86WordSize));
1083 }
1084 NearLabel overflow;
1085 __ cmpw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()),
1086 Immediate(ArtMethod::MaxCounter()));
1087 __ j(kEqual, &overflow);
1088 __ addw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()),
1089 Immediate(1));
1090 __ Bind(&overflow);
1091 if (!is_frame_entry) {
1092 __ popl(EAX);
1093 }
1094 }
1095
1096 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
1097 ScopedObjectAccess soa(Thread::Current());
1098 ProfilingInfo* info = GetGraph()->GetArtMethod()->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001099 if (info != nullptr) {
1100 uint32_t address = reinterpret_cast32<uint32_t>(info);
1101 NearLabel done;
1102 if (HasEmptyFrame()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001103 CHECK(is_frame_entry);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001104 // Alignment
1105 __ subl(ESP, Immediate(8));
1106 __ cfi().AdjustCFAOffset(8);
1107 // We need a temporary. The stub also expects the method at bottom of stack.
1108 __ pushl(EAX);
1109 __ cfi().AdjustCFAOffset(4);
1110 __ movl(EAX, Immediate(address));
1111 __ addw(Address(EAX, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()),
1112 Immediate(1));
1113 __ j(kCarryClear, &done);
1114 GenerateInvokeRuntime(
1115 GetThreadOffset<kX86PointerSize>(kQuickCompileOptimized).Int32Value());
1116 __ Bind(&done);
1117 // We don't strictly require to restore EAX, but this makes the generated
1118 // code easier to reason about.
1119 __ popl(EAX);
1120 __ cfi().AdjustCFAOffset(-4);
1121 __ addl(ESP, Immediate(8));
1122 __ cfi().AdjustCFAOffset(-8);
1123 } else {
1124 if (!RequiresCurrentMethod()) {
1125 CHECK(is_frame_entry);
1126 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1127 }
1128 // We need a temporary.
1129 __ pushl(EAX);
1130 __ cfi().AdjustCFAOffset(4);
1131 __ movl(EAX, Immediate(address));
1132 __ addw(Address(EAX, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()),
1133 Immediate(1));
1134 __ popl(EAX); // Put stack as expected before exiting or calling stub.
1135 __ cfi().AdjustCFAOffset(-4);
1136 __ j(kCarryClear, &done);
1137 GenerateInvokeRuntime(
1138 GetThreadOffset<kX86PointerSize>(kQuickCompileOptimized).Int32Value());
1139 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001140 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001141 }
1142 }
1143}
1144
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001145void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001146 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001147 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001148 bool skip_overflow_check =
1149 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001150 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001151
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001152 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001153 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86);
1154 __ testl(EAX, Address(ESP, -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001155 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001156 }
1157
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001158 if (!HasEmptyFrame()) {
1159 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1160 Register reg = kCoreCalleeSaves[i];
1161 if (allocated_registers_.ContainsCoreRegister(reg)) {
1162 __ pushl(reg);
1163 __ cfi().AdjustCFAOffset(kX86WordSize);
1164 __ cfi().RelOffset(DWARFReg(reg), 0);
1165 }
1166 }
Mark Mendell5f874182015-03-04 15:42:45 -05001167
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001168 int adjust = GetFrameSize() - FrameEntrySpillSize();
1169 __ subl(ESP, Immediate(adjust));
1170 __ cfi().AdjustCFAOffset(adjust);
1171 // Save the current method if we need it. Note that we do not
1172 // do this in HCurrentMethod, as the instruction might have been removed
1173 // in the SSA graph.
1174 if (RequiresCurrentMethod()) {
1175 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1176 }
1177
1178 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1179 // Initialize should_deoptimize flag to 0.
1180 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
Mark Mendell5f874182015-03-04 15:42:45 -05001181 }
1182 }
1183
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001184 MaybeIncrementHotness(/* is_frame_entry= */ true);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001185}
1186
1187void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001188 __ cfi().RememberState();
1189 if (!HasEmptyFrame()) {
1190 int adjust = GetFrameSize() - FrameEntrySpillSize();
1191 __ addl(ESP, Immediate(adjust));
1192 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001193
David Srbeckyc34dc932015-04-12 09:27:43 +01001194 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1195 Register reg = kCoreCalleeSaves[i];
1196 if (allocated_registers_.ContainsCoreRegister(reg)) {
1197 __ popl(reg);
1198 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1199 __ cfi().Restore(DWARFReg(reg));
1200 }
Mark Mendell5f874182015-03-04 15:42:45 -05001201 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001202 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001203 __ ret();
1204 __ cfi().RestoreState();
1205 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001206}
1207
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001208void CodeGeneratorX86::Bind(HBasicBlock* block) {
1209 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001210}
1211
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001212Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001213 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001214 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001215 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001216 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001217 case DataType::Type::kInt8:
1218 case DataType::Type::kUint16:
1219 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08001220 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001221 case DataType::Type::kInt32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001222 return Location::RegisterLocation(EAX);
1223
Aart Bik66c158e2018-01-31 12:55:04 -08001224 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001225 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001226 return Location::RegisterPairLocation(EAX, EDX);
1227
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001228 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001229 return Location::NoLocation();
1230
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001231 case DataType::Type::kFloat64:
1232 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001233 return Location::FpuRegisterLocation(XMM0);
1234 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001235
1236 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001237}
1238
1239Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1240 return Location::RegisterLocation(kMethodRegisterArgument);
1241}
1242
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001243Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001244 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001245 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001246 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001247 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001248 case DataType::Type::kInt8:
1249 case DataType::Type::kUint16:
1250 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001251 case DataType::Type::kInt32: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001252 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001253 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001254 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001255 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001256 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001257 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001258 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001259 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001260
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001261 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001262 uint32_t index = gp_index_;
1263 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001264 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001265 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001266 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1267 calling_convention.GetRegisterPairAt(index));
1268 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001269 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001270 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1271 }
1272 }
1273
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001274 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001275 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001276 stack_index_++;
1277 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1278 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1279 } else {
1280 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1281 }
1282 }
1283
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001284 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001285 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001286 stack_index_ += 2;
1287 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1288 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1289 } else {
1290 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001291 }
1292 }
1293
Aart Bik66c158e2018-01-31 12:55:04 -08001294 case DataType::Type::kUint32:
1295 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001296 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001297 LOG(FATAL) << "Unexpected parameter type " << type;
Elliott Hughesc1896c92018-11-29 11:33:18 -08001298 UNREACHABLE();
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001299 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001300 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001301}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001302
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001303void CodeGeneratorX86::Move32(Location destination, Location source) {
1304 if (source.Equals(destination)) {
1305 return;
1306 }
1307 if (destination.IsRegister()) {
1308 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001309 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001310 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001311 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001312 } else {
1313 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001314 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001315 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001316 } else if (destination.IsFpuRegister()) {
1317 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001318 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001319 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001320 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001321 } else {
1322 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001323 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001324 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001325 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001326 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001327 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001328 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001329 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001330 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001331 } else if (source.IsConstant()) {
1332 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001333 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001334 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001335 } else {
1336 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001337 __ pushl(Address(ESP, source.GetStackIndex()));
1338 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001339 }
1340 }
1341}
1342
1343void CodeGeneratorX86::Move64(Location destination, Location source) {
1344 if (source.Equals(destination)) {
1345 return;
1346 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001347 if (destination.IsRegisterPair()) {
1348 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001349 EmitParallelMoves(
1350 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1351 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001352 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001353 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001354 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001355 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001356 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001357 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1358 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1359 __ psrlq(src_reg, Immediate(32));
1360 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001361 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001362 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001363 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001364 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1365 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001366 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1367 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001368 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001369 if (source.IsFpuRegister()) {
1370 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1371 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001372 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001373 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001374 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001375 // Create stack space for 2 elements.
1376 __ subl(ESP, Immediate(2 * elem_size));
1377 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1378 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1379 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1380 // And remove the temporary stack space we allocated.
1381 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001382 } else {
1383 LOG(FATAL) << "Unimplemented";
1384 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001385 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001386 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001387 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001388 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001389 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001390 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001391 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001392 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001393 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001394 } else if (source.IsConstant()) {
1395 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001396 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1397 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001398 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001399 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1400 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001401 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001402 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001403 EmitParallelMoves(
1404 Location::StackSlot(source.GetStackIndex()),
1405 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001406 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001407 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001408 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001409 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001410 }
1411 }
1412}
1413
Calin Juravle175dc732015-08-25 15:42:32 +01001414void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1415 DCHECK(location.IsRegister());
1416 __ movl(location.AsRegister<Register>(), Immediate(value));
1417}
1418
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001419void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001420 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001421 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1422 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1423 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001424 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001425 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001426 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001427 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001428}
1429
1430void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1431 if (location.IsRegister()) {
1432 locations->AddTemp(location);
1433 } else if (location.IsRegisterPair()) {
1434 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1435 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1436 } else {
1437 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1438 }
1439}
1440
David Brazdilfc6a86a2015-06-26 10:33:45 +00001441void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001442 if (successor->IsExitBlock()) {
1443 DCHECK(got->GetPrevious()->AlwaysThrows());
1444 return; // no code needed
1445 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001446
1447 HBasicBlock* block = got->GetBlock();
1448 HInstruction* previous = got->GetPrevious();
1449
1450 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001451 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001452 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001453 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1454 return;
1455 }
1456
1457 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1458 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1459 }
1460 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001461 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001462 }
1463}
1464
David Brazdilfc6a86a2015-06-26 10:33:45 +00001465void LocationsBuilderX86::VisitGoto(HGoto* got) {
1466 got->SetLocations(nullptr);
1467}
1468
1469void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1470 HandleGoto(got, got->GetSuccessor());
1471}
1472
1473void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1474 try_boundary->SetLocations(nullptr);
1475}
1476
1477void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1478 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1479 if (!successor->IsExitBlock()) {
1480 HandleGoto(try_boundary, successor);
1481 }
1482}
1483
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001484void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001485 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001486}
1487
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001488void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001489}
1490
Mark Mendell152408f2015-12-31 12:28:50 -05001491template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001492void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001493 LabelType* true_label,
1494 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001495 if (cond->IsFPConditionTrueIfNaN()) {
1496 __ j(kUnordered, true_label);
1497 } else if (cond->IsFPConditionFalseIfNaN()) {
1498 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001499 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001500 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001501}
1502
Mark Mendell152408f2015-12-31 12:28:50 -05001503template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001504void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001505 LabelType* true_label,
1506 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001507 LocationSummary* locations = cond->GetLocations();
1508 Location left = locations->InAt(0);
1509 Location right = locations->InAt(1);
1510 IfCondition if_cond = cond->GetCondition();
1511
Mark Mendellc4701932015-04-10 13:18:51 -04001512 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001513 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001514 IfCondition true_high_cond = if_cond;
1515 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001516 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001517
1518 // Set the conditions for the test, remembering that == needs to be
1519 // decided using the low words.
1520 switch (if_cond) {
1521 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001522 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001523 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001524 break;
1525 case kCondLT:
1526 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001527 break;
1528 case kCondLE:
1529 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001530 break;
1531 case kCondGT:
1532 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001533 break;
1534 case kCondGE:
1535 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001536 break;
Aart Bike9f37602015-10-09 11:15:55 -07001537 case kCondB:
1538 false_high_cond = kCondA;
1539 break;
1540 case kCondBE:
1541 true_high_cond = kCondB;
1542 break;
1543 case kCondA:
1544 false_high_cond = kCondB;
1545 break;
1546 case kCondAE:
1547 true_high_cond = kCondA;
1548 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001549 }
1550
1551 if (right.IsConstant()) {
1552 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001553 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001554 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001555
Aart Bika19616e2016-02-01 18:57:58 -08001556 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001557 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001558 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001559 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001560 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001561 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001562 __ j(X86Condition(true_high_cond), true_label);
1563 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001564 }
1565 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001566 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001567 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001568 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001569 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001570
1571 __ cmpl(left_high, right_high);
1572 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001573 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001574 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001575 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001576 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001577 __ j(X86Condition(true_high_cond), true_label);
1578 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001579 }
1580 // Must be equal high, so compare the lows.
1581 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001582 } else {
1583 DCHECK(right.IsDoubleStackSlot());
1584 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1585 if (if_cond == kCondNE) {
1586 __ j(X86Condition(true_high_cond), true_label);
1587 } else if (if_cond == kCondEQ) {
1588 __ j(X86Condition(false_high_cond), false_label);
1589 } else {
1590 __ j(X86Condition(true_high_cond), true_label);
1591 __ j(X86Condition(false_high_cond), false_label);
1592 }
1593 // Must be equal high, so compare the lows.
1594 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001595 }
1596 // The last comparison might be unsigned.
1597 __ j(final_condition, true_label);
1598}
1599
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001600void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1601 Location rhs,
1602 HInstruction* insn,
1603 bool is_double) {
1604 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1605 if (is_double) {
1606 if (rhs.IsFpuRegister()) {
1607 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1608 } else if (const_area != nullptr) {
1609 DCHECK(const_area->IsEmittedAtUseSite());
1610 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1611 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001612 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1613 const_area->GetBaseMethodAddress(),
1614 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001615 } else {
1616 DCHECK(rhs.IsDoubleStackSlot());
1617 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1618 }
1619 } else {
1620 if (rhs.IsFpuRegister()) {
1621 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1622 } else if (const_area != nullptr) {
1623 DCHECK(const_area->IsEmittedAtUseSite());
1624 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1625 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001626 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1627 const_area->GetBaseMethodAddress(),
1628 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001629 } else {
1630 DCHECK(rhs.IsStackSlot());
1631 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1632 }
1633 }
1634}
1635
Mark Mendell152408f2015-12-31 12:28:50 -05001636template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001637void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001638 LabelType* true_target_in,
1639 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001640 // Generated branching requires both targets to be explicit. If either of the
1641 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001642 LabelType fallthrough_target;
1643 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1644 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001645
Mark Mendellc4701932015-04-10 13:18:51 -04001646 LocationSummary* locations = condition->GetLocations();
1647 Location left = locations->InAt(0);
1648 Location right = locations->InAt(1);
1649
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001650 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001651 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001652 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001653 GenerateLongComparesAndJumps(condition, true_target, false_target);
1654 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001655 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001656 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001657 GenerateFPJumps(condition, true_target, false_target);
1658 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001659 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001660 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001661 GenerateFPJumps(condition, true_target, false_target);
1662 break;
1663 default:
1664 LOG(FATAL) << "Unexpected compare type " << type;
1665 }
1666
David Brazdil0debae72015-11-12 18:37:00 +00001667 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001668 __ jmp(false_target);
1669 }
David Brazdil0debae72015-11-12 18:37:00 +00001670
1671 if (fallthrough_target.IsLinked()) {
1672 __ Bind(&fallthrough_target);
1673 }
Mark Mendellc4701932015-04-10 13:18:51 -04001674}
1675
David Brazdil0debae72015-11-12 18:37:00 +00001676static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1677 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1678 // are set only strictly before `branch`. We can't use the eflags on long/FP
1679 // conditions if they are materialized due to the complex branching.
1680 return cond->IsCondition() &&
1681 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001682 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1683 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001684}
1685
Mark Mendell152408f2015-12-31 12:28:50 -05001686template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001687void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001688 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001689 LabelType* true_target,
1690 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001691 HInstruction* cond = instruction->InputAt(condition_input_index);
1692
1693 if (true_target == nullptr && false_target == nullptr) {
1694 // Nothing to do. The code always falls through.
1695 return;
1696 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001697 // Constant condition, statically compared against "true" (integer value 1).
1698 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001699 if (true_target != nullptr) {
1700 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001701 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001702 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001703 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001704 if (false_target != nullptr) {
1705 __ jmp(false_target);
1706 }
1707 }
1708 return;
1709 }
1710
1711 // The following code generates these patterns:
1712 // (1) true_target == nullptr && false_target != nullptr
1713 // - opposite condition true => branch to false_target
1714 // (2) true_target != nullptr && false_target == nullptr
1715 // - condition true => branch to true_target
1716 // (3) true_target != nullptr && false_target != nullptr
1717 // - condition true => branch to true_target
1718 // - branch to false_target
1719 if (IsBooleanValueOrMaterializedCondition(cond)) {
1720 if (AreEflagsSetFrom(cond, instruction)) {
1721 if (true_target == nullptr) {
1722 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1723 } else {
1724 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1725 }
1726 } else {
1727 // Materialized condition, compare against 0.
1728 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1729 if (lhs.IsRegister()) {
1730 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1731 } else {
1732 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1733 }
1734 if (true_target == nullptr) {
1735 __ j(kEqual, false_target);
1736 } else {
1737 __ j(kNotEqual, true_target);
1738 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001739 }
1740 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001741 // Condition has not been materialized, use its inputs as the comparison and
1742 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001743 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001744
1745 // If this is a long or FP comparison that has been folded into
1746 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001747 DataType::Type type = condition->InputAt(0)->GetType();
1748 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001749 GenerateCompareTestAndBranch(condition, true_target, false_target);
1750 return;
1751 }
1752
1753 Location lhs = condition->GetLocations()->InAt(0);
1754 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001755 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001756 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001757 if (true_target == nullptr) {
1758 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1759 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001760 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001761 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001762 }
David Brazdil0debae72015-11-12 18:37:00 +00001763
1764 // If neither branch falls through (case 3), the conditional branch to `true_target`
1765 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1766 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001767 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001768 }
1769}
1770
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001771void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001772 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001773 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001774 locations->SetInAt(0, Location::Any());
1775 }
1776}
1777
1778void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001779 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1780 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1781 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1782 nullptr : codegen_->GetLabelOf(true_successor);
1783 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1784 nullptr : codegen_->GetLabelOf(false_successor);
Andreas Gampe3db70682018-12-26 15:12:03 -08001785 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001786}
1787
1788void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001789 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001790 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001791 InvokeRuntimeCallingConvention calling_convention;
1792 RegisterSet caller_saves = RegisterSet::Empty();
1793 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1794 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001795 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001796 locations->SetInAt(0, Location::Any());
1797 }
1798}
1799
1800void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001801 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001802 GenerateTestAndBranch<Label>(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08001803 /* condition_input_index= */ 0,
David Brazdil74eb1b22015-12-14 11:44:01 +00001804 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08001805 /* false_target= */ nullptr);
David Brazdil74eb1b22015-12-14 11:44:01 +00001806}
1807
Mingyao Yang063fc772016-08-02 11:02:54 -07001808void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001809 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001810 LocationSummary(flag, LocationSummary::kNoCall);
1811 locations->SetOut(Location::RequiresRegister());
1812}
1813
1814void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1815 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1816 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1817}
1818
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001819static bool SelectCanUseCMOV(HSelect* select) {
1820 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001821 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001822 return false;
1823 }
1824
1825 // A FP condition doesn't generate the single CC that we need.
1826 // In 32 bit mode, a long condition doesn't generate a single CC either.
1827 HInstruction* condition = select->GetCondition();
1828 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001829 DataType::Type compare_type = condition->InputAt(0)->GetType();
1830 if (compare_type == DataType::Type::kInt64 ||
1831 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001832 return false;
1833 }
1834 }
1835
1836 // We can generate a CMOV for this Select.
1837 return true;
1838}
1839
David Brazdil74eb1b22015-12-14 11:44:01 +00001840void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001841 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001842 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001843 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001844 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001845 } else {
1846 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001847 if (SelectCanUseCMOV(select)) {
1848 if (select->InputAt(1)->IsConstant()) {
1849 // Cmov can't handle a constant value.
1850 locations->SetInAt(1, Location::RequiresRegister());
1851 } else {
1852 locations->SetInAt(1, Location::Any());
1853 }
1854 } else {
1855 locations->SetInAt(1, Location::Any());
1856 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001857 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001858 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1859 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001860 }
1861 locations->SetOut(Location::SameAsFirstInput());
1862}
1863
1864void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1865 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001866 DCHECK(locations->InAt(0).Equals(locations->Out()));
1867 if (SelectCanUseCMOV(select)) {
1868 // If both the condition and the source types are integer, we can generate
1869 // a CMOV to implement Select.
1870
1871 HInstruction* select_condition = select->GetCondition();
1872 Condition cond = kNotEqual;
1873
1874 // Figure out how to test the 'condition'.
1875 if (select_condition->IsCondition()) {
1876 HCondition* condition = select_condition->AsCondition();
1877 if (!condition->IsEmittedAtUseSite()) {
1878 // This was a previously materialized condition.
1879 // Can we use the existing condition code?
1880 if (AreEflagsSetFrom(condition, select)) {
1881 // Materialization was the previous instruction. Condition codes are right.
1882 cond = X86Condition(condition->GetCondition());
1883 } else {
1884 // No, we have to recreate the condition code.
1885 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1886 __ testl(cond_reg, cond_reg);
1887 }
1888 } else {
1889 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001890 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
1891 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001892 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001893 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001894 cond = X86Condition(condition->GetCondition());
1895 }
1896 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001897 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001898 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1899 __ testl(cond_reg, cond_reg);
1900 }
1901
1902 // If the condition is true, overwrite the output, which already contains false.
1903 Location false_loc = locations->InAt(0);
1904 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001905 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001906 // 64 bit conditional move.
1907 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1908 Register false_low = false_loc.AsRegisterPairLow<Register>();
1909 if (true_loc.IsRegisterPair()) {
1910 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1911 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1912 } else {
1913 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1914 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1915 }
1916 } else {
1917 // 32 bit conditional move.
1918 Register false_reg = false_loc.AsRegister<Register>();
1919 if (true_loc.IsRegister()) {
1920 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1921 } else {
1922 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1923 }
1924 }
1925 } else {
1926 NearLabel false_target;
1927 GenerateTestAndBranch<NearLabel>(
Andreas Gampe3db70682018-12-26 15:12:03 -08001928 select, /* condition_input_index= */ 2, /* true_target= */ nullptr, &false_target);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001929 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1930 __ Bind(&false_target);
1931 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001932}
1933
David Srbecky0cf44932015-12-09 14:09:59 +00001934void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001935 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00001936}
1937
David Srbeckyd28f4a02016-03-14 17:14:24 +00001938void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1939 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001940}
1941
1942void CodeGeneratorX86::GenerateNop() {
1943 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001944}
1945
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001946void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001947 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001948 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001949 // Handle the long/FP comparisons made in instruction simplification.
1950 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001951 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001952 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001953 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001954 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001955 locations->SetOut(Location::RequiresRegister());
1956 }
1957 break;
1958 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001959 case DataType::Type::kFloat32:
1960 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001961 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001962 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1963 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1964 } else if (cond->InputAt(1)->IsConstant()) {
1965 locations->SetInAt(1, Location::RequiresFpuRegister());
1966 } else {
1967 locations->SetInAt(1, Location::Any());
1968 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001969 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001970 locations->SetOut(Location::RequiresRegister());
1971 }
1972 break;
1973 }
1974 default:
1975 locations->SetInAt(0, Location::RequiresRegister());
1976 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001977 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001978 // We need a byte register.
1979 locations->SetOut(Location::RegisterLocation(ECX));
1980 }
1981 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001982 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001983}
1984
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001985void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001986 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001987 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001988 }
Mark Mendellc4701932015-04-10 13:18:51 -04001989
1990 LocationSummary* locations = cond->GetLocations();
1991 Location lhs = locations->InAt(0);
1992 Location rhs = locations->InAt(1);
1993 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001994 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001995
1996 switch (cond->InputAt(0)->GetType()) {
1997 default: {
1998 // Integer case.
1999
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002000 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04002001 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01002002 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07002003 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04002004 return;
2005 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002006 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04002007 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2008 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002009 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002010 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04002011 GenerateFPJumps(cond, &true_label, &false_label);
2012 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002013 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002014 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04002015 GenerateFPJumps(cond, &true_label, &false_label);
2016 break;
2017 }
2018
2019 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002020 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002021
Roland Levillain4fa13f62015-07-06 18:11:54 +01002022 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04002023 __ Bind(&false_label);
2024 __ xorl(reg, reg);
2025 __ jmp(&done_label);
2026
Roland Levillain4fa13f62015-07-06 18:11:54 +01002027 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04002028 __ Bind(&true_label);
2029 __ movl(reg, Immediate(1));
2030 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002031}
2032
2033void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002034 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002035}
2036
2037void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002038 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002039}
2040
2041void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002042 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002043}
2044
2045void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002046 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002047}
2048
2049void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002050 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002051}
2052
2053void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002054 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002055}
2056
2057void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002058 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002059}
2060
2061void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002062 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002063}
2064
2065void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002066 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002067}
2068
2069void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002070 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002071}
2072
2073void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002074 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002075}
2076
2077void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002078 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002079}
2080
Aart Bike9f37602015-10-09 11:15:55 -07002081void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002082 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002083}
2084
2085void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002086 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002087}
2088
2089void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002090 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002091}
2092
2093void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002094 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002095}
2096
2097void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002098 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002099}
2100
2101void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002102 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002103}
2104
2105void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002106 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002107}
2108
2109void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002110 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002111}
2112
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002113void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002114 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002115 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002116 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002117}
2118
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002119void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002120 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002121}
2122
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002123void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2124 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002125 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002126 locations->SetOut(Location::ConstantLocation(constant));
2127}
2128
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002129void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002130 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002131}
2132
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002133void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002134 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002135 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002136 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002137}
2138
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002139void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002140 // Will be generated at use site.
2141}
2142
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002143void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2144 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002145 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002146 locations->SetOut(Location::ConstantLocation(constant));
2147}
2148
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002149void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002150 // Will be generated at use site.
2151}
2152
2153void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2154 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002155 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002156 locations->SetOut(Location::ConstantLocation(constant));
2157}
2158
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002159void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002160 // Will be generated at use site.
2161}
2162
Igor Murashkind01745e2017-04-05 16:40:31 -07002163void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2164 constructor_fence->SetLocations(nullptr);
2165}
2166
2167void InstructionCodeGeneratorX86::VisitConstructorFence(
2168 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2169 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2170}
2171
Calin Juravle27df7582015-04-17 19:12:31 +01002172void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2173 memory_barrier->SetLocations(nullptr);
2174}
2175
2176void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002177 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002178}
2179
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002180void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002181 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002182}
2183
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002184void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002185 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002186}
2187
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002188void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002189 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002190 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002191 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002192 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002193 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002194 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002195 case DataType::Type::kInt8:
2196 case DataType::Type::kUint16:
2197 case DataType::Type::kInt16:
2198 case DataType::Type::kInt32:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002199 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002200 break;
2201
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002202 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002203 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002204 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002205 break;
2206
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002207 case DataType::Type::kFloat32:
2208 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002209 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002210 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002211 break;
2212
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002213 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002214 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002215 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002216}
2217
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002218void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002219 switch (ret->InputAt(0)->GetType()) {
2220 case DataType::Type::kReference:
2221 case DataType::Type::kBool:
2222 case DataType::Type::kUint8:
2223 case DataType::Type::kInt8:
2224 case DataType::Type::kUint16:
2225 case DataType::Type::kInt16:
2226 case DataType::Type::kInt32:
2227 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
2228 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002229
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002230 case DataType::Type::kInt64:
2231 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2232 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
2233 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002234
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002235 case DataType::Type::kFloat32:
2236 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2237 if (GetGraph()->IsCompilingOsr()) {
2238 // To simplify callers of an OSR method, we put the return value in both
2239 // floating point and core registers.
2240 __ movd(EAX, XMM0);
2241 }
2242 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002243
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002244 case DataType::Type::kFloat64:
2245 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2246 if (GetGraph()->IsCompilingOsr()) {
2247 // To simplify callers of an OSR method, we put the return value in both
2248 // floating point and core registers.
2249 __ movd(EAX, XMM0);
2250 // Use XMM1 as temporary register to not clobber XMM0.
2251 __ movaps(XMM1, XMM0);
2252 __ psrlq(XMM1, Immediate(32));
2253 __ movd(EDX, XMM1);
2254 }
2255 break;
2256
2257 default:
2258 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002259 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002260 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002261}
2262
Calin Juravle175dc732015-08-25 15:42:32 +01002263void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2264 // The trampoline uses the same calling convention as dex calling conventions,
2265 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2266 // the method_idx.
2267 HandleInvoke(invoke);
2268}
2269
2270void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2271 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2272}
2273
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002274void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002275 // Explicit clinit checks triggered by static invokes must have been pruned by
2276 // art::PrepareForRegisterAllocation.
2277 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002278
Mark Mendellfb8d2792015-03-31 22:16:59 -04002279 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002280 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002281 if (invoke->GetLocations()->CanCall() &&
2282 invoke->HasPcRelativeMethodLoadKind() &&
2283 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).IsInvalid()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002284 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002285 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002286 return;
2287 }
2288
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002289 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002290
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002291 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002292 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002293 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002294 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002295}
2296
Mark Mendell09ed1a32015-03-25 08:30:06 -04002297static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2298 if (invoke->GetLocations()->Intrinsified()) {
2299 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2300 intrinsic.Dispatch(invoke);
2301 return true;
2302 }
2303 return false;
2304}
2305
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002306void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002307 // Explicit clinit checks triggered by static invokes must have been pruned by
2308 // art::PrepareForRegisterAllocation.
2309 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002310
Mark Mendell09ed1a32015-03-25 08:30:06 -04002311 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2312 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002313 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002314
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002315 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002316 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002317 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002318}
2319
2320void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002321 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2322 if (intrinsic.TryDispatch(invoke)) {
2323 return;
2324 }
2325
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002326 HandleInvoke(invoke);
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002327
2328 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002329 // Add one temporary for inline cache update.
2330 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2331 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002332}
2333
2334void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002335 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002336 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002337}
2338
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002339void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002340 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2341 return;
2342 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002343
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002344 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002345 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002346}
2347
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002348void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002349 // This call to HandleInvoke allocates a temporary (core) register
2350 // which is also used to transfer the hidden argument from FP to
2351 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002352 HandleInvoke(invoke);
2353 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002354 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002355
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002356 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002357 // Add one temporary for inline cache update.
2358 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2359 }
2360}
2361
2362void CodeGeneratorX86::MaybeGenerateInlineCacheCheck(HInstruction* instruction, Register klass) {
2363 DCHECK_EQ(EAX, klass);
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002364 // We know the destination of an intrinsic, so no need to record inline
2365 // caches (also the intrinsic location builder doesn't request an additional
2366 // temporary).
2367 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002368 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002369 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002370 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
2371 ScopedObjectAccess soa(Thread::Current());
2372 ProfilingInfo* info = GetGraph()->GetArtMethod()->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002373 if (info != nullptr) {
2374 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
2375 uint32_t address = reinterpret_cast32<uint32_t>(cache);
2376 if (kIsDebugBuild) {
2377 uint32_t temp_index = instruction->GetLocations()->GetTempCount() - 1u;
2378 CHECK_EQ(EBP, instruction->GetLocations()->GetTemp(temp_index).AsRegister<Register>());
2379 }
2380 Register temp = EBP;
2381 NearLabel done;
2382 __ movl(temp, Immediate(address));
2383 // Fast path for a monomorphic cache.
2384 __ cmpl(klass, Address(temp, InlineCache::ClassesOffset().Int32Value()));
2385 __ j(kEqual, &done);
2386 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(kQuickUpdateInlineCache).Int32Value());
2387 __ Bind(&done);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002388 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002389 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002390}
2391
2392void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2393 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002394 LocationSummary* locations = invoke->GetLocations();
2395 Register temp = locations->GetTemp(0).AsRegister<Register>();
2396 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002397 Location receiver = locations->InAt(0);
2398 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2399
Roland Levillain0d5a2812015-11-13 10:07:31 +00002400 // Set the hidden argument. This is safe to do this here, as XMM7
2401 // won't be modified thereafter, before the `call` instruction.
2402 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002403 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002404 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002405
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002406 if (receiver.IsStackSlot()) {
2407 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002408 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002409 __ movl(temp, Address(temp, class_offset));
2410 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002411 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002412 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002413 }
Roland Levillain4d027112015-07-01 15:41:14 +01002414 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002415 // Instead of simply (possibly) unpoisoning `temp` here, we should
2416 // emit a read barrier for the previous class reference load.
2417 // However this is not required in practice, as this is an
2418 // intermediate/temporary reference and because the current
2419 // concurrent copying collector keeps the from-space memory
2420 // intact/accessible until the end of the marking phase (the
2421 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002422 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002423
2424 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
2425
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002426 // temp = temp->GetAddressOfIMT()
2427 __ movl(temp,
2428 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002429 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002430 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002431 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002432 __ movl(temp, Address(temp, method_offset));
2433 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002434 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002435 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002436
2437 DCHECK(!codegen_->IsLeafMethod());
2438 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2439}
2440
Orion Hodsonac141392017-01-13 11:53:47 +00002441void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2442 HandleInvoke(invoke);
2443}
2444
2445void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2446 codegen_->GenerateInvokePolymorphicCall(invoke);
2447}
2448
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002449void LocationsBuilderX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2450 HandleInvoke(invoke);
2451}
2452
2453void InstructionCodeGeneratorX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2454 codegen_->GenerateInvokeCustomCall(invoke);
2455}
2456
Roland Levillain88cb1752014-10-20 16:36:47 +01002457void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2458 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002459 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002460 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002461 case DataType::Type::kInt32:
2462 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002463 locations->SetInAt(0, Location::RequiresRegister());
2464 locations->SetOut(Location::SameAsFirstInput());
2465 break;
2466
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002467 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002468 locations->SetInAt(0, Location::RequiresFpuRegister());
2469 locations->SetOut(Location::SameAsFirstInput());
2470 locations->AddTemp(Location::RequiresRegister());
2471 locations->AddTemp(Location::RequiresFpuRegister());
2472 break;
2473
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002474 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002475 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002476 locations->SetOut(Location::SameAsFirstInput());
2477 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002478 break;
2479
2480 default:
2481 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2482 }
2483}
2484
2485void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2486 LocationSummary* locations = neg->GetLocations();
2487 Location out = locations->Out();
2488 Location in = locations->InAt(0);
2489 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002490 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002491 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002492 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002493 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002494 break;
2495
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002496 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002497 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002498 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002499 __ negl(out.AsRegisterPairLow<Register>());
2500 // Negation is similar to subtraction from zero. The least
2501 // significant byte triggers a borrow when it is different from
2502 // zero; to take it into account, add 1 to the most significant
2503 // byte if the carry flag (CF) is set to 1 after the first NEGL
2504 // operation.
2505 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2506 __ negl(out.AsRegisterPairHigh<Register>());
2507 break;
2508
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002509 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002510 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002511 Register constant = locations->GetTemp(0).AsRegister<Register>();
2512 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002513 // Implement float negation with an exclusive or with value
2514 // 0x80000000 (mask for bit 31, representing the sign of a
2515 // single-precision floating-point number).
2516 __ movl(constant, Immediate(INT32_C(0x80000000)));
2517 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002518 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002519 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002520 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002521
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002522 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002523 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002524 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002525 // Implement double negation with an exclusive or with value
2526 // 0x8000000000000000 (mask for bit 63, representing the sign of
2527 // a double-precision floating-point number).
2528 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002529 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002530 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002531 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002532
2533 default:
2534 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2535 }
2536}
2537
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002538void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2539 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002540 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002541 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002542 locations->SetInAt(0, Location::RequiresFpuRegister());
2543 locations->SetInAt(1, Location::RequiresRegister());
2544 locations->SetOut(Location::SameAsFirstInput());
2545 locations->AddTemp(Location::RequiresFpuRegister());
2546}
2547
2548void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2549 LocationSummary* locations = neg->GetLocations();
2550 Location out = locations->Out();
2551 DCHECK(locations->InAt(0).Equals(out));
2552
2553 Register constant_area = locations->InAt(1).AsRegister<Register>();
2554 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002555 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002556 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2557 neg->GetBaseMethodAddress(),
2558 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002559 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2560 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002561 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2562 neg->GetBaseMethodAddress(),
2563 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002564 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2565 }
2566}
2567
Roland Levillaindff1f282014-11-05 14:15:05 +00002568void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002569 DataType::Type result_type = conversion->GetResultType();
2570 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002571 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2572 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002573
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002574 // The float-to-long and double-to-long type conversions rely on a
2575 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002576 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002577 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2578 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002579 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002580 : LocationSummary::kNoCall;
2581 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002582 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002583
Roland Levillaindff1f282014-11-05 14:15:05 +00002584 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002585 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002586 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002587 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002588 case DataType::Type::kUint8:
2589 case DataType::Type::kInt8:
2590 case DataType::Type::kUint16:
2591 case DataType::Type::kInt16:
2592 case DataType::Type::kInt32:
2593 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2594 // Make the output overlap to please the register allocator. This greatly simplifies
2595 // the validation of the linear scan implementation
2596 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2597 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002598 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002599 HInstruction* input = conversion->InputAt(0);
2600 Location input_location = input->IsConstant()
2601 ? Location::ConstantLocation(input->AsConstant())
2602 : Location::RegisterPairLocation(EAX, EDX);
2603 locations->SetInAt(0, input_location);
2604 // Make the output overlap to please the register allocator. This greatly simplifies
2605 // the validation of the linear scan implementation
2606 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2607 break;
2608 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002609
2610 default:
2611 LOG(FATAL) << "Unexpected type conversion from " << input_type
2612 << " to " << result_type;
2613 }
2614 break;
2615
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002616 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002617 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002618 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2619 locations->SetInAt(0, Location::Any());
2620 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002621 break;
2622
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002623 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002624 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002625 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002626 locations->SetInAt(0, Location::Any());
2627 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2628 break;
2629
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002630 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002631 locations->SetInAt(0, Location::RequiresFpuRegister());
2632 locations->SetOut(Location::RequiresRegister());
2633 locations->AddTemp(Location::RequiresFpuRegister());
2634 break;
2635
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002636 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002637 locations->SetInAt(0, Location::RequiresFpuRegister());
2638 locations->SetOut(Location::RequiresRegister());
2639 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002640 break;
2641
2642 default:
2643 LOG(FATAL) << "Unexpected type conversion from " << input_type
2644 << " to " << result_type;
2645 }
2646 break;
2647
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002648 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002649 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002650 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002651 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002652 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002653 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002654 case DataType::Type::kInt16:
2655 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002656 locations->SetInAt(0, Location::RegisterLocation(EAX));
2657 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2658 break;
2659
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002660 case DataType::Type::kFloat32:
2661 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002662 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002663 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2664 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2665
Vladimir Marko949c91f2015-01-27 10:48:44 +00002666 // The runtime helper puts the result in EAX, EDX.
2667 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002668 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002669 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002670
2671 default:
2672 LOG(FATAL) << "Unexpected type conversion from " << input_type
2673 << " to " << result_type;
2674 }
2675 break;
2676
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002677 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002678 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002679 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002680 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002681 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002682 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002683 case DataType::Type::kInt16:
2684 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002685 locations->SetInAt(0, Location::RequiresRegister());
2686 locations->SetOut(Location::RequiresFpuRegister());
2687 break;
2688
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002689 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002690 locations->SetInAt(0, Location::Any());
2691 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002692 break;
2693
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002694 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002695 locations->SetInAt(0, Location::RequiresFpuRegister());
2696 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002697 break;
2698
2699 default:
2700 LOG(FATAL) << "Unexpected type conversion from " << input_type
2701 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002702 }
Roland Levillaincff13742014-11-17 14:32:17 +00002703 break;
2704
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002705 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002706 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002707 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002708 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002709 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002710 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002711 case DataType::Type::kInt16:
2712 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002713 locations->SetInAt(0, Location::RequiresRegister());
2714 locations->SetOut(Location::RequiresFpuRegister());
2715 break;
2716
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002717 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002718 locations->SetInAt(0, Location::Any());
2719 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002720 break;
2721
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002722 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002723 locations->SetInAt(0, Location::RequiresFpuRegister());
2724 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002725 break;
2726
2727 default:
2728 LOG(FATAL) << "Unexpected type conversion from " << input_type
2729 << " to " << result_type;
2730 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002731 break;
2732
2733 default:
2734 LOG(FATAL) << "Unexpected type conversion from " << input_type
2735 << " to " << result_type;
2736 }
2737}
2738
2739void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2740 LocationSummary* locations = conversion->GetLocations();
2741 Location out = locations->Out();
2742 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002743 DataType::Type result_type = conversion->GetResultType();
2744 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002745 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2746 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002747 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002748 case DataType::Type::kUint8:
2749 switch (input_type) {
2750 case DataType::Type::kInt8:
2751 case DataType::Type::kUint16:
2752 case DataType::Type::kInt16:
2753 case DataType::Type::kInt32:
2754 if (in.IsRegister()) {
2755 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2756 } else {
2757 DCHECK(in.GetConstant()->IsIntConstant());
2758 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2759 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2760 }
2761 break;
2762 case DataType::Type::kInt64:
2763 if (in.IsRegisterPair()) {
2764 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2765 } else {
2766 DCHECK(in.GetConstant()->IsLongConstant());
2767 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2768 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2769 }
2770 break;
2771
2772 default:
2773 LOG(FATAL) << "Unexpected type conversion from " << input_type
2774 << " to " << result_type;
2775 }
2776 break;
2777
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002778 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002779 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002780 case DataType::Type::kUint8:
2781 case DataType::Type::kUint16:
2782 case DataType::Type::kInt16:
2783 case DataType::Type::kInt32:
2784 if (in.IsRegister()) {
2785 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2786 } else {
2787 DCHECK(in.GetConstant()->IsIntConstant());
2788 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2789 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2790 }
2791 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002792 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002793 if (in.IsRegisterPair()) {
2794 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2795 } else {
2796 DCHECK(in.GetConstant()->IsLongConstant());
2797 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2798 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2799 }
2800 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002801
2802 default:
2803 LOG(FATAL) << "Unexpected type conversion from " << input_type
2804 << " to " << result_type;
2805 }
2806 break;
2807
2808 case DataType::Type::kUint16:
2809 switch (input_type) {
2810 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002811 case DataType::Type::kInt16:
2812 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002813 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002814 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
2815 } else if (in.IsStackSlot()) {
2816 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002817 } else {
2818 DCHECK(in.GetConstant()->IsIntConstant());
2819 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002820 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2821 }
2822 break;
2823 case DataType::Type::kInt64:
2824 if (in.IsRegisterPair()) {
2825 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2826 } else if (in.IsDoubleStackSlot()) {
2827 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2828 } else {
2829 DCHECK(in.GetConstant()->IsLongConstant());
2830 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2831 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002832 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002833 break;
2834
2835 default:
2836 LOG(FATAL) << "Unexpected type conversion from " << input_type
2837 << " to " << result_type;
2838 }
2839 break;
2840
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002841 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002842 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002843 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002844 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00002845 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002846 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002847 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002848 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002849 } else {
2850 DCHECK(in.GetConstant()->IsIntConstant());
2851 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002852 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002853 }
2854 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002855 case DataType::Type::kInt64:
2856 if (in.IsRegisterPair()) {
2857 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2858 } else if (in.IsDoubleStackSlot()) {
2859 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2860 } else {
2861 DCHECK(in.GetConstant()->IsLongConstant());
2862 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2863 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2864 }
2865 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00002866
2867 default:
2868 LOG(FATAL) << "Unexpected type conversion from " << input_type
2869 << " to " << result_type;
2870 }
2871 break;
2872
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002873 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002874 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002875 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002876 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002877 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002878 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002879 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002880 } else {
2881 DCHECK(in.IsConstant());
2882 DCHECK(in.GetConstant()->IsLongConstant());
2883 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002884 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002885 }
2886 break;
2887
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002888 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002889 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2890 Register output = out.AsRegister<Register>();
2891 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002892 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002893
2894 __ movl(output, Immediate(kPrimIntMax));
2895 // temp = int-to-float(output)
2896 __ cvtsi2ss(temp, output);
2897 // if input >= temp goto done
2898 __ comiss(input, temp);
2899 __ j(kAboveEqual, &done);
2900 // if input == NaN goto nan
2901 __ j(kUnordered, &nan);
2902 // output = float-to-int-truncate(input)
2903 __ cvttss2si(output, input);
2904 __ jmp(&done);
2905 __ Bind(&nan);
2906 // output = 0
2907 __ xorl(output, output);
2908 __ Bind(&done);
2909 break;
2910 }
2911
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002912 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002913 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2914 Register output = out.AsRegister<Register>();
2915 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002916 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002917
2918 __ movl(output, Immediate(kPrimIntMax));
2919 // temp = int-to-double(output)
2920 __ cvtsi2sd(temp, output);
2921 // if input >= temp goto done
2922 __ comisd(input, temp);
2923 __ j(kAboveEqual, &done);
2924 // if input == NaN goto nan
2925 __ j(kUnordered, &nan);
2926 // output = double-to-int-truncate(input)
2927 __ cvttsd2si(output, input);
2928 __ jmp(&done);
2929 __ Bind(&nan);
2930 // output = 0
2931 __ xorl(output, output);
2932 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002933 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002934 }
Roland Levillain946e1432014-11-11 17:35:19 +00002935
2936 default:
2937 LOG(FATAL) << "Unexpected type conversion from " << input_type
2938 << " to " << result_type;
2939 }
2940 break;
2941
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002942 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002943 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002944 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002945 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002946 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002947 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002948 case DataType::Type::kInt16:
2949 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002950 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2951 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002952 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002953 __ cdq();
2954 break;
2955
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002956 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002957 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002958 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002959 break;
2960
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002961 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01002962 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002963 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002964 break;
2965
2966 default:
2967 LOG(FATAL) << "Unexpected type conversion from " << input_type
2968 << " to " << result_type;
2969 }
2970 break;
2971
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002972 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002973 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002974 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002975 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002976 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002977 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002978 case DataType::Type::kInt16:
2979 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002980 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002981 break;
2982
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002983 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01002984 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002985
Roland Levillain232ade02015-04-20 15:14:36 +01002986 // Create stack space for the call to
2987 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2988 // TODO: enhance register allocator to ask for stack temporaries.
2989 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002990 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01002991 __ subl(ESP, Immediate(adjustment));
2992 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002993
Roland Levillain232ade02015-04-20 15:14:36 +01002994 // Load the value to the FP stack, using temporaries if needed.
2995 PushOntoFPStack(in, 0, adjustment, false, true);
2996
2997 if (out.IsStackSlot()) {
2998 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2999 } else {
3000 __ fstps(Address(ESP, 0));
3001 Location stack_temp = Location::StackSlot(0);
3002 codegen_->Move32(out, stack_temp);
3003 }
3004
3005 // Remove the temporary stack space we allocated.
3006 if (adjustment != 0) {
3007 __ addl(ESP, Immediate(adjustment));
3008 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003009 break;
3010 }
3011
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003012 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003013 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003014 break;
3015
3016 default:
3017 LOG(FATAL) << "Unexpected type conversion from " << input_type
3018 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003019 }
Roland Levillaincff13742014-11-17 14:32:17 +00003020 break;
3021
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003022 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00003023 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003024 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003025 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003026 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003027 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003028 case DataType::Type::kInt16:
3029 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003030 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003031 break;
3032
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003033 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003034 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00003035
Roland Levillain232ade02015-04-20 15:14:36 +01003036 // Create stack space for the call to
3037 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
3038 // TODO: enhance register allocator to ask for stack temporaries.
3039 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003040 adjustment = DataType::Size(DataType::Type::kInt64);
Roland Levillain232ade02015-04-20 15:14:36 +01003041 __ subl(ESP, Immediate(adjustment));
3042 }
3043
3044 // Load the value to the FP stack, using temporaries if needed.
3045 PushOntoFPStack(in, 0, adjustment, false, true);
3046
3047 if (out.IsDoubleStackSlot()) {
3048 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
3049 } else {
3050 __ fstpl(Address(ESP, 0));
3051 Location stack_temp = Location::DoubleStackSlot(0);
3052 codegen_->Move64(out, stack_temp);
3053 }
3054
3055 // Remove the temporary stack space we allocated.
3056 if (adjustment != 0) {
3057 __ addl(ESP, Immediate(adjustment));
3058 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003059 break;
3060 }
3061
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003062 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003063 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003064 break;
3065
3066 default:
3067 LOG(FATAL) << "Unexpected type conversion from " << input_type
3068 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003069 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003070 break;
3071
3072 default:
3073 LOG(FATAL) << "Unexpected type conversion from " << input_type
3074 << " to " << result_type;
3075 }
3076}
3077
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003078void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003079 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003080 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003081 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003082 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05003083 locations->SetInAt(0, Location::RequiresRegister());
3084 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3085 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3086 break;
3087 }
3088
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003089 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003090 locations->SetInAt(0, Location::RequiresRegister());
3091 locations->SetInAt(1, Location::Any());
3092 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003093 break;
3094 }
3095
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003096 case DataType::Type::kFloat32:
3097 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003098 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003099 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3100 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003101 } else if (add->InputAt(1)->IsConstant()) {
3102 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003103 } else {
3104 locations->SetInAt(1, Location::Any());
3105 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003106 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003107 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003108 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003109
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003110 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003111 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Elliott Hughesc1896c92018-11-29 11:33:18 -08003112 UNREACHABLE();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003113 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003114}
3115
3116void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
3117 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003118 Location first = locations->InAt(0);
3119 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05003120 Location out = locations->Out();
3121
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003122 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003123 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003124 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003125 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3126 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003127 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3128 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05003129 } else {
3130 __ leal(out.AsRegister<Register>(), Address(
3131 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3132 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003133 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003134 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3135 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3136 __ addl(out.AsRegister<Register>(), Immediate(value));
3137 } else {
3138 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3139 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003140 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003141 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003142 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003143 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003144 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003145 }
3146
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003147 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003148 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003149 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3150 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003151 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003152 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3153 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003154 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003155 } else {
3156 DCHECK(second.IsConstant()) << second;
3157 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3158 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3159 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003160 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003161 break;
3162 }
3163
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003164 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003165 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003166 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003167 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3168 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003169 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003170 __ addss(first.AsFpuRegister<XmmRegister>(),
3171 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003172 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3173 const_area->GetBaseMethodAddress(),
3174 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003175 } else {
3176 DCHECK(second.IsStackSlot());
3177 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003178 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003179 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003180 }
3181
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003182 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003183 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003184 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003185 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3186 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003187 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003188 __ addsd(first.AsFpuRegister<XmmRegister>(),
3189 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003190 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3191 const_area->GetBaseMethodAddress(),
3192 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003193 } else {
3194 DCHECK(second.IsDoubleStackSlot());
3195 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003196 }
3197 break;
3198 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003199
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003200 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003201 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003202 }
3203}
3204
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003205void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003206 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003207 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003208 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003209 case DataType::Type::kInt32:
3210 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003211 locations->SetInAt(0, Location::RequiresRegister());
3212 locations->SetInAt(1, Location::Any());
3213 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003214 break;
3215 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003216 case DataType::Type::kFloat32:
3217 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003218 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003219 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3220 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003221 } else if (sub->InputAt(1)->IsConstant()) {
3222 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003223 } else {
3224 locations->SetInAt(1, Location::Any());
3225 }
Calin Juravle11351682014-10-23 15:38:15 +01003226 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003227 break;
Calin Juravle11351682014-10-23 15:38:15 +01003228 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003229
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003230 default:
Calin Juravle11351682014-10-23 15:38:15 +01003231 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003232 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003233}
3234
3235void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3236 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003237 Location first = locations->InAt(0);
3238 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003239 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003240 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003241 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003242 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003243 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003244 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003245 __ subl(first.AsRegister<Register>(),
3246 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003247 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003248 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003249 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003250 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003251 }
3252
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003253 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003254 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003255 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3256 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003257 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003258 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003259 __ sbbl(first.AsRegisterPairHigh<Register>(),
3260 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003261 } else {
3262 DCHECK(second.IsConstant()) << second;
3263 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3264 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3265 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003266 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003267 break;
3268 }
3269
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003270 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003271 if (second.IsFpuRegister()) {
3272 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3273 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3274 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003275 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003276 __ subss(first.AsFpuRegister<XmmRegister>(),
3277 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003278 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3279 const_area->GetBaseMethodAddress(),
3280 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003281 } else {
3282 DCHECK(second.IsStackSlot());
3283 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3284 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003285 break;
Calin Juravle11351682014-10-23 15:38:15 +01003286 }
3287
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003288 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003289 if (second.IsFpuRegister()) {
3290 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3291 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3292 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003293 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003294 __ subsd(first.AsFpuRegister<XmmRegister>(),
3295 codegen_->LiteralDoubleAddress(
3296 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003297 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003298 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3299 } else {
3300 DCHECK(second.IsDoubleStackSlot());
3301 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3302 }
Calin Juravle11351682014-10-23 15:38:15 +01003303 break;
3304 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003305
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003306 default:
Calin Juravle11351682014-10-23 15:38:15 +01003307 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003308 }
3309}
3310
Calin Juravle34bacdf2014-10-07 20:23:36 +01003311void LocationsBuilderX86::VisitMul(HMul* mul) {
3312 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003313 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003314 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003315 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003316 locations->SetInAt(0, Location::RequiresRegister());
3317 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003318 if (mul->InputAt(1)->IsIntConstant()) {
3319 // Can use 3 operand multiply.
3320 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3321 } else {
3322 locations->SetOut(Location::SameAsFirstInput());
3323 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003324 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003325 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003326 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003327 locations->SetInAt(1, Location::Any());
3328 locations->SetOut(Location::SameAsFirstInput());
3329 // Needed for imul on 32bits with 64bits output.
3330 locations->AddTemp(Location::RegisterLocation(EAX));
3331 locations->AddTemp(Location::RegisterLocation(EDX));
3332 break;
3333 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003334 case DataType::Type::kFloat32:
3335 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003336 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003337 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3338 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003339 } else if (mul->InputAt(1)->IsConstant()) {
3340 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003341 } else {
3342 locations->SetInAt(1, Location::Any());
3343 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003344 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003345 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003346 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003347
3348 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003349 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003350 }
3351}
3352
3353void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3354 LocationSummary* locations = mul->GetLocations();
3355 Location first = locations->InAt(0);
3356 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003357 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003358
3359 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003360 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003361 // The constant may have ended up in a register, so test explicitly to avoid
3362 // problems where the output may not be the same as the first operand.
3363 if (mul->InputAt(1)->IsIntConstant()) {
3364 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3365 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3366 } else if (second.IsRegister()) {
3367 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003368 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003369 } else {
3370 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003371 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003372 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003373 }
3374 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003375
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003376 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003377 Register in1_hi = first.AsRegisterPairHigh<Register>();
3378 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003379 Register eax = locations->GetTemp(0).AsRegister<Register>();
3380 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003381
3382 DCHECK_EQ(EAX, eax);
3383 DCHECK_EQ(EDX, edx);
3384
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003385 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003386 // output: in1
3387 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3388 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3389 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003390 if (second.IsConstant()) {
3391 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003392
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003393 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3394 int32_t low_value = Low32Bits(value);
3395 int32_t high_value = High32Bits(value);
3396 Immediate low(low_value);
3397 Immediate high(high_value);
3398
3399 __ movl(eax, high);
3400 // eax <- in1.lo * in2.hi
3401 __ imull(eax, in1_lo);
3402 // in1.hi <- in1.hi * in2.lo
3403 __ imull(in1_hi, low);
3404 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3405 __ addl(in1_hi, eax);
3406 // move in2_lo to eax to prepare for double precision
3407 __ movl(eax, low);
3408 // edx:eax <- in1.lo * in2.lo
3409 __ mull(in1_lo);
3410 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3411 __ addl(in1_hi, edx);
3412 // in1.lo <- (in1.lo * in2.lo)[31:0];
3413 __ movl(in1_lo, eax);
3414 } else if (second.IsRegisterPair()) {
3415 Register in2_hi = second.AsRegisterPairHigh<Register>();
3416 Register in2_lo = second.AsRegisterPairLow<Register>();
3417
3418 __ movl(eax, in2_hi);
3419 // eax <- in1.lo * in2.hi
3420 __ imull(eax, in1_lo);
3421 // in1.hi <- in1.hi * in2.lo
3422 __ imull(in1_hi, in2_lo);
3423 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3424 __ addl(in1_hi, eax);
3425 // move in1_lo to eax to prepare for double precision
3426 __ movl(eax, in1_lo);
3427 // edx:eax <- in1.lo * in2.lo
3428 __ mull(in2_lo);
3429 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3430 __ addl(in1_hi, edx);
3431 // in1.lo <- (in1.lo * in2.lo)[31:0];
3432 __ movl(in1_lo, eax);
3433 } else {
3434 DCHECK(second.IsDoubleStackSlot()) << second;
3435 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3436 Address in2_lo(ESP, second.GetStackIndex());
3437
3438 __ movl(eax, in2_hi);
3439 // eax <- in1.lo * in2.hi
3440 __ imull(eax, in1_lo);
3441 // in1.hi <- in1.hi * in2.lo
3442 __ imull(in1_hi, in2_lo);
3443 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3444 __ addl(in1_hi, eax);
3445 // move in1_lo to eax to prepare for double precision
3446 __ movl(eax, in1_lo);
3447 // edx:eax <- in1.lo * in2.lo
3448 __ mull(in2_lo);
3449 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3450 __ addl(in1_hi, edx);
3451 // in1.lo <- (in1.lo * in2.lo)[31:0];
3452 __ movl(in1_lo, eax);
3453 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003454
3455 break;
3456 }
3457
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003458 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003459 DCHECK(first.Equals(locations->Out()));
3460 if (second.IsFpuRegister()) {
3461 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3462 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3463 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003464 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003465 __ mulss(first.AsFpuRegister<XmmRegister>(),
3466 codegen_->LiteralFloatAddress(
3467 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003468 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003469 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3470 } else {
3471 DCHECK(second.IsStackSlot());
3472 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3473 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003474 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003475 }
3476
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003477 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003478 DCHECK(first.Equals(locations->Out()));
3479 if (second.IsFpuRegister()) {
3480 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3481 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3482 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003483 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003484 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3485 codegen_->LiteralDoubleAddress(
3486 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003487 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003488 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3489 } else {
3490 DCHECK(second.IsDoubleStackSlot());
3491 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3492 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003493 break;
3494 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003495
3496 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003497 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003498 }
3499}
3500
Roland Levillain232ade02015-04-20 15:14:36 +01003501void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3502 uint32_t temp_offset,
3503 uint32_t stack_adjustment,
3504 bool is_fp,
3505 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003506 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003507 DCHECK(!is_wide);
3508 if (is_fp) {
3509 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3510 } else {
3511 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3512 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003513 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003514 DCHECK(is_wide);
3515 if (is_fp) {
3516 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3517 } else {
3518 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3519 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003520 } else {
3521 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003522 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003523 Location stack_temp = Location::StackSlot(temp_offset);
3524 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003525 if (is_fp) {
3526 __ flds(Address(ESP, temp_offset));
3527 } else {
3528 __ filds(Address(ESP, temp_offset));
3529 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003530 } else {
3531 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3532 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003533 if (is_fp) {
3534 __ fldl(Address(ESP, temp_offset));
3535 } else {
3536 __ fildl(Address(ESP, temp_offset));
3537 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003538 }
3539 }
3540}
3541
3542void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003543 DataType::Type type = rem->GetResultType();
3544 bool is_float = type == DataType::Type::kFloat32;
3545 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003546 LocationSummary* locations = rem->GetLocations();
3547 Location first = locations->InAt(0);
3548 Location second = locations->InAt(1);
3549 Location out = locations->Out();
3550
3551 // Create stack space for 2 elements.
3552 // TODO: enhance register allocator to ask for stack temporaries.
3553 __ subl(ESP, Immediate(2 * elem_size));
3554
3555 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003556 const bool is_wide = !is_float;
Andreas Gampe3db70682018-12-26 15:12:03 -08003557 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp= */ true, is_wide);
3558 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp= */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003559
3560 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003561 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003562 __ Bind(&retry);
3563 __ fprem();
3564
3565 // Move FP status to AX.
3566 __ fstsw();
3567
3568 // And see if the argument reduction is complete. This is signaled by the
3569 // C2 FPU flag bit set to 0.
3570 __ andl(EAX, Immediate(kC2ConditionMask));
3571 __ j(kNotEqual, &retry);
3572
3573 // We have settled on the final value. Retrieve it into an XMM register.
3574 // Store FP top of stack to real stack.
3575 if (is_float) {
3576 __ fsts(Address(ESP, 0));
3577 } else {
3578 __ fstl(Address(ESP, 0));
3579 }
3580
3581 // Pop the 2 items from the FP stack.
3582 __ fucompp();
3583
3584 // Load the value from the stack into an XMM register.
3585 DCHECK(out.IsFpuRegister()) << out;
3586 if (is_float) {
3587 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3588 } else {
3589 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3590 }
3591
3592 // And remove the temporary stack space we allocated.
3593 __ addl(ESP, Immediate(2 * elem_size));
3594}
3595
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003596
3597void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3598 DCHECK(instruction->IsDiv() || instruction->IsRem());
3599
3600 LocationSummary* locations = instruction->GetLocations();
3601 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003602 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003603
3604 Register out_register = locations->Out().AsRegister<Register>();
3605 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003606 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003607
3608 DCHECK(imm == 1 || imm == -1);
3609
3610 if (instruction->IsRem()) {
3611 __ xorl(out_register, out_register);
3612 } else {
3613 __ movl(out_register, input_register);
3614 if (imm == -1) {
3615 __ negl(out_register);
3616 }
3617 }
3618}
3619
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303620void InstructionCodeGeneratorX86::RemByPowerOfTwo(HRem* instruction) {
3621 LocationSummary* locations = instruction->GetLocations();
3622 Location second = locations->InAt(1);
3623
3624 Register out = locations->Out().AsRegister<Register>();
3625 Register numerator = locations->InAt(0).AsRegister<Register>();
3626
3627 int32_t imm = Int64FromConstant(second.GetConstant());
3628 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3629 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3630
3631 Register tmp = locations->GetTemp(0).AsRegister<Register>();
3632 NearLabel done;
3633 __ movl(out, numerator);
3634 __ andl(out, Immediate(abs_imm-1));
3635 __ j(Condition::kZero, &done);
3636 __ leal(tmp, Address(out, static_cast<int32_t>(~(abs_imm-1))));
3637 __ testl(numerator, numerator);
3638 __ cmovl(Condition::kLess, out, tmp);
3639 __ Bind(&done);
3640}
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003641
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003642void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003643 LocationSummary* locations = instruction->GetLocations();
3644
3645 Register out_register = locations->Out().AsRegister<Register>();
3646 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003647 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003648 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3649 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003650
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003651 Register num = locations->GetTemp(0).AsRegister<Register>();
3652
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003653 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003654 __ testl(input_register, input_register);
3655 __ cmovl(kGreaterEqual, num, input_register);
3656 int shift = CTZ(imm);
3657 __ sarl(num, Immediate(shift));
3658
3659 if (imm < 0) {
3660 __ negl(num);
3661 }
3662
3663 __ movl(out_register, num);
3664}
3665
3666void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3667 DCHECK(instruction->IsDiv() || instruction->IsRem());
3668
3669 LocationSummary* locations = instruction->GetLocations();
3670 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3671
3672 Register eax = locations->InAt(0).AsRegister<Register>();
3673 Register out = locations->Out().AsRegister<Register>();
3674 Register num;
3675 Register edx;
3676
3677 if (instruction->IsDiv()) {
3678 edx = locations->GetTemp(0).AsRegister<Register>();
3679 num = locations->GetTemp(1).AsRegister<Register>();
3680 } else {
3681 edx = locations->Out().AsRegister<Register>();
3682 num = locations->GetTemp(0).AsRegister<Register>();
3683 }
3684
3685 DCHECK_EQ(EAX, eax);
3686 DCHECK_EQ(EDX, edx);
3687 if (instruction->IsDiv()) {
3688 DCHECK_EQ(EAX, out);
3689 } else {
3690 DCHECK_EQ(EDX, out);
3691 }
3692
3693 int64_t magic;
3694 int shift;
Andreas Gampe3db70682018-12-26 15:12:03 -08003695 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003696
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003697 // Save the numerator.
3698 __ movl(num, eax);
3699
3700 // EAX = magic
3701 __ movl(eax, Immediate(magic));
3702
3703 // EDX:EAX = magic * numerator
3704 __ imull(num);
3705
3706 if (imm > 0 && magic < 0) {
3707 // EDX += num
3708 __ addl(edx, num);
3709 } else if (imm < 0 && magic > 0) {
3710 __ subl(edx, num);
3711 }
3712
3713 // Shift if needed.
3714 if (shift != 0) {
3715 __ sarl(edx, Immediate(shift));
3716 }
3717
3718 // EDX += 1 if EDX < 0
3719 __ movl(eax, edx);
3720 __ shrl(edx, Immediate(31));
3721 __ addl(edx, eax);
3722
3723 if (instruction->IsRem()) {
3724 __ movl(eax, num);
3725 __ imull(edx, Immediate(imm));
3726 __ subl(eax, edx);
3727 __ movl(edx, eax);
3728 } else {
3729 __ movl(eax, edx);
3730 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003731}
3732
Calin Juravlebacfec32014-11-14 15:54:36 +00003733void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3734 DCHECK(instruction->IsDiv() || instruction->IsRem());
3735
3736 LocationSummary* locations = instruction->GetLocations();
3737 Location out = locations->Out();
3738 Location first = locations->InAt(0);
3739 Location second = locations->InAt(1);
3740 bool is_div = instruction->IsDiv();
3741
3742 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003743 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003744 DCHECK_EQ(EAX, first.AsRegister<Register>());
3745 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003746
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003747 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003748 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003749
3750 if (imm == 0) {
3751 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3752 } else if (imm == 1 || imm == -1) {
3753 DivRemOneOrMinusOne(instruction);
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303754 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
3755 if (is_div) {
3756 DivByPowerOfTwo(instruction->AsDiv());
3757 } else {
3758 RemByPowerOfTwo(instruction->AsRem());
3759 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003760 } else {
3761 DCHECK(imm <= -2 || imm >= 2);
3762 GenerateDivRemWithAnyConstant(instruction);
3763 }
3764 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003765 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00003766 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003767 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003768
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003769 Register second_reg = second.AsRegister<Register>();
3770 // 0x80000000/-1 triggers an arithmetic exception!
3771 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3772 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003773
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003774 __ cmpl(second_reg, Immediate(-1));
3775 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003776
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003777 // edx:eax <- sign-extended of eax
3778 __ cdq();
3779 // eax = quotient, edx = remainder
3780 __ idivl(second_reg);
3781 __ Bind(slow_path->GetExitLabel());
3782 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003783 break;
3784 }
3785
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003786 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003787 InvokeRuntimeCallingConvention calling_convention;
3788 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3789 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3790 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3791 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3792 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3793 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3794
3795 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003796 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003797 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003798 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003799 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003800 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003801 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003802 break;
3803 }
3804
3805 default:
3806 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3807 }
3808}
3809
Calin Juravle7c4954d2014-10-28 16:57:40 +00003810void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003811 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003812 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003813 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003814 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003815
Calin Juravle7c4954d2014-10-28 16:57:40 +00003816 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003817 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00003818 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003819 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003820 locations->SetOut(Location::SameAsFirstInput());
3821 // Intel uses edx:eax as the dividend.
3822 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003823 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3824 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3825 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003826 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003827 locations->AddTemp(Location::RequiresRegister());
3828 }
Calin Juravled0d48522014-11-04 16:40:20 +00003829 break;
3830 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003831 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003832 InvokeRuntimeCallingConvention calling_convention;
3833 locations->SetInAt(0, Location::RegisterPairLocation(
3834 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3835 locations->SetInAt(1, Location::RegisterPairLocation(
3836 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3837 // Runtime helper puts the result in EAX, EDX.
3838 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003839 break;
3840 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003841 case DataType::Type::kFloat32:
3842 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003843 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003844 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3845 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003846 } else if (div->InputAt(1)->IsConstant()) {
3847 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003848 } else {
3849 locations->SetInAt(1, Location::Any());
3850 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003851 locations->SetOut(Location::SameAsFirstInput());
3852 break;
3853 }
3854
3855 default:
3856 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3857 }
3858}
3859
3860void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3861 LocationSummary* locations = div->GetLocations();
3862 Location first = locations->InAt(0);
3863 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003864
3865 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003866 case DataType::Type::kInt32:
3867 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003868 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003869 break;
3870 }
3871
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003872 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003873 if (second.IsFpuRegister()) {
3874 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3875 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3876 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003877 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003878 __ divss(first.AsFpuRegister<XmmRegister>(),
3879 codegen_->LiteralFloatAddress(
3880 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003881 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003882 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3883 } else {
3884 DCHECK(second.IsStackSlot());
3885 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3886 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003887 break;
3888 }
3889
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003890 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003891 if (second.IsFpuRegister()) {
3892 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3893 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3894 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003895 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003896 __ divsd(first.AsFpuRegister<XmmRegister>(),
3897 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003898 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3899 const_area->GetBaseMethodAddress(),
3900 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003901 } else {
3902 DCHECK(second.IsDoubleStackSlot());
3903 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3904 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003905 break;
3906 }
3907
3908 default:
3909 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3910 }
3911}
3912
Calin Juravlebacfec32014-11-14 15:54:36 +00003913void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003914 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003915
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003916 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003917 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003918 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003919 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003920
Calin Juravled2ec87d2014-12-08 14:24:46 +00003921 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003922 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003923 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003924 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003925 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003926 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3927 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3928 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003929 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003930 locations->AddTemp(Location::RequiresRegister());
3931 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003932 break;
3933 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003934 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003935 InvokeRuntimeCallingConvention calling_convention;
3936 locations->SetInAt(0, Location::RegisterPairLocation(
3937 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3938 locations->SetInAt(1, Location::RegisterPairLocation(
3939 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3940 // Runtime helper puts the result in EAX, EDX.
3941 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3942 break;
3943 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003944 case DataType::Type::kFloat64:
3945 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003946 locations->SetInAt(0, Location::Any());
3947 locations->SetInAt(1, Location::Any());
3948 locations->SetOut(Location::RequiresFpuRegister());
3949 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003950 break;
3951 }
3952
3953 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003954 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003955 }
3956}
3957
3958void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003959 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00003960 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003961 case DataType::Type::kInt32:
3962 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003963 GenerateDivRemIntegral(rem);
3964 break;
3965 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003966 case DataType::Type::kFloat32:
3967 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003968 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003969 break;
3970 }
3971 default:
3972 LOG(FATAL) << "Unexpected rem type " << type;
3973 }
3974}
3975
Aart Bik1f8d51b2018-02-15 10:42:37 -08003976static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
3977 LocationSummary* locations = new (allocator) LocationSummary(minmax);
3978 switch (minmax->GetResultType()) {
3979 case DataType::Type::kInt32:
3980 locations->SetInAt(0, Location::RequiresRegister());
3981 locations->SetInAt(1, Location::RequiresRegister());
3982 locations->SetOut(Location::SameAsFirstInput());
3983 break;
3984 case DataType::Type::kInt64:
3985 locations->SetInAt(0, Location::RequiresRegister());
3986 locations->SetInAt(1, Location::RequiresRegister());
3987 locations->SetOut(Location::SameAsFirstInput());
3988 // Register to use to perform a long subtract to set cc.
3989 locations->AddTemp(Location::RequiresRegister());
3990 break;
3991 case DataType::Type::kFloat32:
3992 locations->SetInAt(0, Location::RequiresFpuRegister());
3993 locations->SetInAt(1, Location::RequiresFpuRegister());
3994 locations->SetOut(Location::SameAsFirstInput());
3995 locations->AddTemp(Location::RequiresRegister());
3996 break;
3997 case DataType::Type::kFloat64:
3998 locations->SetInAt(0, Location::RequiresFpuRegister());
3999 locations->SetInAt(1, Location::RequiresFpuRegister());
4000 locations->SetOut(Location::SameAsFirstInput());
4001 break;
4002 default:
4003 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
4004 }
4005}
4006
Aart Bik351df3e2018-03-07 11:54:57 -08004007void InstructionCodeGeneratorX86::GenerateMinMaxInt(LocationSummary* locations,
4008 bool is_min,
4009 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08004010 Location op1_loc = locations->InAt(0);
4011 Location op2_loc = locations->InAt(1);
4012
4013 // Shortcut for same input locations.
4014 if (op1_loc.Equals(op2_loc)) {
4015 // Can return immediately, as op1_loc == out_loc.
4016 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
4017 // a copy here.
4018 DCHECK(locations->Out().Equals(op1_loc));
4019 return;
4020 }
4021
4022 if (type == DataType::Type::kInt64) {
4023 // Need to perform a subtract to get the sign right.
4024 // op1 is already in the same location as the output.
4025 Location output = locations->Out();
4026 Register output_lo = output.AsRegisterPairLow<Register>();
4027 Register output_hi = output.AsRegisterPairHigh<Register>();
4028
4029 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
4030 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
4031
4032 // The comparison is performed by subtracting the second operand from
4033 // the first operand and then setting the status flags in the same
4034 // manner as the SUB instruction."
4035 __ cmpl(output_lo, op2_lo);
4036
4037 // Now use a temp and the borrow to finish the subtraction of op2_hi.
4038 Register temp = locations->GetTemp(0).AsRegister<Register>();
4039 __ movl(temp, output_hi);
4040 __ sbbl(temp, op2_hi);
4041
4042 // Now the condition code is correct.
4043 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
4044 __ cmovl(cond, output_lo, op2_lo);
4045 __ cmovl(cond, output_hi, op2_hi);
4046 } else {
4047 DCHECK_EQ(type, DataType::Type::kInt32);
4048 Register out = locations->Out().AsRegister<Register>();
4049 Register op2 = op2_loc.AsRegister<Register>();
4050
4051 // (out := op1)
4052 // out <=? op2
4053 // if out is min jmp done
4054 // out := op2
4055 // done:
4056
4057 __ cmpl(out, op2);
4058 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
4059 __ cmovl(cond, out, op2);
4060 }
4061}
4062
4063void InstructionCodeGeneratorX86::GenerateMinMaxFP(LocationSummary* locations,
4064 bool is_min,
4065 DataType::Type type) {
4066 Location op1_loc = locations->InAt(0);
4067 Location op2_loc = locations->InAt(1);
4068 Location out_loc = locations->Out();
4069 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
4070
4071 // Shortcut for same input locations.
4072 if (op1_loc.Equals(op2_loc)) {
4073 DCHECK(out_loc.Equals(op1_loc));
4074 return;
4075 }
4076
4077 // (out := op1)
4078 // out <=? op2
4079 // if Nan jmp Nan_label
4080 // if out is min jmp done
4081 // if op2 is min jmp op2_label
4082 // handle -0/+0
4083 // jmp done
4084 // Nan_label:
4085 // out := NaN
4086 // op2_label:
4087 // out := op2
4088 // done:
4089 //
4090 // This removes one jmp, but needs to copy one input (op1) to out.
4091 //
4092 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
4093
4094 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
4095
4096 NearLabel nan, done, op2_label;
4097 if (type == DataType::Type::kFloat64) {
4098 __ ucomisd(out, op2);
4099 } else {
4100 DCHECK_EQ(type, DataType::Type::kFloat32);
4101 __ ucomiss(out, op2);
4102 }
4103
4104 __ j(Condition::kParityEven, &nan);
4105
4106 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
4107 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
4108
4109 // Handle 0.0/-0.0.
4110 if (is_min) {
4111 if (type == DataType::Type::kFloat64) {
4112 __ orpd(out, op2);
4113 } else {
4114 __ orps(out, op2);
4115 }
4116 } else {
4117 if (type == DataType::Type::kFloat64) {
4118 __ andpd(out, op2);
4119 } else {
4120 __ andps(out, op2);
4121 }
4122 }
4123 __ jmp(&done);
4124
4125 // NaN handling.
4126 __ Bind(&nan);
4127 if (type == DataType::Type::kFloat64) {
4128 // TODO: Use a constant from the constant table (requires extra input).
4129 __ LoadLongConstant(out, kDoubleNaN);
4130 } else {
4131 Register constant = locations->GetTemp(0).AsRegister<Register>();
4132 __ movl(constant, Immediate(kFloatNaN));
4133 __ movd(out, constant);
4134 }
4135 __ jmp(&done);
4136
4137 // out := op2;
4138 __ Bind(&op2_label);
4139 if (type == DataType::Type::kFloat64) {
4140 __ movsd(out, op2);
4141 } else {
4142 __ movss(out, op2);
4143 }
4144
4145 // Done.
4146 __ Bind(&done);
4147}
4148
Aart Bik351df3e2018-03-07 11:54:57 -08004149void InstructionCodeGeneratorX86::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4150 DataType::Type type = minmax->GetResultType();
4151 switch (type) {
4152 case DataType::Type::kInt32:
4153 case DataType::Type::kInt64:
4154 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
4155 break;
4156 case DataType::Type::kFloat32:
4157 case DataType::Type::kFloat64:
4158 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
4159 break;
4160 default:
4161 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4162 }
4163}
4164
Aart Bik1f8d51b2018-02-15 10:42:37 -08004165void LocationsBuilderX86::VisitMin(HMin* min) {
4166 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4167}
4168
4169void InstructionCodeGeneratorX86::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004170 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004171}
4172
4173void LocationsBuilderX86::VisitMax(HMax* max) {
4174 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4175}
4176
4177void InstructionCodeGeneratorX86::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004178 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004179}
4180
Aart Bik3dad3412018-02-28 12:01:46 -08004181void LocationsBuilderX86::VisitAbs(HAbs* abs) {
4182 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4183 switch (abs->GetResultType()) {
4184 case DataType::Type::kInt32:
4185 locations->SetInAt(0, Location::RegisterLocation(EAX));
4186 locations->SetOut(Location::SameAsFirstInput());
4187 locations->AddTemp(Location::RegisterLocation(EDX));
4188 break;
4189 case DataType::Type::kInt64:
4190 locations->SetInAt(0, Location::RequiresRegister());
4191 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4192 locations->AddTemp(Location::RequiresRegister());
4193 break;
4194 case DataType::Type::kFloat32:
4195 locations->SetInAt(0, Location::RequiresFpuRegister());
4196 locations->SetOut(Location::SameAsFirstInput());
4197 locations->AddTemp(Location::RequiresFpuRegister());
4198 locations->AddTemp(Location::RequiresRegister());
4199 break;
4200 case DataType::Type::kFloat64:
4201 locations->SetInAt(0, Location::RequiresFpuRegister());
4202 locations->SetOut(Location::SameAsFirstInput());
4203 locations->AddTemp(Location::RequiresFpuRegister());
4204 break;
4205 default:
4206 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4207 }
4208}
4209
4210void InstructionCodeGeneratorX86::VisitAbs(HAbs* abs) {
4211 LocationSummary* locations = abs->GetLocations();
4212 switch (abs->GetResultType()) {
4213 case DataType::Type::kInt32: {
4214 Register out = locations->Out().AsRegister<Register>();
4215 DCHECK_EQ(out, EAX);
4216 Register temp = locations->GetTemp(0).AsRegister<Register>();
4217 DCHECK_EQ(temp, EDX);
4218 // Sign extend EAX into EDX.
4219 __ cdq();
4220 // XOR EAX with sign.
4221 __ xorl(EAX, EDX);
4222 // Subtract out sign to correct.
4223 __ subl(EAX, EDX);
4224 // The result is in EAX.
4225 break;
4226 }
4227 case DataType::Type::kInt64: {
4228 Location input = locations->InAt(0);
4229 Register input_lo = input.AsRegisterPairLow<Register>();
4230 Register input_hi = input.AsRegisterPairHigh<Register>();
4231 Location output = locations->Out();
4232 Register output_lo = output.AsRegisterPairLow<Register>();
4233 Register output_hi = output.AsRegisterPairHigh<Register>();
4234 Register temp = locations->GetTemp(0).AsRegister<Register>();
4235 // Compute the sign into the temporary.
4236 __ movl(temp, input_hi);
4237 __ sarl(temp, Immediate(31));
4238 // Store the sign into the output.
4239 __ movl(output_lo, temp);
4240 __ movl(output_hi, temp);
4241 // XOR the input to the output.
4242 __ xorl(output_lo, input_lo);
4243 __ xorl(output_hi, input_hi);
4244 // Subtract the sign.
4245 __ subl(output_lo, temp);
4246 __ sbbl(output_hi, temp);
4247 break;
4248 }
4249 case DataType::Type::kFloat32: {
4250 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4251 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4252 Register constant = locations->GetTemp(1).AsRegister<Register>();
4253 __ movl(constant, Immediate(INT32_C(0x7FFFFFFF)));
4254 __ movd(temp, constant);
4255 __ andps(out, temp);
4256 break;
4257 }
4258 case DataType::Type::kFloat64: {
4259 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4260 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4261 // TODO: Use a constant from the constant table (requires extra input).
4262 __ LoadLongConstant(temp, INT64_C(0x7FFFFFFFFFFFFFFF));
4263 __ andpd(out, temp);
4264 break;
4265 }
4266 default:
4267 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4268 }
4269}
4270
Calin Juravled0d48522014-11-04 16:40:20 +00004271void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004272 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004273 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004274 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004275 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004276 case DataType::Type::kInt8:
4277 case DataType::Type::kUint16:
4278 case DataType::Type::kInt16:
4279 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004280 locations->SetInAt(0, Location::Any());
4281 break;
4282 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004283 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004284 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
4285 if (!instruction->IsConstant()) {
4286 locations->AddTemp(Location::RequiresRegister());
4287 }
4288 break;
4289 }
4290 default:
4291 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4292 }
Calin Juravled0d48522014-11-04 16:40:20 +00004293}
4294
4295void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004296 SlowPathCode* slow_path =
4297 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004298 codegen_->AddSlowPath(slow_path);
4299
4300 LocationSummary* locations = instruction->GetLocations();
4301 Location value = locations->InAt(0);
4302
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004303 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004304 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004305 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004306 case DataType::Type::kInt8:
4307 case DataType::Type::kUint16:
4308 case DataType::Type::kInt16:
4309 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004310 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004311 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004312 __ j(kEqual, slow_path->GetEntryLabel());
4313 } else if (value.IsStackSlot()) {
4314 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
4315 __ j(kEqual, slow_path->GetEntryLabel());
4316 } else {
4317 DCHECK(value.IsConstant()) << value;
4318 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004319 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004320 }
4321 }
4322 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004323 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004324 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004325 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004326 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004327 __ movl(temp, value.AsRegisterPairLow<Register>());
4328 __ orl(temp, value.AsRegisterPairHigh<Register>());
4329 __ j(kEqual, slow_path->GetEntryLabel());
4330 } else {
4331 DCHECK(value.IsConstant()) << value;
4332 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4333 __ jmp(slow_path->GetEntryLabel());
4334 }
4335 }
4336 break;
4337 }
4338 default:
4339 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004340 }
Calin Juravled0d48522014-11-04 16:40:20 +00004341}
4342
Calin Juravle9aec02f2014-11-18 23:06:35 +00004343void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
4344 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4345
4346 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004347 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004348
4349 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004350 case DataType::Type::kInt32:
4351 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004352 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00004353 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00004354 // The shift count needs to be in CL or a constant.
4355 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004356 locations->SetOut(Location::SameAsFirstInput());
4357 break;
4358 }
4359 default:
4360 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4361 }
4362}
4363
4364void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
4365 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4366
4367 LocationSummary* locations = op->GetLocations();
4368 Location first = locations->InAt(0);
4369 Location second = locations->InAt(1);
4370 DCHECK(first.Equals(locations->Out()));
4371
4372 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004373 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00004374 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004375 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004376 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004377 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004378 DCHECK_EQ(ECX, second_reg);
4379 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004380 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004381 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004382 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004383 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004384 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004385 }
4386 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004387 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004388 if (shift == 0) {
4389 return;
4390 }
4391 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004392 if (op->IsShl()) {
4393 __ shll(first_reg, imm);
4394 } else if (op->IsShr()) {
4395 __ sarl(first_reg, imm);
4396 } else {
4397 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004398 }
4399 }
4400 break;
4401 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004402 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004403 if (second.IsRegister()) {
4404 Register second_reg = second.AsRegister<Register>();
4405 DCHECK_EQ(ECX, second_reg);
4406 if (op->IsShl()) {
4407 GenerateShlLong(first, second_reg);
4408 } else if (op->IsShr()) {
4409 GenerateShrLong(first, second_reg);
4410 } else {
4411 GenerateUShrLong(first, second_reg);
4412 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004413 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00004414 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004415 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004416 // Nothing to do if the shift is 0, as the input is already the output.
4417 if (shift != 0) {
4418 if (op->IsShl()) {
4419 GenerateShlLong(first, shift);
4420 } else if (op->IsShr()) {
4421 GenerateShrLong(first, shift);
4422 } else {
4423 GenerateUShrLong(first, shift);
4424 }
4425 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004426 }
4427 break;
4428 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004429 default:
4430 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4431 }
4432}
4433
Mark P Mendell73945692015-04-29 14:56:17 +00004434void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
4435 Register low = loc.AsRegisterPairLow<Register>();
4436 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04004437 if (shift == 1) {
4438 // This is just an addition.
4439 __ addl(low, low);
4440 __ adcl(high, high);
4441 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00004442 // Shift by 32 is easy. High gets low, and low gets 0.
4443 codegen_->EmitParallelMoves(
4444 loc.ToLow(),
4445 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004446 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004447 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4448 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004449 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004450 } else if (shift > 32) {
4451 // Low part becomes 0. High part is low part << (shift-32).
4452 __ movl(high, low);
4453 __ shll(high, Immediate(shift - 32));
4454 __ xorl(low, low);
4455 } else {
4456 // Between 1 and 31.
4457 __ shld(high, low, Immediate(shift));
4458 __ shll(low, Immediate(shift));
4459 }
4460}
4461
Calin Juravle9aec02f2014-11-18 23:06:35 +00004462void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004463 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004464 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4465 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4466 __ testl(shifter, Immediate(32));
4467 __ j(kEqual, &done);
4468 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4469 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4470 __ Bind(&done);
4471}
4472
Mark P Mendell73945692015-04-29 14:56:17 +00004473void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4474 Register low = loc.AsRegisterPairLow<Register>();
4475 Register high = loc.AsRegisterPairHigh<Register>();
4476 if (shift == 32) {
4477 // Need to copy the sign.
4478 DCHECK_NE(low, high);
4479 __ movl(low, high);
4480 __ sarl(high, Immediate(31));
4481 } else if (shift > 32) {
4482 DCHECK_NE(low, high);
4483 // High part becomes sign. Low part is shifted by shift - 32.
4484 __ movl(low, high);
4485 __ sarl(high, Immediate(31));
4486 __ sarl(low, Immediate(shift - 32));
4487 } else {
4488 // Between 1 and 31.
4489 __ shrd(low, high, Immediate(shift));
4490 __ sarl(high, Immediate(shift));
4491 }
4492}
4493
Calin Juravle9aec02f2014-11-18 23:06:35 +00004494void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004495 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004496 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4497 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4498 __ testl(shifter, Immediate(32));
4499 __ j(kEqual, &done);
4500 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4501 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4502 __ Bind(&done);
4503}
4504
Mark P Mendell73945692015-04-29 14:56:17 +00004505void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4506 Register low = loc.AsRegisterPairLow<Register>();
4507 Register high = loc.AsRegisterPairHigh<Register>();
4508 if (shift == 32) {
4509 // Shift by 32 is easy. Low gets high, and high gets 0.
4510 codegen_->EmitParallelMoves(
4511 loc.ToHigh(),
4512 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004513 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004514 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4515 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004516 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004517 } else if (shift > 32) {
4518 // Low part is high >> (shift - 32). High part becomes 0.
4519 __ movl(low, high);
4520 __ shrl(low, Immediate(shift - 32));
4521 __ xorl(high, high);
4522 } else {
4523 // Between 1 and 31.
4524 __ shrd(low, high, Immediate(shift));
4525 __ shrl(high, Immediate(shift));
4526 }
4527}
4528
Calin Juravle9aec02f2014-11-18 23:06:35 +00004529void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004530 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004531 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4532 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4533 __ testl(shifter, Immediate(32));
4534 __ j(kEqual, &done);
4535 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4536 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4537 __ Bind(&done);
4538}
4539
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004540void LocationsBuilderX86::VisitRor(HRor* ror) {
4541 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004542 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004543
4544 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004545 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004546 // Add the temporary needed.
4547 locations->AddTemp(Location::RequiresRegister());
4548 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004549 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004550 locations->SetInAt(0, Location::RequiresRegister());
4551 // The shift count needs to be in CL (unless it is a constant).
4552 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4553 locations->SetOut(Location::SameAsFirstInput());
4554 break;
4555 default:
4556 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4557 UNREACHABLE();
4558 }
4559}
4560
4561void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4562 LocationSummary* locations = ror->GetLocations();
4563 Location first = locations->InAt(0);
4564 Location second = locations->InAt(1);
4565
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004566 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004567 Register first_reg = first.AsRegister<Register>();
4568 if (second.IsRegister()) {
4569 Register second_reg = second.AsRegister<Register>();
4570 __ rorl(first_reg, second_reg);
4571 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004572 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004573 __ rorl(first_reg, imm);
4574 }
4575 return;
4576 }
4577
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004578 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004579 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4580 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4581 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4582 if (second.IsRegister()) {
4583 Register second_reg = second.AsRegister<Register>();
4584 DCHECK_EQ(second_reg, ECX);
4585 __ movl(temp_reg, first_reg_hi);
4586 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4587 __ shrd(first_reg_lo, temp_reg, second_reg);
4588 __ movl(temp_reg, first_reg_hi);
4589 __ testl(second_reg, Immediate(32));
4590 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4591 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4592 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004593 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004594 if (shift_amt == 0) {
4595 // Already fine.
4596 return;
4597 }
4598 if (shift_amt == 32) {
4599 // Just swap.
4600 __ movl(temp_reg, first_reg_lo);
4601 __ movl(first_reg_lo, first_reg_hi);
4602 __ movl(first_reg_hi, temp_reg);
4603 return;
4604 }
4605
4606 Immediate imm(shift_amt);
4607 // Save the constents of the low value.
4608 __ movl(temp_reg, first_reg_lo);
4609
4610 // Shift right into low, feeding bits from high.
4611 __ shrd(first_reg_lo, first_reg_hi, imm);
4612
4613 // Shift right into high, feeding bits from the original low.
4614 __ shrd(first_reg_hi, temp_reg, imm);
4615
4616 // Swap if needed.
4617 if (shift_amt > 32) {
4618 __ movl(temp_reg, first_reg_lo);
4619 __ movl(first_reg_lo, first_reg_hi);
4620 __ movl(first_reg_hi, temp_reg);
4621 }
4622 }
4623}
4624
Calin Juravle9aec02f2014-11-18 23:06:35 +00004625void LocationsBuilderX86::VisitShl(HShl* shl) {
4626 HandleShift(shl);
4627}
4628
4629void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4630 HandleShift(shl);
4631}
4632
4633void LocationsBuilderX86::VisitShr(HShr* shr) {
4634 HandleShift(shr);
4635}
4636
4637void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4638 HandleShift(shr);
4639}
4640
4641void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4642 HandleShift(ushr);
4643}
4644
4645void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4646 HandleShift(ushr);
4647}
4648
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004649void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004650 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4651 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004652 locations->SetOut(Location::RegisterLocation(EAX));
Alex Lightd109e302018-06-27 10:25:41 -07004653 InvokeRuntimeCallingConvention calling_convention;
4654 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004655}
4656
4657void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07004658 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
4659 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4660 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004661}
4662
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004663void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004664 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4665 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004666 locations->SetOut(Location::RegisterLocation(EAX));
4667 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004668 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4669 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004670}
4671
4672void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01004673 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
4674 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004675 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004676 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004677 DCHECK(!codegen_->IsLeafMethod());
4678}
4679
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004680void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004681 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004682 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004683 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4684 if (location.IsStackSlot()) {
4685 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4686 } else if (location.IsDoubleStackSlot()) {
4687 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004688 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004689 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004690}
4691
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004692void InstructionCodeGeneratorX86::VisitParameterValue(
4693 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4694}
4695
4696void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4697 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004698 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004699 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4700}
4701
4702void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004703}
4704
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004705void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4706 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004707 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004708 locations->SetInAt(0, Location::RequiresRegister());
4709 locations->SetOut(Location::RequiresRegister());
4710}
4711
4712void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4713 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004714 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004715 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004716 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004717 __ movl(locations->Out().AsRegister<Register>(),
4718 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004719 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004720 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004721 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004722 __ movl(locations->Out().AsRegister<Register>(),
4723 Address(locations->InAt(0).AsRegister<Register>(),
4724 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4725 // temp = temp->GetImtEntryAt(method_offset);
4726 __ movl(locations->Out().AsRegister<Register>(),
4727 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004728 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004729}
4730
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004731void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004732 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004733 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004734 locations->SetInAt(0, Location::RequiresRegister());
4735 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004736}
4737
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004738void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4739 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004740 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004741 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004742 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004743 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004744 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004745 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004746 break;
4747
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004748 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004749 __ notl(out.AsRegisterPairLow<Register>());
4750 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004751 break;
4752
4753 default:
4754 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4755 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004756}
4757
David Brazdil66d126e2015-04-03 16:02:44 +01004758void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4759 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004760 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004761 locations->SetInAt(0, Location::RequiresRegister());
4762 locations->SetOut(Location::SameAsFirstInput());
4763}
4764
4765void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004766 LocationSummary* locations = bool_not->GetLocations();
4767 Location in = locations->InAt(0);
4768 Location out = locations->Out();
4769 DCHECK(in.Equals(out));
4770 __ xorl(out.AsRegister<Register>(), Immediate(1));
4771}
4772
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004773void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004774 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004775 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004776 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004777 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004778 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004779 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004780 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004781 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004782 case DataType::Type::kInt32:
4783 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004784 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004785 locations->SetInAt(1, Location::Any());
4786 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4787 break;
4788 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004789 case DataType::Type::kFloat32:
4790 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004791 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004792 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4793 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4794 } else if (compare->InputAt(1)->IsConstant()) {
4795 locations->SetInAt(1, Location::RequiresFpuRegister());
4796 } else {
4797 locations->SetInAt(1, Location::Any());
4798 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004799 locations->SetOut(Location::RequiresRegister());
4800 break;
4801 }
4802 default:
4803 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4804 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004805}
4806
4807void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004808 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004809 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004810 Location left = locations->InAt(0);
4811 Location right = locations->InAt(1);
4812
Mark Mendell0c9497d2015-08-21 09:30:05 -04004813 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004814 Condition less_cond = kLess;
4815
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004816 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004817 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004818 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004819 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004820 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004821 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004822 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004823 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004824 break;
4825 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004826 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004827 Register left_low = left.AsRegisterPairLow<Register>();
4828 Register left_high = left.AsRegisterPairHigh<Register>();
4829 int32_t val_low = 0;
4830 int32_t val_high = 0;
4831 bool right_is_const = false;
4832
4833 if (right.IsConstant()) {
4834 DCHECK(right.GetConstant()->IsLongConstant());
4835 right_is_const = true;
4836 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4837 val_low = Low32Bits(val);
4838 val_high = High32Bits(val);
4839 }
4840
Calin Juravleddb7df22014-11-25 20:56:51 +00004841 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004842 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004843 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004844 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004845 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004846 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004847 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004848 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004849 __ j(kLess, &less); // Signed compare.
4850 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004851 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004852 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004853 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004854 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004855 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004856 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004857 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004858 }
Aart Bika19616e2016-02-01 18:57:58 -08004859 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004860 break;
4861 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004862 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004863 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004864 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004865 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004866 break;
4867 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004868 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004869 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004870 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004871 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004872 break;
4873 }
4874 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004875 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004876 }
Aart Bika19616e2016-02-01 18:57:58 -08004877
Calin Juravleddb7df22014-11-25 20:56:51 +00004878 __ movl(out, Immediate(0));
4879 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004880 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004881
4882 __ Bind(&greater);
4883 __ movl(out, Immediate(1));
4884 __ jmp(&done);
4885
4886 __ Bind(&less);
4887 __ movl(out, Immediate(-1));
4888
4889 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004890}
4891
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004892void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004893 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004894 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004895 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004896 locations->SetInAt(i, Location::Any());
4897 }
4898 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004899}
4900
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004901void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004902 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004903}
4904
Roland Levillain7c1559a2015-12-15 10:55:36 +00004905void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004906 /*
4907 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4908 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4909 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4910 */
4911 switch (kind) {
4912 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004913 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004914 break;
4915 }
4916 case MemBarrierKind::kAnyStore:
4917 case MemBarrierKind::kLoadAny:
4918 case MemBarrierKind::kStoreStore: {
4919 // nop
4920 break;
4921 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004922 case MemBarrierKind::kNTStoreStore:
4923 // Non-Temporal Store/Store needs an explicit fence.
Andreas Gampe3db70682018-12-26 15:12:03 -08004924 MemoryFence(/* non-temporal= */ true);
Mark Mendell7aa04a12016-01-27 22:39:07 -05004925 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004926 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004927}
4928
Vladimir Markodc151b22015-10-15 18:02:30 +01004929HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4930 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01004931 ArtMethod* method ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004932 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004933}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004934
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004935Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4936 Register temp) {
4937 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004938 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004939 if (!invoke->GetLocations()->Intrinsified()) {
4940 return location.AsRegister<Register>();
4941 }
4942 // For intrinsics we allow any location, so it may be on the stack.
4943 if (!location.IsRegister()) {
4944 __ movl(temp, Address(ESP, location.GetStackIndex()));
4945 return temp;
4946 }
4947 // For register locations, check if the register was saved. If so, get it from the stack.
4948 // Note: There is a chance that the register was saved but not overwritten, so we could
4949 // save one load. However, since this is just an intrinsic slow path we prefer this
4950 // simple and more robust approach rather that trying to determine if that's the case.
4951 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004952 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4953 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4954 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4955 __ movl(temp, Address(ESP, stack_offset));
4956 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004957 }
4958 return location.AsRegister<Register>();
4959}
4960
Vladimir Markoe7197bf2017-06-02 17:00:23 +01004961void CodeGeneratorX86::GenerateStaticOrDirectCall(
4962 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00004963 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4964 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004965 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004966 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004967 uint32_t offset =
4968 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4969 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004970 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004971 }
Vladimir Marko58155012015-08-19 12:49:41 +00004972 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004973 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004974 break;
Vladimir Marko65979462017-05-19 17:25:12 +01004975 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01004976 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko65979462017-05-19 17:25:12 +01004977 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4978 temp.AsRegister<Register>());
4979 __ leal(temp.AsRegister<Register>(), Address(base_reg, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004980 RecordBootImageMethodPatch(invoke);
Vladimir Marko65979462017-05-19 17:25:12 +01004981 break;
4982 }
Vladimir Markob066d432018-01-03 13:14:37 +00004983 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
4984 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4985 temp.AsRegister<Register>());
4986 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4987 RecordBootImageRelRoPatch(
4988 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Markoe47f60c2018-02-21 13:43:28 +00004989 GetBootImageOffset(invoke));
Vladimir Markob066d432018-01-03 13:14:37 +00004990 break;
4991 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01004992 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004993 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4994 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004995 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00004996 RecordMethodBssEntryPatch(invoke);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01004997 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004998 break;
4999 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005000 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
5001 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
5002 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005003 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
5004 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5005 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01005006 }
Vladimir Marko58155012015-08-19 12:49:41 +00005007 }
5008
5009 switch (invoke->GetCodePtrLocation()) {
5010 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
5011 __ call(GetFrameEntryLabel());
5012 break;
Vladimir Marko58155012015-08-19 12:49:41 +00005013 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5014 // (callee_method + offset_of_quick_compiled_code)()
5015 __ call(Address(callee_method.AsRegister<Register>(),
5016 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005017 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00005018 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04005019 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005020 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Mark Mendell09ed1a32015-03-25 08:30:06 -04005021
5022 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04005023}
5024
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005025void CodeGeneratorX86::GenerateVirtualCall(
5026 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005027 Register temp = temp_in.AsRegister<Register>();
5028 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5029 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005030
5031 // Use the calling convention instead of the location of the receiver, as
5032 // intrinsics may have put the receiver in a different register. In the intrinsics
5033 // slow path, the arguments have been moved to the right place, so here we are
5034 // guaranteed that the receiver is the first register of the calling convention.
5035 InvokeDexCallingConvention calling_convention;
5036 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005037 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005038 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005039 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005040 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005041 // Instead of simply (possibly) unpoisoning `temp` here, we should
5042 // emit a read barrier for the previous class reference load.
5043 // However this is not required in practice, as this is an
5044 // intermediate/temporary reference and because the current
5045 // concurrent copying collector keeps the from-space memory
5046 // intact/accessible until the end of the marking phase (the
5047 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005048 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00005049
5050 MaybeGenerateInlineCacheCheck(invoke, temp);
5051
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005052 // temp = temp->GetMethodAt(method_offset);
5053 __ movl(temp, Address(temp, method_offset));
5054 // call temp->GetEntryPoint();
5055 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07005056 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005057 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005058}
5059
Vladimir Marko6fd16062018-06-26 11:02:04 +01005060void CodeGeneratorX86::RecordBootImageIntrinsicPatch(HX86ComputeBaseMethodAddress* method_address,
5061 uint32_t intrinsic_data) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005062 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005063 method_address, /* target_dex_file= */ nullptr, intrinsic_data);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005064 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005065}
5066
Vladimir Markob066d432018-01-03 13:14:37 +00005067void CodeGeneratorX86::RecordBootImageRelRoPatch(HX86ComputeBaseMethodAddress* method_address,
5068 uint32_t boot_image_offset) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005069 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005070 method_address, /* target_dex_file= */ nullptr, boot_image_offset);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005071 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Markob066d432018-01-03 13:14:37 +00005072}
5073
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005074void CodeGeneratorX86::RecordBootImageMethodPatch(HInvokeStaticOrDirect* invoke) {
Vladimir Marko65979462017-05-19 17:25:12 +01005075 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005076 HX86ComputeBaseMethodAddress* method_address =
Vladimir Marko65979462017-05-19 17:25:12 +01005077 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005078 boot_image_method_patches_.emplace_back(
5079 method_address, invoke->GetTargetMethod().dex_file, invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01005080 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005081}
5082
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005083void CodeGeneratorX86::RecordMethodBssEntryPatch(HInvokeStaticOrDirect* invoke) {
5084 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5085 HX86ComputeBaseMethodAddress* method_address =
5086 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005087 // Add the patch entry and bind its label at the end of the instruction.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005088 method_bss_entry_patches_.emplace_back(
5089 method_address, &GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
5090 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005091}
5092
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005093void CodeGeneratorX86::RecordBootImageTypePatch(HLoadClass* load_class) {
5094 HX86ComputeBaseMethodAddress* method_address =
5095 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5096 boot_image_type_patches_.emplace_back(
5097 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005098 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005099}
5100
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005101Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005102 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005103 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5104 type_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005105 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005106 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005107}
5108
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005109void CodeGeneratorX86::RecordBootImageStringPatch(HLoadString* load_string) {
5110 HX86ComputeBaseMethodAddress* method_address =
5111 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
5112 boot_image_string_patches_.emplace_back(
5113 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
5114 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01005115}
5116
Vladimir Markoaad75c62016-10-03 08:46:48 +00005117Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005118 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005119 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005120 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005121 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005122 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005123}
5124
Vladimir Markoeebb8212018-06-05 14:57:24 +01005125void CodeGeneratorX86::LoadBootImageAddress(Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01005126 uint32_t boot_image_reference,
Vladimir Markoeebb8212018-06-05 14:57:24 +01005127 HInvokeStaticOrDirect* invoke) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005128 if (GetCompilerOptions().IsBootImage()) {
5129 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5130 HX86ComputeBaseMethodAddress* method_address =
5131 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5132 DCHECK(method_address != nullptr);
5133 Register method_address_reg =
5134 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
5135 __ leal(reg, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
5136 RecordBootImageIntrinsicPatch(method_address, boot_image_reference);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01005137 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01005138 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5139 HX86ComputeBaseMethodAddress* method_address =
5140 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5141 DCHECK(method_address != nullptr);
5142 Register method_address_reg =
5143 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
5144 __ movl(reg, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005145 RecordBootImageRelRoPatch(method_address, boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01005146 } else {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005147 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markoeebb8212018-06-05 14:57:24 +01005148 gc::Heap* heap = Runtime::Current()->GetHeap();
5149 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01005150 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01005151 __ movl(reg, Immediate(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address))));
5152 }
5153}
5154
Vladimir Marko6fd16062018-06-26 11:02:04 +01005155void CodeGeneratorX86::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
5156 uint32_t boot_image_offset) {
5157 DCHECK(invoke->IsStatic());
5158 InvokeRuntimeCallingConvention calling_convention;
5159 Register argument = calling_convention.GetRegisterAt(0);
5160 if (GetCompilerOptions().IsBootImage()) {
5161 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
5162 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
5163 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
5164 HX86ComputeBaseMethodAddress* method_address =
5165 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5166 DCHECK(method_address != nullptr);
5167 Register method_address_reg =
5168 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
5169 __ leal(argument, Address(method_address_reg, CodeGeneratorX86::kDummy32BitOffset));
5170 MethodReference target_method = invoke->GetTargetMethod();
5171 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
5172 boot_image_type_patches_.emplace_back(method_address, target_method.dex_file, type_idx.index_);
5173 __ Bind(&boot_image_type_patches_.back().label);
5174 } else {
5175 LoadBootImageAddress(argument, boot_image_offset, invoke);
5176 }
5177 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
5178 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
5179}
5180
Vladimir Markoaad75c62016-10-03 08:46:48 +00005181// The label points to the end of the "movl" or another instruction but the literal offset
5182// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
5183constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
5184
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005185template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005186inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005187 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005188 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005189 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005190 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005191 linker_patches->push_back(Factory(literal_offset,
5192 info.target_dex_file,
5193 GetMethodAddressOffset(info.method_address),
5194 info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005195 }
5196}
5197
Vladimir Marko6fd16062018-06-26 11:02:04 +01005198template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
5199linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
5200 const DexFile* target_dex_file,
5201 uint32_t pc_insn_offset,
5202 uint32_t boot_image_offset) {
5203 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5204 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005205}
5206
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005207void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005208 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005209 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01005210 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005211 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00005212 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01005213 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005214 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01005215 string_bss_entry_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01005216 boot_image_other_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005217 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01005218 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005219 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
5220 boot_image_method_patches_, linker_patches);
5221 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
5222 boot_image_type_patches_, linker_patches);
5223 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005224 boot_image_string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005225 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005226 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005227 DCHECK(boot_image_type_patches_.empty());
5228 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01005229 }
5230 if (GetCompilerOptions().IsBootImage()) {
5231 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5232 boot_image_other_patches_, linker_patches);
5233 } else {
5234 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
5235 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005236 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005237 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5238 method_bss_entry_patches_, linker_patches);
5239 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5240 type_bss_entry_patches_, linker_patches);
5241 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5242 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005243 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005244}
5245
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005246void CodeGeneratorX86::MarkGCCard(Register temp,
5247 Register card,
5248 Register object,
5249 Register value,
5250 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005251 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005252 if (value_can_be_null) {
5253 __ testl(value, value);
5254 __ j(kEqual, &is_null);
5255 }
Roland Levillainc73f0522018-08-14 15:16:50 +01005256 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07005257 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01005258 // Calculate the offset (in the card table) of the card corresponding to
5259 // `object`.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005260 __ movl(temp, object);
5261 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01005262 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
5263 // `object`'s card.
5264 //
5265 // Register `card` contains the address of the card table. Note that the card
5266 // table's base is biased during its creation so that it always starts at an
5267 // address whose least-significant byte is equal to `kCardDirty` (see
5268 // art::gc::accounting::CardTable::Create). Therefore the MOVB instruction
5269 // below writes the `kCardDirty` (byte) value into the `object`'s card
5270 // (located at `card + object >> kCardShift`).
5271 //
5272 // This dual use of the value in register `card` (1. to calculate the location
5273 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
5274 // (no need to explicitly load `kCardDirty` as an immediate value).
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00005275 __ movb(Address(temp, card, TIMES_1, 0),
5276 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005277 if (value_can_be_null) {
5278 __ Bind(&is_null);
5279 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005280}
5281
Calin Juravle52c48962014-12-16 17:02:57 +00005282void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5283 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005284
5285 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005286 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005287 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005288 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5289 kEmitCompilerReadBarrier
5290 ? LocationSummary::kCallOnSlowPath
5291 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005292 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005293 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005294 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005295 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005296
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005297 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005298 locations->SetOut(Location::RequiresFpuRegister());
5299 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005300 // The output overlaps in case of long: we don't want the low move
5301 // to overwrite the object's location. Likewise, in the case of
5302 // an object field get with read barriers enabled, we do not want
5303 // the move to overwrite the object's location, as we need it to emit
5304 // the read barrier.
5305 locations->SetOut(
5306 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005307 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005308 Location::kOutputOverlap :
5309 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005310 }
Calin Juravle52c48962014-12-16 17:02:57 +00005311
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005312 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00005313 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005314 // So we use an XMM register as a temp to achieve atomicity (first
5315 // load the temp into the XMM and then copy the XMM into the
5316 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00005317 locations->AddTemp(Location::RequiresFpuRegister());
5318 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005319}
5320
Calin Juravle52c48962014-12-16 17:02:57 +00005321void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
5322 const FieldInfo& field_info) {
5323 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005324
Calin Juravle52c48962014-12-16 17:02:57 +00005325 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005326 Location base_loc = locations->InAt(0);
5327 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005328 Location out = locations->Out();
5329 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01005330 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5331 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00005332 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5333
Vladimir Marko61b92282017-10-11 13:23:17 +01005334 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005335 case DataType::Type::kBool:
5336 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005337 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005338 break;
5339 }
5340
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005341 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005342 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005343 break;
5344 }
5345
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005346 case DataType::Type::kUint16: {
5347 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005348 break;
5349 }
5350
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005351 case DataType::Type::kInt16: {
5352 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005353 break;
5354 }
5355
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005356 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00005357 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005358 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005359
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005360 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005361 // /* HeapReference<Object> */ out = *(base + offset)
5362 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005363 // Note that a potential implicit null check is handled in this
5364 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
5365 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005366 instruction, out, base, offset, /* needs_null_check= */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005367 if (is_volatile) {
5368 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5369 }
5370 } else {
5371 __ movl(out.AsRegister<Register>(), Address(base, offset));
5372 codegen_->MaybeRecordImplicitNullCheck(instruction);
5373 if (is_volatile) {
5374 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5375 }
5376 // If read barriers are enabled, emit read barriers other than
5377 // Baker's using a slow path (and also unpoison the loaded
5378 // reference, if heap poisoning is enabled).
5379 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5380 }
5381 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005382 }
5383
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005384 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005385 if (is_volatile) {
5386 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5387 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005388 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005389 __ movd(out.AsRegisterPairLow<Register>(), temp);
5390 __ psrlq(temp, Immediate(32));
5391 __ movd(out.AsRegisterPairHigh<Register>(), temp);
5392 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005393 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00005394 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005395 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005396 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
5397 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005398 break;
5399 }
5400
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005401 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00005402 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005403 break;
5404 }
5405
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005406 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005407 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005408 break;
5409 }
5410
Aart Bik66c158e2018-01-31 12:55:04 -08005411 case DataType::Type::kUint32:
5412 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005413 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005414 LOG(FATAL) << "Unreachable type " << load_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005415 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005416 }
Calin Juravle52c48962014-12-16 17:02:57 +00005417
Vladimir Marko61b92282017-10-11 13:23:17 +01005418 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005419 // Potential implicit null checks, in the case of reference or
5420 // long fields, are handled in the previous switch statement.
5421 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005422 codegen_->MaybeRecordImplicitNullCheck(instruction);
5423 }
5424
Calin Juravle52c48962014-12-16 17:02:57 +00005425 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005426 if (load_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005427 // Memory barriers, in the case of references, are also handled
5428 // in the previous switch statement.
5429 } else {
5430 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5431 }
Roland Levillain4d027112015-07-01 15:41:14 +01005432 }
Calin Juravle52c48962014-12-16 17:02:57 +00005433}
5434
5435void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5436 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5437
5438 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005439 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00005440 locations->SetInAt(0, Location::RequiresRegister());
5441 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005442 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005443 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00005444
5445 // The register allocator does not support multiple
5446 // inputs that die at entry with one in a specific register.
5447 if (is_byte_type) {
5448 // Ensure the value is in a byte register.
5449 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005450 } else if (DataType::IsFloatingPointType(field_type)) {
5451 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05005452 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5453 locations->SetInAt(1, Location::RequiresFpuRegister());
5454 } else {
5455 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
5456 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005457 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05005458 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00005459 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005460
Calin Juravle52c48962014-12-16 17:02:57 +00005461 // 64bits value can be atomically written to an address with movsd and an XMM register.
5462 // We need two XMM registers because there's no easier way to (bit) copy a register pair
5463 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
5464 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
5465 // isolated cases when we need this it isn't worth adding the extra complexity.
5466 locations->AddTemp(Location::RequiresFpuRegister());
5467 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005468 } else {
5469 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5470
5471 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5472 // Temporary registers for the write barrier.
5473 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
5474 // Ensure the card is in a byte register.
5475 locations->AddTemp(Location::RegisterLocation(ECX));
5476 }
Calin Juravle52c48962014-12-16 17:02:57 +00005477 }
5478}
5479
5480void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005481 const FieldInfo& field_info,
5482 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005483 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5484
5485 LocationSummary* locations = instruction->GetLocations();
5486 Register base = locations->InAt(0).AsRegister<Register>();
5487 Location value = locations->InAt(1);
5488 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005489 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00005490 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005491 bool needs_write_barrier =
5492 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005493
5494 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005495 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005496 }
5497
Mark Mendell81489372015-11-04 11:30:41 -05005498 bool maybe_record_implicit_null_check_done = false;
5499
Calin Juravle52c48962014-12-16 17:02:57 +00005500 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005501 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005502 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005503 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005504 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
5505 break;
5506 }
5507
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005508 case DataType::Type::kUint16:
5509 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05005510 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005511 __ movw(Address(base, offset),
5512 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05005513 } else {
5514 __ movw(Address(base, offset), value.AsRegister<Register>());
5515 }
Calin Juravle52c48962014-12-16 17:02:57 +00005516 break;
5517 }
5518
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005519 case DataType::Type::kInt32:
5520 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01005521 if (kPoisonHeapReferences && needs_write_barrier) {
5522 // Note that in the case where `value` is a null reference,
5523 // we do not enter this block, as the reference does not
5524 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005525 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01005526 Register temp = locations->GetTemp(0).AsRegister<Register>();
5527 __ movl(temp, value.AsRegister<Register>());
5528 __ PoisonHeapReference(temp);
5529 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05005530 } else if (value.IsConstant()) {
5531 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5532 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005533 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005534 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01005535 __ movl(Address(base, offset), value.AsRegister<Register>());
5536 }
Calin Juravle52c48962014-12-16 17:02:57 +00005537 break;
5538 }
5539
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005540 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005541 if (is_volatile) {
5542 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5543 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5544 __ movd(temp1, value.AsRegisterPairLow<Register>());
5545 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5546 __ punpckldq(temp1, temp2);
5547 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005548 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005549 } else if (value.IsConstant()) {
5550 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5551 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5552 codegen_->MaybeRecordImplicitNullCheck(instruction);
5553 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005554 } else {
5555 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005556 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005557 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5558 }
Mark Mendell81489372015-11-04 11:30:41 -05005559 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005560 break;
5561 }
5562
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005563 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05005564 if (value.IsConstant()) {
5565 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5566 __ movl(Address(base, offset), Immediate(v));
5567 } else {
5568 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5569 }
Calin Juravle52c48962014-12-16 17:02:57 +00005570 break;
5571 }
5572
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005573 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005574 if (value.IsConstant()) {
5575 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5576 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5577 codegen_->MaybeRecordImplicitNullCheck(instruction);
5578 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5579 maybe_record_implicit_null_check_done = true;
5580 } else {
5581 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5582 }
Calin Juravle52c48962014-12-16 17:02:57 +00005583 break;
5584 }
5585
Aart Bik66c158e2018-01-31 12:55:04 -08005586 case DataType::Type::kUint32:
5587 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005588 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005589 LOG(FATAL) << "Unreachable type " << field_type;
5590 UNREACHABLE();
5591 }
5592
Mark Mendell81489372015-11-04 11:30:41 -05005593 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005594 codegen_->MaybeRecordImplicitNullCheck(instruction);
5595 }
5596
Roland Levillain4d027112015-07-01 15:41:14 +01005597 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005598 Register temp = locations->GetTemp(0).AsRegister<Register>();
5599 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005600 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005601 }
5602
Calin Juravle52c48962014-12-16 17:02:57 +00005603 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005604 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005605 }
5606}
5607
5608void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5609 HandleFieldGet(instruction, instruction->GetFieldInfo());
5610}
5611
5612void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5613 HandleFieldGet(instruction, instruction->GetFieldInfo());
5614}
5615
5616void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5617 HandleFieldSet(instruction, instruction->GetFieldInfo());
5618}
5619
5620void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005621 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005622}
5623
5624void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5625 HandleFieldSet(instruction, instruction->GetFieldInfo());
5626}
5627
5628void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005629 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005630}
5631
5632void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5633 HandleFieldGet(instruction, instruction->GetFieldInfo());
5634}
5635
5636void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5637 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005638}
5639
Vladimir Marko552a1342017-10-31 10:56:47 +00005640void LocationsBuilderX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5641 codegen_->CreateStringBuilderAppendLocations(instruction, Location::RegisterLocation(EAX));
5642}
5643
5644void InstructionCodeGeneratorX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5645 __ movl(EAX, Immediate(instruction->GetFormat()->GetValue()));
5646 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
5647}
5648
Calin Juravlee460d1d2015-09-29 04:52:17 +01005649void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5650 HUnresolvedInstanceFieldGet* instruction) {
5651 FieldAccessCallingConventionX86 calling_convention;
5652 codegen_->CreateUnresolvedFieldLocationSummary(
5653 instruction, instruction->GetFieldType(), calling_convention);
5654}
5655
5656void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5657 HUnresolvedInstanceFieldGet* instruction) {
5658 FieldAccessCallingConventionX86 calling_convention;
5659 codegen_->GenerateUnresolvedFieldAccess(instruction,
5660 instruction->GetFieldType(),
5661 instruction->GetFieldIndex(),
5662 instruction->GetDexPc(),
5663 calling_convention);
5664}
5665
5666void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5667 HUnresolvedInstanceFieldSet* instruction) {
5668 FieldAccessCallingConventionX86 calling_convention;
5669 codegen_->CreateUnresolvedFieldLocationSummary(
5670 instruction, instruction->GetFieldType(), calling_convention);
5671}
5672
5673void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5674 HUnresolvedInstanceFieldSet* instruction) {
5675 FieldAccessCallingConventionX86 calling_convention;
5676 codegen_->GenerateUnresolvedFieldAccess(instruction,
5677 instruction->GetFieldType(),
5678 instruction->GetFieldIndex(),
5679 instruction->GetDexPc(),
5680 calling_convention);
5681}
5682
5683void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5684 HUnresolvedStaticFieldGet* instruction) {
5685 FieldAccessCallingConventionX86 calling_convention;
5686 codegen_->CreateUnresolvedFieldLocationSummary(
5687 instruction, instruction->GetFieldType(), calling_convention);
5688}
5689
5690void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5691 HUnresolvedStaticFieldGet* instruction) {
5692 FieldAccessCallingConventionX86 calling_convention;
5693 codegen_->GenerateUnresolvedFieldAccess(instruction,
5694 instruction->GetFieldType(),
5695 instruction->GetFieldIndex(),
5696 instruction->GetDexPc(),
5697 calling_convention);
5698}
5699
5700void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5701 HUnresolvedStaticFieldSet* instruction) {
5702 FieldAccessCallingConventionX86 calling_convention;
5703 codegen_->CreateUnresolvedFieldLocationSummary(
5704 instruction, instruction->GetFieldType(), calling_convention);
5705}
5706
5707void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5708 HUnresolvedStaticFieldSet* instruction) {
5709 FieldAccessCallingConventionX86 calling_convention;
5710 codegen_->GenerateUnresolvedFieldAccess(instruction,
5711 instruction->GetFieldType(),
5712 instruction->GetFieldIndex(),
5713 instruction->GetDexPc(),
5714 calling_convention);
5715}
5716
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005717void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005718 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5719 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5720 ? Location::RequiresRegister()
5721 : Location::Any();
5722 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005723}
5724
Calin Juravle2ae48182016-03-16 14:05:09 +00005725void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5726 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005727 return;
5728 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005729 LocationSummary* locations = instruction->GetLocations();
5730 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005731
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005732 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005733 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005734}
5735
Calin Juravle2ae48182016-03-16 14:05:09 +00005736void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005737 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005738 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005739
5740 LocationSummary* locations = instruction->GetLocations();
5741 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005742
5743 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005744 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005745 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005746 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005747 } else {
5748 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005749 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005750 __ jmp(slow_path->GetEntryLabel());
5751 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005752 }
5753 __ j(kEqual, slow_path->GetEntryLabel());
5754}
5755
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005756void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005757 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005758}
5759
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005760void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005761 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005762 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005763 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005764 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5765 object_array_get_with_read_barrier
5766 ? LocationSummary::kCallOnSlowPath
5767 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005768 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005769 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005770 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005771 locations->SetInAt(0, Location::RequiresRegister());
5772 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005773 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005774 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5775 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005776 // The output overlaps in case of long: we don't want the low move
5777 // to overwrite the array's location. Likewise, in the case of an
5778 // object array get with read barriers enabled, we do not want the
5779 // move to overwrite the array's location, as we need it to emit
5780 // the read barrier.
5781 locations->SetOut(
5782 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005783 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
5784 ? Location::kOutputOverlap
5785 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005786 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005787}
5788
5789void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5790 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005791 Location obj_loc = locations->InAt(0);
5792 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005793 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005794 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005795 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005796
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005797 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00005798 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005799 case DataType::Type::kBool:
5800 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005801 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005802 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005803 break;
5804 }
5805
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005806 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005807 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005808 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005809 break;
5810 }
5811
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005812 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005813 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005814 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5815 // Branch cases into compressed and uncompressed for each index's type.
5816 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5817 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005818 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005819 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005820 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5821 "Expecting 0=compressed, 1=uncompressed");
5822 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005823 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5824 __ jmp(&done);
5825 __ Bind(&not_compressed);
5826 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5827 __ Bind(&done);
5828 } else {
5829 // Common case for charAt of array of char or when string compression's
5830 // feature is turned off.
5831 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5832 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005833 break;
5834 }
5835
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005836 case DataType::Type::kInt16: {
5837 Register out = out_loc.AsRegister<Register>();
5838 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5839 break;
5840 }
5841
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005842 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005843 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005844 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005845 break;
5846 }
5847
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005848 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005849 static_assert(
5850 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5851 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005852 // /* HeapReference<Object> */ out =
5853 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5854 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005855 // Note that a potential implicit null check is handled in this
5856 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5857 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005858 instruction, out_loc, obj, data_offset, index, /* needs_null_check= */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005859 } else {
5860 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005861 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5862 codegen_->MaybeRecordImplicitNullCheck(instruction);
5863 // If read barriers are enabled, emit read barriers other than
5864 // Baker's using a slow path (and also unpoison the loaded
5865 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005866 if (index.IsConstant()) {
5867 uint32_t offset =
5868 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005869 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5870 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005871 codegen_->MaybeGenerateReadBarrierSlow(
5872 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5873 }
5874 }
5875 break;
5876 }
5877
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005878 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005879 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005880 __ movl(out_loc.AsRegisterPairLow<Register>(),
5881 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5882 codegen_->MaybeRecordImplicitNullCheck(instruction);
5883 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5884 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005885 break;
5886 }
5887
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005888 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005889 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005890 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005891 break;
5892 }
5893
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005894 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005895 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005896 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005897 break;
5898 }
5899
Aart Bik66c158e2018-01-31 12:55:04 -08005900 case DataType::Type::kUint32:
5901 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005902 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005903 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005904 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005905 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005906
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005907 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005908 // Potential implicit null checks, in the case of reference or
5909 // long arrays, are handled in the previous switch statement.
5910 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005911 codegen_->MaybeRecordImplicitNullCheck(instruction);
5912 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005913}
5914
5915void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005916 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005917
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005918 bool needs_write_barrier =
5919 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005920 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005921
Vladimir Markoca6fff82017-10-03 14:49:14 +01005922 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01005923 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005924 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005925
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005926 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005927 // We need the inputs to be different than the output in case of long operation.
5928 // In case of a byte operation, the register allocator does not support multiple
5929 // inputs that die at entry with one in a specific register.
5930 locations->SetInAt(0, Location::RequiresRegister());
5931 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5932 if (is_byte_type) {
5933 // Ensure the value is in a byte register.
5934 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005935 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005936 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005937 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005938 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5939 }
5940 if (needs_write_barrier) {
5941 // Temporary registers for the write barrier.
5942 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5943 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005944 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005945 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005946}
5947
5948void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5949 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005950 Location array_loc = locations->InAt(0);
5951 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005952 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005953 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005954 DataType::Type value_type = instruction->GetComponentType();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005955 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005956 bool needs_write_barrier =
5957 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005958
5959 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005960 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005961 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005962 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005963 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005964 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005965 if (value.IsRegister()) {
5966 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005967 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005968 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005969 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005970 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005971 break;
5972 }
5973
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005974 case DataType::Type::kUint16:
5975 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005976 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005977 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005978 if (value.IsRegister()) {
5979 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005980 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005981 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005982 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005983 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005984 break;
5985 }
5986
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005987 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005988 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005989 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005990
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005991 if (!value.IsRegister()) {
5992 // Just setting null.
5993 DCHECK(instruction->InputAt(2)->IsNullConstant());
5994 DCHECK(value.IsConstant()) << value;
5995 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005996 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005997 DCHECK(!needs_write_barrier);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00005998 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005999 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006000 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006001
6002 DCHECK(needs_write_barrier);
6003 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01006004 Location temp_loc = locations->GetTemp(0);
6005 Register temp = temp_loc.AsRegister<Register>();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006006
6007 bool can_value_be_null = instruction->GetValueCanBeNull();
6008 NearLabel do_store;
6009 if (can_value_be_null) {
6010 __ testl(register_value, register_value);
6011 __ j(kEqual, &do_store);
6012 }
6013
6014 SlowPathCode* slow_path = nullptr;
6015 if (needs_type_check) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006016 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006017 codegen_->AddSlowPath(slow_path);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006018
6019 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6020 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6021 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006022
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006023 // Note that when Baker read barriers are enabled, the type
6024 // checks are performed without read barriers. This is fine,
6025 // even in the case where a class object is in the from-space
6026 // after the flip, as a comparison involving such a type would
6027 // not produce a false positive; it may of course produce a
6028 // false negative, in which case we would take the ArraySet
6029 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01006030
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006031 // /* HeapReference<Class> */ temp = array->klass_
6032 __ movl(temp, Address(array, class_offset));
6033 codegen_->MaybeRecordImplicitNullCheck(instruction);
6034 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01006035
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006036 // /* HeapReference<Class> */ temp = temp->component_type_
6037 __ movl(temp, Address(temp, component_offset));
6038 // If heap poisoning is enabled, no need to unpoison `temp`
6039 // nor the object reference in `register_value->klass`, as
6040 // we are comparing two poisoned references.
6041 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01006042
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006043 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006044 NearLabel do_put;
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006045 __ j(kEqual, &do_put);
6046 // If heap poisoning is enabled, the `temp` reference has
6047 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006048 __ MaybeUnpoisonHeapReference(temp);
6049
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006050 // If heap poisoning is enabled, no need to unpoison the
6051 // heap reference loaded below, as it is only used for a
6052 // comparison with null.
6053 __ cmpl(Address(temp, super_offset), Immediate(0));
6054 __ j(kNotEqual, slow_path->GetEntryLabel());
6055 __ Bind(&do_put);
6056 } else {
6057 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006058 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006059 }
6060
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006061 Register card = locations->GetTemp(1).AsRegister<Register>();
6062 codegen_->MarkGCCard(
6063 temp, card, array, value.AsRegister<Register>(), /* value_can_be_null= */ false);
6064
6065 if (can_value_be_null) {
6066 DCHECK(do_store.IsLinked());
6067 __ Bind(&do_store);
6068 }
6069
6070 Register source = register_value;
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006071 if (kPoisonHeapReferences) {
6072 __ movl(temp, register_value);
6073 __ PoisonHeapReference(temp);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006074 source = temp;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006075 }
6076
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006077 __ movl(address, source);
6078
6079 if (can_value_be_null || !needs_type_check) {
6080 codegen_->MaybeRecordImplicitNullCheck(instruction);
6081 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006082
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006083 if (slow_path != nullptr) {
6084 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006085 }
6086
6087 break;
6088 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006089
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006090 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006091 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006092 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006093 if (value.IsRegister()) {
6094 __ movl(address, value.AsRegister<Register>());
6095 } else {
6096 DCHECK(value.IsConstant()) << value;
6097 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
6098 __ movl(address, Immediate(v));
6099 }
6100 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006101 break;
6102 }
6103
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006104 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006105 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006106 if (value.IsRegisterPair()) {
6107 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6108 value.AsRegisterPairLow<Register>());
6109 codegen_->MaybeRecordImplicitNullCheck(instruction);
6110 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6111 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006112 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006113 DCHECK(value.IsConstant());
6114 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
6115 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6116 Immediate(Low32Bits(val)));
6117 codegen_->MaybeRecordImplicitNullCheck(instruction);
6118 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6119 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006120 }
6121 break;
6122 }
6123
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006124 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006125 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006126 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006127 if (value.IsFpuRegister()) {
6128 __ movss(address, value.AsFpuRegister<XmmRegister>());
6129 } else {
6130 DCHECK(value.IsConstant());
6131 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
6132 __ movl(address, Immediate(v));
6133 }
6134 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006135 break;
6136 }
6137
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006138 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006139 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006140 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006141 if (value.IsFpuRegister()) {
6142 __ movsd(address, value.AsFpuRegister<XmmRegister>());
6143 } else {
6144 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006145 Address address_hi =
6146 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05006147 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
6148 __ movl(address, Immediate(Low32Bits(v)));
6149 codegen_->MaybeRecordImplicitNullCheck(instruction);
6150 __ movl(address_hi, Immediate(High32Bits(v)));
6151 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006152 break;
6153 }
6154
Aart Bik66c158e2018-01-31 12:55:04 -08006155 case DataType::Type::kUint32:
6156 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006157 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006158 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07006159 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006160 }
6161}
6162
6163void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006164 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006165 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04006166 if (!instruction->IsEmittedAtUseSite()) {
6167 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6168 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006169}
6170
6171void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04006172 if (instruction->IsEmittedAtUseSite()) {
6173 return;
6174 }
6175
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006176 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006177 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006178 Register obj = locations->InAt(0).AsRegister<Register>();
6179 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006180 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00006181 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07006182 // Mask out most significant bit in case the array is String's array of char.
6183 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006184 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006185 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006186}
6187
6188void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006189 RegisterSet caller_saves = RegisterSet::Empty();
6190 InvokeRuntimeCallingConvention calling_convention;
6191 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6192 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6193 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05006194 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04006195 HInstruction* length = instruction->InputAt(1);
6196 if (!length->IsEmittedAtUseSite()) {
6197 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6198 }
jessicahandojo4877b792016-09-08 19:49:13 -07006199 // Need register to see array's length.
6200 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6201 locations->AddTemp(Location::RequiresRegister());
6202 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006203}
6204
6205void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07006206 const bool is_string_compressed_char_at =
6207 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006208 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05006209 Location index_loc = locations->InAt(0);
6210 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006211 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006212 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006213
Mark Mendell99dbd682015-04-22 16:18:52 -04006214 if (length_loc.IsConstant()) {
6215 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
6216 if (index_loc.IsConstant()) {
6217 // BCE will remove the bounds check if we are guarenteed to pass.
6218 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6219 if (index < 0 || index >= length) {
6220 codegen_->AddSlowPath(slow_path);
6221 __ jmp(slow_path->GetEntryLabel());
6222 } else {
6223 // Some optimization after BCE may have generated this, and we should not
6224 // generate a bounds check if it is a valid range.
6225 }
6226 return;
6227 }
6228
6229 // We have to reverse the jump condition because the length is the constant.
6230 Register index_reg = index_loc.AsRegister<Register>();
6231 __ cmpl(index_reg, Immediate(length));
6232 codegen_->AddSlowPath(slow_path);
6233 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006234 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04006235 HInstruction* array_length = instruction->InputAt(1);
6236 if (array_length->IsEmittedAtUseSite()) {
6237 // Address the length field in the array.
6238 DCHECK(array_length->IsArrayLength());
6239 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
6240 Location array_loc = array_length->GetLocations()->InAt(0);
6241 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07006242 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006243 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
6244 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07006245 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
6246 __ movl(length_reg, array_len);
6247 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006248 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006249 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04006250 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006251 // Checking bounds for general case:
6252 // Array of char or string's array with feature compression off.
6253 if (index_loc.IsConstant()) {
6254 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6255 __ cmpl(array_len, Immediate(value));
6256 } else {
6257 __ cmpl(array_len, index_loc.AsRegister<Register>());
6258 }
6259 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04006260 }
Mark Mendell99dbd682015-04-22 16:18:52 -04006261 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006262 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04006263 }
6264 codegen_->AddSlowPath(slow_path);
6265 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006266 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006267}
6268
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006269void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006270 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006271}
6272
6273void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006274 if (instruction->GetNext()->IsSuspendCheck() &&
6275 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6276 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6277 // The back edge will generate the suspend check.
6278 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6279 }
6280
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006281 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6282}
6283
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006284void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006285 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6286 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07006287 // In suspend check slow path, usually there are no caller-save registers at all.
6288 // If SIMD instructions are present, however, we force spilling all live SIMD
6289 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07006290 locations->SetCustomSlowPathCallerSaves(
6291 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006292}
6293
6294void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006295 HBasicBlock* block = instruction->GetBlock();
6296 if (block->GetLoopInformation() != nullptr) {
6297 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6298 // The back edge will generate the suspend check.
6299 return;
6300 }
6301 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6302 // The goto will generate the suspend check.
6303 return;
6304 }
6305 GenerateSuspendCheck(instruction, nullptr);
6306}
6307
6308void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
6309 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006310 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006311 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
6312 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006313 slow_path =
6314 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006315 instruction->SetSlowPath(slow_path);
6316 codegen_->AddSlowPath(slow_path);
6317 if (successor != nullptr) {
6318 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006319 }
6320 } else {
6321 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6322 }
6323
Andreas Gampe542451c2016-07-26 09:02:02 -07006324 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006325 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006326 if (successor == nullptr) {
6327 __ j(kNotEqual, slow_path->GetEntryLabel());
6328 __ Bind(slow_path->GetReturnLabel());
6329 } else {
6330 __ j(kEqual, codegen_->GetLabelOf(successor));
6331 __ jmp(slow_path->GetEntryLabel());
6332 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006333}
6334
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006335X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
6336 return codegen_->GetAssembler();
6337}
6338
Aart Bikcfe50bb2017-12-12 14:54:12 -08006339void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006340 ScratchRegisterScope ensure_scratch(
6341 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6342 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6343 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05006344
Aart Bikcfe50bb2017-12-12 14:54:12 -08006345 // Now that temp register is available (possibly spilled), move blocks of memory.
6346 for (int i = 0; i < number_of_words; i++) {
6347 __ movl(temp_reg, Address(ESP, src + stack_offset));
6348 __ movl(Address(ESP, dst + stack_offset), temp_reg);
6349 stack_offset += kX86WordSize;
6350 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006351}
6352
6353void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006354 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006355 Location source = move->GetSource();
6356 Location destination = move->GetDestination();
6357
6358 if (source.IsRegister()) {
6359 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006360 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006361 } else if (destination.IsFpuRegister()) {
6362 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006363 } else {
6364 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006365 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006366 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006367 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006368 size_t elem_size = DataType::Size(DataType::Type::kInt32);
David Brazdil74eb1b22015-12-14 11:44:01 +00006369 // Create stack space for 2 elements.
6370 __ subl(ESP, Immediate(2 * elem_size));
6371 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
6372 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
6373 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
6374 // And remove the temporary stack space we allocated.
6375 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006376 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006377 if (destination.IsRegister()) {
6378 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
6379 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006380 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006381 } else if (destination.IsRegisterPair()) {
6382 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
6383 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
6384 __ psrlq(src_reg, Immediate(32));
6385 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006386 } else if (destination.IsStackSlot()) {
6387 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006388 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006389 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006390 } else {
6391 DCHECK(destination.IsSIMDStackSlot());
6392 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05006393 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006394 } else if (source.IsStackSlot()) {
6395 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006396 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006397 } else if (destination.IsFpuRegister()) {
6398 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006399 } else {
6400 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08006401 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006402 }
6403 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006404 if (destination.IsRegisterPair()) {
6405 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
6406 __ movl(destination.AsRegisterPairHigh<Register>(),
6407 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
6408 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006409 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6410 } else {
6411 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006412 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006413 }
Aart Bik5576f372017-03-23 16:17:37 -07006414 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006415 if (destination.IsFpuRegister()) {
6416 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6417 } else {
6418 DCHECK(destination.IsSIMDStackSlot());
6419 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6420 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006421 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006422 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006423 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006424 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006425 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006426 if (value == 0) {
6427 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
6428 } else {
6429 __ movl(destination.AsRegister<Register>(), Immediate(value));
6430 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006431 } else {
6432 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05006433 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006434 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006435 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006436 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006437 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006438 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006439 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006440 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6441 if (value == 0) {
6442 // Easy handling of 0.0.
6443 __ xorps(dest, dest);
6444 } else {
6445 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006446 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6447 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
6448 __ movl(temp, Immediate(value));
6449 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006450 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006451 } else {
6452 DCHECK(destination.IsStackSlot()) << destination;
6453 __ movl(Address(ESP, destination.GetStackIndex()), imm);
6454 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006455 } else if (constant->IsLongConstant()) {
6456 int64_t value = constant->AsLongConstant()->GetValue();
6457 int32_t low_value = Low32Bits(value);
6458 int32_t high_value = High32Bits(value);
6459 Immediate low(low_value);
6460 Immediate high(high_value);
6461 if (destination.IsDoubleStackSlot()) {
6462 __ movl(Address(ESP, destination.GetStackIndex()), low);
6463 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6464 } else {
6465 __ movl(destination.AsRegisterPairLow<Register>(), low);
6466 __ movl(destination.AsRegisterPairHigh<Register>(), high);
6467 }
6468 } else {
6469 DCHECK(constant->IsDoubleConstant());
6470 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006471 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006472 int32_t low_value = Low32Bits(value);
6473 int32_t high_value = High32Bits(value);
6474 Immediate low(low_value);
6475 Immediate high(high_value);
6476 if (destination.IsFpuRegister()) {
6477 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6478 if (value == 0) {
6479 // Easy handling of 0.0.
6480 __ xorpd(dest, dest);
6481 } else {
6482 __ pushl(high);
6483 __ pushl(low);
6484 __ movsd(dest, Address(ESP, 0));
6485 __ addl(ESP, Immediate(8));
6486 }
6487 } else {
6488 DCHECK(destination.IsDoubleStackSlot()) << destination;
6489 __ movl(Address(ESP, destination.GetStackIndex()), low);
6490 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6491 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006492 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006493 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006494 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006495 }
6496}
6497
Mark Mendella5c19ce2015-04-01 12:51:05 -04006498void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006499 Register suggested_scratch = reg == EAX ? EBX : EAX;
6500 ScratchRegisterScope ensure_scratch(
6501 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
6502
6503 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6504 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
6505 __ movl(Address(ESP, mem + stack_offset), reg);
6506 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006507}
6508
Mark Mendell7c8d0092015-01-26 11:21:33 -05006509void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006510 ScratchRegisterScope ensure_scratch(
6511 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6512
6513 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6514 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6515 __ movl(temp_reg, Address(ESP, mem + stack_offset));
6516 __ movss(Address(ESP, mem + stack_offset), reg);
6517 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006518}
6519
Aart Bikcfe50bb2017-12-12 14:54:12 -08006520void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
6521 size_t extra_slot = 4 * kX86WordSize;
6522 __ subl(ESP, Immediate(extra_slot));
6523 __ movups(Address(ESP, 0), XmmRegister(reg));
6524 ExchangeMemory(0, mem + extra_slot, 4);
6525 __ movups(XmmRegister(reg), Address(ESP, 0));
6526 __ addl(ESP, Immediate(extra_slot));
6527}
6528
6529void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006530 ScratchRegisterScope ensure_scratch1(
6531 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006532
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006533 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
6534 ScratchRegisterScope ensure_scratch2(
6535 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006536
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006537 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
6538 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006539
6540 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
6541 for (int i = 0; i < number_of_words; i++) {
6542 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
6543 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
6544 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
6545 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
6546 stack_offset += kX86WordSize;
6547 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006548}
6549
6550void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006551 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006552 Location source = move->GetSource();
6553 Location destination = move->GetDestination();
6554
6555 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04006556 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
6557 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
6558 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
6559 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
6560 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006561 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006562 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006563 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006564 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006565 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006566 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006567 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
6568 // Use XOR Swap algorithm to avoid a temporary.
6569 DCHECK_NE(source.reg(), destination.reg());
6570 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6571 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
6572 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6573 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
6574 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6575 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
6576 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006577 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
6578 // Take advantage of the 16 bytes in the XMM register.
6579 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
6580 Address stack(ESP, destination.GetStackIndex());
6581 // Load the double into the high doubleword.
6582 __ movhpd(reg, stack);
6583
6584 // Store the low double into the destination.
6585 __ movsd(stack, reg);
6586
6587 // Move the high double to the low double.
6588 __ psrldq(reg, Immediate(8));
6589 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6590 // Take advantage of the 16 bytes in the XMM register.
6591 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6592 Address stack(ESP, source.GetStackIndex());
6593 // Load the double into the high doubleword.
6594 __ movhpd(reg, stack);
6595
6596 // Store the low double into the destination.
6597 __ movsd(stack, reg);
6598
6599 // Move the high double to the low double.
6600 __ psrldq(reg, Immediate(8));
6601 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006602 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
6603 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
6604 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6605 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
6606 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6607 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
6608 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006609 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006610 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006611 }
6612}
6613
6614void ParallelMoveResolverX86::SpillScratch(int reg) {
6615 __ pushl(static_cast<Register>(reg));
6616}
6617
6618void ParallelMoveResolverX86::RestoreScratch(int reg) {
6619 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006620}
6621
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006622HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6623 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006624 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006625 case HLoadClass::LoadKind::kInvalid:
6626 LOG(FATAL) << "UNREACHABLE";
6627 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006628 case HLoadClass::LoadKind::kReferrersClass:
6629 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006630 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006631 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006632 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006633 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006634 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006635 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006636 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006637 DCHECK(Runtime::Current()->UseJitCompilation());
6638 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006639 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006640 break;
6641 }
6642 return desired_class_load_kind;
6643}
6644
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006645void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006646 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006647 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006648 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006649 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006650 cls,
6651 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006652 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006653 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006654 return;
6655 }
Vladimir Marko41559982017-01-06 14:04:23 +00006656 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006657
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006658 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6659 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006660 ? LocationSummary::kCallOnSlowPath
6661 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006662 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006663 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006664 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006665 }
6666
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006667 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006668 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006669 load_kind == HLoadClass::LoadKind::kBootImageRelRo ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006670 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006671 locations->SetInAt(0, Location::RequiresRegister());
6672 }
6673 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006674 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6675 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6676 // Rely on the type resolution and/or initialization to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006677 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006678 } else {
6679 // For non-Baker read barrier we have a temp-clobbering call.
6680 }
6681 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006682}
6683
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006684Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006685 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006686 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006687 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006688 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006689 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006690 PatchInfo<Label>* info = &jit_class_patches_.back();
6691 return &info->label;
6692}
6693
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006694// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6695// move.
6696void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006697 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006698 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006699 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006700 return;
6701 }
Vladimir Marko41559982017-01-06 14:04:23 +00006702 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006703
Vladimir Marko41559982017-01-06 14:04:23 +00006704 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006705 Location out_loc = locations->Out();
6706 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006707
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006708 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006709 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6710 ? kWithoutReadBarrier
6711 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006712 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006713 case HLoadClass::LoadKind::kReferrersClass: {
6714 DCHECK(!cls->CanCallRuntime());
6715 DCHECK(!cls->MustGenerateClinitCheck());
6716 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6717 Register current_method = locations->InAt(0).AsRegister<Register>();
6718 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006719 cls,
6720 out_loc,
6721 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Andreas Gampe3db70682018-12-26 15:12:03 -08006722 /* fixup_label= */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006723 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006724 break;
6725 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006726 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01006727 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
6728 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006729 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006730 Register method_address = locations->InAt(0).AsRegister<Register>();
6731 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006732 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006733 break;
6734 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006735 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006736 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6737 Register method_address = locations->InAt(0).AsRegister<Register>();
6738 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006739 codegen_->RecordBootImageRelRoPatch(cls->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6740 codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006741 break;
6742 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006743 case HLoadClass::LoadKind::kBssEntry: {
6744 Register method_address = locations->InAt(0).AsRegister<Register>();
6745 Address address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6746 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6747 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01006748 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006749 generate_null_check = true;
6750 break;
6751 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006752 case HLoadClass::LoadKind::kJitBootImageAddress: {
6753 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
6754 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
6755 DCHECK_NE(address, 0u);
6756 __ movl(out, Immediate(address));
6757 break;
6758 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006759 case HLoadClass::LoadKind::kJitTableAddress: {
6760 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6761 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006762 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006763 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006764 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006765 break;
6766 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006767 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006768 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006769 LOG(FATAL) << "UNREACHABLE";
6770 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006771 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006772
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006773 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6774 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006775 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006776 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006777
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006778 if (generate_null_check) {
6779 __ testl(out, out);
6780 __ j(kEqual, slow_path->GetEntryLabel());
6781 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006782
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006783 if (cls->MustGenerateClinitCheck()) {
6784 GenerateClassInitializationCheck(slow_path, out);
6785 } else {
6786 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006787 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006788 }
6789}
6790
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006791void LocationsBuilderX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6792 InvokeRuntimeCallingConvention calling_convention;
6793 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6794 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
6795}
6796
6797void InstructionCodeGeneratorX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6798 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
6799}
6800
Orion Hodson18259d72018-04-12 11:18:23 +01006801void LocationsBuilderX86::VisitLoadMethodType(HLoadMethodType* load) {
6802 InvokeRuntimeCallingConvention calling_convention;
6803 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6804 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
6805}
6806
6807void InstructionCodeGeneratorX86::VisitLoadMethodType(HLoadMethodType* load) {
6808 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
6809}
6810
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006811void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6812 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006813 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006814 locations->SetInAt(0, Location::RequiresRegister());
6815 if (check->HasUses()) {
6816 locations->SetOut(Location::SameAsFirstInput());
6817 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006818 // Rely on the type initialization to save everything we need.
6819 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006820}
6821
6822void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006823 // We assume the class to not be null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006824 SlowPathCode* slow_path =
6825 new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(check->GetLoadClass(), check);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006826 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006827 GenerateClassInitializationCheck(slow_path,
6828 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006829}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006830
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006831void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006832 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00006833 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
6834 const size_t status_byte_offset =
6835 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
Vladimir Markobf121912019-06-04 13:49:05 +01006836 constexpr uint32_t shifted_visibly_initialized_value =
6837 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << (status_lsb_position % kBitsPerByte);
Vladimir Markodc682aa2018-01-04 18:42:57 +00006838
Vladimir Markobf121912019-06-04 13:49:05 +01006839 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_visibly_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00006840 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006841 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006842}
6843
Vladimir Marko175e7862018-03-27 09:03:13 +00006844void InstructionCodeGeneratorX86::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
6845 Register temp) {
6846 uint32_t path_to_root = check->GetBitstringPathToRoot();
6847 uint32_t mask = check->GetBitstringMask();
6848 DCHECK(IsPowerOfTwo(mask + 1));
6849 size_t mask_bits = WhichPowerOf2(mask + 1);
6850
6851 if (mask_bits == 16u) {
6852 // Compare the bitstring in memory.
6853 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
6854 } else {
6855 // /* uint32_t */ temp = temp->status_
6856 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
6857 // Compare the bitstring bits using SUB.
6858 __ subl(temp, Immediate(path_to_root));
6859 // Shift out bits that do not contribute to the comparison.
6860 __ shll(temp, Immediate(32u - mask_bits));
6861 }
6862}
6863
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006864HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6865 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006866 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006867 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006868 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006869 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko764d4542017-05-16 10:31:41 +01006870 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006871 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006872 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006873 case HLoadString::LoadKind::kJitTableAddress:
6874 DCHECK(Runtime::Current()->UseJitCompilation());
6875 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006876 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006877 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006878 }
6879 return desired_string_load_kind;
6880}
6881
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006882void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006883 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006884 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006885 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006886 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006887 load_kind == HLoadString::LoadKind::kBootImageRelRo ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006888 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006889 locations->SetInAt(0, Location::RequiresRegister());
6890 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006891 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006892 locations->SetOut(Location::RegisterLocation(EAX));
6893 } else {
6894 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006895 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6896 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006897 // Rely on the pResolveString to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006898 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006899 } else {
6900 // For non-Baker read barrier we have a temp-clobbering call.
6901 }
6902 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006903 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006904}
6905
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006906Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006907 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006908 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006909 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006910 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006911 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006912 PatchInfo<Label>* info = &jit_string_patches_.back();
6913 return &info->label;
6914}
6915
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006916// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6917// move.
6918void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006919 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006920 Location out_loc = locations->Out();
6921 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006922
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006923 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006924 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01006925 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
6926 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006927 Register method_address = locations->InAt(0).AsRegister<Register>();
6928 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006929 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006930 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006931 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006932 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006933 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6934 Register method_address = locations->InAt(0).AsRegister<Register>();
6935 __ movl(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006936 codegen_->RecordBootImageRelRoPatch(load->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6937 codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01006938 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006939 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006940 case HLoadString::LoadKind::kBssEntry: {
6941 Register method_address = locations->InAt(0).AsRegister<Register>();
6942 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6943 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006944 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006945 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01006946 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko174b2e22017-10-12 13:34:49 +01006947 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006948 codegen_->AddSlowPath(slow_path);
6949 __ testl(out, out);
6950 __ j(kEqual, slow_path->GetEntryLabel());
6951 __ Bind(slow_path->GetExitLabel());
6952 return;
6953 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006954 case HLoadString::LoadKind::kJitBootImageAddress: {
6955 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
6956 DCHECK_NE(address, 0u);
6957 __ movl(out, Immediate(address));
6958 return;
6959 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006960 case HLoadString::LoadKind::kJitTableAddress: {
6961 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6962 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006963 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006964 // /* GcRoot<mirror::String> */ out = *address
6965 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6966 return;
6967 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006968 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006969 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006970 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006971
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006972 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006973 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006974 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006975 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006976 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6977 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006978}
6979
David Brazdilcb1c0552015-08-04 16:22:25 +01006980static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006981 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006982}
6983
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006984void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6985 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006986 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006987 locations->SetOut(Location::RequiresRegister());
6988}
6989
6990void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006991 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6992}
6993
6994void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006995 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01006996}
6997
6998void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6999 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007000}
7001
7002void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007003 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7004 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007005 InvokeRuntimeCallingConvention calling_convention;
7006 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7007}
7008
7009void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007010 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007011 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007012}
7013
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007014// Temp is used for read barrier.
7015static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7016 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00007017 !kUseBakerReadBarrier &&
7018 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00007019 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007020 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7021 return 1;
7022 }
7023 return 0;
7024}
7025
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007026// Interface case has 2 temps, one for holding the number of interfaces, one for the current
7027// interface pointer, the current interface is compared in memory.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007028// The other checks have one temp for loading the object's class.
7029static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007030 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007031 return 2;
7032 }
7033 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007034}
7035
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007036void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007037 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007038 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01007039 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007040 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007041 case TypeCheckKind::kExactCheck:
7042 case TypeCheckKind::kAbstractClassCheck:
7043 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007044 case TypeCheckKind::kArrayObjectCheck: {
7045 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7046 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7047 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007048 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007049 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007050 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007051 case TypeCheckKind::kUnresolvedCheck:
7052 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007053 call_kind = LocationSummary::kCallOnSlowPath;
7054 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007055 case TypeCheckKind::kBitstringCheck:
7056 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007057 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007058
Vladimir Markoca6fff82017-10-03 14:49:14 +01007059 LocationSummary* locations =
7060 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01007061 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007062 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007063 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007064 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007065 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7066 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7067 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7068 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7069 } else {
7070 locations->SetInAt(1, Location::Any());
7071 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007072 // Note that TypeCheckSlowPathX86 uses this "out" register too.
7073 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007074 // When read barriers are enabled, we need a temporary register for some cases.
7075 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007076}
7077
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007078void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007079 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007080 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007081 Location obj_loc = locations->InAt(0);
7082 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007083 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007084 Location out_loc = locations->Out();
7085 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007086 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7087 DCHECK_LE(num_temps, 1u);
7088 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007089 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007090 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7091 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7092 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07007093 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007094 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007095
7096 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007097 // Avoid null check if we know obj is not null.
7098 if (instruction->MustDoNullCheck()) {
7099 __ testl(obj, obj);
7100 __ j(kEqual, &zero);
7101 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007102
Roland Levillain7c1559a2015-12-15 10:55:36 +00007103 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007104 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007105 ReadBarrierOption read_barrier_option =
7106 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007107 // /* HeapReference<Class> */ out = obj->klass_
7108 GenerateReferenceLoadTwoRegisters(instruction,
7109 out_loc,
7110 obj_loc,
7111 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007112 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007113 if (cls.IsRegister()) {
7114 __ cmpl(out, cls.AsRegister<Register>());
7115 } else {
7116 DCHECK(cls.IsStackSlot()) << cls;
7117 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7118 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007119
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007120 // Classes must be equal for the instanceof to succeed.
7121 __ j(kNotEqual, &zero);
7122 __ movl(out, Immediate(1));
7123 __ jmp(&done);
7124 break;
7125 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007126
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007127 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007128 ReadBarrierOption read_barrier_option =
7129 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007130 // /* HeapReference<Class> */ out = obj->klass_
7131 GenerateReferenceLoadTwoRegisters(instruction,
7132 out_loc,
7133 obj_loc,
7134 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007135 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007136 // If the class is abstract, we eagerly fetch the super class of the
7137 // object to avoid doing a comparison we know will fail.
7138 NearLabel loop;
7139 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007140 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007141 GenerateReferenceLoadOneRegister(instruction,
7142 out_loc,
7143 super_offset,
7144 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007145 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007146 __ testl(out, out);
7147 // If `out` is null, we use it for the result, and jump to `done`.
7148 __ j(kEqual, &done);
7149 if (cls.IsRegister()) {
7150 __ cmpl(out, cls.AsRegister<Register>());
7151 } else {
7152 DCHECK(cls.IsStackSlot()) << cls;
7153 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7154 }
7155 __ j(kNotEqual, &loop);
7156 __ movl(out, Immediate(1));
7157 if (zero.IsLinked()) {
7158 __ jmp(&done);
7159 }
7160 break;
7161 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007162
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007163 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007164 ReadBarrierOption read_barrier_option =
7165 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007166 // /* HeapReference<Class> */ out = obj->klass_
7167 GenerateReferenceLoadTwoRegisters(instruction,
7168 out_loc,
7169 obj_loc,
7170 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007171 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007172 // Walk over the class hierarchy to find a match.
7173 NearLabel loop, success;
7174 __ Bind(&loop);
7175 if (cls.IsRegister()) {
7176 __ cmpl(out, cls.AsRegister<Register>());
7177 } else {
7178 DCHECK(cls.IsStackSlot()) << cls;
7179 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7180 }
7181 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007182 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007183 GenerateReferenceLoadOneRegister(instruction,
7184 out_loc,
7185 super_offset,
7186 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007187 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007188 __ testl(out, out);
7189 __ j(kNotEqual, &loop);
7190 // If `out` is null, we use it for the result, and jump to `done`.
7191 __ jmp(&done);
7192 __ Bind(&success);
7193 __ movl(out, Immediate(1));
7194 if (zero.IsLinked()) {
7195 __ jmp(&done);
7196 }
7197 break;
7198 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007199
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007200 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007201 ReadBarrierOption read_barrier_option =
7202 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007203 // /* HeapReference<Class> */ out = obj->klass_
7204 GenerateReferenceLoadTwoRegisters(instruction,
7205 out_loc,
7206 obj_loc,
7207 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007208 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007209 // Do an exact check.
7210 NearLabel exact_check;
7211 if (cls.IsRegister()) {
7212 __ cmpl(out, cls.AsRegister<Register>());
7213 } else {
7214 DCHECK(cls.IsStackSlot()) << cls;
7215 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7216 }
7217 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007218 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007219 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007220 GenerateReferenceLoadOneRegister(instruction,
7221 out_loc,
7222 component_offset,
7223 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007224 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007225 __ testl(out, out);
7226 // If `out` is null, we use it for the result, and jump to `done`.
7227 __ j(kEqual, &done);
7228 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
7229 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007230 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007231 __ movl(out, Immediate(1));
7232 __ jmp(&done);
7233 break;
7234 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007235
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007236 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007237 // No read barrier since the slow path will retry upon failure.
7238 // /* HeapReference<Class> */ out = obj->klass_
7239 GenerateReferenceLoadTwoRegisters(instruction,
7240 out_loc,
7241 obj_loc,
7242 class_offset,
7243 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007244 if (cls.IsRegister()) {
7245 __ cmpl(out, cls.AsRegister<Register>());
7246 } else {
7247 DCHECK(cls.IsStackSlot()) << cls;
7248 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7249 }
7250 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007251 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007252 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007253 codegen_->AddSlowPath(slow_path);
7254 __ j(kNotEqual, slow_path->GetEntryLabel());
7255 __ movl(out, Immediate(1));
7256 if (zero.IsLinked()) {
7257 __ jmp(&done);
7258 }
7259 break;
7260 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007261
Calin Juravle98893e12015-10-02 21:05:03 +01007262 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007263 case TypeCheckKind::kInterfaceCheck: {
7264 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007265 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00007266 // cases.
7267 //
7268 // We cannot directly call the InstanceofNonTrivial runtime
7269 // entry point without resorting to a type checking slow path
7270 // here (i.e. by calling InvokeRuntime directly), as it would
7271 // require to assign fixed registers for the inputs of this
7272 // HInstanceOf instruction (following the runtime calling
7273 // convention), which might be cluttered by the potential first
7274 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007275 //
7276 // TODO: Introduce a new runtime entry point taking the object
7277 // to test (instead of its class) as argument, and let it deal
7278 // with the read barrier issues. This will let us refactor this
7279 // case of the `switch` code as it was previously (with a direct
7280 // call to the runtime not using a type checking slow path).
7281 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007282 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007283 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007284 instruction, /* is_fatal= */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007285 codegen_->AddSlowPath(slow_path);
7286 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007287 if (zero.IsLinked()) {
7288 __ jmp(&done);
7289 }
7290 break;
7291 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007292
7293 case TypeCheckKind::kBitstringCheck: {
7294 // /* HeapReference<Class> */ temp = obj->klass_
7295 GenerateReferenceLoadTwoRegisters(instruction,
7296 out_loc,
7297 obj_loc,
7298 class_offset,
7299 kWithoutReadBarrier);
7300
7301 GenerateBitstringTypeCheckCompare(instruction, out);
7302 __ j(kNotEqual, &zero);
7303 __ movl(out, Immediate(1));
7304 __ jmp(&done);
7305 break;
7306 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007307 }
7308
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007309 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007310 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007311 __ xorl(out, out);
7312 }
7313
7314 if (done.IsLinked()) {
7315 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007316 }
7317
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007318 if (slow_path != nullptr) {
7319 __ Bind(slow_path->GetExitLabel());
7320 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007321}
7322
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007323void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007324 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007325 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007326 LocationSummary* locations =
7327 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007328 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007329 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7330 // Require a register for the interface check since there is a loop that compares the class to
7331 // a memory address.
7332 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007333 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7334 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7335 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7336 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007337 } else {
7338 locations->SetInAt(1, Location::Any());
7339 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007340 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007341 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
7342}
7343
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007344void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007345 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007346 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007347 Location obj_loc = locations->InAt(0);
7348 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007349 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007350 Location temp_loc = locations->GetTemp(0);
7351 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007352 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7353 DCHECK_GE(num_temps, 1u);
7354 DCHECK_LE(num_temps, 2u);
7355 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7356 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7357 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7358 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7359 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7360 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7361 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7362 const uint32_t object_array_data_offset =
7363 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007364
Vladimir Marko87584542017-12-12 17:47:52 +00007365 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007366 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007367 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7368 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007369 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007370
Roland Levillain0d5a2812015-11-13 10:07:31 +00007371 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007372 // Avoid null check if we know obj is not null.
7373 if (instruction->MustDoNullCheck()) {
7374 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007375 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007376 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007377
Roland Levillain0d5a2812015-11-13 10:07:31 +00007378 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007379 case TypeCheckKind::kExactCheck:
7380 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007381 // /* HeapReference<Class> */ temp = obj->klass_
7382 GenerateReferenceLoadTwoRegisters(instruction,
7383 temp_loc,
7384 obj_loc,
7385 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007386 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007387
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007388 if (cls.IsRegister()) {
7389 __ cmpl(temp, cls.AsRegister<Register>());
7390 } else {
7391 DCHECK(cls.IsStackSlot()) << cls;
7392 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7393 }
7394 // Jump to slow path for throwing the exception or doing a
7395 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007396 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007397 break;
7398 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007399
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007400 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007401 // /* HeapReference<Class> */ temp = obj->klass_
7402 GenerateReferenceLoadTwoRegisters(instruction,
7403 temp_loc,
7404 obj_loc,
7405 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007406 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007407
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007408 // If the class is abstract, we eagerly fetch the super class of the
7409 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007410 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007411 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007412 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007413 GenerateReferenceLoadOneRegister(instruction,
7414 temp_loc,
7415 super_offset,
7416 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007417 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007418
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007419 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7420 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007421 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007422 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007423
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007424 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007425 if (cls.IsRegister()) {
7426 __ cmpl(temp, cls.AsRegister<Register>());
7427 } else {
7428 DCHECK(cls.IsStackSlot()) << cls;
7429 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7430 }
7431 __ j(kNotEqual, &loop);
7432 break;
7433 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007434
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007435 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007436 // /* HeapReference<Class> */ temp = obj->klass_
7437 GenerateReferenceLoadTwoRegisters(instruction,
7438 temp_loc,
7439 obj_loc,
7440 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007441 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007442
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007443 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007444 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007445 __ Bind(&loop);
7446 if (cls.IsRegister()) {
7447 __ cmpl(temp, cls.AsRegister<Register>());
7448 } else {
7449 DCHECK(cls.IsStackSlot()) << cls;
7450 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7451 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007452 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007453
Roland Levillain0d5a2812015-11-13 10:07:31 +00007454 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007455 GenerateReferenceLoadOneRegister(instruction,
7456 temp_loc,
7457 super_offset,
7458 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007459 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007460
7461 // If the class reference currently in `temp` is not null, jump
7462 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007463 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007464 __ j(kNotZero, &loop);
7465 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007466 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007467 break;
7468 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007469
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007470 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007471 // /* HeapReference<Class> */ temp = obj->klass_
7472 GenerateReferenceLoadTwoRegisters(instruction,
7473 temp_loc,
7474 obj_loc,
7475 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007476 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007477
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007478 // Do an exact check.
7479 if (cls.IsRegister()) {
7480 __ cmpl(temp, cls.AsRegister<Register>());
7481 } else {
7482 DCHECK(cls.IsStackSlot()) << cls;
7483 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7484 }
7485 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007486
7487 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007488 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007489 GenerateReferenceLoadOneRegister(instruction,
7490 temp_loc,
7491 component_offset,
7492 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007493 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007494
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007495 // If the component type is null (i.e. the object not an array), jump to the slow path to
7496 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007497 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007498 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007499
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007500 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007501 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007502 break;
7503 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007504
Calin Juravle98893e12015-10-02 21:05:03 +01007505 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007506 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007507 // We cannot directly call the CheckCast runtime entry point
7508 // without resorting to a type checking slow path here (i.e. by
7509 // calling InvokeRuntime directly), as it would require to
7510 // assign fixed registers for the inputs of this HInstanceOf
7511 // instruction (following the runtime calling convention), which
7512 // might be cluttered by the potential first read barrier
7513 // emission at the beginning of this method.
7514 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007515 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007516
7517 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007518 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
7519 // We can not get false positives by doing this.
7520 // /* HeapReference<Class> */ temp = obj->klass_
7521 GenerateReferenceLoadTwoRegisters(instruction,
7522 temp_loc,
7523 obj_loc,
7524 class_offset,
7525 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007526
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007527 // /* HeapReference<Class> */ temp = temp->iftable_
7528 GenerateReferenceLoadTwoRegisters(instruction,
7529 temp_loc,
7530 temp_loc,
7531 iftable_offset,
7532 kWithoutReadBarrier);
7533 // Iftable is never null.
7534 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
7535 // Maybe poison the `cls` for direct comparison with memory.
7536 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
7537 // Loop through the iftable and check if any class matches.
7538 NearLabel start_loop;
7539 __ Bind(&start_loop);
7540 // Need to subtract first to handle the empty array case.
7541 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
7542 __ j(kNegative, type_check_slow_path->GetEntryLabel());
7543 // Go to next interface if the classes do not match.
7544 __ cmpl(cls.AsRegister<Register>(),
7545 CodeGeneratorX86::ArrayAddress(temp,
7546 maybe_temp2_loc,
7547 TIMES_4,
7548 object_array_data_offset));
7549 __ j(kNotEqual, &start_loop);
7550 // If `cls` was poisoned above, unpoison it.
7551 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007552 break;
7553 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007554
7555 case TypeCheckKind::kBitstringCheck: {
7556 // /* HeapReference<Class> */ temp = obj->klass_
7557 GenerateReferenceLoadTwoRegisters(instruction,
7558 temp_loc,
7559 obj_loc,
7560 class_offset,
7561 kWithoutReadBarrier);
7562
7563 GenerateBitstringTypeCheckCompare(instruction, temp);
7564 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
7565 break;
7566 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007567 }
7568 __ Bind(&done);
7569
Roland Levillain0d5a2812015-11-13 10:07:31 +00007570 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007571}
7572
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007573void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007574 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7575 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007576 InvokeRuntimeCallingConvention calling_convention;
7577 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7578}
7579
7580void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007581 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
7582 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01007583 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01007584 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007585 if (instruction->IsEnter()) {
7586 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7587 } else {
7588 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7589 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007590}
7591
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307592void LocationsBuilderX86::VisitX86AndNot(HX86AndNot* instruction) {
7593 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7594 DCHECK(DataType::IsIntOrLongType(instruction->GetType())) << instruction->GetType();
7595 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7596 locations->SetInAt(0, Location::RequiresRegister());
7597 locations->SetInAt(1, Location::RequiresRegister());
7598 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7599}
7600
7601void InstructionCodeGeneratorX86::VisitX86AndNot(HX86AndNot* instruction) {
7602 LocationSummary* locations = instruction->GetLocations();
7603 Location first = locations->InAt(0);
7604 Location second = locations->InAt(1);
7605 Location dest = locations->Out();
7606 if (instruction->GetResultType() == DataType::Type::kInt32) {
7607 __ andn(dest.AsRegister<Register>(),
7608 first.AsRegister<Register>(),
7609 second.AsRegister<Register>());
7610 } else {
7611 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
7612 __ andn(dest.AsRegisterPairLow<Register>(),
7613 first.AsRegisterPairLow<Register>(),
7614 second.AsRegisterPairLow<Register>());
7615 __ andn(dest.AsRegisterPairHigh<Register>(),
7616 first.AsRegisterPairHigh<Register>(),
7617 second.AsRegisterPairHigh<Register>());
7618 }
7619}
7620
7621void LocationsBuilderX86::VisitX86MaskOrResetLeastSetBit(HX86MaskOrResetLeastSetBit* instruction) {
7622 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7623 DCHECK(instruction->GetType() == DataType::Type::kInt32) << instruction->GetType();
7624 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7625 locations->SetInAt(0, Location::RequiresRegister());
7626 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7627}
7628
7629void InstructionCodeGeneratorX86::VisitX86MaskOrResetLeastSetBit(
7630 HX86MaskOrResetLeastSetBit* instruction) {
7631 LocationSummary* locations = instruction->GetLocations();
7632 Location src = locations->InAt(0);
7633 Location dest = locations->Out();
7634 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
7635 switch (instruction->GetOpKind()) {
7636 case HInstruction::kAnd:
7637 __ blsr(dest.AsRegister<Register>(), src.AsRegister<Register>());
7638 break;
7639 case HInstruction::kXor:
7640 __ blsmsk(dest.AsRegister<Register>(), src.AsRegister<Register>());
7641 break;
7642 default:
7643 LOG(FATAL) << "Unreachable";
7644 }
7645}
7646
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007647void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
7648void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
7649void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
7650
7651void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7652 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007653 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007654 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7655 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007656 locations->SetInAt(0, Location::RequiresRegister());
7657 locations->SetInAt(1, Location::Any());
7658 locations->SetOut(Location::SameAsFirstInput());
7659}
7660
7661void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
7662 HandleBitwiseOperation(instruction);
7663}
7664
7665void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
7666 HandleBitwiseOperation(instruction);
7667}
7668
7669void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
7670 HandleBitwiseOperation(instruction);
7671}
7672
7673void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7674 LocationSummary* locations = instruction->GetLocations();
7675 Location first = locations->InAt(0);
7676 Location second = locations->InAt(1);
7677 DCHECK(first.Equals(locations->Out()));
7678
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007679 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007680 if (second.IsRegister()) {
7681 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007682 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007683 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007684 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007685 } else {
7686 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007687 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007688 }
7689 } else if (second.IsConstant()) {
7690 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007691 __ andl(first.AsRegister<Register>(),
7692 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007693 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007694 __ orl(first.AsRegister<Register>(),
7695 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007696 } else {
7697 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00007698 __ xorl(first.AsRegister<Register>(),
7699 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007700 }
7701 } else {
7702 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007703 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007704 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007705 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007706 } else {
7707 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007708 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007709 }
7710 }
7711 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007712 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007713 if (second.IsRegisterPair()) {
7714 if (instruction->IsAnd()) {
7715 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7716 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7717 } else if (instruction->IsOr()) {
7718 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7719 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7720 } else {
7721 DCHECK(instruction->IsXor());
7722 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7723 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7724 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007725 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007726 if (instruction->IsAnd()) {
7727 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7728 __ andl(first.AsRegisterPairHigh<Register>(),
7729 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7730 } else if (instruction->IsOr()) {
7731 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7732 __ orl(first.AsRegisterPairHigh<Register>(),
7733 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7734 } else {
7735 DCHECK(instruction->IsXor());
7736 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7737 __ xorl(first.AsRegisterPairHigh<Register>(),
7738 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7739 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007740 } else {
7741 DCHECK(second.IsConstant()) << second;
7742 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007743 int32_t low_value = Low32Bits(value);
7744 int32_t high_value = High32Bits(value);
7745 Immediate low(low_value);
7746 Immediate high(high_value);
7747 Register first_low = first.AsRegisterPairLow<Register>();
7748 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007749 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007750 if (low_value == 0) {
7751 __ xorl(first_low, first_low);
7752 } else if (low_value != -1) {
7753 __ andl(first_low, low);
7754 }
7755 if (high_value == 0) {
7756 __ xorl(first_high, first_high);
7757 } else if (high_value != -1) {
7758 __ andl(first_high, high);
7759 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007760 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007761 if (low_value != 0) {
7762 __ orl(first_low, low);
7763 }
7764 if (high_value != 0) {
7765 __ orl(first_high, high);
7766 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007767 } else {
7768 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007769 if (low_value != 0) {
7770 __ xorl(first_low, low);
7771 }
7772 if (high_value != 0) {
7773 __ xorl(first_high, high);
7774 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007775 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007776 }
7777 }
7778}
7779
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007780void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7781 HInstruction* instruction,
7782 Location out,
7783 uint32_t offset,
7784 Location maybe_temp,
7785 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007786 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007787 if (read_barrier_option == kWithReadBarrier) {
7788 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007789 if (kUseBakerReadBarrier) {
7790 // Load with fast path based Baker's read barrier.
7791 // /* HeapReference<Object> */ out = *(out + offset)
7792 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08007793 instruction, out, out_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007794 } else {
7795 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007796 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007797 // in the following move operation, as we will need it for the
7798 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007799 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007800 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007801 // /* HeapReference<Object> */ out = *(out + offset)
7802 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007803 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007804 }
7805 } else {
7806 // Plain load with no read barrier.
7807 // /* HeapReference<Object> */ out = *(out + offset)
7808 __ movl(out_reg, Address(out_reg, offset));
7809 __ MaybeUnpoisonHeapReference(out_reg);
7810 }
7811}
7812
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007813void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7814 HInstruction* instruction,
7815 Location out,
7816 Location obj,
7817 uint32_t offset,
7818 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007819 Register out_reg = out.AsRegister<Register>();
7820 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007821 if (read_barrier_option == kWithReadBarrier) {
7822 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007823 if (kUseBakerReadBarrier) {
7824 // Load with fast path based Baker's read barrier.
7825 // /* HeapReference<Object> */ out = *(obj + offset)
7826 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08007827 instruction, out, obj_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007828 } else {
7829 // Load with slow path based read barrier.
7830 // /* HeapReference<Object> */ out = *(obj + offset)
7831 __ movl(out_reg, Address(obj_reg, offset));
7832 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7833 }
7834 } else {
7835 // Plain load with no read barrier.
7836 // /* HeapReference<Object> */ out = *(obj + offset)
7837 __ movl(out_reg, Address(obj_reg, offset));
7838 __ MaybeUnpoisonHeapReference(out_reg);
7839 }
7840}
7841
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007842void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7843 HInstruction* instruction,
7844 Location root,
7845 const Address& address,
7846 Label* fixup_label,
7847 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007848 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007849 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007850 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007851 if (kUseBakerReadBarrier) {
7852 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7853 // Baker's read barrier are used:
7854 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007855 // root = obj.field;
7856 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7857 // if (temp != null) {
7858 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007859 // }
7860
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007861 // /* GcRoot<mirror::Object> */ root = *address
7862 __ movl(root_reg, address);
7863 if (fixup_label != nullptr) {
7864 __ Bind(fixup_label);
7865 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007866 static_assert(
7867 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7868 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7869 "have different sizes.");
7870 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7871 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7872 "have different sizes.");
7873
Vladimir Marko953437b2016-08-24 08:30:46 +00007874 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007875 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007876 instruction, root, /* unpoison_ref_before_marking= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007877 codegen_->AddSlowPath(slow_path);
7878
Roland Levillaind966ce72017-02-09 16:20:14 +00007879 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7880 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007881 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007882 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7883 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007884 __ j(kNotEqual, slow_path->GetEntryLabel());
7885 __ Bind(slow_path->GetExitLabel());
7886 } else {
7887 // GC root loaded through a slow path for read barriers other
7888 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007889 // /* GcRoot<mirror::Object>* */ root = address
7890 __ leal(root_reg, address);
7891 if (fixup_label != nullptr) {
7892 __ Bind(fixup_label);
7893 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007894 // /* mirror::Object* */ root = root->Read()
7895 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7896 }
7897 } else {
7898 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007899 // /* GcRoot<mirror::Object> */ root = *address
7900 __ movl(root_reg, address);
7901 if (fixup_label != nullptr) {
7902 __ Bind(fixup_label);
7903 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007904 // Note that GC roots are not affected by heap poisoning, thus we
7905 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007906 }
7907}
7908
7909void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7910 Location ref,
7911 Register obj,
7912 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007913 bool needs_null_check) {
7914 DCHECK(kEmitCompilerReadBarrier);
7915 DCHECK(kUseBakerReadBarrier);
7916
7917 // /* HeapReference<Object> */ ref = *(obj + offset)
7918 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007919 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007920}
7921
7922void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7923 Location ref,
7924 Register obj,
7925 uint32_t data_offset,
7926 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007927 bool needs_null_check) {
7928 DCHECK(kEmitCompilerReadBarrier);
7929 DCHECK(kUseBakerReadBarrier);
7930
Roland Levillain3d312422016-06-23 13:53:42 +01007931 static_assert(
7932 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7933 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007934 // /* HeapReference<Object> */ ref =
7935 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007936 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007937 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007938}
7939
7940void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7941 Location ref,
7942 Register obj,
7943 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007944 bool needs_null_check,
7945 bool always_update_field,
7946 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007947 DCHECK(kEmitCompilerReadBarrier);
7948 DCHECK(kUseBakerReadBarrier);
7949
7950 // In slow path based read barriers, the read barrier call is
7951 // inserted after the original load. However, in fast path based
7952 // Baker's read barriers, we need to perform the load of
7953 // mirror::Object::monitor_ *before* the original reference load.
7954 // This load-load ordering is required by the read barrier.
7955 // The fast path/slow path (for Baker's algorithm) should look like:
7956 //
7957 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7958 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7959 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007960 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007961 // if (is_gray) {
7962 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7963 // }
7964 //
7965 // Note: the original implementation in ReadBarrier::Barrier is
7966 // slightly more complex as:
7967 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007968 // the high-bits of rb_state, which are expected to be all zeroes
7969 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7970 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007971 // - it performs additional checks that we do not do here for
7972 // performance reasons.
7973
7974 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007975 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7976
Vladimir Marko953437b2016-08-24 08:30:46 +00007977 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01007978 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007979 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007980 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7981 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7982 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7983
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007984 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007985 // ref = ReadBarrier::Mark(ref);
7986 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7987 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007988 if (needs_null_check) {
7989 MaybeRecordImplicitNullCheck(instruction);
7990 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007991
7992 // Load fence to prevent load-load reordering.
7993 // Note that this is a no-op, thanks to the x86 memory model.
7994 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7995
7996 // The actual reference load.
7997 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007998 __ movl(ref_reg, src); // Flags are unaffected.
7999
8000 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
8001 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008002 SlowPathCode* slow_path;
8003 if (always_update_field) {
8004 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01008005 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008006 instruction, ref, obj, src, /* unpoison_ref_before_marking= */ true, *temp);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008007 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008008 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008009 instruction, ref, /* unpoison_ref_before_marking= */ true);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008010 }
Vladimir Marko953437b2016-08-24 08:30:46 +00008011 AddSlowPath(slow_path);
8012
8013 // We have done the "if" of the gray bit check above, now branch based on the flags.
8014 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00008015
8016 // Object* ref = ref_addr->AsMirrorPtr()
8017 __ MaybeUnpoisonHeapReference(ref_reg);
8018
Roland Levillain7c1559a2015-12-15 10:55:36 +00008019 __ Bind(slow_path->GetExitLabel());
8020}
8021
8022void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
8023 Location out,
8024 Location ref,
8025 Location obj,
8026 uint32_t offset,
8027 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008028 DCHECK(kEmitCompilerReadBarrier);
8029
Roland Levillain7c1559a2015-12-15 10:55:36 +00008030 // Insert a slow path based read barrier *after* the reference load.
8031 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008032 // If heap poisoning is enabled, the unpoisoning of the loaded
8033 // reference will be carried out by the runtime within the slow
8034 // path.
8035 //
8036 // Note that `ref` currently does not get unpoisoned (when heap
8037 // poisoning is enabled), which is alright as the `ref` argument is
8038 // not used by the artReadBarrierSlow entry point.
8039 //
8040 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008041 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00008042 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
8043 AddSlowPath(slow_path);
8044
Roland Levillain0d5a2812015-11-13 10:07:31 +00008045 __ jmp(slow_path->GetEntryLabel());
8046 __ Bind(slow_path->GetExitLabel());
8047}
8048
Roland Levillain7c1559a2015-12-15 10:55:36 +00008049void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
8050 Location out,
8051 Location ref,
8052 Location obj,
8053 uint32_t offset,
8054 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008055 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008056 // Baker's read barriers shall be handled by the fast path
8057 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
8058 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008059 // If heap poisoning is enabled, unpoisoning will be taken care of
8060 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008061 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008062 } else if (kPoisonHeapReferences) {
8063 __ UnpoisonHeapReference(out.AsRegister<Register>());
8064 }
8065}
8066
Roland Levillain7c1559a2015-12-15 10:55:36 +00008067void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8068 Location out,
8069 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008070 DCHECK(kEmitCompilerReadBarrier);
8071
Roland Levillain7c1559a2015-12-15 10:55:36 +00008072 // Insert a slow path based read barrier *after* the GC root load.
8073 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008074 // Note that GC roots are not affected by heap poisoning, so we do
8075 // not need to do anything special for this here.
8076 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008077 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008078 AddSlowPath(slow_path);
8079
Roland Levillain0d5a2812015-11-13 10:07:31 +00008080 __ jmp(slow_path->GetEntryLabel());
8081 __ Bind(slow_path->GetExitLabel());
8082}
8083
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008084void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008085 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008086 LOG(FATAL) << "Unreachable";
8087}
8088
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008089void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008090 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008091 LOG(FATAL) << "Unreachable";
8092}
8093
Mark Mendellfe57faa2015-09-18 09:26:15 -04008094// Simple implementation of packed switch - generate cascaded compare/jumps.
8095void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8096 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008097 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04008098 locations->SetInAt(0, Location::RequiresRegister());
8099}
8100
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008101void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
8102 int32_t lower_bound,
8103 uint32_t num_entries,
8104 HBasicBlock* switch_block,
8105 HBasicBlock* default_block) {
8106 // Figure out the correct compare values and jump conditions.
8107 // Handle the first compare/branch as a special case because it might
8108 // jump to the default case.
8109 DCHECK_GT(num_entries, 2u);
8110 Condition first_condition;
8111 uint32_t index;
8112 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
8113 if (lower_bound != 0) {
8114 first_condition = kLess;
8115 __ cmpl(value_reg, Immediate(lower_bound));
8116 __ j(first_condition, codegen_->GetLabelOf(default_block));
8117 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008118
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008119 index = 1;
8120 } else {
8121 // Handle all the compare/jumps below.
8122 first_condition = kBelow;
8123 index = 0;
8124 }
8125
8126 // Handle the rest of the compare/jumps.
8127 for (; index + 1 < num_entries; index += 2) {
8128 int32_t compare_to_value = lower_bound + index + 1;
8129 __ cmpl(value_reg, Immediate(compare_to_value));
8130 // Jump to successors[index] if value < case_value[index].
8131 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
8132 // Jump to successors[index + 1] if value == case_value[index + 1].
8133 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
8134 }
8135
8136 if (index != num_entries) {
8137 // There are an odd number of entries. Handle the last one.
8138 DCHECK_EQ(index + 1, num_entries);
8139 __ cmpl(value_reg, Immediate(lower_bound + index));
8140 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008141 }
8142
8143 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008144 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
8145 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008146 }
8147}
8148
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008149void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8150 int32_t lower_bound = switch_instr->GetStartValue();
8151 uint32_t num_entries = switch_instr->GetNumEntries();
8152 LocationSummary* locations = switch_instr->GetLocations();
8153 Register value_reg = locations->InAt(0).AsRegister<Register>();
8154
8155 GenPackedSwitchWithCompares(value_reg,
8156 lower_bound,
8157 num_entries,
8158 switch_instr->GetBlock(),
8159 switch_instr->GetDefaultBlock());
8160}
8161
Mark Mendell805b3b52015-09-18 14:10:29 -04008162void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8163 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008164 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04008165 locations->SetInAt(0, Location::RequiresRegister());
8166
8167 // Constant area pointer.
8168 locations->SetInAt(1, Location::RequiresRegister());
8169
8170 // And the temporary we need.
8171 locations->AddTemp(Location::RequiresRegister());
8172}
8173
8174void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8175 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008176 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04008177 LocationSummary* locations = switch_instr->GetLocations();
8178 Register value_reg = locations->InAt(0).AsRegister<Register>();
8179 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8180
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008181 if (num_entries <= kPackedSwitchJumpTableThreshold) {
8182 GenPackedSwitchWithCompares(value_reg,
8183 lower_bound,
8184 num_entries,
8185 switch_instr->GetBlock(),
8186 default_block);
8187 return;
8188 }
8189
Mark Mendell805b3b52015-09-18 14:10:29 -04008190 // Optimizing has a jump area.
8191 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
8192 Register constant_area = locations->InAt(1).AsRegister<Register>();
8193
8194 // Remove the bias, if needed.
8195 if (lower_bound != 0) {
8196 __ leal(temp_reg, Address(value_reg, -lower_bound));
8197 value_reg = temp_reg;
8198 }
8199
8200 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008201 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04008202 __ cmpl(value_reg, Immediate(num_entries - 1));
8203 __ j(kAbove, codegen_->GetLabelOf(default_block));
8204
8205 // We are in the range of the table.
8206 // Load (target-constant_area) from the jump table, indexing by the value.
8207 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
8208
8209 // Compute the actual target address by adding in constant_area.
8210 __ addl(temp_reg, constant_area);
8211
8212 // And jump.
8213 __ jmp(temp_reg);
8214}
8215
Mark Mendell0616ae02015-04-17 12:49:27 -04008216void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
8217 HX86ComputeBaseMethodAddress* insn) {
8218 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008219 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008220 locations->SetOut(Location::RequiresRegister());
8221}
8222
8223void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
8224 HX86ComputeBaseMethodAddress* insn) {
8225 LocationSummary* locations = insn->GetLocations();
8226 Register reg = locations->Out().AsRegister<Register>();
8227
8228 // Generate call to next instruction.
8229 Label next_instruction;
8230 __ call(&next_instruction);
8231 __ Bind(&next_instruction);
8232
8233 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008234 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04008235
8236 // Grab the return address off the stack.
8237 __ popl(reg);
8238}
8239
8240void LocationsBuilderX86::VisitX86LoadFromConstantTable(
8241 HX86LoadFromConstantTable* insn) {
8242 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008243 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008244
8245 locations->SetInAt(0, Location::RequiresRegister());
8246 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
8247
8248 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00008249 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008250 return;
8251 }
8252
8253 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008254 case DataType::Type::kFloat32:
8255 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008256 locations->SetOut(Location::RequiresFpuRegister());
8257 break;
8258
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008259 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008260 locations->SetOut(Location::RequiresRegister());
8261 break;
8262
8263 default:
8264 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8265 }
8266}
8267
8268void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00008269 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008270 return;
8271 }
8272
8273 LocationSummary* locations = insn->GetLocations();
8274 Location out = locations->Out();
8275 Register const_area = locations->InAt(0).AsRegister<Register>();
8276 HConstant *value = insn->GetConstant();
8277
8278 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008279 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008280 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008281 codegen_->LiteralFloatAddress(
8282 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008283 break;
8284
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008285 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008286 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008287 codegen_->LiteralDoubleAddress(
8288 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008289 break;
8290
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008291 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008292 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008293 codegen_->LiteralInt32Address(
8294 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008295 break;
8296
8297 default:
8298 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8299 }
8300}
8301
Mark Mendell0616ae02015-04-17 12:49:27 -04008302/**
8303 * Class to handle late fixup of offsets into constant area.
8304 */
Vladimir Marko5233f932015-09-29 19:01:15 +01008305class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04008306 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008307 RIPFixup(CodeGeneratorX86& codegen,
8308 HX86ComputeBaseMethodAddress* base_method_address,
8309 size_t offset)
8310 : codegen_(&codegen),
8311 base_method_address_(base_method_address),
8312 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008313
8314 protected:
8315 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
8316
8317 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008318 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008319
8320 private:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008321 void Process(const MemoryRegion& region, int pos) override {
Mark Mendell0616ae02015-04-17 12:49:27 -04008322 // Patch the correct offset for the instruction. The place to patch is the
8323 // last 4 bytes of the instruction.
8324 // The value to patch is the distance from the offset in the constant area
8325 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04008326 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008327 int32_t relative_position =
8328 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04008329
8330 // Patch in the right value.
8331 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
8332 }
8333
Mark Mendell0616ae02015-04-17 12:49:27 -04008334 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04008335 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008336};
8337
Mark Mendell805b3b52015-09-18 14:10:29 -04008338/**
8339 * Class to handle late fixup of offsets to a jump table that will be created in the
8340 * constant area.
8341 */
8342class JumpTableRIPFixup : public RIPFixup {
8343 public:
8344 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008345 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
8346 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008347
8348 void CreateJumpTable() {
8349 X86Assembler* assembler = codegen_->GetAssembler();
8350
8351 // Ensure that the reference to the jump table has the correct offset.
8352 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
8353 SetOffset(offset_in_constant_table);
8354
8355 // The label values in the jump table are computed relative to the
8356 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008357 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04008358
8359 // Populate the jump table with the correct values for the jump table.
8360 int32_t num_entries = switch_instr_->GetNumEntries();
8361 HBasicBlock* block = switch_instr_->GetBlock();
8362 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
8363 // The value that we want is the target offset - the position of the table.
8364 for (int32_t i = 0; i < num_entries; i++) {
8365 HBasicBlock* b = successors[i];
8366 Label* l = codegen_->GetLabelOf(b);
8367 DCHECK(l->IsBound());
8368 int32_t offset_to_block = l->Position() - relative_offset;
8369 assembler->AppendInt32(offset_to_block);
8370 }
8371 }
8372
8373 private:
8374 const HX86PackedSwitch* switch_instr_;
8375};
8376
8377void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
8378 // Generate the constant area if needed.
8379 X86Assembler* assembler = GetAssembler();
jaishank20d1c942019-03-08 15:08:17 +05308380
Mark Mendell805b3b52015-09-18 14:10:29 -04008381 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
8382 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
8383 // byte values.
8384 assembler->Align(4, 0);
8385 constant_area_start_ = assembler->CodeSize();
8386
8387 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008388 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04008389 jump_table->CreateJumpTable();
8390 }
8391
8392 // And now add the constant area to the generated code.
8393 assembler->AddConstantArea();
8394 }
8395
8396 // And finish up.
8397 CodeGenerator::Finalize(allocator);
8398}
8399
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008400Address CodeGeneratorX86::LiteralDoubleAddress(double v,
8401 HX86ComputeBaseMethodAddress* method_base,
8402 Register reg) {
8403 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008404 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008405 return Address(reg, kDummy32BitOffset, fixup);
8406}
8407
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008408Address CodeGeneratorX86::LiteralFloatAddress(float v,
8409 HX86ComputeBaseMethodAddress* method_base,
8410 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008411 AssemblerFixup* fixup =
8412 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008413 return Address(reg, kDummy32BitOffset, fixup);
8414}
8415
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008416Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
8417 HX86ComputeBaseMethodAddress* method_base,
8418 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008419 AssemblerFixup* fixup =
8420 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008421 return Address(reg, kDummy32BitOffset, fixup);
8422}
8423
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008424Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
8425 HX86ComputeBaseMethodAddress* method_base,
8426 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008427 AssemblerFixup* fixup =
8428 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Mark Mendell0616ae02015-04-17 12:49:27 -04008429 return Address(reg, kDummy32BitOffset, fixup);
8430}
8431
Aart Bika19616e2016-02-01 18:57:58 -08008432void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
8433 if (value == 0) {
8434 __ xorl(dest, dest);
8435 } else {
8436 __ movl(dest, Immediate(value));
8437 }
8438}
8439
8440void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
8441 if (value == 0) {
8442 __ testl(dest, dest);
8443 } else {
8444 __ cmpl(dest, Immediate(value));
8445 }
8446}
8447
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008448void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
8449 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07008450 GenerateIntCompare(lhs_reg, rhs);
8451}
8452
8453void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008454 if (rhs.IsConstant()) {
8455 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07008456 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008457 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07008458 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008459 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07008460 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008461 }
8462}
8463
8464Address CodeGeneratorX86::ArrayAddress(Register obj,
8465 Location index,
8466 ScaleFactor scale,
8467 uint32_t data_offset) {
8468 return index.IsConstant() ?
8469 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
8470 Address(obj, index.AsRegister<Register>(), scale, data_offset);
8471}
8472
Mark Mendell805b3b52015-09-18 14:10:29 -04008473Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
8474 Register reg,
8475 Register value) {
8476 // Create a fixup to be used to create and address the jump table.
8477 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008478 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04008479
8480 // We have to populate the jump tables.
8481 fixups_to_jump_tables_.push_back(table_fixup);
8482
8483 // We want a scaled address, as we are extracting the correct offset from the table.
8484 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
8485}
8486
Andreas Gampe85b62f22015-09-09 13:15:38 -07008487// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008488void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07008489 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008490 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008491 return;
8492 }
8493
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008494 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008495
8496 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
8497 if (target.Equals(return_loc)) {
8498 return;
8499 }
8500
8501 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8502 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008503 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008504 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008505 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
8506 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008507 GetMoveResolver()->EmitNativeCode(&parallel_move);
8508 } else {
8509 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01008510 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07008511 parallel_move.AddMove(return_loc, target, type, nullptr);
8512 GetMoveResolver()->EmitNativeCode(&parallel_move);
8513 }
8514}
8515
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008516void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
8517 const uint8_t* roots_data,
8518 const PatchInfo<Label>& info,
8519 uint64_t index_in_table) const {
8520 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
8521 uintptr_t address =
8522 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
Andreas Gampec55bb392018-09-21 00:02:02 +00008523 using unaligned_uint32_t __attribute__((__aligned__(1))) = uint32_t;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008524 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
8525 dchecked_integral_cast<uint32_t>(address);
8526}
8527
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008528void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8529 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008530 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008531 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008532 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008533 }
8534
8535 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008536 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008537 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008538 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008539 }
8540}
8541
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008542void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8543 ATTRIBUTE_UNUSED) {
8544 LOG(FATAL) << "Unreachable";
8545}
8546
8547void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8548 ATTRIBUTE_UNUSED) {
8549 LOG(FATAL) << "Unreachable";
8550}
8551
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05308552bool LocationsBuilderX86::CpuHasAvxFeatureFlag() {
8553 return codegen_->GetInstructionSetFeatures().HasAVX();
8554}
8555bool LocationsBuilderX86::CpuHasAvx2FeatureFlag() {
8556 return codegen_->GetInstructionSetFeatures().HasAVX2();
8557}
8558bool InstructionCodeGeneratorX86::CpuHasAvxFeatureFlag() {
8559 return codegen_->GetInstructionSetFeatures().HasAVX();
8560}
8561bool InstructionCodeGeneratorX86::CpuHasAvx2FeatureFlag() {
8562 return codegen_->GetInstructionSetFeatures().HasAVX2();
8563}
8564
Roland Levillain4d027112015-07-01 15:41:14 +01008565#undef __
8566
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008567} // namespace x86
8568} // namespace art