blob: ce27083d0076d53467fb81a3eebafaabc33a4c49 [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
Vladimir Marko86c87522020-05-11 16:55:55 +010019#include "arch/x86/jni_frame_x86.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000020#include "art_method-inl.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010021#include "class_table.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010022#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000023#include "compiled_method.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010024#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000025#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010026#include "gc/accounting/card_table.h"
Vladimir Markoeebb8212018-06-05 14:57:24 +010027#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070028#include "heap_poisoning.h"
Nicolas Geoffray8b8d93d2020-09-17 14:30:01 +010029#include "interpreter/mterp/nterp.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040030#include "intrinsics.h"
Ulya Trafimovichec696e52022-01-26 10:21:32 +000031#include "intrinsics_utils.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040032#include "intrinsics_x86.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000033#include "jit/profiling_info.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010034#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070035#include "lock_word.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070036#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070037#include "mirror/class-inl.h"
Andra Danciu52d2c0c2020-09-15 14:27:21 +000038#include "mirror/var_handle.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000039#include "scoped_thread_state_change-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010040#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000041#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010042#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000043#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010044#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000045
Vladimir Marko0a516052019-10-14 13:00:44 +000046namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010047
Roland Levillain0d5a2812015-11-13 10:07:31 +000048template<class MirrorType>
49class GcRoot;
50
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000051namespace x86 {
52
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010053static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010054static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050055static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010056
Mark Mendell24f2dfa2015-01-14 19:51:45 -050057static constexpr int kC2ConditionMask = 0x400;
58
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000059static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000060
Aart Bik1f8d51b2018-02-15 10:42:37 -080061static constexpr int64_t kDoubleNaN = INT64_C(0x7FF8000000000000);
62static constexpr int32_t kFloatNaN = INT32_C(0x7FC00000);
63
Vladimir Marko3232dbb2018-07-25 15:42:46 +010064static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
65 InvokeRuntimeCallingConvention calling_convention;
66 RegisterSet caller_saves = RegisterSet::Empty();
67 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
68 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
69 // that the the kPrimNot result register is the same as the first argument register.
70 return caller_saves;
71}
72
Roland Levillain7cbd27f2016-08-11 23:53:33 +010073// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
74#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070075#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076
Andreas Gampe85b62f22015-09-09 13:15:38 -070077class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010078 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000079 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010081 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +010082 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010083 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000084 if (instruction_->CanThrowIntoCatchBlock()) {
85 // Live registers will be restored in the catch block if caught.
86 SaveLiveRegisters(codegen, instruction_->GetLocations());
87 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010088 x86_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexandre Rames8158f282015-08-07 10:26:17 +010089 instruction_,
90 instruction_->GetDexPc(),
91 this);
Roland Levillain888d0672015-11-23 18:53:50 +000092 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010093 }
94
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010095 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +010096
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010097 const char* GetDescription() const override { return "NullCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +010098
Nicolas Geoffraye5038322014-07-04 09:41:32 +010099 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100100 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
101};
102
Andreas Gampe85b62f22015-09-09 13:15:38 -0700103class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000104 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000105 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000106
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100107 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +0100108 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +0000109 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100110 x86_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000111 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000112 }
113
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100114 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100115
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100116 const char* GetDescription() const override { return "DivZeroCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100117
Calin Juravled0d48522014-11-04 16:40:20 +0000118 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000119 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
120};
121
Andreas Gampe85b62f22015-09-09 13:15:38 -0700122class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000123 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000124 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
125 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000126
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100127 void EmitNativeCode(CodeGenerator* codegen) override {
Calin Juravled0d48522014-11-04 16:40:20 +0000128 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000129 if (is_div_) {
130 __ negl(reg_);
131 } else {
132 __ movl(reg_, Immediate(0));
133 }
Calin Juravled0d48522014-11-04 16:40:20 +0000134 __ jmp(GetExitLabel());
135 }
136
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100137 const char* GetDescription() const override { return "DivRemMinusOneSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100138
Calin Juravled0d48522014-11-04 16:40:20 +0000139 private:
140 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000141 bool is_div_;
142 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000143};
144
Andreas Gampe85b62f22015-09-09 13:15:38 -0700145class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100146 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000147 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100148
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100149 void EmitNativeCode(CodeGenerator* codegen) override {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100150 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100151 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100152 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000153 if (instruction_->CanThrowIntoCatchBlock()) {
154 // Live registers will be restored in the catch block if caught.
Vladimir Markod77cf742022-04-12 10:46:28 +0100155 SaveLiveRegisters(codegen, locations);
David Brazdil77a48ae2015-09-15 12:34:04 +0000156 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400157
Vladimir Markod77cf742022-04-12 10:46:28 +0100158 Location index_loc = locations->InAt(0);
Mark Mendellee8d9712016-07-12 11:13:15 -0400159 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100160 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markod77cf742022-04-12 10:46:28 +0100161 Location index_arg = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
162 Location length_arg = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
163
164 // Are we using an array length from memory?
165 if (!length_loc.IsValid()) {
166 DCHECK(instruction_->InputAt(1)->IsArrayLength());
167 HArrayLength* array_length = instruction_->InputAt(1)->AsArrayLength();
168 DCHECK(array_length->IsEmittedAtUseSite());
169 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -0400170 Location array_loc = array_length->GetLocations()->InAt(0);
Vladimir Markod77cf742022-04-12 10:46:28 +0100171 if (!index_loc.Equals(length_arg)) {
172 // The index is not clobbered by loading the length directly to `length_arg`.
173 __ movl(length_arg.AsRegister<Register>(),
174 Address(array_loc.AsRegister<Register>(), len_offset));
175 x86_codegen->Move32(index_arg, index_loc);
176 } else if (!array_loc.Equals(index_arg)) {
177 // The array reference is not clobbered by the index move.
178 x86_codegen->Move32(index_arg, index_loc);
179 __ movl(length_arg.AsRegister<Register>(),
180 Address(array_loc.AsRegister<Register>(), len_offset));
181 } else {
182 // We do not have a temporary we could use, so swap the registers using the
183 // parallel move resolver and replace the array with the length afterwards.
184 codegen->EmitParallelMoves(
185 index_loc,
186 index_arg,
187 DataType::Type::kInt32,
188 array_loc,
189 length_arg,
190 DataType::Type::kReference);
191 __ movl(length_arg.AsRegister<Register>(),
192 Address(length_arg.AsRegister<Register>(), len_offset));
Mark Mendellee8d9712016-07-12 11:13:15 -0400193 }
Vladimir Markod77cf742022-04-12 10:46:28 +0100194 if (mirror::kUseStringCompression && array_length->IsStringLength()) {
195 __ shrl(length_arg.AsRegister<Register>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700196 }
Vladimir Markod77cf742022-04-12 10:46:28 +0100197 } else {
198 // We're moving two locations to locations that could overlap,
199 // so we need a parallel move resolver.
200 codegen->EmitParallelMoves(
201 index_loc,
202 index_arg,
203 DataType::Type::kInt32,
204 length_loc,
205 length_arg,
206 DataType::Type::kInt32);
Mark Mendellee8d9712016-07-12 11:13:15 -0400207 }
Vladimir Markod77cf742022-04-12 10:46:28 +0100208
Serban Constantinescuba45db02016-07-12 22:53:02 +0100209 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
210 ? kQuickThrowStringBounds
211 : kQuickThrowArrayBounds;
212 x86_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100213 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000214 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100215 }
216
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100217 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100218
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100219 const char* GetDescription() const override { return "BoundsCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100220
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100221 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100222 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
223};
224
Andreas Gampe85b62f22015-09-09 13:15:38 -0700225class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000226 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000227 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000228 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000229
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100230 void EmitNativeCode(CodeGenerator* codegen) override {
Aart Bikb13c65b2017-03-21 20:14:07 -0700231 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100232 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000233 __ Bind(GetEntryLabel());
Aart Bik24b905f2017-04-06 09:59:06 -0700234 SaveLiveRegisters(codegen, locations); // Only saves full width XMM for SIMD.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100235 x86_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000236 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Aart Bik24b905f2017-04-06 09:59:06 -0700237 RestoreLiveRegisters(codegen, locations); // Only restores full width XMM for SIMD.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100238 if (successor_ == nullptr) {
239 __ jmp(GetReturnLabel());
240 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100241 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100242 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000243 }
244
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100245 Label* GetReturnLabel() {
246 DCHECK(successor_ == nullptr);
247 return &return_label_;
248 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000249
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100250 HBasicBlock* GetSuccessor() const {
251 return successor_;
252 }
253
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100254 const char* GetDescription() const override { return "SuspendCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100255
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000256 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100257 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000258 Label return_label_;
259
260 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
261};
262
Vladimir Markoaad75c62016-10-03 08:46:48 +0000263class LoadStringSlowPathX86 : public SlowPathCode {
264 public:
265 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
266
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100267 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000268 LocationSummary* locations = instruction_->GetLocations();
269 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
270
271 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
272 __ Bind(GetEntryLabel());
273 SaveLiveRegisters(codegen, locations);
274
275 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000276 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
277 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index.index_));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000278 x86_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
279 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
280 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
281 RestoreLiveRegisters(codegen, locations);
282
Vladimir Markoaad75c62016-10-03 08:46:48 +0000283 __ jmp(GetExitLabel());
284 }
285
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100286 const char* GetDescription() const override { return "LoadStringSlowPathX86"; }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000287
288 private:
289 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
290};
291
Andreas Gampe85b62f22015-09-09 13:15:38 -0700292class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000293 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100294 LoadClassSlowPathX86(HLoadClass* cls, HInstruction* at)
295 : SlowPathCode(at), cls_(cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000296 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100297 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000298 }
299
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100300 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000301 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100302 Location out = locations->Out();
303 const uint32_t dex_pc = instruction_->GetDexPc();
304 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
305 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
306
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000307 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
308 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000309 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000310
311 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100312 if (must_resolve_type) {
Santiago Aboy Solanesa0232ad2021-11-08 17:00:06 +0000313 DCHECK(IsSameDexFile(cls_->GetDexFile(), x86_codegen->GetGraph()->GetDexFile()) ||
Santiago Aboy Solanes69a87e32022-03-08 16:43:54 +0000314 x86_codegen->GetCompilerOptions().WithinOatFile(&cls_->GetDexFile()) ||
315 ContainsElement(Runtime::Current()->GetClassLinker()->GetBootClassPath(),
316 &cls_->GetDexFile()));
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100317 dex::TypeIndex type_index = cls_->GetTypeIndex();
318 __ movl(calling_convention.GetRegisterAt(0), Immediate(type_index.index_));
Vladimir Marko8f63f102020-09-28 12:10:28 +0100319 if (cls_->NeedsAccessCheck()) {
320 CheckEntrypointTypes<kQuickResolveTypeAndVerifyAccess, void*, uint32_t>();
321 x86_codegen->InvokeRuntime(kQuickResolveTypeAndVerifyAccess, instruction_, dex_pc, this);
322 } else {
323 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
324 x86_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
325 }
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100326 // If we also must_do_clinit, the resolved type is now in the correct register.
327 } else {
328 DCHECK(must_do_clinit);
329 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
330 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), source);
331 }
332 if (must_do_clinit) {
333 x86_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
334 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Roland Levillain888d0672015-11-23 18:53:50 +0000335 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000336
337 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000338 if (out.IsValid()) {
339 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
340 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000341 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000342 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000343 __ jmp(GetExitLabel());
344 }
345
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100346 const char* GetDescription() const override { return "LoadClassSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100347
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000348 private:
349 // The class this slow path will load.
350 HLoadClass* const cls_;
351
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000352 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
353};
354
Andreas Gampe85b62f22015-09-09 13:15:38 -0700355class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000356 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000357 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000358 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000359
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100360 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000361 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000362 DCHECK(instruction_->IsCheckCast()
363 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000364
365 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
366 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000367
Vladimir Markoe619f6c2017-12-12 16:00:01 +0000368 if (kPoisonHeapReferences &&
369 instruction_->IsCheckCast() &&
370 instruction_->AsCheckCast()->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) {
371 // First, unpoison the `cls` reference that was poisoned for direct memory comparison.
372 __ UnpoisonHeapReference(locations->InAt(1).AsRegister<Register>());
373 }
374
Vladimir Marko87584542017-12-12 17:47:52 +0000375 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000376 SaveLiveRegisters(codegen, locations);
377 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000378
379 // We're moving two locations to locations that could overlap, so we need a parallel
380 // move resolver.
381 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800382 x86_codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800383 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100384 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800385 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800386 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100387 DataType::Type::kReference);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000388 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100389 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100390 instruction_,
391 instruction_->GetDexPc(),
392 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800393 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000394 } else {
395 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800396 x86_codegen->InvokeRuntime(kQuickCheckInstanceOf,
397 instruction_,
398 instruction_->GetDexPc(),
399 this);
400 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000401 }
402
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000403 if (!is_fatal_) {
404 if (instruction_->IsInstanceOf()) {
405 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
406 }
407 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000408
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000409 __ jmp(GetExitLabel());
410 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000411 }
412
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100413 const char* GetDescription() const override { return "TypeCheckSlowPathX86"; }
414 bool IsFatal() const override { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100415
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000416 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000417 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000418
419 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
420};
421
Andreas Gampe85b62f22015-09-09 13:15:38 -0700422class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700423 public:
Aart Bik42249c32016-01-07 15:33:50 -0800424 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000425 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700426
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100427 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +0100428 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700429 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100430 LocationSummary* locations = instruction_->GetLocations();
431 SaveLiveRegisters(codegen, locations);
432 InvokeRuntimeCallingConvention calling_convention;
433 x86_codegen->Load32BitValue(
434 calling_convention.GetRegisterAt(0),
435 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100436 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100437 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700438 }
439
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100440 const char* GetDescription() const override { return "DeoptimizationSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100441
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700442 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700443 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
444};
445
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100446class ArraySetSlowPathX86 : public SlowPathCode {
447 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000448 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100449
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100450 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100451 LocationSummary* locations = instruction_->GetLocations();
452 __ Bind(GetEntryLabel());
453 SaveLiveRegisters(codegen, locations);
454
455 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100456 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100457 parallel_move.AddMove(
458 locations->InAt(0),
459 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100460 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100461 nullptr);
462 parallel_move.AddMove(
463 locations->InAt(1),
464 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100465 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100466 nullptr);
467 parallel_move.AddMove(
468 locations->InAt(2),
469 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100470 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100471 nullptr);
472 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
473
474 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100475 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000476 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100477 RestoreLiveRegisters(codegen, locations);
478 __ jmp(GetExitLabel());
479 }
480
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100481 const char* GetDescription() const override { return "ArraySetSlowPathX86"; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100482
483 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100484 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
485};
486
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100487// Slow path marking an object reference `ref` during a read
488// barrier. The field `obj.field` in the object `obj` holding this
489// reference does not get updated by this slow path after marking (see
490// ReadBarrierMarkAndUpdateFieldSlowPathX86 below for that).
491//
492// This means that after the execution of this slow path, `ref` will
493// always be up-to-date, but `obj.field` may not; i.e., after the
494// flip, `ref` will be a to-space reference, but `obj.field` will
495// probably still be a from-space reference (unless it gets updated by
496// another thread, or if another thread installed another object
497// reference (different from `ref`) in `obj.field`).
Roland Levillain7c1559a2015-12-15 10:55:36 +0000498class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
499 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100500 ReadBarrierMarkSlowPathX86(HInstruction* instruction,
501 Location ref,
502 bool unpoison_ref_before_marking)
503 : SlowPathCode(instruction),
504 ref_(ref),
505 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +0000506 DCHECK(gUseReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000507 }
508
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100509 const char* GetDescription() const override { return "ReadBarrierMarkSlowPathX86"; }
Roland Levillain7c1559a2015-12-15 10:55:36 +0000510
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100511 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000512 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100513 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000514 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100515 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000516 DCHECK(instruction_->IsInstanceFieldGet() ||
Alex Light3a73ffb2021-01-25 14:11:05 +0000517 instruction_->IsPredicatedInstanceFieldGet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000518 instruction_->IsStaticFieldGet() ||
519 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100520 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000521 instruction_->IsLoadClass() ||
522 instruction_->IsLoadString() ||
523 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100524 instruction_->IsCheckCast() ||
Andra Danciu1ca6f322020-08-12 08:58:07 +0000525 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000526 << "Unexpected instruction in read barrier marking slow path: "
527 << instruction_->DebugName();
528
529 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100530 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000531 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100532 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000533 }
Roland Levillain4359e612016-07-20 11:32:19 +0100534 // No need to save live registers; it's taken care of by the
535 // entrypoint. Also, there is no need to update the stack mask,
536 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000537 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100538 DCHECK_NE(ref_reg, ESP);
539 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100540 // "Compact" slow path, saving two moves.
541 //
542 // Instead of using the standard runtime calling convention (input
543 // and output in EAX):
544 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100545 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100546 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100547 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100548 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100549 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100550 // of a dedicated entrypoint:
551 //
552 // rX <- ReadBarrierMarkRegX(rX)
553 //
Roland Levillain97c46462017-05-11 14:04:03 +0100554 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100555 // This runtime call does not require a stack map.
556 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000557 __ jmp(GetExitLabel());
558 }
559
560 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100561 // The location (register) of the marked object reference.
562 const Location ref_;
563 // Should the reference in `ref_` be unpoisoned prior to marking it?
564 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000565
566 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
567};
568
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100569// Slow path marking an object reference `ref` during a read barrier,
570// and if needed, atomically updating the field `obj.field` in the
571// object `obj` holding this reference after marking (contrary to
572// ReadBarrierMarkSlowPathX86 above, which never tries to update
573// `obj.field`).
574//
575// This means that after the execution of this slow path, both `ref`
576// and `obj.field` will be up-to-date; i.e., after the flip, both will
577// hold the same to-space reference (unless another thread installed
578// another object reference (different from `ref`) in `obj.field`).
579class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
580 public:
581 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
582 Location ref,
583 Register obj,
584 const Address& field_addr,
585 bool unpoison_ref_before_marking,
586 Register temp)
587 : SlowPathCode(instruction),
588 ref_(ref),
589 obj_(obj),
590 field_addr_(field_addr),
591 unpoison_ref_before_marking_(unpoison_ref_before_marking),
592 temp_(temp) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +0000593 DCHECK(gUseReadBarrier);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100594 }
595
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100596 const char* GetDescription() const override { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100597
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100598 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100599 LocationSummary* locations = instruction_->GetLocations();
600 Register ref_reg = ref_.AsRegister<Register>();
601 DCHECK(locations->CanCall());
602 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Andra Danciu5e13d452020-09-08 14:35:09 +0000603 DCHECK((instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100604 << "Unexpected instruction in read barrier marking and field updating slow path: "
605 << instruction_->DebugName();
Ulya Trafimovichec696e52022-01-26 10:21:32 +0000606 HInvoke* invoke = instruction_->AsInvoke();
607 DCHECK(IsUnsafeCASObject(invoke) || IsVarHandleCASFamily(invoke)) << invoke->GetIntrinsic();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100608
609 __ Bind(GetEntryLabel());
610 if (unpoison_ref_before_marking_) {
611 // Object* ref = ref_addr->AsMirrorPtr()
612 __ MaybeUnpoisonHeapReference(ref_reg);
613 }
614
615 // Save the old (unpoisoned) reference.
616 __ movl(temp_, ref_reg);
617
618 // No need to save live registers; it's taken care of by the
619 // entrypoint. Also, there is no need to update the stack mask,
620 // as this runtime call will not trigger a garbage collection.
621 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
622 DCHECK_NE(ref_reg, ESP);
623 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
624 // "Compact" slow path, saving two moves.
625 //
626 // Instead of using the standard runtime calling convention (input
627 // and output in EAX):
628 //
629 // EAX <- ref
630 // EAX <- ReadBarrierMark(EAX)
631 // ref <- EAX
632 //
633 // we just use rX (the register containing `ref`) as input and output
634 // of a dedicated entrypoint:
635 //
636 // rX <- ReadBarrierMarkRegX(rX)
637 //
Roland Levillain97c46462017-05-11 14:04:03 +0100638 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100639 // This runtime call does not require a stack map.
640 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
641
642 // If the new reference is different from the old reference,
643 // update the field in the holder (`*field_addr`).
644 //
645 // Note that this field could also hold a different object, if
646 // another thread had concurrently changed it. In that case, the
647 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
648 // operation below would abort the CAS, leaving the field as-is.
649 NearLabel done;
650 __ cmpl(temp_, ref_reg);
651 __ j(kEqual, &done);
652
653 // Update the the holder's field atomically. This may fail if
654 // mutator updates before us, but it's OK. This is achieved
655 // using a strong compare-and-set (CAS) operation with relaxed
656 // memory synchronization ordering, where the expected value is
657 // the old reference and the desired value is the new reference.
658 // This operation is implemented with a 32-bit LOCK CMPXLCHG
659 // instruction, which requires the expected value (the old
660 // reference) to be in EAX. Save EAX beforehand, and move the
661 // expected value (stored in `temp_`) into EAX.
662 __ pushl(EAX);
663 __ movl(EAX, temp_);
664
665 // Convenience aliases.
666 Register base = obj_;
667 Register expected = EAX;
668 Register value = ref_reg;
669
670 bool base_equals_value = (base == value);
671 if (kPoisonHeapReferences) {
672 if (base_equals_value) {
673 // If `base` and `value` are the same register location, move
674 // `value` to a temporary register. This way, poisoning
675 // `value` won't invalidate `base`.
676 value = temp_;
677 __ movl(value, base);
678 }
679
680 // Check that the register allocator did not assign the location
681 // of `expected` (EAX) to `value` nor to `base`, so that heap
682 // poisoning (when enabled) works as intended below.
683 // - If `value` were equal to `expected`, both references would
684 // be poisoned twice, meaning they would not be poisoned at
685 // all, as heap poisoning uses address negation.
686 // - If `base` were equal to `expected`, poisoning `expected`
687 // would invalidate `base`.
688 DCHECK_NE(value, expected);
689 DCHECK_NE(base, expected);
690
691 __ PoisonHeapReference(expected);
692 __ PoisonHeapReference(value);
693 }
694
695 __ LockCmpxchgl(field_addr_, value);
696
697 // If heap poisoning is enabled, we need to unpoison the values
698 // that were poisoned earlier.
699 if (kPoisonHeapReferences) {
700 if (base_equals_value) {
701 // `value` has been moved to a temporary register, no need
702 // to unpoison it.
703 } else {
704 __ UnpoisonHeapReference(value);
705 }
706 // No need to unpoison `expected` (EAX), as it is be overwritten below.
707 }
708
709 // Restore EAX.
710 __ popl(EAX);
711
712 __ Bind(&done);
713 __ jmp(GetExitLabel());
714 }
715
716 private:
717 // The location (register) of the marked object reference.
718 const Location ref_;
719 // The register containing the object holding the marked object reference field.
720 const Register obj_;
721 // The address of the marked reference field. The base of this address must be `obj_`.
722 const Address field_addr_;
723
724 // Should the reference in `ref_` be unpoisoned prior to marking it?
725 const bool unpoison_ref_before_marking_;
726
727 const Register temp_;
728
729 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
730};
731
Roland Levillain0d5a2812015-11-13 10:07:31 +0000732// Slow path generating a read barrier for a heap reference.
733class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
734 public:
735 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
736 Location out,
737 Location ref,
738 Location obj,
739 uint32_t offset,
740 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000741 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000742 out_(out),
743 ref_(ref),
744 obj_(obj),
745 offset_(offset),
746 index_(index) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +0000747 DCHECK(gUseReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000748 // If `obj` is equal to `out` or `ref`, it means the initial object
749 // has been overwritten by (or after) the heap object reference load
750 // to be instrumented, e.g.:
751 //
752 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000753 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000754 //
755 // In that case, we have lost the information about the original
756 // object, and the emitted read barrier cannot work properly.
757 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
758 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
759 }
760
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100761 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000762 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
763 LocationSummary* locations = instruction_->GetLocations();
764 Register reg_out = out_.AsRegister<Register>();
765 DCHECK(locations->CanCall());
766 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100767 DCHECK(instruction_->IsInstanceFieldGet() ||
Alex Light3a73ffb2021-01-25 14:11:05 +0000768 instruction_->IsPredicatedInstanceFieldGet() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100769 instruction_->IsStaticFieldGet() ||
770 instruction_->IsArrayGet() ||
771 instruction_->IsInstanceOf() ||
772 instruction_->IsCheckCast() ||
Vladimir Marko94d2c812020-11-05 10:04:45 +0000773 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000774 << "Unexpected instruction in read barrier for heap reference slow path: "
775 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000776
777 __ Bind(GetEntryLabel());
778 SaveLiveRegisters(codegen, locations);
779
780 // We may have to change the index's value, but as `index_` is a
781 // constant member (like other "inputs" of this slow path),
782 // introduce a copy of it, `index`.
783 Location index = index_;
784 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100785 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000786 if (instruction_->IsArrayGet()) {
787 // Compute the actual memory offset and store it in `index`.
788 Register index_reg = index_.AsRegister<Register>();
789 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
790 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
791 // We are about to change the value of `index_reg` (see the
792 // calls to art::x86::X86Assembler::shll and
793 // art::x86::X86Assembler::AddImmediate below), but it has
794 // not been saved by the previous call to
795 // art::SlowPathCode::SaveLiveRegisters, as it is a
796 // callee-save register --
797 // art::SlowPathCode::SaveLiveRegisters does not consider
798 // callee-save registers, as it has been designed with the
799 // assumption that callee-save registers are supposed to be
800 // handled by the called function. So, as a callee-save
801 // register, `index_reg` _would_ eventually be saved onto
802 // the stack, but it would be too late: we would have
803 // changed its value earlier. Therefore, we manually save
804 // it here into another freely available register,
805 // `free_reg`, chosen of course among the caller-save
806 // registers (as a callee-save `free_reg` register would
807 // exhibit the same problem).
808 //
809 // Note we could have requested a temporary register from
810 // the register allocator instead; but we prefer not to, as
811 // this is a slow path, and we know we can find a
812 // caller-save register that is available.
813 Register free_reg = FindAvailableCallerSaveRegister(codegen);
814 __ movl(free_reg, index_reg);
815 index_reg = free_reg;
816 index = Location::RegisterLocation(index_reg);
817 } else {
818 // The initial register stored in `index_` has already been
819 // saved in the call to art::SlowPathCode::SaveLiveRegisters
820 // (as it is not a callee-save register), so we can freely
821 // use it.
822 }
823 // Shifting the index value contained in `index_reg` by the scale
824 // factor (2) cannot overflow in practice, as the runtime is
825 // unable to allocate object arrays with a size larger than
826 // 2^26 - 1 (that is, 2^28 - 4 bytes).
827 __ shll(index_reg, Immediate(TIMES_4));
828 static_assert(
829 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
830 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
831 __ AddImmediate(index_reg, Immediate(offset_));
832 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100833 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
834 // intrinsics, `index_` is not shifted by a scale factor of 2
835 // (as in the case of ArrayGet), as it is actually an offset
836 // to an object field within an object.
837 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000838 DCHECK(instruction_->GetLocations()->Intrinsified());
839 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
Sorin Basca2f01e8e2021-06-18 06:44:07 +0000840 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile) ||
841 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kJdkUnsafeGetObject) ||
Sorin Basca4a4696a2021-10-09 07:14:40 +0000842 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kJdkUnsafeGetObjectVolatile) ||
843 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kJdkUnsafeGetObjectAcquire))
Roland Levillain0d5a2812015-11-13 10:07:31 +0000844 << instruction_->AsInvoke()->GetIntrinsic();
845 DCHECK_EQ(offset_, 0U);
846 DCHECK(index_.IsRegisterPair());
847 // UnsafeGet's offset location is a register pair, the low
848 // part contains the correct offset.
849 index = index_.ToLow();
850 }
851 }
852
853 // We're moving two or three locations to locations that could
854 // overlap, so we need a parallel move resolver.
855 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100856 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000857 parallel_move.AddMove(ref_,
858 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100859 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000860 nullptr);
861 parallel_move.AddMove(obj_,
862 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100863 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000864 nullptr);
865 if (index.IsValid()) {
866 parallel_move.AddMove(index,
867 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100868 DataType::Type::kInt32,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000869 nullptr);
870 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
871 } else {
872 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
873 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
874 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100875 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000876 CheckEntrypointTypes<
877 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
878 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
879
880 RestoreLiveRegisters(codegen, locations);
881 __ jmp(GetExitLabel());
882 }
883
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100884 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathX86"; }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000885
886 private:
887 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
888 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
889 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
890 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
891 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
892 return static_cast<Register>(i);
893 }
894 }
895 // We shall never fail to find a free caller-save register, as
896 // there are more than two core caller-save registers on x86
897 // (meaning it is possible to find one which is different from
898 // `ref` and `obj`).
899 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
900 LOG(FATAL) << "Could not find a free caller-save register";
901 UNREACHABLE();
902 }
903
Roland Levillain0d5a2812015-11-13 10:07:31 +0000904 const Location out_;
905 const Location ref_;
906 const Location obj_;
907 const uint32_t offset_;
908 // An additional location containing an index to an array.
909 // Only used for HArrayGet and the UnsafeGetObject &
910 // UnsafeGetObjectVolatile intrinsics.
911 const Location index_;
912
913 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
914};
915
916// Slow path generating a read barrier for a GC root.
917class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
918 public:
919 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000920 : SlowPathCode(instruction), out_(out), root_(root) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +0000921 DCHECK(gUseReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000922 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000923
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100924 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000925 LocationSummary* locations = instruction_->GetLocations();
926 Register reg_out = out_.AsRegister<Register>();
927 DCHECK(locations->CanCall());
928 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000929 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
930 << "Unexpected instruction in read barrier for GC root slow path: "
931 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000932
933 __ Bind(GetEntryLabel());
934 SaveLiveRegisters(codegen, locations);
935
936 InvokeRuntimeCallingConvention calling_convention;
937 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
938 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100939 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000940 instruction_,
941 instruction_->GetDexPc(),
942 this);
943 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
944 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
945
946 RestoreLiveRegisters(codegen, locations);
947 __ jmp(GetExitLabel());
948 }
949
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100950 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathX86"; }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000951
952 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000953 const Location out_;
954 const Location root_;
955
956 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
957};
958
Mythri Alle5097f832021-11-02 14:52:30 +0000959class MethodEntryExitHooksSlowPathX86 : public SlowPathCode {
960 public:
961 explicit MethodEntryExitHooksSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
962
963 void EmitNativeCode(CodeGenerator* codegen) override {
964 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
965 LocationSummary* locations = instruction_->GetLocations();
966 QuickEntrypointEnum entry_point =
967 (instruction_->IsMethodEntryHook()) ? kQuickMethodEntryHook : kQuickMethodExitHook;
968 __ Bind(GetEntryLabel());
969 SaveLiveRegisters(codegen, locations);
970 x86_codegen->InvokeRuntime(entry_point, instruction_, instruction_->GetDexPc(), this);
971 RestoreLiveRegisters(codegen, locations);
972 __ jmp(GetExitLabel());
973 }
974
975 const char* GetDescription() const override {
976 return "MethodEntryExitHooksSlowPath";
977 }
978
979 private:
980 DISALLOW_COPY_AND_ASSIGN(MethodEntryExitHooksSlowPathX86);
981};
982
Nicolas Geoffray9e598902021-11-19 14:53:07 +0000983class CompileOptimizedSlowPathX86 : public SlowPathCode {
984 public:
985 CompileOptimizedSlowPathX86() : SlowPathCode(/* instruction= */ nullptr) {}
986
987 void EmitNativeCode(CodeGenerator* codegen) override {
988 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
989 __ Bind(GetEntryLabel());
990 x86_codegen->GenerateInvokeRuntime(
991 GetThreadOffset<kX86PointerSize>(kQuickCompileOptimized).Int32Value());
992 __ jmp(GetExitLabel());
993 }
994
995 const char* GetDescription() const override {
996 return "CompileOptimizedSlowPath";
997 }
998
999 private:
1000 DISALLOW_COPY_AND_ASSIGN(CompileOptimizedSlowPathX86);
1001};
1002
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001003#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001004// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1005#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001006
Aart Bike9f37602015-10-09 11:15:55 -07001007inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001008 switch (cond) {
1009 case kCondEQ: return kEqual;
1010 case kCondNE: return kNotEqual;
1011 case kCondLT: return kLess;
1012 case kCondLE: return kLessEqual;
1013 case kCondGT: return kGreater;
1014 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -07001015 case kCondB: return kBelow;
1016 case kCondBE: return kBelowEqual;
1017 case kCondA: return kAbove;
1018 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -07001019 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001020 LOG(FATAL) << "Unreachable";
1021 UNREACHABLE();
1022}
1023
Aart Bike9f37602015-10-09 11:15:55 -07001024// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001025inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
1026 switch (cond) {
1027 case kCondEQ: return kEqual;
1028 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -07001029 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001030 case kCondLT: return kBelow;
1031 case kCondLE: return kBelowEqual;
1032 case kCondGT: return kAbove;
1033 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -07001034 // Unsigned remain unchanged.
1035 case kCondB: return kBelow;
1036 case kCondBE: return kBelowEqual;
1037 case kCondA: return kAbove;
1038 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001039 }
1040 LOG(FATAL) << "Unreachable";
1041 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001042}
1043
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001044void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001045 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001046}
1047
1048void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001049 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001050}
1051
Vladimir Markoa0431112018-06-25 09:32:54 +01001052const X86InstructionSetFeatures& CodeGeneratorX86::GetInstructionSetFeatures() const {
1053 return *GetCompilerOptions().GetInstructionSetFeatures()->AsX86InstructionSetFeatures();
1054}
1055
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001056size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1057 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
1058 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001059}
1060
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001061size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1062 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
1063 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001064}
1065
Mark Mendell7c8d0092015-01-26 11:21:33 -05001066size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001067 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001068 __ movups(Address(ESP, stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -07001069 } else {
1070 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
1071 }
Artem Serov6a0b6572019-07-26 20:38:37 +01001072 return GetSlowPathFPWidth();
Mark Mendell7c8d0092015-01-26 11:21:33 -05001073}
1074
1075size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001076 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001077 __ movups(XmmRegister(reg_id), Address(ESP, stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -07001078 } else {
1079 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
1080 }
Artem Serov6a0b6572019-07-26 20:38:37 +01001081 return GetSlowPathFPWidth();
Mark Mendell7c8d0092015-01-26 11:21:33 -05001082}
1083
Calin Juravle175dc732015-08-25 15:42:32 +01001084void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
1085 HInstruction* instruction,
1086 uint32_t dex_pc,
1087 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001088 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001089 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
1090 if (EntrypointRequiresStackMap(entrypoint)) {
1091 RecordPcInfo(instruction, dex_pc, slow_path);
1092 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001093}
1094
Roland Levillaindec8f632016-07-22 17:10:06 +01001095void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1096 HInstruction* instruction,
1097 SlowPathCode* slow_path) {
1098 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001099 GenerateInvokeRuntime(entry_point_offset);
1100}
1101
1102void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001103 __ fs()->call(Address::Absolute(entry_point_offset));
1104}
1105
Mark Mendellfb8d2792015-03-31 22:16:59 -04001106CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001107 const CompilerOptions& compiler_options,
1108 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001109 : CodeGenerator(graph,
1110 kNumberOfCpuRegisters,
1111 kNumberOfXmmRegisters,
1112 kNumberOfRegisterPairs,
1113 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1114 arraysize(kCoreCalleeSaves))
1115 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001116 0,
1117 compiler_options,
1118 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001119 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001120 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001121 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001122 move_resolver_(graph->GetAllocator(), this),
Shalini Salomi Bodapati6545ee32021-11-02 20:01:06 +05301123 assembler_(graph->GetAllocator(),
1124 compiler_options.GetInstructionSetFeatures()->AsX86InstructionSetFeatures()),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001125 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1126 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1127 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1128 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko8f63f102020-09-28 12:10:28 +01001129 public_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1130 package_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001131 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001132 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoeb9eb002020-10-02 13:54:19 +01001133 boot_image_jni_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +01001134 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001135 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1136 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001137 constant_area_start_(-1),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001138 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001139 method_address_offset_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001140 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001141 // Use a fake return address register to mimic Quick.
1142 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001143}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001144
David Brazdil58282f42016-01-14 12:45:10 +00001145void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001146 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001147 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001148}
1149
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001150InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001151 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001152 assembler_(codegen->GetAssembler()),
1153 codegen_(codegen) {}
1154
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001155static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001156 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001157}
1158
Mythri Alle5097f832021-11-02 14:52:30 +00001159void SetInForReturnValue(HInstruction* ret, LocationSummary* locations) {
1160 switch (ret->InputAt(0)->GetType()) {
1161 case DataType::Type::kReference:
1162 case DataType::Type::kBool:
1163 case DataType::Type::kUint8:
1164 case DataType::Type::kInt8:
1165 case DataType::Type::kUint16:
1166 case DataType::Type::kInt16:
1167 case DataType::Type::kInt32:
1168 locations->SetInAt(0, Location::RegisterLocation(EAX));
1169 break;
1170
1171 case DataType::Type::kInt64:
1172 locations->SetInAt(0, Location::RegisterPairLocation(EAX, EDX));
1173 break;
1174
1175 case DataType::Type::kFloat32:
1176 case DataType::Type::kFloat64:
1177 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
1178 break;
1179
1180 case DataType::Type::kVoid:
1181 locations->SetInAt(0, Location::NoLocation());
1182 break;
1183
1184 default:
1185 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
1186 }
1187}
1188
1189void LocationsBuilderX86::VisitMethodExitHook(HMethodExitHook* method_hook) {
1190 LocationSummary* locations = new (GetGraph()->GetAllocator())
1191 LocationSummary(method_hook, LocationSummary::kCallOnSlowPath);
1192 SetInForReturnValue(method_hook, locations);
1193}
1194
1195void InstructionCodeGeneratorX86::GenerateMethodEntryExitHook(HInstruction* instruction) {
1196 SlowPathCode* slow_path =
1197 new (codegen_->GetScopedAllocator()) MethodEntryExitHooksSlowPathX86(instruction);
1198 codegen_->AddSlowPath(slow_path);
1199
1200 uint64_t address = reinterpret_cast64<uint64_t>(Runtime::Current()->GetInstrumentation());
1201 int offset = instrumentation::Instrumentation::NeedsEntryExitHooksOffset().Int32Value();
Mythri Alle9575c122021-11-12 12:04:41 +00001202 __ cmpb(Address::Absolute(address + offset), Immediate(0));
1203 __ j(kNotEqual, slow_path->GetEntryLabel());
Mythri Alle5097f832021-11-02 14:52:30 +00001204 __ Bind(slow_path->GetExitLabel());
1205}
1206
1207void InstructionCodeGeneratorX86::VisitMethodExitHook(HMethodExitHook* instruction) {
1208 DCHECK(codegen_->GetCompilerOptions().IsJitCompiler() && GetGraph()->IsDebuggable());
1209 DCHECK(codegen_->RequiresCurrentMethod());
1210 GenerateMethodEntryExitHook(instruction);
1211}
1212
1213void LocationsBuilderX86::VisitMethodEntryHook(HMethodEntryHook* method_hook) {
1214 new (GetGraph()->GetAllocator()) LocationSummary(method_hook, LocationSummary::kCallOnSlowPath);
1215}
1216
1217void InstructionCodeGeneratorX86::VisitMethodEntryHook(HMethodEntryHook* instruction) {
1218 DCHECK(codegen_->GetCompilerOptions().IsJitCompiler() && GetGraph()->IsDebuggable());
1219 DCHECK(codegen_->RequiresCurrentMethod());
1220 GenerateMethodEntryExitHook(instruction);
1221}
1222
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001223void CodeGeneratorX86::MaybeIncrementHotness(bool is_frame_entry) {
1224 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1225 Register reg = EAX;
1226 if (is_frame_entry) {
1227 reg = kMethodRegisterArgument;
1228 } else {
1229 __ pushl(EAX);
Vladimir Markodec78172020-06-19 15:31:23 +01001230 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001231 __ movl(EAX, Address(ESP, kX86WordSize));
1232 }
1233 NearLabel overflow;
1234 __ cmpw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()),
Nicolas Geoffray61673dc2021-11-06 13:58:31 +00001235 Immediate(interpreter::kNterpHotnessValue));
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001236 __ j(kEqual, &overflow);
Nicolas Geoffray61673dc2021-11-06 13:58:31 +00001237 __ addw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()), Immediate(-1));
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001238 __ Bind(&overflow);
1239 if (!is_frame_entry) {
1240 __ popl(EAX);
Vladimir Markodec78172020-06-19 15:31:23 +01001241 __ cfi().AdjustCFAOffset(-4);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001242 }
1243 }
1244
1245 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffray9e598902021-11-19 14:53:07 +00001246 SlowPathCode* slow_path = new (GetScopedAllocator()) CompileOptimizedSlowPathX86();
1247 AddSlowPath(slow_path);
1248 ProfilingInfo* info = GetGraph()->GetProfilingInfo();
1249 DCHECK(info != nullptr);
1250 uint32_t address = reinterpret_cast32<uint32_t>(info) +
1251 ProfilingInfo::BaselineHotnessCountOffset().Int32Value();
1252 DCHECK(!HasEmptyFrame());
1253 // With multiple threads, this can overflow. This is OK, we will eventually get to see
1254 // it reaching 0. Also, at this point we have no register available to look
1255 // at the counter directly.
1256 __ addw(Address::Absolute(address), Immediate(-1));
1257 __ j(kEqual, slow_path->GetEntryLabel());
1258 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001259 }
1260}
1261
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001262void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001263 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffrayab3f8d22022-07-23 15:49:51 +00001264
1265 // Check if we need to generate the clinit check. We will jump to the
1266 // resolution stub if the class is not initialized and the executing thread is
1267 // not the thread initializing it.
1268 // We do this before constructing the frame to get the correct stack trace if
1269 // an exception is thrown.
1270 if (GetCompilerOptions().ShouldCompileWithClinitCheck(GetGraph()->GetArtMethod())) {
1271 NearLabel continue_execution, resolution;
1272 // We'll use EBP as temporary.
1273 __ pushl(EBP);
1274 // Check if we're visibly initialized.
1275
1276 // We don't emit a read barrier here to save on code size. We rely on the
1277 // resolution trampoline to do a suspend check before re-entering this code.
1278 __ movl(EBP, Address(kMethodRegisterArgument, ArtMethod::DeclaringClassOffset().Int32Value()));
1279 __ cmpb(Address(EBP, status_byte_offset), Immediate(shifted_visibly_initialized_value));
1280 __ j(kAboveEqual, &continue_execution);
1281
1282 // Check if we're initializing and the thread initializing is the one
1283 // executing the code.
1284 __ cmpb(Address(EBP, status_byte_offset), Immediate(shifted_initializing_value));
1285 __ j(kBelow, &resolution);
1286
1287 __ movl(EBP, Address(EBP, mirror::Class::ClinitThreadIdOffset().Int32Value()));
1288 __ fs()->cmpl(EBP, Address::Absolute(Thread::TidOffset<kX86PointerSize>().Int32Value()));
1289 __ j(kEqual, &continue_execution);
1290 __ Bind(&resolution);
1291
1292 __ popl(EBP);
1293 // Jump to the resolution stub.
1294 ThreadOffset32 entrypoint_offset =
1295 GetThreadOffset<kX86PointerSize>(kQuickQuickResolutionTrampoline);
1296 __ fs()->jmp(Address::Absolute(entrypoint_offset));
1297
1298 __ Bind(&continue_execution);
1299 __ popl(EBP);
1300 }
1301
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001302 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001303 bool skip_overflow_check =
1304 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001305 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001306
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001307 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001308 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86);
1309 __ testl(EAX, Address(ESP, -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001310 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001311 }
1312
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001313 if (!HasEmptyFrame()) {
1314 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1315 Register reg = kCoreCalleeSaves[i];
1316 if (allocated_registers_.ContainsCoreRegister(reg)) {
1317 __ pushl(reg);
1318 __ cfi().AdjustCFAOffset(kX86WordSize);
1319 __ cfi().RelOffset(DWARFReg(reg), 0);
1320 }
1321 }
Mark Mendell5f874182015-03-04 15:42:45 -05001322
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001323 int adjust = GetFrameSize() - FrameEntrySpillSize();
Vladimir Markodec78172020-06-19 15:31:23 +01001324 IncreaseFrame(adjust);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001325 // Save the current method if we need it. Note that we do not
1326 // do this in HCurrentMethod, as the instruction might have been removed
1327 // in the SSA graph.
1328 if (RequiresCurrentMethod()) {
1329 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1330 }
1331
1332 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1333 // Initialize should_deoptimize flag to 0.
1334 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
Mark Mendell5f874182015-03-04 15:42:45 -05001335 }
1336 }
1337
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001338 MaybeIncrementHotness(/* is_frame_entry= */ true);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001339}
1340
1341void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001342 __ cfi().RememberState();
1343 if (!HasEmptyFrame()) {
1344 int adjust = GetFrameSize() - FrameEntrySpillSize();
Vladimir Markodec78172020-06-19 15:31:23 +01001345 DecreaseFrame(adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001346
David Srbeckyc34dc932015-04-12 09:27:43 +01001347 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1348 Register reg = kCoreCalleeSaves[i];
1349 if (allocated_registers_.ContainsCoreRegister(reg)) {
1350 __ popl(reg);
1351 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1352 __ cfi().Restore(DWARFReg(reg));
1353 }
Mark Mendell5f874182015-03-04 15:42:45 -05001354 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001355 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001356 __ ret();
1357 __ cfi().RestoreState();
1358 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001359}
1360
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001361void CodeGeneratorX86::Bind(HBasicBlock* block) {
1362 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001363}
1364
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001365Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001366 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001367 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001368 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001369 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001370 case DataType::Type::kInt8:
1371 case DataType::Type::kUint16:
1372 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08001373 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001374 case DataType::Type::kInt32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001375 return Location::RegisterLocation(EAX);
1376
Aart Bik66c158e2018-01-31 12:55:04 -08001377 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001378 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001379 return Location::RegisterPairLocation(EAX, EDX);
1380
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001381 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001382 return Location::NoLocation();
1383
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001384 case DataType::Type::kFloat64:
1385 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001386 return Location::FpuRegisterLocation(XMM0);
1387 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001388
1389 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001390}
1391
1392Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1393 return Location::RegisterLocation(kMethodRegisterArgument);
1394}
1395
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001396Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001397 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001398 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001399 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001400 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001401 case DataType::Type::kInt8:
1402 case DataType::Type::kUint16:
1403 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001404 case DataType::Type::kInt32: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001405 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001406 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001407 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001408 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001409 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001410 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001411 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001412 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001413
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001414 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001415 uint32_t index = gp_index_;
1416 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001417 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001418 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001419 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1420 calling_convention.GetRegisterPairAt(index));
1421 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001422 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001423 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1424 }
1425 }
1426
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001427 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001428 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001429 stack_index_++;
1430 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1431 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1432 } else {
1433 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1434 }
1435 }
1436
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001437 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001438 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001439 stack_index_ += 2;
1440 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1441 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1442 } else {
1443 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001444 }
1445 }
1446
Aart Bik66c158e2018-01-31 12:55:04 -08001447 case DataType::Type::kUint32:
1448 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001449 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001450 LOG(FATAL) << "Unexpected parameter type " << type;
Elliott Hughesc1896c92018-11-29 11:33:18 -08001451 UNREACHABLE();
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001452 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001453 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001454}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001455
Vladimir Marko86c87522020-05-11 16:55:55 +01001456Location CriticalNativeCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
1457 DCHECK_NE(type, DataType::Type::kReference);
1458
1459 Location location;
1460 if (DataType::Is64BitType(type)) {
1461 location = Location::DoubleStackSlot(stack_offset_);
1462 stack_offset_ += 2 * kFramePointerSize;
1463 } else {
1464 location = Location::StackSlot(stack_offset_);
1465 stack_offset_ += kFramePointerSize;
1466 }
1467 if (for_register_allocation_) {
1468 location = Location::Any();
1469 }
1470 return location;
1471}
1472
1473Location CriticalNativeCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
1474 // We perform conversion to the managed ABI return register after the call if needed.
1475 InvokeDexCallingConventionVisitorX86 dex_calling_convention;
1476 return dex_calling_convention.GetReturnLocation(type);
1477}
1478
1479Location CriticalNativeCallingConventionVisitorX86::GetMethodLocation() const {
1480 // Pass the method in the hidden argument EAX.
1481 return Location::RegisterLocation(EAX);
1482}
1483
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001484void CodeGeneratorX86::Move32(Location destination, Location source) {
1485 if (source.Equals(destination)) {
1486 return;
1487 }
1488 if (destination.IsRegister()) {
1489 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001490 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001491 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001492 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Andra Danciu5e13d452020-09-08 14:35:09 +00001493 } else if (source.IsConstant()) {
1494 int32_t value = GetInt32ValueOf(source.GetConstant());
1495 __ movl(destination.AsRegister<Register>(), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001496 } else {
1497 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001498 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001499 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001500 } else if (destination.IsFpuRegister()) {
1501 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001502 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001503 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001504 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001505 } else {
1506 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001507 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001508 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001509 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001510 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001511 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001512 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001513 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001514 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001515 } else if (source.IsConstant()) {
1516 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001517 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001518 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001519 } else {
1520 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001521 __ pushl(Address(ESP, source.GetStackIndex()));
1522 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001523 }
1524 }
1525}
1526
1527void CodeGeneratorX86::Move64(Location destination, Location source) {
1528 if (source.Equals(destination)) {
1529 return;
1530 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001531 if (destination.IsRegisterPair()) {
1532 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001533 EmitParallelMoves(
1534 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1535 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001536 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001537 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001538 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001539 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001540 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001541 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1542 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1543 __ psrlq(src_reg, Immediate(32));
1544 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001545 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001546 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001547 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001548 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1549 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001550 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1551 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001552 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001553 if (source.IsFpuRegister()) {
1554 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1555 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001556 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001557 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001558 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Vladimir Markodec78172020-06-19 15:31:23 +01001559 // Push the 2 source registers to the stack.
1560 __ pushl(source.AsRegisterPairHigh<Register>());
1561 __ cfi().AdjustCFAOffset(elem_size);
1562 __ pushl(source.AsRegisterPairLow<Register>());
1563 __ cfi().AdjustCFAOffset(elem_size);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001564 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1565 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01001566 DecreaseFrame(2 * elem_size);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001567 } else {
1568 LOG(FATAL) << "Unimplemented";
1569 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001570 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001571 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001572 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001573 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001574 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001575 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001576 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001577 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001578 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001579 } else if (source.IsConstant()) {
1580 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001581 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1582 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001583 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001584 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1585 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001586 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001587 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001588 EmitParallelMoves(
1589 Location::StackSlot(source.GetStackIndex()),
1590 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001591 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001592 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001593 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001594 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001595 }
1596 }
1597}
1598
Andra Danciu1ca6f322020-08-12 08:58:07 +00001599static Address CreateAddress(Register base,
1600 Register index = Register::kNoRegister,
1601 ScaleFactor scale = TIMES_1,
1602 int32_t disp = 0) {
1603 if (index == Register::kNoRegister) {
1604 return Address(base, disp);
1605 }
1606
1607 return Address(base, index, scale, disp);
1608}
1609
Andra Danciud0f71f22020-09-17 09:00:15 +00001610void CodeGeneratorX86::LoadFromMemoryNoBarrier(DataType::Type dst_type,
1611 Location dst,
1612 Address src,
Ulya Trafimovich322eced2021-06-02 15:39:36 +01001613 HInstruction* instr,
Andra Danciud0f71f22020-09-17 09:00:15 +00001614 XmmRegister temp,
1615 bool is_atomic_load) {
Andra Danciu1ca6f322020-08-12 08:58:07 +00001616 switch (dst_type) {
1617 case DataType::Type::kBool:
1618 case DataType::Type::kUint8:
1619 __ movzxb(dst.AsRegister<Register>(), src);
1620 break;
1621 case DataType::Type::kInt8:
1622 __ movsxb(dst.AsRegister<Register>(), src);
1623 break;
1624 case DataType::Type::kInt16:
1625 __ movsxw(dst.AsRegister<Register>(), src);
1626 break;
1627 case DataType::Type::kUint16:
1628 __ movzxw(dst.AsRegister<Register>(), src);
1629 break;
1630 case DataType::Type::kInt32:
Andra Danciu1ca6f322020-08-12 08:58:07 +00001631 __ movl(dst.AsRegister<Register>(), src);
1632 break;
Andra Danciud0f71f22020-09-17 09:00:15 +00001633 case DataType::Type::kInt64: {
1634 if (is_atomic_load) {
1635 __ movsd(temp, src);
Ulya Trafimovich322eced2021-06-02 15:39:36 +01001636 if (instr != nullptr) {
1637 MaybeRecordImplicitNullCheck(instr);
1638 }
Andra Danciud0f71f22020-09-17 09:00:15 +00001639 __ movd(dst.AsRegisterPairLow<Register>(), temp);
1640 __ psrlq(temp, Immediate(32));
1641 __ movd(dst.AsRegisterPairHigh<Register>(), temp);
1642 } else {
1643 DCHECK_NE(src.GetBaseRegister(), dst.AsRegisterPairLow<Register>());
Ulya Trafimovich893e2ed2021-06-10 16:18:12 +01001644 Address src_high = Address::displace(src, kX86WordSize);
Andra Danciud0f71f22020-09-17 09:00:15 +00001645 __ movl(dst.AsRegisterPairLow<Register>(), src);
Ulya Trafimovich322eced2021-06-02 15:39:36 +01001646 if (instr != nullptr) {
1647 MaybeRecordImplicitNullCheck(instr);
1648 }
Andra Danciud0f71f22020-09-17 09:00:15 +00001649 __ movl(dst.AsRegisterPairHigh<Register>(), src_high);
1650 }
Andra Danciu1ca6f322020-08-12 08:58:07 +00001651 break;
1652 }
1653 case DataType::Type::kFloat32:
1654 __ movss(dst.AsFpuRegister<XmmRegister>(), src);
1655 break;
1656 case DataType::Type::kFloat64:
1657 __ movsd(dst.AsFpuRegister<XmmRegister>(), src);
1658 break;
Andra Danciu1ca6f322020-08-12 08:58:07 +00001659 case DataType::Type::kReference:
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00001660 DCHECK(!gUseReadBarrier);
Andra Danciud0f71f22020-09-17 09:00:15 +00001661 __ movl(dst.AsRegister<Register>(), src);
1662 __ MaybeUnpoisonHeapReference(dst.AsRegister<Register>());
1663 break;
1664 default:
Andra Danciu1ca6f322020-08-12 08:58:07 +00001665 LOG(FATAL) << "Unreachable type " << dst_type;
1666 }
Ulya Trafimovich322eced2021-06-02 15:39:36 +01001667 if (instr != nullptr && dst_type != DataType::Type::kInt64) {
1668 // kInt64 needs special handling that is done in the above switch.
1669 MaybeRecordImplicitNullCheck(instr);
1670 }
Andra Danciu1ca6f322020-08-12 08:58:07 +00001671}
1672
Andra Danciu73c31802020-09-01 13:17:05 +00001673void CodeGeneratorX86::MoveToMemory(DataType::Type src_type,
1674 Location src,
1675 Register dst_base,
1676 Register dst_index,
1677 ScaleFactor dst_scale,
1678 int32_t dst_disp) {
1679 DCHECK(dst_base != Register::kNoRegister);
1680 Address dst = CreateAddress(dst_base, dst_index, dst_scale, dst_disp);
1681
1682 switch (src_type) {
1683 case DataType::Type::kBool:
1684 case DataType::Type::kUint8:
1685 case DataType::Type::kInt8: {
1686 if (src.IsConstant()) {
1687 __ movb(dst, Immediate(CodeGenerator::GetInt8ValueOf(src.GetConstant())));
1688 } else {
1689 __ movb(dst, src.AsRegister<ByteRegister>());
1690 }
1691 break;
1692 }
1693 case DataType::Type::kUint16:
1694 case DataType::Type::kInt16: {
1695 if (src.IsConstant()) {
1696 __ movw(dst, Immediate(CodeGenerator::GetInt16ValueOf(src.GetConstant())));
1697 } else {
1698 __ movw(dst, src.AsRegister<Register>());
1699 }
1700 break;
1701 }
1702 case DataType::Type::kUint32:
1703 case DataType::Type::kInt32: {
1704 if (src.IsConstant()) {
1705 int32_t v = CodeGenerator::GetInt32ValueOf(src.GetConstant());
1706 __ movl(dst, Immediate(v));
1707 } else {
1708 __ movl(dst, src.AsRegister<Register>());
1709 }
1710 break;
1711 }
1712 case DataType::Type::kUint64:
1713 case DataType::Type::kInt64: {
1714 Address dst_next_4_bytes = CreateAddress(dst_base, dst_index, dst_scale, dst_disp + 4);
1715 if (src.IsConstant()) {
1716 int64_t v = CodeGenerator::GetInt64ValueOf(src.GetConstant());
1717 __ movl(dst, Immediate(Low32Bits(v)));
1718 __ movl(dst_next_4_bytes, Immediate(High32Bits(v)));
1719 } else {
1720 __ movl(dst, src.AsRegisterPairLow<Register>());
1721 __ movl(dst_next_4_bytes, src.AsRegisterPairHigh<Register>());
1722 }
1723 break;
1724 }
1725 case DataType::Type::kFloat32: {
1726 if (src.IsConstant()) {
1727 int32_t v = CodeGenerator::GetInt32ValueOf(src.GetConstant());
1728 __ movl(dst, Immediate(v));
1729 } else {
1730 __ movss(dst, src.AsFpuRegister<XmmRegister>());
1731 }
1732 break;
1733 }
1734 case DataType::Type::kFloat64: {
1735 Address dst_next_4_bytes = CreateAddress(dst_base, dst_index, dst_scale, dst_disp + 4);
1736 if (src.IsConstant()) {
1737 int64_t v = CodeGenerator::GetInt64ValueOf(src.GetConstant());
1738 __ movl(dst, Immediate(Low32Bits(v)));
1739 __ movl(dst_next_4_bytes, Immediate(High32Bits(v)));
1740 } else {
1741 __ movsd(dst, src.AsFpuRegister<XmmRegister>());
1742 }
1743 break;
1744 }
1745 case DataType::Type::kVoid:
1746 case DataType::Type::kReference:
1747 LOG(FATAL) << "Unreachable type " << src_type;
1748 }
1749}
1750
Calin Juravle175dc732015-08-25 15:42:32 +01001751void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1752 DCHECK(location.IsRegister());
1753 __ movl(location.AsRegister<Register>(), Immediate(value));
1754}
1755
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001756void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001757 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001758 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1759 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1760 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001761 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001762 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001763 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001764 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001765}
1766
1767void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1768 if (location.IsRegister()) {
1769 locations->AddTemp(location);
1770 } else if (location.IsRegisterPair()) {
1771 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1772 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1773 } else {
1774 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1775 }
1776}
1777
David Brazdilfc6a86a2015-06-26 10:33:45 +00001778void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001779 if (successor->IsExitBlock()) {
1780 DCHECK(got->GetPrevious()->AlwaysThrows());
1781 return; // no code needed
1782 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001783
1784 HBasicBlock* block = got->GetBlock();
1785 HInstruction* previous = got->GetPrevious();
1786
1787 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001788 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001789 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001790 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1791 return;
1792 }
1793
1794 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1795 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1796 }
1797 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001798 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001799 }
1800}
1801
David Brazdilfc6a86a2015-06-26 10:33:45 +00001802void LocationsBuilderX86::VisitGoto(HGoto* got) {
1803 got->SetLocations(nullptr);
1804}
1805
1806void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1807 HandleGoto(got, got->GetSuccessor());
1808}
1809
1810void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1811 try_boundary->SetLocations(nullptr);
1812}
1813
1814void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1815 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1816 if (!successor->IsExitBlock()) {
1817 HandleGoto(try_boundary, successor);
1818 }
1819}
1820
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001821void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001822 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001823}
1824
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001825void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001826}
1827
Mark Mendell152408f2015-12-31 12:28:50 -05001828template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001829void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001830 LabelType* true_label,
1831 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001832 if (cond->IsFPConditionTrueIfNaN()) {
1833 __ j(kUnordered, true_label);
1834 } else if (cond->IsFPConditionFalseIfNaN()) {
1835 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001836 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001837 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001838}
1839
Mark Mendell152408f2015-12-31 12:28:50 -05001840template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001841void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001842 LabelType* true_label,
1843 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001844 LocationSummary* locations = cond->GetLocations();
1845 Location left = locations->InAt(0);
1846 Location right = locations->InAt(1);
1847 IfCondition if_cond = cond->GetCondition();
1848
Mark Mendellc4701932015-04-10 13:18:51 -04001849 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001850 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001851 IfCondition true_high_cond = if_cond;
1852 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001853 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001854
1855 // Set the conditions for the test, remembering that == needs to be
1856 // decided using the low words.
1857 switch (if_cond) {
1858 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001859 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001860 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001861 break;
1862 case kCondLT:
1863 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001864 break;
1865 case kCondLE:
1866 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001867 break;
1868 case kCondGT:
1869 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001870 break;
1871 case kCondGE:
1872 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001873 break;
Aart Bike9f37602015-10-09 11:15:55 -07001874 case kCondB:
1875 false_high_cond = kCondA;
1876 break;
1877 case kCondBE:
1878 true_high_cond = kCondB;
1879 break;
1880 case kCondA:
1881 false_high_cond = kCondB;
1882 break;
1883 case kCondAE:
1884 true_high_cond = kCondA;
1885 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001886 }
1887
1888 if (right.IsConstant()) {
1889 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001890 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001891 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001892
Aart Bika19616e2016-02-01 18:57:58 -08001893 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001894 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001895 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001896 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001897 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001898 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001899 __ j(X86Condition(true_high_cond), true_label);
1900 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001901 }
1902 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001903 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001904 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001905 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001906 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001907
1908 __ cmpl(left_high, right_high);
1909 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001910 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001911 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001912 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001913 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001914 __ j(X86Condition(true_high_cond), true_label);
1915 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001916 }
1917 // Must be equal high, so compare the lows.
1918 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001919 } else {
1920 DCHECK(right.IsDoubleStackSlot());
1921 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1922 if (if_cond == kCondNE) {
1923 __ j(X86Condition(true_high_cond), true_label);
1924 } else if (if_cond == kCondEQ) {
1925 __ j(X86Condition(false_high_cond), false_label);
1926 } else {
1927 __ j(X86Condition(true_high_cond), true_label);
1928 __ j(X86Condition(false_high_cond), false_label);
1929 }
1930 // Must be equal high, so compare the lows.
1931 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001932 }
1933 // The last comparison might be unsigned.
1934 __ j(final_condition, true_label);
1935}
1936
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001937void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1938 Location rhs,
1939 HInstruction* insn,
1940 bool is_double) {
1941 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1942 if (is_double) {
1943 if (rhs.IsFpuRegister()) {
1944 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1945 } else if (const_area != nullptr) {
1946 DCHECK(const_area->IsEmittedAtUseSite());
1947 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1948 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001949 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1950 const_area->GetBaseMethodAddress(),
1951 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001952 } else {
1953 DCHECK(rhs.IsDoubleStackSlot());
1954 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1955 }
1956 } else {
1957 if (rhs.IsFpuRegister()) {
1958 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1959 } else if (const_area != nullptr) {
1960 DCHECK(const_area->IsEmittedAtUseSite());
1961 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1962 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001963 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1964 const_area->GetBaseMethodAddress(),
1965 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001966 } else {
1967 DCHECK(rhs.IsStackSlot());
1968 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1969 }
1970 }
1971}
1972
Mark Mendell152408f2015-12-31 12:28:50 -05001973template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001974void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001975 LabelType* true_target_in,
1976 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001977 // Generated branching requires both targets to be explicit. If either of the
1978 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001979 LabelType fallthrough_target;
1980 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1981 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001982
Mark Mendellc4701932015-04-10 13:18:51 -04001983 LocationSummary* locations = condition->GetLocations();
1984 Location left = locations->InAt(0);
1985 Location right = locations->InAt(1);
1986
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001987 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001988 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001989 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001990 GenerateLongComparesAndJumps(condition, true_target, false_target);
1991 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001992 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001993 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001994 GenerateFPJumps(condition, true_target, false_target);
1995 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001996 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001997 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001998 GenerateFPJumps(condition, true_target, false_target);
1999 break;
2000 default:
2001 LOG(FATAL) << "Unexpected compare type " << type;
2002 }
2003
David Brazdil0debae72015-11-12 18:37:00 +00002004 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04002005 __ jmp(false_target);
2006 }
David Brazdil0debae72015-11-12 18:37:00 +00002007
2008 if (fallthrough_target.IsLinked()) {
2009 __ Bind(&fallthrough_target);
2010 }
Mark Mendellc4701932015-04-10 13:18:51 -04002011}
2012
David Brazdil0debae72015-11-12 18:37:00 +00002013static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
2014 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
2015 // are set only strictly before `branch`. We can't use the eflags on long/FP
2016 // conditions if they are materialized due to the complex branching.
2017 return cond->IsCondition() &&
2018 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002019 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
2020 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00002021}
2022
Mark Mendell152408f2015-12-31 12:28:50 -05002023template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002024void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002025 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05002026 LabelType* true_target,
2027 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002028 HInstruction* cond = instruction->InputAt(condition_input_index);
2029
2030 if (true_target == nullptr && false_target == nullptr) {
2031 // Nothing to do. The code always falls through.
2032 return;
2033 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002034 // Constant condition, statically compared against "true" (integer value 1).
2035 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002036 if (true_target != nullptr) {
2037 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002038 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002039 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002040 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002041 if (false_target != nullptr) {
2042 __ jmp(false_target);
2043 }
2044 }
2045 return;
2046 }
2047
2048 // The following code generates these patterns:
2049 // (1) true_target == nullptr && false_target != nullptr
2050 // - opposite condition true => branch to false_target
2051 // (2) true_target != nullptr && false_target == nullptr
2052 // - condition true => branch to true_target
2053 // (3) true_target != nullptr && false_target != nullptr
2054 // - condition true => branch to true_target
2055 // - branch to false_target
2056 if (IsBooleanValueOrMaterializedCondition(cond)) {
2057 if (AreEflagsSetFrom(cond, instruction)) {
2058 if (true_target == nullptr) {
2059 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
2060 } else {
2061 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
2062 }
2063 } else {
2064 // Materialized condition, compare against 0.
2065 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
2066 if (lhs.IsRegister()) {
2067 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
2068 } else {
2069 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
2070 }
2071 if (true_target == nullptr) {
2072 __ j(kEqual, false_target);
2073 } else {
2074 __ j(kNotEqual, true_target);
2075 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002076 }
2077 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002078 // Condition has not been materialized, use its inputs as the comparison and
2079 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04002080 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00002081
2082 // If this is a long or FP comparison that has been folded into
2083 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002084 DataType::Type type = condition->InputAt(0)->GetType();
2085 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00002086 GenerateCompareTestAndBranch(condition, true_target, false_target);
2087 return;
2088 }
2089
2090 Location lhs = condition->GetLocations()->InAt(0);
2091 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002092 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002093 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002094 if (true_target == nullptr) {
2095 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
2096 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04002097 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07002098 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002099 }
David Brazdil0debae72015-11-12 18:37:00 +00002100
2101 // If neither branch falls through (case 3), the conditional branch to `true_target`
2102 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2103 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002104 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002105 }
2106}
2107
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002108void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002109 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002110 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002111 locations->SetInAt(0, Location::Any());
2112 }
2113}
2114
2115void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002116 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2117 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2118 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2119 nullptr : codegen_->GetLabelOf(true_successor);
2120 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2121 nullptr : codegen_->GetLabelOf(false_successor);
Andreas Gampe3db70682018-12-26 15:12:03 -08002122 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002123}
2124
2125void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002126 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002127 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01002128 InvokeRuntimeCallingConvention calling_convention;
2129 RegisterSet caller_saves = RegisterSet::Empty();
2130 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2131 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00002132 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002133 locations->SetInAt(0, Location::Any());
2134 }
2135}
2136
2137void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002138 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00002139 GenerateTestAndBranch<Label>(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08002140 /* condition_input_index= */ 0,
David Brazdil74eb1b22015-12-14 11:44:01 +00002141 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08002142 /* false_target= */ nullptr);
David Brazdil74eb1b22015-12-14 11:44:01 +00002143}
2144
Mingyao Yang063fc772016-08-02 11:02:54 -07002145void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002146 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07002147 LocationSummary(flag, LocationSummary::kNoCall);
2148 locations->SetOut(Location::RequiresRegister());
2149}
2150
2151void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2152 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
2153 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
2154}
2155
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002156static bool SelectCanUseCMOV(HSelect* select) {
2157 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002158 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002159 return false;
2160 }
2161
2162 // A FP condition doesn't generate the single CC that we need.
2163 // In 32 bit mode, a long condition doesn't generate a single CC either.
2164 HInstruction* condition = select->GetCondition();
2165 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002166 DataType::Type compare_type = condition->InputAt(0)->GetType();
2167 if (compare_type == DataType::Type::kInt64 ||
2168 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002169 return false;
2170 }
2171 }
2172
2173 // We can generate a CMOV for this Select.
2174 return true;
2175}
2176
David Brazdil74eb1b22015-12-14 11:44:01 +00002177void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002178 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002179 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00002180 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002181 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00002182 } else {
2183 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002184 if (SelectCanUseCMOV(select)) {
2185 if (select->InputAt(1)->IsConstant()) {
2186 // Cmov can't handle a constant value.
2187 locations->SetInAt(1, Location::RequiresRegister());
2188 } else {
2189 locations->SetInAt(1, Location::Any());
2190 }
2191 } else {
2192 locations->SetInAt(1, Location::Any());
2193 }
David Brazdil74eb1b22015-12-14 11:44:01 +00002194 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002195 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2196 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00002197 }
2198 locations->SetOut(Location::SameAsFirstInput());
2199}
2200
2201void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
2202 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002203 DCHECK(locations->InAt(0).Equals(locations->Out()));
2204 if (SelectCanUseCMOV(select)) {
2205 // If both the condition and the source types are integer, we can generate
2206 // a CMOV to implement Select.
2207
2208 HInstruction* select_condition = select->GetCondition();
2209 Condition cond = kNotEqual;
2210
2211 // Figure out how to test the 'condition'.
2212 if (select_condition->IsCondition()) {
2213 HCondition* condition = select_condition->AsCondition();
2214 if (!condition->IsEmittedAtUseSite()) {
2215 // This was a previously materialized condition.
2216 // Can we use the existing condition code?
2217 if (AreEflagsSetFrom(condition, select)) {
2218 // Materialization was the previous instruction. Condition codes are right.
2219 cond = X86Condition(condition->GetCondition());
2220 } else {
2221 // No, we have to recreate the condition code.
2222 Register cond_reg = locations->InAt(2).AsRegister<Register>();
2223 __ testl(cond_reg, cond_reg);
2224 }
2225 } else {
2226 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002227 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
2228 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002229 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01002230 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002231 cond = X86Condition(condition->GetCondition());
2232 }
2233 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002234 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002235 Register cond_reg = locations->InAt(2).AsRegister<Register>();
2236 __ testl(cond_reg, cond_reg);
2237 }
2238
2239 // If the condition is true, overwrite the output, which already contains false.
2240 Location false_loc = locations->InAt(0);
2241 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002242 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002243 // 64 bit conditional move.
2244 Register false_high = false_loc.AsRegisterPairHigh<Register>();
2245 Register false_low = false_loc.AsRegisterPairLow<Register>();
2246 if (true_loc.IsRegisterPair()) {
2247 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
2248 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
2249 } else {
2250 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
2251 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
2252 }
2253 } else {
2254 // 32 bit conditional move.
2255 Register false_reg = false_loc.AsRegister<Register>();
2256 if (true_loc.IsRegister()) {
2257 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
2258 } else {
2259 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
2260 }
2261 }
2262 } else {
2263 NearLabel false_target;
2264 GenerateTestAndBranch<NearLabel>(
Andreas Gampe3db70682018-12-26 15:12:03 -08002265 select, /* condition_input_index= */ 2, /* true_target= */ nullptr, &false_target);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002266 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2267 __ Bind(&false_target);
2268 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002269}
2270
Santiago Aboy Solanescaf9b652022-06-24 10:03:30 +01002271void LocationsBuilderX86::VisitNop(HNop* nop) {
2272 new (GetGraph()->GetAllocator()) LocationSummary(nop);
David Srbecky0cf44932015-12-09 14:09:59 +00002273}
2274
Santiago Aboy Solanescaf9b652022-06-24 10:03:30 +01002275void InstructionCodeGeneratorX86::VisitNop(HNop*) {
2276 // The environment recording already happened in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002277}
2278
Vladimir Markodec78172020-06-19 15:31:23 +01002279void CodeGeneratorX86::IncreaseFrame(size_t adjustment) {
2280 __ subl(ESP, Immediate(adjustment));
2281 __ cfi().AdjustCFAOffset(adjustment);
2282}
2283
2284void CodeGeneratorX86::DecreaseFrame(size_t adjustment) {
2285 __ addl(ESP, Immediate(adjustment));
2286 __ cfi().AdjustCFAOffset(-adjustment);
2287}
2288
David Srbeckyc7098ff2016-02-09 14:30:11 +00002289void CodeGeneratorX86::GenerateNop() {
2290 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002291}
2292
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002293void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002294 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002295 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04002296 // Handle the long/FP comparisons made in instruction simplification.
2297 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002298 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04002299 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05002300 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00002301 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002302 locations->SetOut(Location::RequiresRegister());
2303 }
2304 break;
2305 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002306 case DataType::Type::kFloat32:
2307 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04002308 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002309 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
2310 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
2311 } else if (cond->InputAt(1)->IsConstant()) {
2312 locations->SetInAt(1, Location::RequiresFpuRegister());
2313 } else {
2314 locations->SetInAt(1, Location::Any());
2315 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002316 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002317 locations->SetOut(Location::RequiresRegister());
2318 }
2319 break;
2320 }
2321 default:
2322 locations->SetInAt(0, Location::RequiresRegister());
2323 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00002324 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002325 // We need a byte register.
2326 locations->SetOut(Location::RegisterLocation(ECX));
2327 }
2328 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002329 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002330}
2331
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002332void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002333 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002334 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002335 }
Mark Mendellc4701932015-04-10 13:18:51 -04002336
2337 LocationSummary* locations = cond->GetLocations();
2338 Location lhs = locations->InAt(0);
2339 Location rhs = locations->InAt(1);
2340 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05002341 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002342
2343 switch (cond->InputAt(0)->GetType()) {
2344 default: {
2345 // Integer case.
2346
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002347 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04002348 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01002349 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07002350 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04002351 return;
2352 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002353 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04002354 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2355 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002356 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002357 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04002358 GenerateFPJumps(cond, &true_label, &false_label);
2359 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002360 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002361 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04002362 GenerateFPJumps(cond, &true_label, &false_label);
2363 break;
2364 }
2365
2366 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002367 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002368
Roland Levillain4fa13f62015-07-06 18:11:54 +01002369 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04002370 __ Bind(&false_label);
2371 __ xorl(reg, reg);
2372 __ jmp(&done_label);
2373
Roland Levillain4fa13f62015-07-06 18:11:54 +01002374 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04002375 __ Bind(&true_label);
2376 __ movl(reg, Immediate(1));
2377 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002378}
2379
2380void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002381 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002382}
2383
2384void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002385 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002386}
2387
2388void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002389 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002390}
2391
2392void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002393 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002394}
2395
2396void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002397 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002398}
2399
2400void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002401 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002402}
2403
2404void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002405 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002406}
2407
2408void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002409 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002410}
2411
2412void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002413 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002414}
2415
2416void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002417 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002418}
2419
2420void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002421 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002422}
2423
2424void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002425 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002426}
2427
Aart Bike9f37602015-10-09 11:15:55 -07002428void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002429 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002430}
2431
2432void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002433 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002434}
2435
2436void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002437 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002438}
2439
2440void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002441 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002442}
2443
2444void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002445 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002446}
2447
2448void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002449 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002450}
2451
2452void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002453 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002454}
2455
2456void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002457 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002458}
2459
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002460void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002461 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002462 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002463 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002464}
2465
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002466void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002467 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002468}
2469
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002470void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2471 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002472 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002473 locations->SetOut(Location::ConstantLocation(constant));
2474}
2475
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002476void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002477 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002478}
2479
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002480void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002481 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002482 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002483 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002484}
2485
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002486void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002487 // Will be generated at use site.
2488}
2489
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002490void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2491 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002492 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002493 locations->SetOut(Location::ConstantLocation(constant));
2494}
2495
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002496void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002497 // Will be generated at use site.
2498}
2499
2500void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2501 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002502 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002503 locations->SetOut(Location::ConstantLocation(constant));
2504}
2505
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002506void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002507 // Will be generated at use site.
2508}
2509
Igor Murashkind01745e2017-04-05 16:40:31 -07002510void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2511 constructor_fence->SetLocations(nullptr);
2512}
2513
2514void InstructionCodeGeneratorX86::VisitConstructorFence(
2515 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2516 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2517}
2518
Calin Juravle27df7582015-04-17 19:12:31 +01002519void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2520 memory_barrier->SetLocations(nullptr);
2521}
2522
2523void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002524 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002525}
2526
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002527void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002528 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002529}
2530
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002531void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002532 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002533}
2534
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002535void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002536 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002537 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Mythri Alle5097f832021-11-02 14:52:30 +00002538 SetInForReturnValue(ret, locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002539}
2540
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002541void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002542 switch (ret->InputAt(0)->GetType()) {
2543 case DataType::Type::kReference:
2544 case DataType::Type::kBool:
2545 case DataType::Type::kUint8:
2546 case DataType::Type::kInt8:
2547 case DataType::Type::kUint16:
2548 case DataType::Type::kInt16:
2549 case DataType::Type::kInt32:
2550 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
2551 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002552
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002553 case DataType::Type::kInt64:
2554 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2555 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
2556 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002557
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002558 case DataType::Type::kFloat32:
2559 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2560 if (GetGraph()->IsCompilingOsr()) {
2561 // To simplify callers of an OSR method, we put the return value in both
2562 // floating point and core registers.
2563 __ movd(EAX, XMM0);
2564 }
2565 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002566
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002567 case DataType::Type::kFloat64:
2568 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2569 if (GetGraph()->IsCompilingOsr()) {
2570 // To simplify callers of an OSR method, we put the return value in both
2571 // floating point and core registers.
2572 __ movd(EAX, XMM0);
2573 // Use XMM1 as temporary register to not clobber XMM0.
2574 __ movaps(XMM1, XMM0);
2575 __ psrlq(XMM1, Immediate(32));
2576 __ movd(EDX, XMM1);
2577 }
2578 break;
2579
2580 default:
2581 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002582 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002583 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002584}
2585
Calin Juravle175dc732015-08-25 15:42:32 +01002586void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2587 // The trampoline uses the same calling convention as dex calling conventions,
2588 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2589 // the method_idx.
2590 HandleInvoke(invoke);
2591}
2592
2593void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2594 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2595}
2596
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002597void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002598 // Explicit clinit checks triggered by static invokes must have been pruned by
2599 // art::PrepareForRegisterAllocation.
2600 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002601
Mark Mendellfb8d2792015-03-31 22:16:59 -04002602 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002603 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002604 if (invoke->GetLocations()->CanCall() &&
2605 invoke->HasPcRelativeMethodLoadKind() &&
2606 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).IsInvalid()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002607 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002608 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002609 return;
2610 }
2611
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01002612 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
Vladimir Marko86c87522020-05-11 16:55:55 +01002613 CriticalNativeCallingConventionVisitorX86 calling_convention_visitor(
2614 /*for_register_allocation=*/ true);
2615 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2616 } else {
2617 HandleInvoke(invoke);
2618 }
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002619
Vladimir Marko86c87522020-05-11 16:55:55 +01002620 // For PC-relative load kinds the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002621 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002622 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002623 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002624}
2625
Mark Mendell09ed1a32015-03-25 08:30:06 -04002626static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2627 if (invoke->GetLocations()->Intrinsified()) {
2628 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2629 intrinsic.Dispatch(invoke);
2630 return true;
2631 }
2632 return false;
2633}
2634
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002635void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002636 // Explicit clinit checks triggered by static invokes must have been pruned by
2637 // art::PrepareForRegisterAllocation.
2638 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002639
Mark Mendell09ed1a32015-03-25 08:30:06 -04002640 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2641 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002642 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002643
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002644 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002645 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002646 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002647}
2648
2649void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002650 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2651 if (intrinsic.TryDispatch(invoke)) {
2652 return;
2653 }
2654
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002655 HandleInvoke(invoke);
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002656
2657 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002658 // Add one temporary for inline cache update.
2659 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2660 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002661}
2662
2663void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002664 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002665 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002666}
2667
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002668void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002669 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2670 return;
2671 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002672
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002673 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002674 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002675}
2676
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002677void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002678 // This call to HandleInvoke allocates a temporary (core) register
2679 // which is also used to transfer the hidden argument from FP to
2680 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002681 HandleInvoke(invoke);
2682 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002683 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002684
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002685 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002686 // Add one temporary for inline cache update.
2687 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2688 }
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01002689
2690 // For PC-relative load kinds the invoke has an extra input, the PC-relative address base.
2691 if (IsPcRelativeMethodLoadKind(invoke->GetHiddenArgumentLoadKind())) {
2692 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2693 }
2694
2695 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
2696 invoke->GetLocations()->SetInAt(invoke->GetNumberOfArguments() - 1,
2697 Location::RequiresRegister());
2698 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002699}
2700
2701void CodeGeneratorX86::MaybeGenerateInlineCacheCheck(HInstruction* instruction, Register klass) {
2702 DCHECK_EQ(EAX, klass);
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002703 // We know the destination of an intrinsic, so no need to record inline
2704 // caches (also the intrinsic location builder doesn't request an additional
2705 // temporary).
2706 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002707 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002708 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002709 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
Nicolas Geoffray9e598902021-11-19 14:53:07 +00002710 ProfilingInfo* info = GetGraph()->GetProfilingInfo();
2711 DCHECK(info != nullptr);
2712 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
2713 uint32_t address = reinterpret_cast32<uint32_t>(cache);
2714 if (kIsDebugBuild) {
2715 uint32_t temp_index = instruction->GetLocations()->GetTempCount() - 1u;
2716 CHECK_EQ(EBP, instruction->GetLocations()->GetTemp(temp_index).AsRegister<Register>());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002717 }
Nicolas Geoffray9e598902021-11-19 14:53:07 +00002718 Register temp = EBP;
2719 NearLabel done;
2720 __ movl(temp, Immediate(address));
2721 // Fast path for a monomorphic cache.
2722 __ cmpl(klass, Address(temp, InlineCache::ClassesOffset().Int32Value()));
2723 __ j(kEqual, &done);
2724 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(kQuickUpdateInlineCache).Int32Value());
2725 __ Bind(&done);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002726 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002727}
2728
2729void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2730 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002731 LocationSummary* locations = invoke->GetLocations();
2732 Register temp = locations->GetTemp(0).AsRegister<Register>();
2733 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002734 Location receiver = locations->InAt(0);
2735 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2736
Roland Levillain0d5a2812015-11-13 10:07:31 +00002737 // Set the hidden argument. This is safe to do this here, as XMM7
2738 // won't be modified thereafter, before the `call` instruction.
2739 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01002740 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
2741 __ movd(hidden_reg, locations->InAt(invoke->GetNumberOfArguments() - 1).AsRegister<Register>());
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00002742 } else if (invoke->GetHiddenArgumentLoadKind() != MethodLoadKind::kRuntimeCall) {
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01002743 codegen_->LoadMethod(invoke->GetHiddenArgumentLoadKind(), locations->GetTemp(0), invoke);
2744 __ movd(hidden_reg, temp);
2745 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002746
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002747 if (receiver.IsStackSlot()) {
2748 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002749 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002750 __ movl(temp, Address(temp, class_offset));
2751 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002752 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002753 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002754 }
Roland Levillain4d027112015-07-01 15:41:14 +01002755 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002756 // Instead of simply (possibly) unpoisoning `temp` here, we should
2757 // emit a read barrier for the previous class reference load.
2758 // However this is not required in practice, as this is an
2759 // intermediate/temporary reference and because the current
2760 // concurrent copying collector keeps the from-space memory
2761 // intact/accessible until the end of the marking phase (the
2762 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002763 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002764
2765 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
2766
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002767 // temp = temp->GetAddressOfIMT()
2768 __ movl(temp,
2769 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002770 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002771 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002772 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002773 __ movl(temp, Address(temp, method_offset));
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00002774 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRuntimeCall) {
2775 // We pass the method from the IMT in case of a conflict. This will ensure
2776 // we go into the runtime to resolve the actual method.
2777 __ movd(hidden_reg, temp);
2778 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002779 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002780 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002781 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002782
2783 DCHECK(!codegen_->IsLeafMethod());
2784 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2785}
2786
Orion Hodsonac141392017-01-13 11:53:47 +00002787void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00002788 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2789 if (intrinsic.TryDispatch(invoke)) {
2790 return;
2791 }
Orion Hodsonac141392017-01-13 11:53:47 +00002792 HandleInvoke(invoke);
2793}
2794
2795void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00002796 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2797 return;
2798 }
Orion Hodsonac141392017-01-13 11:53:47 +00002799 codegen_->GenerateInvokePolymorphicCall(invoke);
2800}
2801
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002802void LocationsBuilderX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2803 HandleInvoke(invoke);
2804}
2805
2806void InstructionCodeGeneratorX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2807 codegen_->GenerateInvokeCustomCall(invoke);
2808}
2809
Roland Levillain88cb1752014-10-20 16:36:47 +01002810void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2811 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002812 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002813 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002814 case DataType::Type::kInt32:
2815 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002816 locations->SetInAt(0, Location::RequiresRegister());
2817 locations->SetOut(Location::SameAsFirstInput());
2818 break;
2819
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002820 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002821 locations->SetInAt(0, Location::RequiresFpuRegister());
2822 locations->SetOut(Location::SameAsFirstInput());
2823 locations->AddTemp(Location::RequiresRegister());
2824 locations->AddTemp(Location::RequiresFpuRegister());
2825 break;
2826
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002827 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002828 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002829 locations->SetOut(Location::SameAsFirstInput());
2830 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002831 break;
2832
2833 default:
2834 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2835 }
2836}
2837
2838void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2839 LocationSummary* locations = neg->GetLocations();
2840 Location out = locations->Out();
2841 Location in = locations->InAt(0);
2842 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002843 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002844 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002845 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002846 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002847 break;
2848
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002849 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002850 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002851 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002852 __ negl(out.AsRegisterPairLow<Register>());
2853 // Negation is similar to subtraction from zero. The least
2854 // significant byte triggers a borrow when it is different from
2855 // zero; to take it into account, add 1 to the most significant
2856 // byte if the carry flag (CF) is set to 1 after the first NEGL
2857 // operation.
2858 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2859 __ negl(out.AsRegisterPairHigh<Register>());
2860 break;
2861
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002862 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002863 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002864 Register constant = locations->GetTemp(0).AsRegister<Register>();
2865 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002866 // Implement float negation with an exclusive or with value
2867 // 0x80000000 (mask for bit 31, representing the sign of a
2868 // single-precision floating-point number).
2869 __ movl(constant, Immediate(INT32_C(0x80000000)));
2870 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002871 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002872 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002873 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002874
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002875 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002876 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002877 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002878 // Implement double negation with an exclusive or with value
2879 // 0x8000000000000000 (mask for bit 63, representing the sign of
2880 // a double-precision floating-point number).
2881 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002882 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002883 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002884 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002885
2886 default:
2887 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2888 }
2889}
2890
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002891void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2892 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002893 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002894 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002895 locations->SetInAt(0, Location::RequiresFpuRegister());
2896 locations->SetInAt(1, Location::RequiresRegister());
2897 locations->SetOut(Location::SameAsFirstInput());
2898 locations->AddTemp(Location::RequiresFpuRegister());
2899}
2900
2901void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2902 LocationSummary* locations = neg->GetLocations();
2903 Location out = locations->Out();
2904 DCHECK(locations->InAt(0).Equals(out));
2905
2906 Register constant_area = locations->InAt(1).AsRegister<Register>();
2907 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002908 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002909 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2910 neg->GetBaseMethodAddress(),
2911 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002912 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2913 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002914 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2915 neg->GetBaseMethodAddress(),
2916 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002917 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2918 }
2919}
2920
Roland Levillaindff1f282014-11-05 14:15:05 +00002921void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002922 DataType::Type result_type = conversion->GetResultType();
2923 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002924 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2925 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002926
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002927 // The float-to-long and double-to-long type conversions rely on a
2928 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002929 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002930 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2931 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002932 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002933 : LocationSummary::kNoCall;
2934 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002935 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002936
Roland Levillaindff1f282014-11-05 14:15:05 +00002937 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002938 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002939 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002940 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002941 case DataType::Type::kUint8:
2942 case DataType::Type::kInt8:
2943 case DataType::Type::kUint16:
2944 case DataType::Type::kInt16:
2945 case DataType::Type::kInt32:
2946 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2947 // Make the output overlap to please the register allocator. This greatly simplifies
2948 // the validation of the linear scan implementation
2949 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2950 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002951 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002952 HInstruction* input = conversion->InputAt(0);
2953 Location input_location = input->IsConstant()
2954 ? Location::ConstantLocation(input->AsConstant())
2955 : Location::RegisterPairLocation(EAX, EDX);
2956 locations->SetInAt(0, input_location);
2957 // Make the output overlap to please the register allocator. This greatly simplifies
2958 // the validation of the linear scan implementation
2959 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2960 break;
2961 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002962
2963 default:
2964 LOG(FATAL) << "Unexpected type conversion from " << input_type
2965 << " to " << result_type;
2966 }
2967 break;
2968
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002969 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002970 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002971 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2972 locations->SetInAt(0, Location::Any());
2973 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002974 break;
2975
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002976 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002977 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002978 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002979 locations->SetInAt(0, Location::Any());
2980 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2981 break;
2982
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002983 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002984 locations->SetInAt(0, Location::RequiresFpuRegister());
2985 locations->SetOut(Location::RequiresRegister());
2986 locations->AddTemp(Location::RequiresFpuRegister());
2987 break;
2988
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002989 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002990 locations->SetInAt(0, Location::RequiresFpuRegister());
2991 locations->SetOut(Location::RequiresRegister());
2992 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002993 break;
2994
2995 default:
2996 LOG(FATAL) << "Unexpected type conversion from " << input_type
2997 << " to " << result_type;
2998 }
2999 break;
3000
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003001 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00003002 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003003 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003004 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003005 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003006 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003007 case DataType::Type::kInt16:
3008 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00003009 locations->SetInAt(0, Location::RegisterLocation(EAX));
3010 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3011 break;
3012
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003013 case DataType::Type::kFloat32:
3014 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00003015 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00003016 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
3017 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
3018
Vladimir Marko949c91f2015-01-27 10:48:44 +00003019 // The runtime helper puts the result in EAX, EDX.
3020 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00003021 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00003022 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00003023
3024 default:
3025 LOG(FATAL) << "Unexpected type conversion from " << input_type
3026 << " to " << result_type;
3027 }
3028 break;
3029
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003030 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00003031 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003032 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003033 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003034 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003035 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003036 case DataType::Type::kInt16:
3037 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00003038 locations->SetInAt(0, Location::RequiresRegister());
3039 locations->SetOut(Location::RequiresFpuRegister());
3040 break;
3041
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003042 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01003043 locations->SetInAt(0, Location::Any());
3044 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00003045 break;
3046
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003047 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003048 locations->SetInAt(0, Location::RequiresFpuRegister());
3049 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00003050 break;
3051
3052 default:
3053 LOG(FATAL) << "Unexpected type conversion from " << input_type
3054 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003055 }
Roland Levillaincff13742014-11-17 14:32:17 +00003056 break;
3057
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003058 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00003059 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003060 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003061 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003062 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003063 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003064 case DataType::Type::kInt16:
3065 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00003066 locations->SetInAt(0, Location::RequiresRegister());
3067 locations->SetOut(Location::RequiresFpuRegister());
3068 break;
3069
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003070 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01003071 locations->SetInAt(0, Location::Any());
3072 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00003073 break;
3074
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003075 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003076 locations->SetInAt(0, Location::RequiresFpuRegister());
3077 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00003078 break;
3079
3080 default:
3081 LOG(FATAL) << "Unexpected type conversion from " << input_type
3082 << " to " << result_type;
3083 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003084 break;
3085
3086 default:
3087 LOG(FATAL) << "Unexpected type conversion from " << input_type
3088 << " to " << result_type;
3089 }
3090}
3091
3092void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
3093 LocationSummary* locations = conversion->GetLocations();
3094 Location out = locations->Out();
3095 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003096 DataType::Type result_type = conversion->GetResultType();
3097 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003098 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3099 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00003100 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003101 case DataType::Type::kUint8:
3102 switch (input_type) {
3103 case DataType::Type::kInt8:
3104 case DataType::Type::kUint16:
3105 case DataType::Type::kInt16:
3106 case DataType::Type::kInt32:
3107 if (in.IsRegister()) {
3108 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
3109 } else {
3110 DCHECK(in.GetConstant()->IsIntConstant());
3111 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
3112 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
3113 }
3114 break;
3115 case DataType::Type::kInt64:
3116 if (in.IsRegisterPair()) {
3117 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
3118 } else {
3119 DCHECK(in.GetConstant()->IsLongConstant());
3120 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3121 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
3122 }
3123 break;
3124
3125 default:
3126 LOG(FATAL) << "Unexpected type conversion from " << input_type
3127 << " to " << result_type;
3128 }
3129 break;
3130
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003131 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00003132 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003133 case DataType::Type::kUint8:
3134 case DataType::Type::kUint16:
3135 case DataType::Type::kInt16:
3136 case DataType::Type::kInt32:
3137 if (in.IsRegister()) {
3138 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
3139 } else {
3140 DCHECK(in.GetConstant()->IsIntConstant());
3141 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
3142 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
3143 }
3144 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003145 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003146 if (in.IsRegisterPair()) {
3147 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
3148 } else {
3149 DCHECK(in.GetConstant()->IsLongConstant());
3150 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3151 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
3152 }
3153 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003154
3155 default:
3156 LOG(FATAL) << "Unexpected type conversion from " << input_type
3157 << " to " << result_type;
3158 }
3159 break;
3160
3161 case DataType::Type::kUint16:
3162 switch (input_type) {
3163 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003164 case DataType::Type::kInt16:
3165 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003166 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003167 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
3168 } else if (in.IsStackSlot()) {
3169 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003170 } else {
3171 DCHECK(in.GetConstant()->IsIntConstant());
3172 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003173 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
3174 }
3175 break;
3176 case DataType::Type::kInt64:
3177 if (in.IsRegisterPair()) {
3178 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
3179 } else if (in.IsDoubleStackSlot()) {
3180 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
3181 } else {
3182 DCHECK(in.GetConstant()->IsLongConstant());
3183 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3184 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003185 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00003186 break;
3187
3188 default:
3189 LOG(FATAL) << "Unexpected type conversion from " << input_type
3190 << " to " << result_type;
3191 }
3192 break;
3193
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003194 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00003195 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003196 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003197 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00003198 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003199 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00003200 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003201 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00003202 } else {
3203 DCHECK(in.GetConstant()->IsIntConstant());
3204 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003205 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00003206 }
3207 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003208 case DataType::Type::kInt64:
3209 if (in.IsRegisterPair()) {
3210 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
3211 } else if (in.IsDoubleStackSlot()) {
3212 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
3213 } else {
3214 DCHECK(in.GetConstant()->IsLongConstant());
3215 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3216 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
3217 }
3218 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00003219
3220 default:
3221 LOG(FATAL) << "Unexpected type conversion from " << input_type
3222 << " to " << result_type;
3223 }
3224 break;
3225
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003226 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00003227 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003228 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00003229 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003230 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00003231 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003232 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00003233 } else {
3234 DCHECK(in.IsConstant());
3235 DCHECK(in.GetConstant()->IsLongConstant());
3236 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003237 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00003238 }
3239 break;
3240
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003241 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00003242 XmmRegister input = in.AsFpuRegister<XmmRegister>();
3243 Register output = out.AsRegister<Register>();
3244 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04003245 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00003246
3247 __ movl(output, Immediate(kPrimIntMax));
3248 // temp = int-to-float(output)
3249 __ cvtsi2ss(temp, output);
3250 // if input >= temp goto done
3251 __ comiss(input, temp);
3252 __ j(kAboveEqual, &done);
3253 // if input == NaN goto nan
3254 __ j(kUnordered, &nan);
3255 // output = float-to-int-truncate(input)
3256 __ cvttss2si(output, input);
3257 __ jmp(&done);
3258 __ Bind(&nan);
3259 // output = 0
3260 __ xorl(output, output);
3261 __ Bind(&done);
3262 break;
3263 }
3264
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003265 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003266 XmmRegister input = in.AsFpuRegister<XmmRegister>();
3267 Register output = out.AsRegister<Register>();
3268 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04003269 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003270
3271 __ movl(output, Immediate(kPrimIntMax));
3272 // temp = int-to-double(output)
3273 __ cvtsi2sd(temp, output);
3274 // if input >= temp goto done
3275 __ comisd(input, temp);
3276 __ j(kAboveEqual, &done);
3277 // if input == NaN goto nan
3278 __ j(kUnordered, &nan);
3279 // output = double-to-int-truncate(input)
3280 __ cvttsd2si(output, input);
3281 __ jmp(&done);
3282 __ Bind(&nan);
3283 // output = 0
3284 __ xorl(output, output);
3285 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00003286 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003287 }
Roland Levillain946e1432014-11-11 17:35:19 +00003288
3289 default:
3290 LOG(FATAL) << "Unexpected type conversion from " << input_type
3291 << " to " << result_type;
3292 }
3293 break;
3294
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003295 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00003296 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003297 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003298 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003299 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003300 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003301 case DataType::Type::kInt16:
3302 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00003303 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
3304 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003305 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00003306 __ cdq();
3307 break;
3308
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003309 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01003310 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003311 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00003312 break;
3313
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003314 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01003315 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003316 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00003317 break;
3318
3319 default:
3320 LOG(FATAL) << "Unexpected type conversion from " << input_type
3321 << " to " << result_type;
3322 }
3323 break;
3324
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003325 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00003326 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003327 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003328 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003329 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003330 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003331 case DataType::Type::kInt16:
3332 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003333 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003334 break;
3335
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003336 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003337 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00003338
Roland Levillain232ade02015-04-20 15:14:36 +01003339 // Create stack space for the call to
3340 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
3341 // TODO: enhance register allocator to ask for stack temporaries.
3342 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003343 adjustment = DataType::Size(DataType::Type::kInt64);
Vladimir Markodec78172020-06-19 15:31:23 +01003344 codegen_->IncreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003345 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003346
Roland Levillain232ade02015-04-20 15:14:36 +01003347 // Load the value to the FP stack, using temporaries if needed.
3348 PushOntoFPStack(in, 0, adjustment, false, true);
3349
3350 if (out.IsStackSlot()) {
3351 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
3352 } else {
3353 __ fstps(Address(ESP, 0));
3354 Location stack_temp = Location::StackSlot(0);
3355 codegen_->Move32(out, stack_temp);
3356 }
3357
3358 // Remove the temporary stack space we allocated.
3359 if (adjustment != 0) {
Vladimir Markodec78172020-06-19 15:31:23 +01003360 codegen_->DecreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003361 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003362 break;
3363 }
3364
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003365 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003366 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003367 break;
3368
3369 default:
3370 LOG(FATAL) << "Unexpected type conversion from " << input_type
3371 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003372 }
Roland Levillaincff13742014-11-17 14:32:17 +00003373 break;
3374
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003375 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00003376 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003377 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003378 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003379 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003380 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003381 case DataType::Type::kInt16:
3382 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003383 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003384 break;
3385
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003386 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003387 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00003388
Roland Levillain232ade02015-04-20 15:14:36 +01003389 // Create stack space for the call to
3390 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
3391 // TODO: enhance register allocator to ask for stack temporaries.
3392 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003393 adjustment = DataType::Size(DataType::Type::kInt64);
Vladimir Markodec78172020-06-19 15:31:23 +01003394 codegen_->IncreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003395 }
3396
3397 // Load the value to the FP stack, using temporaries if needed.
3398 PushOntoFPStack(in, 0, adjustment, false, true);
3399
3400 if (out.IsDoubleStackSlot()) {
3401 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
3402 } else {
3403 __ fstpl(Address(ESP, 0));
3404 Location stack_temp = Location::DoubleStackSlot(0);
3405 codegen_->Move64(out, stack_temp);
3406 }
3407
3408 // Remove the temporary stack space we allocated.
3409 if (adjustment != 0) {
Vladimir Markodec78172020-06-19 15:31:23 +01003410 codegen_->DecreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003411 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003412 break;
3413 }
3414
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003415 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003416 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003417 break;
3418
3419 default:
3420 LOG(FATAL) << "Unexpected type conversion from " << input_type
3421 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003422 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003423 break;
3424
3425 default:
3426 LOG(FATAL) << "Unexpected type conversion from " << input_type
3427 << " to " << result_type;
3428 }
3429}
3430
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003431void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003432 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003433 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003434 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003435 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05003436 locations->SetInAt(0, Location::RequiresRegister());
3437 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3438 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3439 break;
3440 }
3441
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003442 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003443 locations->SetInAt(0, Location::RequiresRegister());
3444 locations->SetInAt(1, Location::Any());
3445 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003446 break;
3447 }
3448
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003449 case DataType::Type::kFloat32:
3450 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003451 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003452 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3453 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003454 } else if (add->InputAt(1)->IsConstant()) {
3455 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003456 } else {
3457 locations->SetInAt(1, Location::Any());
3458 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003459 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003460 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003461 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003462
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003463 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003464 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Elliott Hughesc1896c92018-11-29 11:33:18 -08003465 UNREACHABLE();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003466 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003467}
3468
3469void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
3470 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003471 Location first = locations->InAt(0);
3472 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05003473 Location out = locations->Out();
3474
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003475 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003476 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003477 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003478 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3479 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003480 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3481 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05003482 } else {
3483 __ leal(out.AsRegister<Register>(), Address(
3484 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3485 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003486 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003487 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3488 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3489 __ addl(out.AsRegister<Register>(), Immediate(value));
3490 } else {
3491 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3492 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003493 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003494 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003495 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003496 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003497 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003498 }
3499
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003500 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003501 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003502 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3503 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003504 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003505 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3506 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003507 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003508 } else {
3509 DCHECK(second.IsConstant()) << second;
3510 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3511 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3512 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003513 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003514 break;
3515 }
3516
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003517 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003518 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003519 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003520 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3521 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003522 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003523 __ addss(first.AsFpuRegister<XmmRegister>(),
3524 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003525 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3526 const_area->GetBaseMethodAddress(),
3527 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003528 } else {
3529 DCHECK(second.IsStackSlot());
3530 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003531 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003532 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003533 }
3534
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003535 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003536 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003537 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003538 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3539 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003540 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003541 __ addsd(first.AsFpuRegister<XmmRegister>(),
3542 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003543 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3544 const_area->GetBaseMethodAddress(),
3545 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003546 } else {
3547 DCHECK(second.IsDoubleStackSlot());
3548 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003549 }
3550 break;
3551 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003552
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003553 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003554 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003555 }
3556}
3557
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003558void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003559 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003560 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003561 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003562 case DataType::Type::kInt32:
3563 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003564 locations->SetInAt(0, Location::RequiresRegister());
3565 locations->SetInAt(1, Location::Any());
3566 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003567 break;
3568 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003569 case DataType::Type::kFloat32:
3570 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003571 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003572 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3573 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003574 } else if (sub->InputAt(1)->IsConstant()) {
3575 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003576 } else {
3577 locations->SetInAt(1, Location::Any());
3578 }
Calin Juravle11351682014-10-23 15:38:15 +01003579 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003580 break;
Calin Juravle11351682014-10-23 15:38:15 +01003581 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003582
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003583 default:
Calin Juravle11351682014-10-23 15:38:15 +01003584 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003585 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003586}
3587
3588void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3589 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003590 Location first = locations->InAt(0);
3591 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003592 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003593 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003594 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003595 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003596 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003597 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003598 __ subl(first.AsRegister<Register>(),
3599 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003600 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003601 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003602 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003603 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003604 }
3605
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003606 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003607 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003608 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3609 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003610 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003611 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003612 __ sbbl(first.AsRegisterPairHigh<Register>(),
3613 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003614 } else {
3615 DCHECK(second.IsConstant()) << second;
3616 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3617 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3618 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003619 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003620 break;
3621 }
3622
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003623 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003624 if (second.IsFpuRegister()) {
3625 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3626 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3627 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003628 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003629 __ subss(first.AsFpuRegister<XmmRegister>(),
3630 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003631 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3632 const_area->GetBaseMethodAddress(),
3633 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003634 } else {
3635 DCHECK(second.IsStackSlot());
3636 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3637 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003638 break;
Calin Juravle11351682014-10-23 15:38:15 +01003639 }
3640
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003641 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003642 if (second.IsFpuRegister()) {
3643 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3644 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3645 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003646 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003647 __ subsd(first.AsFpuRegister<XmmRegister>(),
3648 codegen_->LiteralDoubleAddress(
3649 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003650 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003651 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3652 } else {
3653 DCHECK(second.IsDoubleStackSlot());
3654 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3655 }
Calin Juravle11351682014-10-23 15:38:15 +01003656 break;
3657 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003658
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003659 default:
Calin Juravle11351682014-10-23 15:38:15 +01003660 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003661 }
3662}
3663
Calin Juravle34bacdf2014-10-07 20:23:36 +01003664void LocationsBuilderX86::VisitMul(HMul* mul) {
3665 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003666 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003667 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003668 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003669 locations->SetInAt(0, Location::RequiresRegister());
3670 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003671 if (mul->InputAt(1)->IsIntConstant()) {
3672 // Can use 3 operand multiply.
3673 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3674 } else {
3675 locations->SetOut(Location::SameAsFirstInput());
3676 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003677 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003678 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003679 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003680 locations->SetInAt(1, Location::Any());
3681 locations->SetOut(Location::SameAsFirstInput());
3682 // Needed for imul on 32bits with 64bits output.
3683 locations->AddTemp(Location::RegisterLocation(EAX));
3684 locations->AddTemp(Location::RegisterLocation(EDX));
3685 break;
3686 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003687 case DataType::Type::kFloat32:
3688 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003689 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003690 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3691 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003692 } else if (mul->InputAt(1)->IsConstant()) {
3693 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003694 } else {
3695 locations->SetInAt(1, Location::Any());
3696 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003697 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003698 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003699 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003700
3701 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003702 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003703 }
3704}
3705
3706void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3707 LocationSummary* locations = mul->GetLocations();
3708 Location first = locations->InAt(0);
3709 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003710 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003711
3712 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003713 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003714 // The constant may have ended up in a register, so test explicitly to avoid
3715 // problems where the output may not be the same as the first operand.
3716 if (mul->InputAt(1)->IsIntConstant()) {
3717 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3718 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3719 } else if (second.IsRegister()) {
3720 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003721 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003722 } else {
3723 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003724 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003725 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003726 }
3727 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003728
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003729 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003730 Register in1_hi = first.AsRegisterPairHigh<Register>();
3731 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003732 Register eax = locations->GetTemp(0).AsRegister<Register>();
3733 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003734
3735 DCHECK_EQ(EAX, eax);
3736 DCHECK_EQ(EDX, edx);
3737
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003738 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003739 // output: in1
3740 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3741 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3742 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003743 if (second.IsConstant()) {
3744 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003745
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003746 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3747 int32_t low_value = Low32Bits(value);
3748 int32_t high_value = High32Bits(value);
3749 Immediate low(low_value);
3750 Immediate high(high_value);
3751
3752 __ movl(eax, high);
3753 // eax <- in1.lo * in2.hi
3754 __ imull(eax, in1_lo);
3755 // in1.hi <- in1.hi * in2.lo
3756 __ imull(in1_hi, low);
3757 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3758 __ addl(in1_hi, eax);
3759 // move in2_lo to eax to prepare for double precision
3760 __ movl(eax, low);
3761 // edx:eax <- in1.lo * in2.lo
3762 __ mull(in1_lo);
3763 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3764 __ addl(in1_hi, edx);
3765 // in1.lo <- (in1.lo * in2.lo)[31:0];
3766 __ movl(in1_lo, eax);
3767 } else if (second.IsRegisterPair()) {
3768 Register in2_hi = second.AsRegisterPairHigh<Register>();
3769 Register in2_lo = second.AsRegisterPairLow<Register>();
3770
3771 __ movl(eax, in2_hi);
3772 // eax <- in1.lo * in2.hi
3773 __ imull(eax, in1_lo);
3774 // in1.hi <- in1.hi * in2.lo
3775 __ imull(in1_hi, in2_lo);
3776 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3777 __ addl(in1_hi, eax);
3778 // move in1_lo to eax to prepare for double precision
3779 __ movl(eax, in1_lo);
3780 // edx:eax <- in1.lo * in2.lo
3781 __ mull(in2_lo);
3782 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3783 __ addl(in1_hi, edx);
3784 // in1.lo <- (in1.lo * in2.lo)[31:0];
3785 __ movl(in1_lo, eax);
3786 } else {
3787 DCHECK(second.IsDoubleStackSlot()) << second;
3788 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3789 Address in2_lo(ESP, second.GetStackIndex());
3790
3791 __ movl(eax, in2_hi);
3792 // eax <- in1.lo * in2.hi
3793 __ imull(eax, in1_lo);
3794 // in1.hi <- in1.hi * in2.lo
3795 __ imull(in1_hi, in2_lo);
3796 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3797 __ addl(in1_hi, eax);
3798 // move in1_lo to eax to prepare for double precision
3799 __ movl(eax, in1_lo);
3800 // edx:eax <- in1.lo * in2.lo
3801 __ mull(in2_lo);
3802 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3803 __ addl(in1_hi, edx);
3804 // in1.lo <- (in1.lo * in2.lo)[31:0];
3805 __ movl(in1_lo, eax);
3806 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003807
3808 break;
3809 }
3810
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003811 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003812 DCHECK(first.Equals(locations->Out()));
3813 if (second.IsFpuRegister()) {
3814 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3815 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3816 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003817 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003818 __ mulss(first.AsFpuRegister<XmmRegister>(),
3819 codegen_->LiteralFloatAddress(
3820 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003821 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003822 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3823 } else {
3824 DCHECK(second.IsStackSlot());
3825 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3826 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003827 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003828 }
3829
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003830 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003831 DCHECK(first.Equals(locations->Out()));
3832 if (second.IsFpuRegister()) {
3833 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3834 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3835 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003836 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003837 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3838 codegen_->LiteralDoubleAddress(
3839 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003840 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003841 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3842 } else {
3843 DCHECK(second.IsDoubleStackSlot());
3844 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3845 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003846 break;
3847 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003848
3849 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003850 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003851 }
3852}
3853
Roland Levillain232ade02015-04-20 15:14:36 +01003854void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3855 uint32_t temp_offset,
3856 uint32_t stack_adjustment,
3857 bool is_fp,
3858 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003859 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003860 DCHECK(!is_wide);
3861 if (is_fp) {
3862 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3863 } else {
3864 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3865 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003866 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003867 DCHECK(is_wide);
3868 if (is_fp) {
3869 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3870 } else {
3871 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3872 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003873 } else {
3874 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003875 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003876 Location stack_temp = Location::StackSlot(temp_offset);
3877 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003878 if (is_fp) {
3879 __ flds(Address(ESP, temp_offset));
3880 } else {
3881 __ filds(Address(ESP, temp_offset));
3882 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003883 } else {
3884 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3885 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003886 if (is_fp) {
3887 __ fldl(Address(ESP, temp_offset));
3888 } else {
3889 __ fildl(Address(ESP, temp_offset));
3890 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003891 }
3892 }
3893}
3894
3895void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003896 DataType::Type type = rem->GetResultType();
3897 bool is_float = type == DataType::Type::kFloat32;
3898 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003899 LocationSummary* locations = rem->GetLocations();
3900 Location first = locations->InAt(0);
3901 Location second = locations->InAt(1);
3902 Location out = locations->Out();
3903
3904 // Create stack space for 2 elements.
3905 // TODO: enhance register allocator to ask for stack temporaries.
Vladimir Markodec78172020-06-19 15:31:23 +01003906 codegen_->IncreaseFrame(2 * elem_size);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003907
3908 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003909 const bool is_wide = !is_float;
Andreas Gampe3db70682018-12-26 15:12:03 -08003910 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp= */ true, is_wide);
3911 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp= */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003912
3913 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003914 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003915 __ Bind(&retry);
3916 __ fprem();
3917
3918 // Move FP status to AX.
3919 __ fstsw();
3920
3921 // And see if the argument reduction is complete. This is signaled by the
3922 // C2 FPU flag bit set to 0.
3923 __ andl(EAX, Immediate(kC2ConditionMask));
3924 __ j(kNotEqual, &retry);
3925
3926 // We have settled on the final value. Retrieve it into an XMM register.
3927 // Store FP top of stack to real stack.
3928 if (is_float) {
3929 __ fsts(Address(ESP, 0));
3930 } else {
3931 __ fstl(Address(ESP, 0));
3932 }
3933
3934 // Pop the 2 items from the FP stack.
3935 __ fucompp();
3936
3937 // Load the value from the stack into an XMM register.
3938 DCHECK(out.IsFpuRegister()) << out;
3939 if (is_float) {
3940 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3941 } else {
3942 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3943 }
3944
3945 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01003946 codegen_->DecreaseFrame(2 * elem_size);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003947}
3948
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003949
3950void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3951 DCHECK(instruction->IsDiv() || instruction->IsRem());
3952
3953 LocationSummary* locations = instruction->GetLocations();
3954 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003955 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003956
3957 Register out_register = locations->Out().AsRegister<Register>();
3958 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003959 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003960
3961 DCHECK(imm == 1 || imm == -1);
3962
3963 if (instruction->IsRem()) {
3964 __ xorl(out_register, out_register);
3965 } else {
3966 __ movl(out_register, input_register);
3967 if (imm == -1) {
3968 __ negl(out_register);
3969 }
3970 }
3971}
3972
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303973void InstructionCodeGeneratorX86::RemByPowerOfTwo(HRem* instruction) {
3974 LocationSummary* locations = instruction->GetLocations();
3975 Location second = locations->InAt(1);
3976
3977 Register out = locations->Out().AsRegister<Register>();
3978 Register numerator = locations->InAt(0).AsRegister<Register>();
3979
3980 int32_t imm = Int64FromConstant(second.GetConstant());
3981 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3982 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3983
3984 Register tmp = locations->GetTemp(0).AsRegister<Register>();
3985 NearLabel done;
3986 __ movl(out, numerator);
3987 __ andl(out, Immediate(abs_imm-1));
3988 __ j(Condition::kZero, &done);
3989 __ leal(tmp, Address(out, static_cast<int32_t>(~(abs_imm-1))));
3990 __ testl(numerator, numerator);
3991 __ cmovl(Condition::kLess, out, tmp);
3992 __ Bind(&done);
3993}
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003994
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003995void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003996 LocationSummary* locations = instruction->GetLocations();
3997
3998 Register out_register = locations->Out().AsRegister<Register>();
3999 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01004000 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004001 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
4002 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004003
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004004 Register num = locations->GetTemp(0).AsRegister<Register>();
4005
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004006 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004007 __ testl(input_register, input_register);
4008 __ cmovl(kGreaterEqual, num, input_register);
4009 int shift = CTZ(imm);
4010 __ sarl(num, Immediate(shift));
4011
4012 if (imm < 0) {
4013 __ negl(num);
4014 }
4015
4016 __ movl(out_register, num);
4017}
4018
4019void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4020 DCHECK(instruction->IsDiv() || instruction->IsRem());
4021
4022 LocationSummary* locations = instruction->GetLocations();
4023 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
4024
4025 Register eax = locations->InAt(0).AsRegister<Register>();
4026 Register out = locations->Out().AsRegister<Register>();
4027 Register num;
4028 Register edx;
4029
4030 if (instruction->IsDiv()) {
4031 edx = locations->GetTemp(0).AsRegister<Register>();
4032 num = locations->GetTemp(1).AsRegister<Register>();
4033 } else {
4034 edx = locations->Out().AsRegister<Register>();
4035 num = locations->GetTemp(0).AsRegister<Register>();
4036 }
4037
4038 DCHECK_EQ(EAX, eax);
4039 DCHECK_EQ(EDX, edx);
4040 if (instruction->IsDiv()) {
4041 DCHECK_EQ(EAX, out);
4042 } else {
4043 DCHECK_EQ(EDX, out);
4044 }
4045
4046 int64_t magic;
4047 int shift;
Andreas Gampe3db70682018-12-26 15:12:03 -08004048 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004049
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004050 // Save the numerator.
4051 __ movl(num, eax);
4052
4053 // EAX = magic
4054 __ movl(eax, Immediate(magic));
4055
4056 // EDX:EAX = magic * numerator
4057 __ imull(num);
4058
4059 if (imm > 0 && magic < 0) {
4060 // EDX += num
4061 __ addl(edx, num);
4062 } else if (imm < 0 && magic > 0) {
4063 __ subl(edx, num);
4064 }
4065
4066 // Shift if needed.
4067 if (shift != 0) {
4068 __ sarl(edx, Immediate(shift));
4069 }
4070
4071 // EDX += 1 if EDX < 0
4072 __ movl(eax, edx);
4073 __ shrl(edx, Immediate(31));
4074 __ addl(edx, eax);
4075
4076 if (instruction->IsRem()) {
4077 __ movl(eax, num);
4078 __ imull(edx, Immediate(imm));
4079 __ subl(eax, edx);
4080 __ movl(edx, eax);
4081 } else {
4082 __ movl(eax, edx);
4083 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004084}
4085
Calin Juravlebacfec32014-11-14 15:54:36 +00004086void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
4087 DCHECK(instruction->IsDiv() || instruction->IsRem());
4088
4089 LocationSummary* locations = instruction->GetLocations();
4090 Location out = locations->Out();
4091 Location first = locations->InAt(0);
4092 Location second = locations->InAt(1);
4093 bool is_div = instruction->IsDiv();
4094
4095 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004096 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004097 DCHECK_EQ(EAX, first.AsRegister<Register>());
4098 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00004099
Vladimir Marko13c86fd2015-11-11 12:37:46 +00004100 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01004101 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004102
4103 if (imm == 0) {
4104 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
4105 } else if (imm == 1 || imm == -1) {
4106 DivRemOneOrMinusOne(instruction);
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05304107 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
4108 if (is_div) {
4109 DivByPowerOfTwo(instruction->AsDiv());
4110 } else {
4111 RemByPowerOfTwo(instruction->AsRem());
4112 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004113 } else {
4114 DCHECK(imm <= -2 || imm >= 2);
4115 GenerateDivRemWithAnyConstant(instruction);
4116 }
4117 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004118 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00004119 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004120 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00004121
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004122 Register second_reg = second.AsRegister<Register>();
4123 // 0x80000000/-1 triggers an arithmetic exception!
4124 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
4125 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00004126
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004127 __ cmpl(second_reg, Immediate(-1));
4128 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00004129
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004130 // edx:eax <- sign-extended of eax
4131 __ cdq();
4132 // eax = quotient, edx = remainder
4133 __ idivl(second_reg);
4134 __ Bind(slow_path->GetExitLabel());
4135 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004136 break;
4137 }
4138
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004139 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004140 InvokeRuntimeCallingConvention calling_convention;
4141 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
4142 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
4143 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
4144 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
4145 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
4146 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
4147
4148 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004149 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004150 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004151 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004152 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004153 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004154 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004155 break;
4156 }
4157
4158 default:
4159 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
4160 }
4161}
4162
Calin Juravle7c4954d2014-10-28 16:57:40 +00004163void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004164 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004165 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004166 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01004167 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004168
Calin Juravle7c4954d2014-10-28 16:57:40 +00004169 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004170 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00004171 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004172 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00004173 locations->SetOut(Location::SameAsFirstInput());
4174 // Intel uses edx:eax as the dividend.
4175 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004176 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
4177 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
4178 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01004179 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004180 locations->AddTemp(Location::RequiresRegister());
4181 }
Calin Juravled0d48522014-11-04 16:40:20 +00004182 break;
4183 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004184 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004185 InvokeRuntimeCallingConvention calling_convention;
4186 locations->SetInAt(0, Location::RegisterPairLocation(
4187 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4188 locations->SetInAt(1, Location::RegisterPairLocation(
4189 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4190 // Runtime helper puts the result in EAX, EDX.
4191 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00004192 break;
4193 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004194 case DataType::Type::kFloat32:
4195 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004196 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00004197 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
4198 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00004199 } else if (div->InputAt(1)->IsConstant()) {
4200 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00004201 } else {
4202 locations->SetInAt(1, Location::Any());
4203 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004204 locations->SetOut(Location::SameAsFirstInput());
4205 break;
4206 }
4207
4208 default:
4209 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4210 }
4211}
4212
4213void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
4214 LocationSummary* locations = div->GetLocations();
4215 Location first = locations->InAt(0);
4216 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004217
4218 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004219 case DataType::Type::kInt32:
4220 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004221 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004222 break;
4223 }
4224
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004225 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04004226 if (second.IsFpuRegister()) {
4227 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
4228 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
4229 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00004230 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04004231 __ divss(first.AsFpuRegister<XmmRegister>(),
4232 codegen_->LiteralFloatAddress(
4233 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004234 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04004235 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
4236 } else {
4237 DCHECK(second.IsStackSlot());
4238 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
4239 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004240 break;
4241 }
4242
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004243 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04004244 if (second.IsFpuRegister()) {
4245 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
4246 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
4247 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00004248 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04004249 __ divsd(first.AsFpuRegister<XmmRegister>(),
4250 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004251 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
4252 const_area->GetBaseMethodAddress(),
4253 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04004254 } else {
4255 DCHECK(second.IsDoubleStackSlot());
4256 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
4257 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004258 break;
4259 }
4260
4261 default:
4262 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4263 }
4264}
4265
Calin Juravlebacfec32014-11-14 15:54:36 +00004266void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004267 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004268
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004269 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004270 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004271 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01004272 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00004273
Calin Juravled2ec87d2014-12-08 14:24:46 +00004274 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004275 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004276 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004277 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00004278 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004279 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
4280 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
4281 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01004282 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004283 locations->AddTemp(Location::RequiresRegister());
4284 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004285 break;
4286 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004287 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004288 InvokeRuntimeCallingConvention calling_convention;
4289 locations->SetInAt(0, Location::RegisterPairLocation(
4290 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4291 locations->SetInAt(1, Location::RegisterPairLocation(
4292 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4293 // Runtime helper puts the result in EAX, EDX.
4294 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
4295 break;
4296 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004297 case DataType::Type::kFloat64:
4298 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05004299 locations->SetInAt(0, Location::Any());
4300 locations->SetInAt(1, Location::Any());
4301 locations->SetOut(Location::RequiresFpuRegister());
4302 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00004303 break;
4304 }
4305
4306 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004307 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004308 }
4309}
4310
4311void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004312 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00004313 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004314 case DataType::Type::kInt32:
4315 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004316 GenerateDivRemIntegral(rem);
4317 break;
4318 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004319 case DataType::Type::kFloat32:
4320 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05004321 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00004322 break;
4323 }
4324 default:
4325 LOG(FATAL) << "Unexpected rem type " << type;
4326 }
4327}
4328
Aart Bik1f8d51b2018-02-15 10:42:37 -08004329static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
4330 LocationSummary* locations = new (allocator) LocationSummary(minmax);
4331 switch (minmax->GetResultType()) {
4332 case DataType::Type::kInt32:
4333 locations->SetInAt(0, Location::RequiresRegister());
4334 locations->SetInAt(1, Location::RequiresRegister());
4335 locations->SetOut(Location::SameAsFirstInput());
4336 break;
4337 case DataType::Type::kInt64:
4338 locations->SetInAt(0, Location::RequiresRegister());
4339 locations->SetInAt(1, Location::RequiresRegister());
4340 locations->SetOut(Location::SameAsFirstInput());
4341 // Register to use to perform a long subtract to set cc.
4342 locations->AddTemp(Location::RequiresRegister());
4343 break;
4344 case DataType::Type::kFloat32:
4345 locations->SetInAt(0, Location::RequiresFpuRegister());
4346 locations->SetInAt(1, Location::RequiresFpuRegister());
4347 locations->SetOut(Location::SameAsFirstInput());
4348 locations->AddTemp(Location::RequiresRegister());
4349 break;
4350 case DataType::Type::kFloat64:
4351 locations->SetInAt(0, Location::RequiresFpuRegister());
4352 locations->SetInAt(1, Location::RequiresFpuRegister());
4353 locations->SetOut(Location::SameAsFirstInput());
4354 break;
4355 default:
4356 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
4357 }
4358}
4359
Aart Bik351df3e2018-03-07 11:54:57 -08004360void InstructionCodeGeneratorX86::GenerateMinMaxInt(LocationSummary* locations,
4361 bool is_min,
4362 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08004363 Location op1_loc = locations->InAt(0);
4364 Location op2_loc = locations->InAt(1);
4365
4366 // Shortcut for same input locations.
4367 if (op1_loc.Equals(op2_loc)) {
4368 // Can return immediately, as op1_loc == out_loc.
4369 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
4370 // a copy here.
4371 DCHECK(locations->Out().Equals(op1_loc));
4372 return;
4373 }
4374
4375 if (type == DataType::Type::kInt64) {
4376 // Need to perform a subtract to get the sign right.
4377 // op1 is already in the same location as the output.
4378 Location output = locations->Out();
4379 Register output_lo = output.AsRegisterPairLow<Register>();
4380 Register output_hi = output.AsRegisterPairHigh<Register>();
4381
4382 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
4383 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
4384
4385 // The comparison is performed by subtracting the second operand from
4386 // the first operand and then setting the status flags in the same
4387 // manner as the SUB instruction."
4388 __ cmpl(output_lo, op2_lo);
4389
4390 // Now use a temp and the borrow to finish the subtraction of op2_hi.
4391 Register temp = locations->GetTemp(0).AsRegister<Register>();
4392 __ movl(temp, output_hi);
4393 __ sbbl(temp, op2_hi);
4394
4395 // Now the condition code is correct.
4396 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
4397 __ cmovl(cond, output_lo, op2_lo);
4398 __ cmovl(cond, output_hi, op2_hi);
4399 } else {
4400 DCHECK_EQ(type, DataType::Type::kInt32);
4401 Register out = locations->Out().AsRegister<Register>();
4402 Register op2 = op2_loc.AsRegister<Register>();
4403
4404 // (out := op1)
4405 // out <=? op2
4406 // if out is min jmp done
4407 // out := op2
4408 // done:
4409
4410 __ cmpl(out, op2);
4411 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
4412 __ cmovl(cond, out, op2);
4413 }
4414}
4415
4416void InstructionCodeGeneratorX86::GenerateMinMaxFP(LocationSummary* locations,
4417 bool is_min,
4418 DataType::Type type) {
4419 Location op1_loc = locations->InAt(0);
4420 Location op2_loc = locations->InAt(1);
4421 Location out_loc = locations->Out();
4422 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
4423
4424 // Shortcut for same input locations.
4425 if (op1_loc.Equals(op2_loc)) {
4426 DCHECK(out_loc.Equals(op1_loc));
4427 return;
4428 }
4429
4430 // (out := op1)
4431 // out <=? op2
4432 // if Nan jmp Nan_label
4433 // if out is min jmp done
4434 // if op2 is min jmp op2_label
4435 // handle -0/+0
4436 // jmp done
4437 // Nan_label:
4438 // out := NaN
4439 // op2_label:
4440 // out := op2
4441 // done:
4442 //
4443 // This removes one jmp, but needs to copy one input (op1) to out.
4444 //
4445 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
4446
4447 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
4448
4449 NearLabel nan, done, op2_label;
4450 if (type == DataType::Type::kFloat64) {
4451 __ ucomisd(out, op2);
4452 } else {
4453 DCHECK_EQ(type, DataType::Type::kFloat32);
4454 __ ucomiss(out, op2);
4455 }
4456
4457 __ j(Condition::kParityEven, &nan);
4458
4459 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
4460 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
4461
4462 // Handle 0.0/-0.0.
4463 if (is_min) {
4464 if (type == DataType::Type::kFloat64) {
4465 __ orpd(out, op2);
4466 } else {
4467 __ orps(out, op2);
4468 }
4469 } else {
4470 if (type == DataType::Type::kFloat64) {
4471 __ andpd(out, op2);
4472 } else {
4473 __ andps(out, op2);
4474 }
4475 }
4476 __ jmp(&done);
4477
4478 // NaN handling.
4479 __ Bind(&nan);
4480 if (type == DataType::Type::kFloat64) {
4481 // TODO: Use a constant from the constant table (requires extra input).
4482 __ LoadLongConstant(out, kDoubleNaN);
4483 } else {
4484 Register constant = locations->GetTemp(0).AsRegister<Register>();
4485 __ movl(constant, Immediate(kFloatNaN));
4486 __ movd(out, constant);
4487 }
4488 __ jmp(&done);
4489
4490 // out := op2;
4491 __ Bind(&op2_label);
4492 if (type == DataType::Type::kFloat64) {
4493 __ movsd(out, op2);
4494 } else {
4495 __ movss(out, op2);
4496 }
4497
4498 // Done.
4499 __ Bind(&done);
4500}
4501
Aart Bik351df3e2018-03-07 11:54:57 -08004502void InstructionCodeGeneratorX86::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4503 DataType::Type type = minmax->GetResultType();
4504 switch (type) {
4505 case DataType::Type::kInt32:
4506 case DataType::Type::kInt64:
4507 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
4508 break;
4509 case DataType::Type::kFloat32:
4510 case DataType::Type::kFloat64:
4511 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
4512 break;
4513 default:
4514 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4515 }
4516}
4517
Aart Bik1f8d51b2018-02-15 10:42:37 -08004518void LocationsBuilderX86::VisitMin(HMin* min) {
4519 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4520}
4521
4522void InstructionCodeGeneratorX86::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004523 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004524}
4525
4526void LocationsBuilderX86::VisitMax(HMax* max) {
4527 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4528}
4529
4530void InstructionCodeGeneratorX86::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004531 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004532}
4533
Aart Bik3dad3412018-02-28 12:01:46 -08004534void LocationsBuilderX86::VisitAbs(HAbs* abs) {
4535 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4536 switch (abs->GetResultType()) {
4537 case DataType::Type::kInt32:
4538 locations->SetInAt(0, Location::RegisterLocation(EAX));
4539 locations->SetOut(Location::SameAsFirstInput());
4540 locations->AddTemp(Location::RegisterLocation(EDX));
4541 break;
4542 case DataType::Type::kInt64:
4543 locations->SetInAt(0, Location::RequiresRegister());
4544 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4545 locations->AddTemp(Location::RequiresRegister());
4546 break;
4547 case DataType::Type::kFloat32:
4548 locations->SetInAt(0, Location::RequiresFpuRegister());
4549 locations->SetOut(Location::SameAsFirstInput());
4550 locations->AddTemp(Location::RequiresFpuRegister());
4551 locations->AddTemp(Location::RequiresRegister());
4552 break;
4553 case DataType::Type::kFloat64:
4554 locations->SetInAt(0, Location::RequiresFpuRegister());
4555 locations->SetOut(Location::SameAsFirstInput());
4556 locations->AddTemp(Location::RequiresFpuRegister());
4557 break;
4558 default:
4559 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4560 }
4561}
4562
4563void InstructionCodeGeneratorX86::VisitAbs(HAbs* abs) {
4564 LocationSummary* locations = abs->GetLocations();
4565 switch (abs->GetResultType()) {
4566 case DataType::Type::kInt32: {
4567 Register out = locations->Out().AsRegister<Register>();
4568 DCHECK_EQ(out, EAX);
4569 Register temp = locations->GetTemp(0).AsRegister<Register>();
4570 DCHECK_EQ(temp, EDX);
4571 // Sign extend EAX into EDX.
4572 __ cdq();
4573 // XOR EAX with sign.
4574 __ xorl(EAX, EDX);
4575 // Subtract out sign to correct.
4576 __ subl(EAX, EDX);
4577 // The result is in EAX.
4578 break;
4579 }
4580 case DataType::Type::kInt64: {
4581 Location input = locations->InAt(0);
4582 Register input_lo = input.AsRegisterPairLow<Register>();
4583 Register input_hi = input.AsRegisterPairHigh<Register>();
4584 Location output = locations->Out();
4585 Register output_lo = output.AsRegisterPairLow<Register>();
4586 Register output_hi = output.AsRegisterPairHigh<Register>();
4587 Register temp = locations->GetTemp(0).AsRegister<Register>();
4588 // Compute the sign into the temporary.
4589 __ movl(temp, input_hi);
4590 __ sarl(temp, Immediate(31));
4591 // Store the sign into the output.
4592 __ movl(output_lo, temp);
4593 __ movl(output_hi, temp);
4594 // XOR the input to the output.
4595 __ xorl(output_lo, input_lo);
4596 __ xorl(output_hi, input_hi);
4597 // Subtract the sign.
4598 __ subl(output_lo, temp);
4599 __ sbbl(output_hi, temp);
4600 break;
4601 }
4602 case DataType::Type::kFloat32: {
4603 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4604 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4605 Register constant = locations->GetTemp(1).AsRegister<Register>();
4606 __ movl(constant, Immediate(INT32_C(0x7FFFFFFF)));
4607 __ movd(temp, constant);
4608 __ andps(out, temp);
4609 break;
4610 }
4611 case DataType::Type::kFloat64: {
4612 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4613 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4614 // TODO: Use a constant from the constant table (requires extra input).
4615 __ LoadLongConstant(temp, INT64_C(0x7FFFFFFFFFFFFFFF));
4616 __ andpd(out, temp);
4617 break;
4618 }
4619 default:
4620 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4621 }
4622}
4623
Calin Juravled0d48522014-11-04 16:40:20 +00004624void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004625 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004626 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004627 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004628 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004629 case DataType::Type::kInt8:
4630 case DataType::Type::kUint16:
4631 case DataType::Type::kInt16:
4632 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004633 locations->SetInAt(0, Location::Any());
4634 break;
4635 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004636 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004637 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
4638 if (!instruction->IsConstant()) {
4639 locations->AddTemp(Location::RequiresRegister());
4640 }
4641 break;
4642 }
4643 default:
4644 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4645 }
Calin Juravled0d48522014-11-04 16:40:20 +00004646}
4647
4648void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004649 SlowPathCode* slow_path =
4650 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004651 codegen_->AddSlowPath(slow_path);
4652
4653 LocationSummary* locations = instruction->GetLocations();
4654 Location value = locations->InAt(0);
4655
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004656 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004657 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004658 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004659 case DataType::Type::kInt8:
4660 case DataType::Type::kUint16:
4661 case DataType::Type::kInt16:
4662 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004663 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004664 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004665 __ j(kEqual, slow_path->GetEntryLabel());
4666 } else if (value.IsStackSlot()) {
4667 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
4668 __ j(kEqual, slow_path->GetEntryLabel());
4669 } else {
4670 DCHECK(value.IsConstant()) << value;
4671 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004672 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004673 }
4674 }
4675 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004676 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004677 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004678 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004679 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004680 __ movl(temp, value.AsRegisterPairLow<Register>());
4681 __ orl(temp, value.AsRegisterPairHigh<Register>());
4682 __ j(kEqual, slow_path->GetEntryLabel());
4683 } else {
4684 DCHECK(value.IsConstant()) << value;
4685 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4686 __ jmp(slow_path->GetEntryLabel());
4687 }
4688 }
4689 break;
4690 }
4691 default:
4692 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004693 }
Calin Juravled0d48522014-11-04 16:40:20 +00004694}
4695
Calin Juravle9aec02f2014-11-18 23:06:35 +00004696void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
4697 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4698
4699 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004700 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004701
4702 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004703 case DataType::Type::kInt32:
4704 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004705 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00004706 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00004707 // The shift count needs to be in CL or a constant.
4708 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004709 locations->SetOut(Location::SameAsFirstInput());
4710 break;
4711 }
4712 default:
4713 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4714 }
4715}
4716
4717void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
4718 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4719
4720 LocationSummary* locations = op->GetLocations();
4721 Location first = locations->InAt(0);
4722 Location second = locations->InAt(1);
4723 DCHECK(first.Equals(locations->Out()));
4724
4725 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004726 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00004727 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004728 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004729 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004730 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004731 DCHECK_EQ(ECX, second_reg);
4732 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004733 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004734 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004735 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004736 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004737 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004738 }
4739 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004740 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004741 if (shift == 0) {
4742 return;
4743 }
4744 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004745 if (op->IsShl()) {
4746 __ shll(first_reg, imm);
4747 } else if (op->IsShr()) {
4748 __ sarl(first_reg, imm);
4749 } else {
4750 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004751 }
4752 }
4753 break;
4754 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004755 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004756 if (second.IsRegister()) {
4757 Register second_reg = second.AsRegister<Register>();
4758 DCHECK_EQ(ECX, second_reg);
4759 if (op->IsShl()) {
4760 GenerateShlLong(first, second_reg);
4761 } else if (op->IsShr()) {
4762 GenerateShrLong(first, second_reg);
4763 } else {
4764 GenerateUShrLong(first, second_reg);
4765 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004766 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00004767 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004768 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004769 // Nothing to do if the shift is 0, as the input is already the output.
4770 if (shift != 0) {
4771 if (op->IsShl()) {
4772 GenerateShlLong(first, shift);
4773 } else if (op->IsShr()) {
4774 GenerateShrLong(first, shift);
4775 } else {
4776 GenerateUShrLong(first, shift);
4777 }
4778 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004779 }
4780 break;
4781 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004782 default:
4783 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4784 }
4785}
4786
Mark P Mendell73945692015-04-29 14:56:17 +00004787void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
4788 Register low = loc.AsRegisterPairLow<Register>();
4789 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04004790 if (shift == 1) {
4791 // This is just an addition.
4792 __ addl(low, low);
4793 __ adcl(high, high);
4794 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00004795 // Shift by 32 is easy. High gets low, and low gets 0.
4796 codegen_->EmitParallelMoves(
4797 loc.ToLow(),
4798 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004799 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004800 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4801 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004802 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004803 } else if (shift > 32) {
4804 // Low part becomes 0. High part is low part << (shift-32).
4805 __ movl(high, low);
4806 __ shll(high, Immediate(shift - 32));
4807 __ xorl(low, low);
4808 } else {
4809 // Between 1 and 31.
4810 __ shld(high, low, Immediate(shift));
4811 __ shll(low, Immediate(shift));
4812 }
4813}
4814
Calin Juravle9aec02f2014-11-18 23:06:35 +00004815void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004816 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004817 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4818 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4819 __ testl(shifter, Immediate(32));
4820 __ j(kEqual, &done);
4821 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4822 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4823 __ Bind(&done);
4824}
4825
Mark P Mendell73945692015-04-29 14:56:17 +00004826void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4827 Register low = loc.AsRegisterPairLow<Register>();
4828 Register high = loc.AsRegisterPairHigh<Register>();
4829 if (shift == 32) {
4830 // Need to copy the sign.
4831 DCHECK_NE(low, high);
4832 __ movl(low, high);
4833 __ sarl(high, Immediate(31));
4834 } else if (shift > 32) {
4835 DCHECK_NE(low, high);
4836 // High part becomes sign. Low part is shifted by shift - 32.
4837 __ movl(low, high);
4838 __ sarl(high, Immediate(31));
4839 __ sarl(low, Immediate(shift - 32));
4840 } else {
4841 // Between 1 and 31.
4842 __ shrd(low, high, Immediate(shift));
4843 __ sarl(high, Immediate(shift));
4844 }
4845}
4846
Calin Juravle9aec02f2014-11-18 23:06:35 +00004847void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004848 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004849 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4850 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4851 __ testl(shifter, Immediate(32));
4852 __ j(kEqual, &done);
4853 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4854 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4855 __ Bind(&done);
4856}
4857
Mark P Mendell73945692015-04-29 14:56:17 +00004858void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4859 Register low = loc.AsRegisterPairLow<Register>();
4860 Register high = loc.AsRegisterPairHigh<Register>();
4861 if (shift == 32) {
4862 // Shift by 32 is easy. Low gets high, and high gets 0.
4863 codegen_->EmitParallelMoves(
4864 loc.ToHigh(),
4865 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004866 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004867 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4868 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004869 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004870 } else if (shift > 32) {
4871 // Low part is high >> (shift - 32). High part becomes 0.
4872 __ movl(low, high);
4873 __ shrl(low, Immediate(shift - 32));
4874 __ xorl(high, high);
4875 } else {
4876 // Between 1 and 31.
4877 __ shrd(low, high, Immediate(shift));
4878 __ shrl(high, Immediate(shift));
4879 }
4880}
4881
Calin Juravle9aec02f2014-11-18 23:06:35 +00004882void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004883 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004884 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4885 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4886 __ testl(shifter, Immediate(32));
4887 __ j(kEqual, &done);
4888 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4889 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4890 __ Bind(&done);
4891}
4892
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004893void LocationsBuilderX86::VisitRor(HRor* ror) {
4894 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004895 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004896
4897 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004898 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004899 // Add the temporary needed.
4900 locations->AddTemp(Location::RequiresRegister());
4901 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004902 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004903 locations->SetInAt(0, Location::RequiresRegister());
4904 // The shift count needs to be in CL (unless it is a constant).
4905 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4906 locations->SetOut(Location::SameAsFirstInput());
4907 break;
4908 default:
4909 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4910 UNREACHABLE();
4911 }
4912}
4913
4914void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4915 LocationSummary* locations = ror->GetLocations();
4916 Location first = locations->InAt(0);
4917 Location second = locations->InAt(1);
4918
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004919 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004920 Register first_reg = first.AsRegister<Register>();
4921 if (second.IsRegister()) {
4922 Register second_reg = second.AsRegister<Register>();
4923 __ rorl(first_reg, second_reg);
4924 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004925 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004926 __ rorl(first_reg, imm);
4927 }
4928 return;
4929 }
4930
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004931 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004932 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4933 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4934 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4935 if (second.IsRegister()) {
4936 Register second_reg = second.AsRegister<Register>();
4937 DCHECK_EQ(second_reg, ECX);
4938 __ movl(temp_reg, first_reg_hi);
4939 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4940 __ shrd(first_reg_lo, temp_reg, second_reg);
4941 __ movl(temp_reg, first_reg_hi);
4942 __ testl(second_reg, Immediate(32));
4943 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4944 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4945 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004946 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004947 if (shift_amt == 0) {
4948 // Already fine.
4949 return;
4950 }
4951 if (shift_amt == 32) {
4952 // Just swap.
4953 __ movl(temp_reg, first_reg_lo);
4954 __ movl(first_reg_lo, first_reg_hi);
4955 __ movl(first_reg_hi, temp_reg);
4956 return;
4957 }
4958
4959 Immediate imm(shift_amt);
4960 // Save the constents of the low value.
4961 __ movl(temp_reg, first_reg_lo);
4962
4963 // Shift right into low, feeding bits from high.
4964 __ shrd(first_reg_lo, first_reg_hi, imm);
4965
4966 // Shift right into high, feeding bits from the original low.
4967 __ shrd(first_reg_hi, temp_reg, imm);
4968
4969 // Swap if needed.
4970 if (shift_amt > 32) {
4971 __ movl(temp_reg, first_reg_lo);
4972 __ movl(first_reg_lo, first_reg_hi);
4973 __ movl(first_reg_hi, temp_reg);
4974 }
4975 }
4976}
4977
Calin Juravle9aec02f2014-11-18 23:06:35 +00004978void LocationsBuilderX86::VisitShl(HShl* shl) {
4979 HandleShift(shl);
4980}
4981
4982void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4983 HandleShift(shl);
4984}
4985
4986void LocationsBuilderX86::VisitShr(HShr* shr) {
4987 HandleShift(shr);
4988}
4989
4990void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4991 HandleShift(shr);
4992}
4993
4994void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4995 HandleShift(ushr);
4996}
4997
4998void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4999 HandleShift(ushr);
5000}
5001
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01005002void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005003 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5004 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005005 locations->SetOut(Location::RegisterLocation(EAX));
Alex Lightd109e302018-06-27 10:25:41 -07005006 InvokeRuntimeCallingConvention calling_convention;
5007 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01005008}
5009
5010void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005011 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5012 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
5013 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01005014}
5015
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005016void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005017 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5018 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005019 locations->SetOut(Location::RegisterLocation(EAX));
5020 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005021 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5022 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005023}
5024
5025void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01005026 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
5027 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00005028 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005029 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005030 DCHECK(!codegen_->IsLeafMethod());
5031}
5032
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005033void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005034 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005035 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01005036 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5037 if (location.IsStackSlot()) {
5038 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5039 } else if (location.IsDoubleStackSlot()) {
5040 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005041 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01005042 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005043}
5044
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005045void InstructionCodeGeneratorX86::VisitParameterValue(
5046 HParameterValue* instruction ATTRIBUTE_UNUSED) {
5047}
5048
5049void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
5050 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005051 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005052 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
5053}
5054
5055void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005056}
5057
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005058void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
5059 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005060 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005061 locations->SetInAt(0, Location::RequiresRegister());
5062 locations->SetOut(Location::RequiresRegister());
5063}
5064
5065void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
5066 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005067 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005068 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005069 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005070 __ movl(locations->Out().AsRegister<Register>(),
5071 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005072 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005073 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005074 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005075 __ movl(locations->Out().AsRegister<Register>(),
5076 Address(locations->InAt(0).AsRegister<Register>(),
5077 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
5078 // temp = temp->GetImtEntryAt(method_offset);
5079 __ movl(locations->Out().AsRegister<Register>(),
5080 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005081 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005082}
5083
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005084void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005085 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005086 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01005087 locations->SetInAt(0, Location::RequiresRegister());
5088 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005089}
5090
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005091void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
5092 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01005093 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01005094 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01005095 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005096 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005097 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00005098 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005099 break;
5100
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005101 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01005102 __ notl(out.AsRegisterPairLow<Register>());
5103 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005104 break;
5105
5106 default:
5107 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
5108 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005109}
5110
David Brazdil66d126e2015-04-03 16:02:44 +01005111void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
5112 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005113 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01005114 locations->SetInAt(0, Location::RequiresRegister());
5115 locations->SetOut(Location::SameAsFirstInput());
5116}
5117
5118void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01005119 LocationSummary* locations = bool_not->GetLocations();
5120 Location in = locations->InAt(0);
5121 Location out = locations->Out();
5122 DCHECK(in.Equals(out));
5123 __ xorl(out.AsRegister<Register>(), Immediate(1));
5124}
5125
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005126void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005127 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005128 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00005129 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005130 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005131 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005132 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005133 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005134 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005135 case DataType::Type::kInt32:
5136 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00005137 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00005138 locations->SetInAt(1, Location::Any());
5139 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5140 break;
5141 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005142 case DataType::Type::kFloat32:
5143 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00005144 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00005145 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
5146 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
5147 } else if (compare->InputAt(1)->IsConstant()) {
5148 locations->SetInAt(1, Location::RequiresFpuRegister());
5149 } else {
5150 locations->SetInAt(1, Location::Any());
5151 }
Calin Juravleddb7df22014-11-25 20:56:51 +00005152 locations->SetOut(Location::RequiresRegister());
5153 break;
5154 }
5155 default:
5156 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5157 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005158}
5159
5160void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005161 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005162 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00005163 Location left = locations->InAt(0);
5164 Location right = locations->InAt(1);
5165
Mark Mendell0c9497d2015-08-21 09:30:05 -04005166 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08005167 Condition less_cond = kLess;
5168
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005169 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005170 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005171 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005172 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005173 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005174 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005175 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01005176 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08005177 break;
5178 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005179 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005180 Register left_low = left.AsRegisterPairLow<Register>();
5181 Register left_high = left.AsRegisterPairHigh<Register>();
5182 int32_t val_low = 0;
5183 int32_t val_high = 0;
5184 bool right_is_const = false;
5185
5186 if (right.IsConstant()) {
5187 DCHECK(right.GetConstant()->IsLongConstant());
5188 right_is_const = true;
5189 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
5190 val_low = Low32Bits(val);
5191 val_high = High32Bits(val);
5192 }
5193
Calin Juravleddb7df22014-11-25 20:56:51 +00005194 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005195 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005196 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005197 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005198 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005199 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08005200 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005201 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005202 __ j(kLess, &less); // Signed compare.
5203 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005204 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005205 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005206 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005207 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005208 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005209 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08005210 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005211 }
Aart Bika19616e2016-02-01 18:57:58 -08005212 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00005213 break;
5214 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005215 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00005216 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00005217 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08005218 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00005219 break;
5220 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005221 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00005222 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00005223 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08005224 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005225 break;
5226 }
5227 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00005228 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005229 }
Aart Bika19616e2016-02-01 18:57:58 -08005230
Calin Juravleddb7df22014-11-25 20:56:51 +00005231 __ movl(out, Immediate(0));
5232 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08005233 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00005234
5235 __ Bind(&greater);
5236 __ movl(out, Immediate(1));
5237 __ jmp(&done);
5238
5239 __ Bind(&less);
5240 __ movl(out, Immediate(-1));
5241
5242 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005243}
5244
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005245void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005246 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005247 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005248 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01005249 locations->SetInAt(i, Location::Any());
5250 }
5251 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005252}
5253
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005254void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005255 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005256}
5257
Roland Levillain7c1559a2015-12-15 10:55:36 +00005258void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00005259 /*
5260 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
5261 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
5262 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
5263 */
5264 switch (kind) {
5265 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00005266 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00005267 break;
5268 }
5269 case MemBarrierKind::kAnyStore:
5270 case MemBarrierKind::kLoadAny:
5271 case MemBarrierKind::kStoreStore: {
5272 // nop
5273 break;
5274 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05005275 case MemBarrierKind::kNTStoreStore:
5276 // Non-Temporal Store/Store needs an explicit fence.
Andreas Gampe3db70682018-12-26 15:12:03 -08005277 MemoryFence(/* non-temporal= */ true);
Mark Mendell7aa04a12016-01-27 22:39:07 -05005278 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005279 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005280}
5281
Vladimir Markodc151b22015-10-15 18:02:30 +01005282HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
5283 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01005284 ArtMethod* method ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005285 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005286}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005287
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005288Register CodeGeneratorX86::GetInvokeExtraParameter(HInvoke* invoke, Register temp) {
5289 if (invoke->IsInvokeStaticOrDirect()) {
5290 return GetInvokeStaticOrDirectExtraParameter(invoke->AsInvokeStaticOrDirect(), temp);
5291 }
5292 DCHECK(invoke->IsInvokeInterface());
5293 Location location =
5294 invoke->GetLocations()->InAt(invoke->AsInvokeInterface()->GetSpecialInputIndex());
5295 return location.AsRegister<Register>();
5296}
5297
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005298Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
5299 Register temp) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00005300 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005301 if (!invoke->GetLocations()->Intrinsified()) {
5302 return location.AsRegister<Register>();
5303 }
5304 // For intrinsics we allow any location, so it may be on the stack.
5305 if (!location.IsRegister()) {
5306 __ movl(temp, Address(ESP, location.GetStackIndex()));
5307 return temp;
5308 }
5309 // For register locations, check if the register was saved. If so, get it from the stack.
5310 // Note: There is a chance that the register was saved but not overwritten, so we could
5311 // save one load. However, since this is just an intrinsic slow path we prefer this
5312 // simple and more robust approach rather that trying to determine if that's the case.
5313 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00005314 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
5315 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
5316 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
5317 __ movl(temp, Address(ESP, stack_offset));
5318 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005319 }
5320 return location.AsRegister<Register>();
5321}
5322
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005323void CodeGeneratorX86::LoadMethod(MethodLoadKind load_kind, Location temp, HInvoke* invoke) {
5324 switch (load_kind) {
5325 case MethodLoadKind::kBootImageLinkTimePcRelative: {
5326 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
5327 Register base_reg = GetInvokeExtraParameter(invoke, temp.AsRegister<Register>());
5328 __ leal(temp.AsRegister<Register>(),
5329 Address(base_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
5330 RecordBootImageMethodPatch(invoke);
5331 break;
5332 }
5333 case MethodLoadKind::kBootImageRelRo: {
5334 size_t index = invoke->IsInvokeInterface()
5335 ? invoke->AsInvokeInterface()->GetSpecialInputIndex()
5336 : invoke->AsInvokeStaticOrDirect()->GetSpecialInputIndex();
5337 Register base_reg = GetInvokeExtraParameter(invoke, temp.AsRegister<Register>());
5338 __ movl(temp.AsRegister<Register>(), Address(base_reg, kPlaceholder32BitOffset));
5339 RecordBootImageRelRoPatch(
5340 invoke->InputAt(index)->AsX86ComputeBaseMethodAddress(),
5341 GetBootImageOffset(invoke));
5342 break;
5343 }
5344 case MethodLoadKind::kBssEntry: {
5345 Register base_reg = GetInvokeExtraParameter(invoke, temp.AsRegister<Register>());
5346 __ movl(temp.AsRegister<Register>(), Address(base_reg, kPlaceholder32BitOffset));
5347 RecordMethodBssEntryPatch(invoke);
5348 // No need for memory fence, thanks to the x86 memory model.
5349 break;
5350 }
5351 case MethodLoadKind::kJitDirectAddress: {
5352 __ movl(temp.AsRegister<Register>(),
5353 Immediate(reinterpret_cast32<uint32_t>(invoke->GetResolvedMethod())));
5354 break;
5355 }
5356 case MethodLoadKind::kRuntimeCall: {
5357 // Test situation, don't do anything.
5358 break;
5359 }
5360 default: {
5361 LOG(FATAL) << "Load kind should have already been handled " << load_kind;
5362 UNREACHABLE();
5363 }
5364 }
5365}
5366
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005367void CodeGeneratorX86::GenerateStaticOrDirectCall(
5368 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00005369 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
5370 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005371 case MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005372 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005373 uint32_t offset =
5374 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
5375 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00005376 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005377 }
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005378 case MethodLoadKind::kRecursive: {
Vladimir Marko86c87522020-05-11 16:55:55 +01005379 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005380 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005381 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005382 case MethodLoadKind::kRuntimeCall: {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005383 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5384 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01005385 }
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005386 case MethodLoadKind::kBootImageLinkTimePcRelative:
5387 // For kCallCriticalNative we skip loading the method and do the call directly.
5388 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
5389 break;
5390 }
5391 FALLTHROUGH_INTENDED;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005392 default: {
5393 LoadMethod(invoke->GetMethodLoadKind(), callee_method, invoke);
5394 }
Vladimir Marko58155012015-08-19 12:49:41 +00005395 }
5396
5397 switch (invoke->GetCodePtrLocation()) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005398 case CodePtrLocation::kCallSelf:
Nicolas Geoffray282795c2021-09-24 18:16:41 +01005399 DCHECK(!GetGraph()->HasShouldDeoptimizeFlag());
Vladimir Marko58155012015-08-19 12:49:41 +00005400 __ call(GetFrameEntryLabel());
Vladimir Marko86c87522020-05-11 16:55:55 +01005401 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko58155012015-08-19 12:49:41 +00005402 break;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005403 case CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01005404 size_t out_frame_size =
5405 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorX86,
5406 kNativeStackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01005407 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005408 if (invoke->GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative) {
5409 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
5410 Register base_reg = GetInvokeExtraParameter(invoke, temp.AsRegister<Register>());
5411 __ call(Address(base_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
5412 RecordBootImageJniEntrypointPatch(invoke);
5413 } else {
5414 // (callee_method + offset_of_jni_entry_point)()
5415 __ call(Address(callee_method.AsRegister<Register>(),
5416 ArtMethod::EntryPointFromJniOffset(kX86PointerSize).Int32Value()));
5417 }
Vladimir Marko86c87522020-05-11 16:55:55 +01005418 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5419 if (out_frame_size == 0u && DataType::IsFloatingPointType(invoke->GetType())) {
5420 // Create space for conversion.
5421 out_frame_size = 8u;
Vladimir Markodec78172020-06-19 15:31:23 +01005422 IncreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01005423 }
5424 // Zero-/sign-extend or move the result when needed due to native and managed ABI mismatch.
5425 switch (invoke->GetType()) {
5426 case DataType::Type::kBool:
5427 __ movzxb(EAX, AL);
5428 break;
5429 case DataType::Type::kInt8:
5430 __ movsxb(EAX, AL);
5431 break;
5432 case DataType::Type::kUint16:
5433 __ movzxw(EAX, EAX);
5434 break;
5435 case DataType::Type::kInt16:
5436 __ movsxw(EAX, EAX);
5437 break;
5438 case DataType::Type::kFloat32:
5439 __ fstps(Address(ESP, 0));
5440 __ movss(XMM0, Address(ESP, 0));
5441 break;
5442 case DataType::Type::kFloat64:
5443 __ fstpl(Address(ESP, 0));
5444 __ movsd(XMM0, Address(ESP, 0));
5445 break;
5446 case DataType::Type::kInt32:
5447 case DataType::Type::kInt64:
5448 case DataType::Type::kVoid:
5449 break;
5450 default:
5451 DCHECK(false) << invoke->GetType();
5452 break;
5453 }
5454 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01005455 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01005456 }
5457 break;
5458 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005459 case CodePtrLocation::kCallArtMethod:
Vladimir Marko58155012015-08-19 12:49:41 +00005460 // (callee_method + offset_of_quick_compiled_code)()
5461 __ call(Address(callee_method.AsRegister<Register>(),
5462 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005463 kX86PointerSize).Int32Value()));
Vladimir Marko86c87522020-05-11 16:55:55 +01005464 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko58155012015-08-19 12:49:41 +00005465 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04005466 }
5467
5468 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04005469}
5470
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005471void CodeGeneratorX86::GenerateVirtualCall(
5472 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005473 Register temp = temp_in.AsRegister<Register>();
5474 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5475 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005476
5477 // Use the calling convention instead of the location of the receiver, as
5478 // intrinsics may have put the receiver in a different register. In the intrinsics
5479 // slow path, the arguments have been moved to the right place, so here we are
5480 // guaranteed that the receiver is the first register of the calling convention.
5481 InvokeDexCallingConvention calling_convention;
5482 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005483 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005484 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005485 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005486 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005487 // Instead of simply (possibly) unpoisoning `temp` here, we should
5488 // emit a read barrier for the previous class reference load.
5489 // However this is not required in practice, as this is an
5490 // intermediate/temporary reference and because the current
5491 // concurrent copying collector keeps the from-space memory
5492 // intact/accessible until the end of the marking phase (the
5493 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005494 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00005495
5496 MaybeGenerateInlineCacheCheck(invoke, temp);
5497
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005498 // temp = temp->GetMethodAt(method_offset);
5499 __ movl(temp, Address(temp, method_offset));
5500 // call temp->GetEntryPoint();
5501 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07005502 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005503 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005504}
5505
Vladimir Marko6fd16062018-06-26 11:02:04 +01005506void CodeGeneratorX86::RecordBootImageIntrinsicPatch(HX86ComputeBaseMethodAddress* method_address,
5507 uint32_t intrinsic_data) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005508 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005509 method_address, /* target_dex_file= */ nullptr, intrinsic_data);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005510 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005511}
5512
Vladimir Markob066d432018-01-03 13:14:37 +00005513void CodeGeneratorX86::RecordBootImageRelRoPatch(HX86ComputeBaseMethodAddress* method_address,
5514 uint32_t boot_image_offset) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005515 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005516 method_address, /* target_dex_file= */ nullptr, boot_image_offset);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005517 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Markob066d432018-01-03 13:14:37 +00005518}
5519
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005520void CodeGeneratorX86::RecordBootImageMethodPatch(HInvoke* invoke) {
5521 size_t index = invoke->IsInvokeInterface()
5522 ? invoke->AsInvokeInterface()->GetSpecialInputIndex()
5523 : invoke->AsInvokeStaticOrDirect()->GetSpecialInputIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005524 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005525 invoke->InputAt(index)->AsX86ComputeBaseMethodAddress();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005526 boot_image_method_patches_.emplace_back(
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005527 method_address,
5528 invoke->GetResolvedMethodReference().dex_file,
5529 invoke->GetResolvedMethodReference().index);
Vladimir Marko65979462017-05-19 17:25:12 +01005530 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005531}
5532
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005533void CodeGeneratorX86::RecordMethodBssEntryPatch(HInvoke* invoke) {
5534 size_t index = invoke->IsInvokeInterface()
5535 ? invoke->AsInvokeInterface()->GetSpecialInputIndex()
5536 : invoke->AsInvokeStaticOrDirect()->GetSpecialInputIndex();
Santiago Aboy Solanesa0232ad2021-11-08 17:00:06 +00005537 DCHECK(IsSameDexFile(GetGraph()->GetDexFile(), *invoke->GetMethodReference().dex_file) ||
Santiago Aboy Solanes69a87e32022-03-08 16:43:54 +00005538 GetCompilerOptions().WithinOatFile(invoke->GetMethodReference().dex_file) ||
5539 ContainsElement(Runtime::Current()->GetClassLinker()->GetBootClassPath(),
5540 invoke->GetMethodReference().dex_file));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005541 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005542 invoke->InputAt(index)->AsX86ComputeBaseMethodAddress();
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005543 // Add the patch entry and bind its label at the end of the instruction.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005544 method_bss_entry_patches_.emplace_back(
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005545 method_address,
5546 invoke->GetMethodReference().dex_file,
5547 invoke->GetMethodReference().index);
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005548 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005549}
5550
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005551void CodeGeneratorX86::RecordBootImageTypePatch(HLoadClass* load_class) {
5552 HX86ComputeBaseMethodAddress* method_address =
5553 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5554 boot_image_type_patches_.emplace_back(
5555 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005556 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005557}
5558
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005559Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005560 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005561 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko8f63f102020-09-28 12:10:28 +01005562 ArenaDeque<X86PcRelativePatchInfo>* patches = nullptr;
5563 switch (load_class->GetLoadKind()) {
5564 case HLoadClass::LoadKind::kBssEntry:
5565 patches = &type_bss_entry_patches_;
5566 break;
5567 case HLoadClass::LoadKind::kBssEntryPublic:
5568 patches = &public_type_bss_entry_patches_;
5569 break;
5570 case HLoadClass::LoadKind::kBssEntryPackage:
5571 patches = &package_type_bss_entry_patches_;
5572 break;
5573 default:
5574 LOG(FATAL) << "Unexpected load kind: " << load_class->GetLoadKind();
5575 UNREACHABLE();
5576 }
5577 patches->emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005578 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005579 return &patches->back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005580}
5581
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005582void CodeGeneratorX86::RecordBootImageStringPatch(HLoadString* load_string) {
5583 HX86ComputeBaseMethodAddress* method_address =
5584 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
5585 boot_image_string_patches_.emplace_back(
5586 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
5587 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01005588}
5589
Vladimir Markoaad75c62016-10-03 08:46:48 +00005590Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005591 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005592 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005593 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005594 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005595 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005596}
5597
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005598void CodeGeneratorX86::RecordBootImageJniEntrypointPatch(HInvokeStaticOrDirect* invoke) {
5599 HX86ComputeBaseMethodAddress* method_address =
5600 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5601 boot_image_jni_entrypoint_patches_.emplace_back(
5602 method_address,
5603 invoke->GetResolvedMethodReference().dex_file,
5604 invoke->GetResolvedMethodReference().index);
5605 __ Bind(&boot_image_jni_entrypoint_patches_.back().label);
5606}
5607
Vladimir Markoeebb8212018-06-05 14:57:24 +01005608void CodeGeneratorX86::LoadBootImageAddress(Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01005609 uint32_t boot_image_reference,
Vladimir Markoeebb8212018-06-05 14:57:24 +01005610 HInvokeStaticOrDirect* invoke) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005611 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005612 HX86ComputeBaseMethodAddress* method_address =
5613 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5614 DCHECK(method_address != nullptr);
5615 Register method_address_reg =
5616 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005617 __ leal(reg, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005618 RecordBootImageIntrinsicPatch(method_address, boot_image_reference);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01005619 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01005620 HX86ComputeBaseMethodAddress* method_address =
5621 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5622 DCHECK(method_address != nullptr);
5623 Register method_address_reg =
5624 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005625 __ movl(reg, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005626 RecordBootImageRelRoPatch(method_address, boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01005627 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01005628 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01005629 gc::Heap* heap = Runtime::Current()->GetHeap();
5630 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01005631 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01005632 __ movl(reg, Immediate(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address))));
5633 }
5634}
5635
Vladimir Markode91ca92020-10-27 13:41:40 +00005636void CodeGeneratorX86::LoadIntrinsicDeclaringClass(Register reg, HInvokeStaticOrDirect* invoke) {
5637 DCHECK_NE(invoke->GetIntrinsic(), Intrinsics::kNone);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005638 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005639 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
Vladimir Marko6fd16062018-06-26 11:02:04 +01005640 HX86ComputeBaseMethodAddress* method_address =
5641 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5642 DCHECK(method_address != nullptr);
5643 Register method_address_reg =
5644 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Markode91ca92020-10-27 13:41:40 +00005645 __ leal(reg, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005646 MethodReference target_method = invoke->GetResolvedMethodReference();
Vladimir Marko6fd16062018-06-26 11:02:04 +01005647 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
5648 boot_image_type_patches_.emplace_back(method_address, target_method.dex_file, type_idx.index_);
5649 __ Bind(&boot_image_type_patches_.back().label);
5650 } else {
Vladimir Markode91ca92020-10-27 13:41:40 +00005651 uint32_t boot_image_offset = GetBootImageOffsetOfIntrinsicDeclaringClass(invoke);
5652 LoadBootImageAddress(reg, boot_image_offset, invoke);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005653 }
Vladimir Marko6fd16062018-06-26 11:02:04 +01005654}
5655
Vladimir Markoaad75c62016-10-03 08:46:48 +00005656// The label points to the end of the "movl" or another instruction but the literal offset
5657// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
5658constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
5659
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005660template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005661inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005662 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005663 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005664 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005665 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005666 linker_patches->push_back(Factory(literal_offset,
5667 info.target_dex_file,
5668 GetMethodAddressOffset(info.method_address),
5669 info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005670 }
5671}
5672
Vladimir Marko6fd16062018-06-26 11:02:04 +01005673template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
5674linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
5675 const DexFile* target_dex_file,
5676 uint32_t pc_insn_offset,
5677 uint32_t boot_image_offset) {
5678 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5679 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005680}
5681
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005682void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005683 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005684 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01005685 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005686 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00005687 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01005688 type_bss_entry_patches_.size() +
Vladimir Marko8f63f102020-09-28 12:10:28 +01005689 public_type_bss_entry_patches_.size() +
5690 package_type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005691 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01005692 string_bss_entry_patches_.size() +
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005693 boot_image_jni_entrypoint_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01005694 boot_image_other_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005695 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01005696 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005697 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
5698 boot_image_method_patches_, linker_patches);
5699 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
5700 boot_image_type_patches_, linker_patches);
5701 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005702 boot_image_string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005703 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005704 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005705 DCHECK(boot_image_type_patches_.empty());
5706 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01005707 }
5708 if (GetCompilerOptions().IsBootImage()) {
5709 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5710 boot_image_other_patches_, linker_patches);
5711 } else {
5712 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
5713 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005714 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005715 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5716 method_bss_entry_patches_, linker_patches);
5717 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5718 type_bss_entry_patches_, linker_patches);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005719 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PublicTypeBssEntryPatch>(
5720 public_type_bss_entry_patches_, linker_patches);
5721 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PackageTypeBssEntryPatch>(
5722 package_type_bss_entry_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005723 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5724 string_bss_entry_patches_, linker_patches);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005725 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeJniEntrypointPatch>(
5726 boot_image_jni_entrypoint_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005727 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005728}
5729
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005730void CodeGeneratorX86::MarkGCCard(Register temp,
5731 Register card,
5732 Register object,
5733 Register value,
5734 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005735 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005736 if (value_can_be_null) {
5737 __ testl(value, value);
5738 __ j(kEqual, &is_null);
5739 }
Roland Levillainc73f0522018-08-14 15:16:50 +01005740 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07005741 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01005742 // Calculate the offset (in the card table) of the card corresponding to
5743 // `object`.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005744 __ movl(temp, object);
5745 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01005746 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
5747 // `object`'s card.
5748 //
5749 // Register `card` contains the address of the card table. Note that the card
5750 // table's base is biased during its creation so that it always starts at an
5751 // address whose least-significant byte is equal to `kCardDirty` (see
5752 // art::gc::accounting::CardTable::Create). Therefore the MOVB instruction
5753 // below writes the `kCardDirty` (byte) value into the `object`'s card
5754 // (located at `card + object >> kCardShift`).
5755 //
5756 // This dual use of the value in register `card` (1. to calculate the location
5757 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
5758 // (no need to explicitly load `kCardDirty` as an immediate value).
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00005759 __ movb(Address(temp, card, TIMES_1, 0),
5760 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005761 if (value_can_be_null) {
5762 __ Bind(&is_null);
5763 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005764}
5765
Calin Juravle52c48962014-12-16 17:02:57 +00005766void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Alex Light3a73ffb2021-01-25 14:11:05 +00005767 DCHECK(instruction->IsInstanceFieldGet() ||
5768 instruction->IsStaticFieldGet() ||
5769 instruction->IsPredicatedInstanceFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005770
5771 bool object_field_get_with_read_barrier =
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00005772 gUseReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alex Light3a73ffb2021-01-25 14:11:05 +00005773 bool is_predicated = instruction->IsPredicatedInstanceFieldGet();
Nicolas Geoffray39468442014-09-02 15:17:15 +01005774 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005775 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00005776 gUseReadBarrier
Vladimir Markoca6fff82017-10-03 14:49:14 +01005777 ? LocationSummary::kCallOnSlowPath
5778 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005779 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005780 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005781 }
Alex Light3a73ffb2021-01-25 14:11:05 +00005782 // receiver_input
5783 locations->SetInAt(is_predicated ? 1 : 0, Location::RequiresRegister());
5784 if (is_predicated) {
5785 if (DataType::IsFloatingPointType(instruction->GetType())) {
5786 locations->SetInAt(0, Location::RequiresFpuRegister());
5787 } else {
5788 locations->SetInAt(0, Location::RequiresRegister());
5789 }
5790 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005791 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alex Light3a73ffb2021-01-25 14:11:05 +00005792 locations->SetOut(is_predicated ? Location::SameAsFirstInput()
5793 : Location::RequiresFpuRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005794 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005795 // The output overlaps in case of long: we don't want the low move
5796 // to overwrite the object's location. Likewise, in the case of
5797 // an object field get with read barriers enabled, we do not want
5798 // the move to overwrite the object's location, as we need it to emit
5799 // the read barrier.
Alex Light3a73ffb2021-01-25 14:11:05 +00005800 locations->SetOut(is_predicated ? Location::SameAsFirstInput() : Location::RequiresRegister(),
5801 (object_field_get_with_read_barrier ||
5802 instruction->GetType() == DataType::Type::kInt64 ||
5803 is_predicated)
5804 ? Location::kOutputOverlap
5805 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005806 }
Calin Juravle52c48962014-12-16 17:02:57 +00005807
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005808 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00005809 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005810 // So we use an XMM register as a temp to achieve atomicity (first
5811 // load the temp into the XMM and then copy the XMM into the
5812 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00005813 locations->AddTemp(Location::RequiresFpuRegister());
5814 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005815}
5816
Calin Juravle52c48962014-12-16 17:02:57 +00005817void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
5818 const FieldInfo& field_info) {
Alex Light3a73ffb2021-01-25 14:11:05 +00005819 DCHECK(instruction->IsInstanceFieldGet() ||
5820 instruction->IsStaticFieldGet() ||
5821 instruction->IsPredicatedInstanceFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005822
Calin Juravle52c48962014-12-16 17:02:57 +00005823 LocationSummary* locations = instruction->GetLocations();
Alex Light3a73ffb2021-01-25 14:11:05 +00005824 Location base_loc = locations->InAt(instruction->IsPredicatedInstanceFieldGet() ? 1 : 0);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005825 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005826 Location out = locations->Out();
5827 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01005828 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5829 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00005830 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5831
Ulya Trafimovich322eced2021-06-02 15:39:36 +01005832 if (load_type == DataType::Type::kReference) {
5833 // /* HeapReference<Object> */ out = *(base + offset)
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00005834 if (gUseReadBarrier && kUseBakerReadBarrier) {
Ulya Trafimovich322eced2021-06-02 15:39:36 +01005835 // Note that a potential implicit null check is handled in this
5836 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
5837 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5838 instruction, out, base, offset, /* needs_null_check= */ true);
Calin Juravle52c48962014-12-16 17:02:57 +00005839 if (is_volatile) {
Ulya Trafimovich322eced2021-06-02 15:39:36 +01005840 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005841 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00005842 } else {
Ulya Trafimovich322eced2021-06-02 15:39:36 +01005843 __ movl(out.AsRegister<Register>(), Address(base, offset));
5844 codegen_->MaybeRecordImplicitNullCheck(instruction);
5845 if (is_volatile) {
5846 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5847 }
5848 // If read barriers are enabled, emit read barriers other than
5849 // Baker's using a slow path (and also unpoison the loaded
5850 // reference, if heap poisoning is enabled).
5851 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5852 }
5853 } else {
5854 Address src(base, offset);
5855 XmmRegister temp = (load_type == DataType::Type::kInt64 && is_volatile)
5856 ? locations->GetTemp(0).AsFpuRegister<XmmRegister>()
5857 : kNoXmmRegister;
5858 codegen_->LoadFromMemoryNoBarrier(load_type, out, src, instruction, temp, is_volatile);
5859 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005860 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5861 }
Roland Levillain4d027112015-07-01 15:41:14 +01005862 }
Calin Juravle52c48962014-12-16 17:02:57 +00005863}
5864
5865void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5866 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5867
5868 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005869 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00005870 locations->SetInAt(0, Location::RequiresRegister());
5871 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005872 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005873 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00005874
5875 // The register allocator does not support multiple
5876 // inputs that die at entry with one in a specific register.
5877 if (is_byte_type) {
5878 // Ensure the value is in a byte register.
5879 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005880 } else if (DataType::IsFloatingPointType(field_type)) {
5881 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05005882 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5883 locations->SetInAt(1, Location::RequiresFpuRegister());
5884 } else {
5885 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
5886 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005887 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05005888 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00005889 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005890
Calin Juravle52c48962014-12-16 17:02:57 +00005891 // 64bits value can be atomically written to an address with movsd and an XMM register.
5892 // We need two XMM registers because there's no easier way to (bit) copy a register pair
5893 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
5894 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
5895 // isolated cases when we need this it isn't worth adding the extra complexity.
5896 locations->AddTemp(Location::RequiresFpuRegister());
5897 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005898 } else {
5899 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5900
5901 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5902 // Temporary registers for the write barrier.
5903 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
5904 // Ensure the card is in a byte register.
5905 locations->AddTemp(Location::RegisterLocation(ECX));
5906 }
Calin Juravle52c48962014-12-16 17:02:57 +00005907 }
5908}
5909
5910void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Andra Danciucde98192020-09-13 12:32:09 +00005911 uint32_t value_index,
5912 DataType::Type field_type,
5913 Address field_addr,
5914 Register base,
5915 bool is_volatile,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005916 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005917 LocationSummary* locations = instruction->GetLocations();
Andra Danciucde98192020-09-13 12:32:09 +00005918 Location value = locations->InAt(value_index);
Roland Levillain4d027112015-07-01 15:41:14 +01005919 bool needs_write_barrier =
Andra Danciucde98192020-09-13 12:32:09 +00005920 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(value_index));
Calin Juravle52c48962014-12-16 17:02:57 +00005921
5922 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005923 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005924 }
5925
Mark Mendell81489372015-11-04 11:30:41 -05005926 bool maybe_record_implicit_null_check_done = false;
5927
Calin Juravle52c48962014-12-16 17:02:57 +00005928 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005929 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005930 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005931 case DataType::Type::kInt8: {
Andra Danciucde98192020-09-13 12:32:09 +00005932 if (value.IsConstant()) {
5933 __ movb(field_addr, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
5934 } else {
5935 __ movb(field_addr, value.AsRegister<ByteRegister>());
5936 }
Calin Juravle52c48962014-12-16 17:02:57 +00005937 break;
5938 }
5939
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005940 case DataType::Type::kUint16:
5941 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05005942 if (value.IsConstant()) {
Andra Danciucde98192020-09-13 12:32:09 +00005943 __ movw(field_addr, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05005944 } else {
Andra Danciucde98192020-09-13 12:32:09 +00005945 __ movw(field_addr, value.AsRegister<Register>());
Mark Mendell81489372015-11-04 11:30:41 -05005946 }
Calin Juravle52c48962014-12-16 17:02:57 +00005947 break;
5948 }
5949
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005950 case DataType::Type::kInt32:
5951 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01005952 if (kPoisonHeapReferences && needs_write_barrier) {
5953 // Note that in the case where `value` is a null reference,
5954 // we do not enter this block, as the reference does not
5955 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005956 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01005957 Register temp = locations->GetTemp(0).AsRegister<Register>();
5958 __ movl(temp, value.AsRegister<Register>());
5959 __ PoisonHeapReference(temp);
Andra Danciucde98192020-09-13 12:32:09 +00005960 __ movl(field_addr, temp);
Mark Mendell81489372015-11-04 11:30:41 -05005961 } else if (value.IsConstant()) {
5962 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Andra Danciucde98192020-09-13 12:32:09 +00005963 __ movl(field_addr, Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005964 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005965 DCHECK(value.IsRegister()) << value;
Andra Danciucde98192020-09-13 12:32:09 +00005966 __ movl(field_addr, value.AsRegister<Register>());
Roland Levillain4d027112015-07-01 15:41:14 +01005967 }
Calin Juravle52c48962014-12-16 17:02:57 +00005968 break;
5969 }
5970
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005971 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005972 if (is_volatile) {
5973 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5974 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5975 __ movd(temp1, value.AsRegisterPairLow<Register>());
5976 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5977 __ punpckldq(temp1, temp2);
Andra Danciucde98192020-09-13 12:32:09 +00005978 __ movsd(field_addr, temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005979 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005980 } else if (value.IsConstant()) {
5981 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
Andra Danciucde98192020-09-13 12:32:09 +00005982 __ movl(field_addr, Immediate(Low32Bits(v)));
Mark Mendell81489372015-11-04 11:30:41 -05005983 codegen_->MaybeRecordImplicitNullCheck(instruction);
Ulya Trafimovich893e2ed2021-06-10 16:18:12 +01005984 __ movl(Address::displace(field_addr, kX86WordSize), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005985 } else {
Andra Danciucde98192020-09-13 12:32:09 +00005986 __ movl(field_addr, value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005987 codegen_->MaybeRecordImplicitNullCheck(instruction);
Ulya Trafimovich893e2ed2021-06-10 16:18:12 +01005988 __ movl(Address::displace(field_addr, kX86WordSize), value.AsRegisterPairHigh<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00005989 }
Mark Mendell81489372015-11-04 11:30:41 -05005990 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005991 break;
5992 }
5993
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005994 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05005995 if (value.IsConstant()) {
5996 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Andra Danciucde98192020-09-13 12:32:09 +00005997 __ movl(field_addr, Immediate(v));
Mark Mendell81489372015-11-04 11:30:41 -05005998 } else {
Andra Danciucde98192020-09-13 12:32:09 +00005999 __ movss(field_addr, value.AsFpuRegister<XmmRegister>());
Mark Mendell81489372015-11-04 11:30:41 -05006000 }
Calin Juravle52c48962014-12-16 17:02:57 +00006001 break;
6002 }
6003
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006004 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05006005 if (value.IsConstant()) {
Andra Danciuc992e422020-09-16 08:12:02 +00006006 DCHECK(!is_volatile);
Mark Mendell81489372015-11-04 11:30:41 -05006007 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
Andra Danciucde98192020-09-13 12:32:09 +00006008 __ movl(field_addr, Immediate(Low32Bits(v)));
Mark Mendell81489372015-11-04 11:30:41 -05006009 codegen_->MaybeRecordImplicitNullCheck(instruction);
Ulya Trafimovich893e2ed2021-06-10 16:18:12 +01006010 __ movl(Address::displace(field_addr, kX86WordSize), Immediate(High32Bits(v)));
Mark Mendell81489372015-11-04 11:30:41 -05006011 maybe_record_implicit_null_check_done = true;
6012 } else {
Andra Danciucde98192020-09-13 12:32:09 +00006013 __ movsd(field_addr, value.AsFpuRegister<XmmRegister>());
Mark Mendell81489372015-11-04 11:30:41 -05006014 }
Calin Juravle52c48962014-12-16 17:02:57 +00006015 break;
6016 }
6017
Aart Bik66c158e2018-01-31 12:55:04 -08006018 case DataType::Type::kUint32:
6019 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006020 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00006021 LOG(FATAL) << "Unreachable type " << field_type;
6022 UNREACHABLE();
6023 }
6024
Mark Mendell81489372015-11-04 11:30:41 -05006025 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00006026 codegen_->MaybeRecordImplicitNullCheck(instruction);
6027 }
6028
Roland Levillain4d027112015-07-01 15:41:14 +01006029 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00006030 Register temp = locations->GetTemp(0).AsRegister<Register>();
6031 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006032 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00006033 }
6034
Calin Juravle52c48962014-12-16 17:02:57 +00006035 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006036 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00006037 }
6038}
6039
Andra Danciucde98192020-09-13 12:32:09 +00006040void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
6041 const FieldInfo& field_info,
6042 bool value_can_be_null) {
6043 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
6044
6045 LocationSummary* locations = instruction->GetLocations();
6046 Register base = locations->InAt(0).AsRegister<Register>();
6047 bool is_volatile = field_info.IsVolatile();
6048 DataType::Type field_type = field_info.GetFieldType();
6049 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alex Light3a73ffb2021-01-25 14:11:05 +00006050 bool is_predicated =
6051 instruction->IsInstanceFieldSet() && instruction->AsInstanceFieldSet()->GetIsPredicatedSet();
Andra Danciucde98192020-09-13 12:32:09 +00006052
6053 Address field_addr(base, offset);
6054
Alex Light3a73ffb2021-01-25 14:11:05 +00006055 NearLabel pred_is_null;
6056 if (is_predicated) {
6057 __ testl(base, base);
6058 __ j(kEqual, &pred_is_null);
6059 }
6060
Andra Danciucde98192020-09-13 12:32:09 +00006061 HandleFieldSet(instruction,
6062 /* value_index= */ 1,
6063 field_type,
6064 field_addr,
6065 base,
6066 is_volatile,
6067 value_can_be_null);
Alex Light3a73ffb2021-01-25 14:11:05 +00006068
6069 if (is_predicated) {
6070 __ Bind(&pred_is_null);
6071 }
Andra Danciucde98192020-09-13 12:32:09 +00006072}
6073
Calin Juravle52c48962014-12-16 17:02:57 +00006074void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6075 HandleFieldGet(instruction, instruction->GetFieldInfo());
6076}
6077
6078void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6079 HandleFieldGet(instruction, instruction->GetFieldInfo());
6080}
6081
6082void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6083 HandleFieldSet(instruction, instruction->GetFieldInfo());
6084}
6085
6086void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006087 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00006088}
6089
6090void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6091 HandleFieldSet(instruction, instruction->GetFieldInfo());
6092}
6093
6094void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006095 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00006096}
6097
Alex Light3a73ffb2021-01-25 14:11:05 +00006098void LocationsBuilderX86::VisitPredicatedInstanceFieldGet(
6099 HPredicatedInstanceFieldGet* instruction) {
6100 HandleFieldGet(instruction, instruction->GetFieldInfo());
6101}
6102
Calin Juravle52c48962014-12-16 17:02:57 +00006103void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6104 HandleFieldGet(instruction, instruction->GetFieldInfo());
6105}
6106
Alex Light3a73ffb2021-01-25 14:11:05 +00006107void InstructionCodeGeneratorX86::VisitPredicatedInstanceFieldGet(
6108 HPredicatedInstanceFieldGet* instruction) {
6109 NearLabel finish;
6110 LocationSummary* locations = instruction->GetLocations();
6111 Register recv = locations->InAt(1).AsRegister<Register>();
6112 __ testl(recv, recv);
6113 __ j(kZero, &finish);
6114 HandleFieldGet(instruction, instruction->GetFieldInfo());
6115 __ Bind(&finish);
6116}
Calin Juravle52c48962014-12-16 17:02:57 +00006117void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6118 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006119}
6120
Vladimir Marko552a1342017-10-31 10:56:47 +00006121void LocationsBuilderX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6122 codegen_->CreateStringBuilderAppendLocations(instruction, Location::RegisterLocation(EAX));
6123}
6124
6125void InstructionCodeGeneratorX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6126 __ movl(EAX, Immediate(instruction->GetFormat()->GetValue()));
6127 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
6128}
6129
Calin Juravlee460d1d2015-09-29 04:52:17 +01006130void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
6131 HUnresolvedInstanceFieldGet* instruction) {
6132 FieldAccessCallingConventionX86 calling_convention;
6133 codegen_->CreateUnresolvedFieldLocationSummary(
6134 instruction, instruction->GetFieldType(), calling_convention);
6135}
6136
6137void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
6138 HUnresolvedInstanceFieldGet* instruction) {
6139 FieldAccessCallingConventionX86 calling_convention;
6140 codegen_->GenerateUnresolvedFieldAccess(instruction,
6141 instruction->GetFieldType(),
6142 instruction->GetFieldIndex(),
6143 instruction->GetDexPc(),
6144 calling_convention);
6145}
6146
6147void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
6148 HUnresolvedInstanceFieldSet* instruction) {
6149 FieldAccessCallingConventionX86 calling_convention;
6150 codegen_->CreateUnresolvedFieldLocationSummary(
6151 instruction, instruction->GetFieldType(), calling_convention);
6152}
6153
6154void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
6155 HUnresolvedInstanceFieldSet* instruction) {
6156 FieldAccessCallingConventionX86 calling_convention;
6157 codegen_->GenerateUnresolvedFieldAccess(instruction,
6158 instruction->GetFieldType(),
6159 instruction->GetFieldIndex(),
6160 instruction->GetDexPc(),
6161 calling_convention);
6162}
6163
6164void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
6165 HUnresolvedStaticFieldGet* instruction) {
6166 FieldAccessCallingConventionX86 calling_convention;
6167 codegen_->CreateUnresolvedFieldLocationSummary(
6168 instruction, instruction->GetFieldType(), calling_convention);
6169}
6170
6171void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
6172 HUnresolvedStaticFieldGet* instruction) {
6173 FieldAccessCallingConventionX86 calling_convention;
6174 codegen_->GenerateUnresolvedFieldAccess(instruction,
6175 instruction->GetFieldType(),
6176 instruction->GetFieldIndex(),
6177 instruction->GetDexPc(),
6178 calling_convention);
6179}
6180
6181void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
6182 HUnresolvedStaticFieldSet* instruction) {
6183 FieldAccessCallingConventionX86 calling_convention;
6184 codegen_->CreateUnresolvedFieldLocationSummary(
6185 instruction, instruction->GetFieldType(), calling_convention);
6186}
6187
6188void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
6189 HUnresolvedStaticFieldSet* instruction) {
6190 FieldAccessCallingConventionX86 calling_convention;
6191 codegen_->GenerateUnresolvedFieldAccess(instruction,
6192 instruction->GetFieldType(),
6193 instruction->GetFieldIndex(),
6194 instruction->GetDexPc(),
6195 calling_convention);
6196}
6197
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006198void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006199 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6200 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
6201 ? Location::RequiresRegister()
6202 : Location::Any();
6203 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006204}
6205
Calin Juravle2ae48182016-03-16 14:05:09 +00006206void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
6207 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00006208 return;
6209 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006210 LocationSummary* locations = instruction->GetLocations();
6211 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00006212
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006213 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00006214 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006215}
6216
Calin Juravle2ae48182016-03-16 14:05:09 +00006217void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006218 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006219 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006220
6221 LocationSummary* locations = instruction->GetLocations();
6222 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006223
6224 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04006225 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006226 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006227 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006228 } else {
6229 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00006230 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006231 __ jmp(slow_path->GetEntryLabel());
6232 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006233 }
6234 __ j(kEqual, slow_path->GetEntryLabel());
6235}
6236
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006237void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006238 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006239}
6240
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006241void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006242 bool object_array_get_with_read_barrier =
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006243 gUseReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006244 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006245 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
6246 object_array_get_with_read_barrier
6247 ? LocationSummary::kCallOnSlowPath
6248 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01006249 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006250 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006251 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006252 locations->SetInAt(0, Location::RequiresRegister());
6253 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006254 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01006255 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6256 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006257 // The output overlaps in case of long: we don't want the low move
6258 // to overwrite the array's location. Likewise, in the case of an
6259 // object array get with read barriers enabled, we do not want the
6260 // move to overwrite the array's location, as we need it to emit
6261 // the read barrier.
6262 locations->SetOut(
6263 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006264 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
6265 ? Location::kOutputOverlap
6266 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01006267 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006268}
6269
6270void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
6271 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006272 Location obj_loc = locations->InAt(0);
6273 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006274 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006275 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006276 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006277
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006278 DataType::Type type = instruction->GetType();
Ulya Trafimovich322eced2021-06-02 15:39:36 +01006279 if (type == DataType::Type::kReference) {
6280 static_assert(
6281 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6282 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6283 // /* HeapReference<Object> */ out =
6284 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006285 if (gUseReadBarrier && kUseBakerReadBarrier) {
Ulya Trafimovich322eced2021-06-02 15:39:36 +01006286 // Note that a potential implicit null check is handled in this
6287 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
6288 codegen_->GenerateArrayLoadWithBakerReadBarrier(
6289 instruction, out_loc, obj, data_offset, index, /* needs_null_check= */ true);
6290 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006291 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006292 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006293 codegen_->MaybeRecordImplicitNullCheck(instruction);
Ulya Trafimovich322eced2021-06-02 15:39:36 +01006294 // If read barriers are enabled, emit read barriers other than
6295 // Baker's using a slow path (and also unpoison the loaded
6296 // reference, if heap poisoning is enabled).
6297 if (index.IsConstant()) {
6298 uint32_t offset =
6299 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
6300 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6301 } else {
6302 codegen_->MaybeGenerateReadBarrierSlow(
6303 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6304 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006305 }
Ulya Trafimovich322eced2021-06-02 15:39:36 +01006306 } else if (type == DataType::Type::kUint16
6307 && mirror::kUseStringCompression
6308 && instruction->IsStringCharAt()) {
6309 // Branch cases into compressed and uncompressed for each index's type.
6310 Register out = out_loc.AsRegister<Register>();
6311 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
6312 NearLabel done, not_compressed;
6313 __ testb(Address(obj, count_offset), Immediate(1));
Calin Juravle77520bc2015-01-12 18:45:46 +00006314 codegen_->MaybeRecordImplicitNullCheck(instruction);
Ulya Trafimovich322eced2021-06-02 15:39:36 +01006315 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6316 "Expecting 0=compressed, 1=uncompressed");
6317 __ j(kNotZero, &not_compressed);
6318 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
6319 __ jmp(&done);
6320 __ Bind(&not_compressed);
6321 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
6322 __ Bind(&done);
6323 } else {
Ulya Trafimovichc8451cb2021-06-02 17:35:16 +01006324 ScaleFactor scale = CodeGenerator::ScaleFactorForType(type);
6325 Address src = CodeGeneratorX86::ArrayAddress(obj, index, scale, data_offset);
Ulya Trafimovich322eced2021-06-02 15:39:36 +01006326 codegen_->LoadFromMemoryNoBarrier(type, out_loc, src, instruction);
Calin Juravle77520bc2015-01-12 18:45:46 +00006327 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006328}
6329
6330void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006331 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006332
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006333 bool needs_write_barrier =
6334 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006335 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006336
Vladimir Markoca6fff82017-10-03 14:49:14 +01006337 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01006338 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006339 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006340
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006341 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006342 // We need the inputs to be different than the output in case of long operation.
6343 // In case of a byte operation, the register allocator does not support multiple
6344 // inputs that die at entry with one in a specific register.
6345 locations->SetInAt(0, Location::RequiresRegister());
6346 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6347 if (is_byte_type) {
6348 // Ensure the value is in a byte register.
6349 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006350 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05006351 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006352 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006353 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
6354 }
6355 if (needs_write_barrier) {
6356 // Temporary registers for the write barrier.
6357 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6358 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00006359 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006360 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006361}
6362
6363void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
6364 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006365 Location array_loc = locations->InAt(0);
6366 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006367 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006368 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006369 DataType::Type value_type = instruction->GetComponentType();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006370 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006371 bool needs_write_barrier =
6372 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006373
6374 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006375 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006376 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006377 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006378 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006379 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006380 if (value.IsRegister()) {
6381 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006382 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01006383 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006384 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006385 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006386 break;
6387 }
6388
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006389 case DataType::Type::kUint16:
6390 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006391 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006392 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006393 if (value.IsRegister()) {
6394 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006395 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01006396 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006397 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006398 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006399 break;
6400 }
6401
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006402 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006403 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006404 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006405
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006406 if (!value.IsRegister()) {
6407 // Just setting null.
6408 DCHECK(instruction->InputAt(2)->IsNullConstant());
6409 DCHECK(value.IsConstant()) << value;
6410 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00006411 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006412 DCHECK(!needs_write_barrier);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006413 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006414 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006415 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006416
6417 DCHECK(needs_write_barrier);
6418 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01006419 Location temp_loc = locations->GetTemp(0);
6420 Register temp = temp_loc.AsRegister<Register>();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006421
6422 bool can_value_be_null = instruction->GetValueCanBeNull();
6423 NearLabel do_store;
6424 if (can_value_be_null) {
6425 __ testl(register_value, register_value);
6426 __ j(kEqual, &do_store);
6427 }
6428
6429 SlowPathCode* slow_path = nullptr;
6430 if (needs_type_check) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006431 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006432 codegen_->AddSlowPath(slow_path);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006433
6434 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6435 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6436 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006437
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006438 // Note that when Baker read barriers are enabled, the type
6439 // checks are performed without read barriers. This is fine,
6440 // even in the case where a class object is in the from-space
6441 // after the flip, as a comparison involving such a type would
6442 // not produce a false positive; it may of course produce a
6443 // false negative, in which case we would take the ArraySet
6444 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01006445
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006446 // /* HeapReference<Class> */ temp = array->klass_
6447 __ movl(temp, Address(array, class_offset));
6448 codegen_->MaybeRecordImplicitNullCheck(instruction);
6449 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01006450
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006451 // /* HeapReference<Class> */ temp = temp->component_type_
6452 __ movl(temp, Address(temp, component_offset));
6453 // If heap poisoning is enabled, no need to unpoison `temp`
6454 // nor the object reference in `register_value->klass`, as
6455 // we are comparing two poisoned references.
6456 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01006457
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006458 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006459 NearLabel do_put;
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006460 __ j(kEqual, &do_put);
6461 // If heap poisoning is enabled, the `temp` reference has
6462 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006463 __ MaybeUnpoisonHeapReference(temp);
6464
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006465 // If heap poisoning is enabled, no need to unpoison the
6466 // heap reference loaded below, as it is only used for a
6467 // comparison with null.
6468 __ cmpl(Address(temp, super_offset), Immediate(0));
6469 __ j(kNotEqual, slow_path->GetEntryLabel());
6470 __ Bind(&do_put);
6471 } else {
6472 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006473 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006474 }
6475
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006476 Register card = locations->GetTemp(1).AsRegister<Register>();
6477 codegen_->MarkGCCard(
6478 temp, card, array, value.AsRegister<Register>(), /* value_can_be_null= */ false);
6479
6480 if (can_value_be_null) {
6481 DCHECK(do_store.IsLinked());
6482 __ Bind(&do_store);
6483 }
6484
6485 Register source = register_value;
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006486 if (kPoisonHeapReferences) {
6487 __ movl(temp, register_value);
6488 __ PoisonHeapReference(temp);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006489 source = temp;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006490 }
6491
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006492 __ movl(address, source);
6493
6494 if (can_value_be_null || !needs_type_check) {
6495 codegen_->MaybeRecordImplicitNullCheck(instruction);
6496 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006497
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006498 if (slow_path != nullptr) {
6499 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006500 }
6501
6502 break;
6503 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006504
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006505 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006506 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006507 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006508 if (value.IsRegister()) {
6509 __ movl(address, value.AsRegister<Register>());
6510 } else {
6511 DCHECK(value.IsConstant()) << value;
6512 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
6513 __ movl(address, Immediate(v));
6514 }
6515 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006516 break;
6517 }
6518
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006519 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006520 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006521 if (value.IsRegisterPair()) {
6522 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6523 value.AsRegisterPairLow<Register>());
6524 codegen_->MaybeRecordImplicitNullCheck(instruction);
6525 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6526 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006527 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006528 DCHECK(value.IsConstant());
6529 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
6530 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6531 Immediate(Low32Bits(val)));
6532 codegen_->MaybeRecordImplicitNullCheck(instruction);
6533 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6534 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006535 }
6536 break;
6537 }
6538
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006539 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006540 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006541 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006542 if (value.IsFpuRegister()) {
6543 __ movss(address, value.AsFpuRegister<XmmRegister>());
6544 } else {
6545 DCHECK(value.IsConstant());
6546 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
6547 __ movl(address, Immediate(v));
6548 }
6549 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006550 break;
6551 }
6552
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006553 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006554 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006555 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006556 if (value.IsFpuRegister()) {
6557 __ movsd(address, value.AsFpuRegister<XmmRegister>());
6558 } else {
6559 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006560 Address address_hi =
6561 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05006562 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
6563 __ movl(address, Immediate(Low32Bits(v)));
6564 codegen_->MaybeRecordImplicitNullCheck(instruction);
6565 __ movl(address_hi, Immediate(High32Bits(v)));
6566 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006567 break;
6568 }
6569
Aart Bik66c158e2018-01-31 12:55:04 -08006570 case DataType::Type::kUint32:
6571 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006572 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006573 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07006574 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006575 }
6576}
6577
6578void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006579 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006580 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04006581 if (!instruction->IsEmittedAtUseSite()) {
6582 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6583 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006584}
6585
6586void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04006587 if (instruction->IsEmittedAtUseSite()) {
6588 return;
6589 }
6590
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006591 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006592 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006593 Register obj = locations->InAt(0).AsRegister<Register>();
6594 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006595 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00006596 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07006597 // Mask out most significant bit in case the array is String's array of char.
6598 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006599 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006600 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006601}
6602
6603void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006604 RegisterSet caller_saves = RegisterSet::Empty();
6605 InvokeRuntimeCallingConvention calling_convention;
6606 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6607 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6608 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05006609 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04006610 HInstruction* length = instruction->InputAt(1);
6611 if (!length->IsEmittedAtUseSite()) {
6612 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6613 }
jessicahandojo4877b792016-09-08 19:49:13 -07006614 // Need register to see array's length.
6615 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6616 locations->AddTemp(Location::RequiresRegister());
6617 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006618}
6619
6620void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07006621 const bool is_string_compressed_char_at =
6622 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006623 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05006624 Location index_loc = locations->InAt(0);
6625 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006626 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006627 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006628
Mark Mendell99dbd682015-04-22 16:18:52 -04006629 if (length_loc.IsConstant()) {
6630 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
6631 if (index_loc.IsConstant()) {
6632 // BCE will remove the bounds check if we are guarenteed to pass.
6633 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6634 if (index < 0 || index >= length) {
6635 codegen_->AddSlowPath(slow_path);
6636 __ jmp(slow_path->GetEntryLabel());
6637 } else {
6638 // Some optimization after BCE may have generated this, and we should not
6639 // generate a bounds check if it is a valid range.
6640 }
6641 return;
6642 }
6643
6644 // We have to reverse the jump condition because the length is the constant.
6645 Register index_reg = index_loc.AsRegister<Register>();
6646 __ cmpl(index_reg, Immediate(length));
6647 codegen_->AddSlowPath(slow_path);
6648 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006649 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04006650 HInstruction* array_length = instruction->InputAt(1);
6651 if (array_length->IsEmittedAtUseSite()) {
6652 // Address the length field in the array.
6653 DCHECK(array_length->IsArrayLength());
6654 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
6655 Location array_loc = array_length->GetLocations()->InAt(0);
6656 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07006657 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006658 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
6659 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07006660 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
6661 __ movl(length_reg, array_len);
6662 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006663 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006664 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04006665 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006666 // Checking bounds for general case:
6667 // Array of char or string's array with feature compression off.
6668 if (index_loc.IsConstant()) {
6669 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6670 __ cmpl(array_len, Immediate(value));
6671 } else {
6672 __ cmpl(array_len, index_loc.AsRegister<Register>());
6673 }
6674 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04006675 }
Mark Mendell99dbd682015-04-22 16:18:52 -04006676 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006677 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04006678 }
6679 codegen_->AddSlowPath(slow_path);
6680 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006681 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006682}
6683
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006684void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006685 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006686}
6687
6688void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006689 if (instruction->GetNext()->IsSuspendCheck() &&
6690 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6691 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6692 // The back edge will generate the suspend check.
6693 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6694 }
6695
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006696 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6697}
6698
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006699void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006700 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6701 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07006702 // In suspend check slow path, usually there are no caller-save registers at all.
6703 // If SIMD instructions are present, however, we force spilling all live SIMD
6704 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07006705 locations->SetCustomSlowPathCallerSaves(
6706 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006707}
6708
6709void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006710 HBasicBlock* block = instruction->GetBlock();
6711 if (block->GetLoopInformation() != nullptr) {
6712 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6713 // The back edge will generate the suspend check.
6714 return;
6715 }
6716 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6717 // The goto will generate the suspend check.
6718 return;
6719 }
6720 GenerateSuspendCheck(instruction, nullptr);
6721}
6722
6723void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
6724 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006725 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006726 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
6727 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006728 slow_path =
6729 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006730 instruction->SetSlowPath(slow_path);
6731 codegen_->AddSlowPath(slow_path);
6732 if (successor != nullptr) {
6733 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006734 }
6735 } else {
6736 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6737 }
6738
Vladimir Marko254a8582021-11-29 14:08:37 +00006739 __ fs()->testl(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
6740 Immediate(Thread::SuspendOrCheckpointRequestFlags()));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006741 if (successor == nullptr) {
Vladimir Marko254a8582021-11-29 14:08:37 +00006742 __ j(kNotZero, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006743 __ Bind(slow_path->GetReturnLabel());
6744 } else {
Vladimir Marko254a8582021-11-29 14:08:37 +00006745 __ j(kZero, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006746 __ jmp(slow_path->GetEntryLabel());
6747 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006748}
6749
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006750X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
6751 return codegen_->GetAssembler();
6752}
6753
Aart Bikcfe50bb2017-12-12 14:54:12 -08006754void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006755 ScratchRegisterScope ensure_scratch(
6756 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6757 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6758 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05006759
Aart Bikcfe50bb2017-12-12 14:54:12 -08006760 // Now that temp register is available (possibly spilled), move blocks of memory.
6761 for (int i = 0; i < number_of_words; i++) {
6762 __ movl(temp_reg, Address(ESP, src + stack_offset));
6763 __ movl(Address(ESP, dst + stack_offset), temp_reg);
6764 stack_offset += kX86WordSize;
6765 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006766}
6767
6768void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006769 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006770 Location source = move->GetSource();
6771 Location destination = move->GetDestination();
6772
6773 if (source.IsRegister()) {
6774 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006775 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006776 } else if (destination.IsFpuRegister()) {
6777 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006778 } else {
6779 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006780 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006781 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006782 } else if (source.IsRegisterPair()) {
Vladimir Marko86c87522020-05-11 16:55:55 +01006783 if (destination.IsRegisterPair()) {
6784 __ movl(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
6785 DCHECK_NE(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
6786 __ movl(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
6787 } else if (destination.IsFpuRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006788 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Vladimir Markodec78172020-06-19 15:31:23 +01006789 // Push the 2 source registers to the stack.
Vladimir Marko86c87522020-05-11 16:55:55 +01006790 __ pushl(source.AsRegisterPairHigh<Register>());
6791 __ cfi().AdjustCFAOffset(elem_size);
6792 __ pushl(source.AsRegisterPairLow<Register>());
6793 __ cfi().AdjustCFAOffset(elem_size);
6794 // Load the destination register.
David Brazdil74eb1b22015-12-14 11:44:01 +00006795 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
6796 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01006797 codegen_->DecreaseFrame(2 * elem_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01006798 } else {
6799 DCHECK(destination.IsDoubleStackSlot());
6800 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
6801 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
6802 source.AsRegisterPairHigh<Register>());
6803 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006804 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006805 if (destination.IsRegister()) {
6806 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
6807 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006808 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006809 } else if (destination.IsRegisterPair()) {
Vladimir Marko86c87522020-05-11 16:55:55 +01006810 size_t elem_size = DataType::Size(DataType::Type::kInt32);
6811 // Create stack space for 2 elements.
Vladimir Markodec78172020-06-19 15:31:23 +01006812 codegen_->IncreaseFrame(2 * elem_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01006813 // Store the source register.
6814 __ movsd(Address(ESP, 0), source.AsFpuRegister<XmmRegister>());
6815 // And pop the values into destination registers.
6816 __ popl(destination.AsRegisterPairLow<Register>());
6817 __ cfi().AdjustCFAOffset(-elem_size);
6818 __ popl(destination.AsRegisterPairHigh<Register>());
6819 __ cfi().AdjustCFAOffset(-elem_size);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006820 } else if (destination.IsStackSlot()) {
6821 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006822 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006823 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006824 } else {
6825 DCHECK(destination.IsSIMDStackSlot());
6826 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05006827 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006828 } else if (source.IsStackSlot()) {
6829 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006830 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006831 } else if (destination.IsFpuRegister()) {
6832 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006833 } else {
6834 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08006835 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006836 }
6837 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006838 if (destination.IsRegisterPair()) {
6839 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
6840 __ movl(destination.AsRegisterPairHigh<Register>(),
6841 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
6842 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006843 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6844 } else {
6845 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006846 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006847 }
Aart Bik5576f372017-03-23 16:17:37 -07006848 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006849 if (destination.IsFpuRegister()) {
6850 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6851 } else {
6852 DCHECK(destination.IsSIMDStackSlot());
6853 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6854 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006855 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006856 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006857 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006858 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006859 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006860 if (value == 0) {
6861 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
6862 } else {
6863 __ movl(destination.AsRegister<Register>(), Immediate(value));
6864 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006865 } else {
6866 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05006867 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006868 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006869 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006870 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006871 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006872 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006873 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006874 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6875 if (value == 0) {
6876 // Easy handling of 0.0.
6877 __ xorps(dest, dest);
6878 } else {
6879 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006880 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6881 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
6882 __ movl(temp, Immediate(value));
6883 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006884 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006885 } else {
6886 DCHECK(destination.IsStackSlot()) << destination;
6887 __ movl(Address(ESP, destination.GetStackIndex()), imm);
6888 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006889 } else if (constant->IsLongConstant()) {
6890 int64_t value = constant->AsLongConstant()->GetValue();
6891 int32_t low_value = Low32Bits(value);
6892 int32_t high_value = High32Bits(value);
6893 Immediate low(low_value);
6894 Immediate high(high_value);
6895 if (destination.IsDoubleStackSlot()) {
6896 __ movl(Address(ESP, destination.GetStackIndex()), low);
6897 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6898 } else {
6899 __ movl(destination.AsRegisterPairLow<Register>(), low);
6900 __ movl(destination.AsRegisterPairHigh<Register>(), high);
6901 }
6902 } else {
6903 DCHECK(constant->IsDoubleConstant());
6904 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006905 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006906 int32_t low_value = Low32Bits(value);
6907 int32_t high_value = High32Bits(value);
6908 Immediate low(low_value);
6909 Immediate high(high_value);
6910 if (destination.IsFpuRegister()) {
6911 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6912 if (value == 0) {
6913 // Easy handling of 0.0.
6914 __ xorpd(dest, dest);
6915 } else {
6916 __ pushl(high);
Vladimir Marko86c87522020-05-11 16:55:55 +01006917 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006918 __ pushl(low);
Vladimir Marko86c87522020-05-11 16:55:55 +01006919 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006920 __ movsd(dest, Address(ESP, 0));
Vladimir Markodec78172020-06-19 15:31:23 +01006921 codegen_->DecreaseFrame(8);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006922 }
6923 } else {
6924 DCHECK(destination.IsDoubleStackSlot()) << destination;
6925 __ movl(Address(ESP, destination.GetStackIndex()), low);
6926 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6927 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006928 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006929 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006930 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006931 }
6932}
6933
Mark Mendella5c19ce2015-04-01 12:51:05 -04006934void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006935 Register suggested_scratch = reg == EAX ? EBX : EAX;
6936 ScratchRegisterScope ensure_scratch(
6937 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
6938
6939 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6940 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
6941 __ movl(Address(ESP, mem + stack_offset), reg);
6942 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006943}
6944
Mark Mendell7c8d0092015-01-26 11:21:33 -05006945void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006946 ScratchRegisterScope ensure_scratch(
6947 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6948
6949 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6950 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6951 __ movl(temp_reg, Address(ESP, mem + stack_offset));
6952 __ movss(Address(ESP, mem + stack_offset), reg);
6953 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006954}
6955
Aart Bikcfe50bb2017-12-12 14:54:12 -08006956void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
6957 size_t extra_slot = 4 * kX86WordSize;
Vladimir Markodec78172020-06-19 15:31:23 +01006958 codegen_->IncreaseFrame(extra_slot);
Aart Bikcfe50bb2017-12-12 14:54:12 -08006959 __ movups(Address(ESP, 0), XmmRegister(reg));
6960 ExchangeMemory(0, mem + extra_slot, 4);
6961 __ movups(XmmRegister(reg), Address(ESP, 0));
Vladimir Markodec78172020-06-19 15:31:23 +01006962 codegen_->DecreaseFrame(extra_slot);
Aart Bikcfe50bb2017-12-12 14:54:12 -08006963}
6964
6965void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006966 ScratchRegisterScope ensure_scratch1(
6967 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006968
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006969 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
6970 ScratchRegisterScope ensure_scratch2(
6971 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006972
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006973 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
6974 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006975
6976 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
6977 for (int i = 0; i < number_of_words; i++) {
6978 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
6979 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
6980 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
6981 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
6982 stack_offset += kX86WordSize;
6983 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006984}
6985
6986void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006987 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006988 Location source = move->GetSource();
6989 Location destination = move->GetDestination();
6990
6991 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04006992 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
6993 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
6994 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
6995 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
6996 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006997 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006998 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006999 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007000 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01007001 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08007002 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05007003 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
7004 // Use XOR Swap algorithm to avoid a temporary.
7005 DCHECK_NE(source.reg(), destination.reg());
7006 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
7007 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
7008 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
7009 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
7010 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
7011 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
7012 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007013 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
7014 // Take advantage of the 16 bytes in the XMM register.
7015 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
7016 Address stack(ESP, destination.GetStackIndex());
7017 // Load the double into the high doubleword.
7018 __ movhpd(reg, stack);
7019
7020 // Store the low double into the destination.
7021 __ movsd(stack, reg);
7022
7023 // Move the high double to the low double.
7024 __ psrldq(reg, Immediate(8));
7025 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
7026 // Take advantage of the 16 bytes in the XMM register.
7027 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
7028 Address stack(ESP, source.GetStackIndex());
7029 // Load the double into the high doubleword.
7030 __ movhpd(reg, stack);
7031
7032 // Store the low double into the destination.
7033 __ movsd(stack, reg);
7034
7035 // Move the high double to the low double.
7036 __ psrldq(reg, Immediate(8));
7037 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08007038 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
7039 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
7040 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
7041 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
7042 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
7043 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
7044 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01007045 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05007046 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01007047 }
7048}
7049
7050void ParallelMoveResolverX86::SpillScratch(int reg) {
7051 __ pushl(static_cast<Register>(reg));
7052}
7053
7054void ParallelMoveResolverX86::RestoreScratch(int reg) {
7055 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007056}
7057
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007058HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
7059 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007060 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007061 case HLoadClass::LoadKind::kInvalid:
7062 LOG(FATAL) << "UNREACHABLE";
7063 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007064 case HLoadClass::LoadKind::kReferrersClass:
7065 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007066 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007067 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007068 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko8f63f102020-09-28 12:10:28 +01007069 case HLoadClass::LoadKind::kBssEntryPublic:
7070 case HLoadClass::LoadKind::kBssEntryPackage:
Vladimir Marko695348f2020-05-19 14:42:02 +01007071 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007072 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007073 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007074 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01007075 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007076 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007077 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007078 break;
7079 }
7080 return desired_class_load_kind;
7081}
7082
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007083void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007084 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007085 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007086 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00007087 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007088 cls,
7089 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00007090 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00007091 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007092 return;
7093 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01007094 DCHECK_EQ(cls->NeedsAccessCheck(),
7095 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
7096 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007097
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00007098 const bool requires_read_barrier = gUseReadBarrier && !cls->IsInBootImage();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007099 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007100 ? LocationSummary::kCallOnSlowPath
7101 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007102 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007103 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007104 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007105 }
7106
Vladimir Marko8f63f102020-09-28 12:10:28 +01007107 if (load_kind == HLoadClass::LoadKind::kReferrersClass || cls->HasPcRelativeLoadKind()) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007108 locations->SetInAt(0, Location::RequiresRegister());
7109 }
7110 locations->SetOut(Location::RequiresRegister());
Vladimir Marko8f63f102020-09-28 12:10:28 +01007111 if (call_kind == LocationSummary::kCallOnSlowPath && cls->HasPcRelativeLoadKind()) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00007112 if (!gUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007113 // Rely on the type resolution and/or initialization to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007114 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007115 } else {
7116 // For non-Baker read barrier we have a temp-clobbering call.
7117 }
7118 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007119}
7120
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007121Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01007122 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007123 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007124 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007125 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007126 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007127 PatchInfo<Label>* info = &jit_class_patches_.back();
7128 return &info->label;
7129}
7130
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007131// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7132// move.
7133void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007134 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007135 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007136 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01007137 return;
7138 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01007139 DCHECK_EQ(cls->NeedsAccessCheck(),
7140 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
7141 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Calin Juravle580b6092015-10-06 17:35:58 +01007142
Vladimir Marko41559982017-01-06 14:04:23 +00007143 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007144 Location out_loc = locations->Out();
7145 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007146
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007147 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007148 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7149 ? kWithoutReadBarrier
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00007150 : gCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00007151 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007152 case HLoadClass::LoadKind::kReferrersClass: {
7153 DCHECK(!cls->CanCallRuntime());
7154 DCHECK(!cls->MustGenerateClinitCheck());
7155 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7156 Register current_method = locations->InAt(0).AsRegister<Register>();
7157 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007158 cls,
7159 out_loc,
7160 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Andreas Gampe3db70682018-12-26 15:12:03 -08007161 /* fixup_label= */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007162 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007163 break;
7164 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007165 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007166 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7167 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007168 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007169 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007170 __ leal(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007171 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007172 break;
7173 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007174 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007175 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7176 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007177 __ movl(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007178 codegen_->RecordBootImageRelRoPatch(cls->InputAt(0)->AsX86ComputeBaseMethodAddress(),
Vladimir Markode91ca92020-10-27 13:41:40 +00007179 CodeGenerator::GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007180 break;
7181 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01007182 case HLoadClass::LoadKind::kBssEntry:
7183 case HLoadClass::LoadKind::kBssEntryPublic:
7184 case HLoadClass::LoadKind::kBssEntryPackage: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007185 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007186 Address address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007187 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
7188 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007189 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007190 generate_null_check = true;
7191 break;
7192 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007193 case HLoadClass::LoadKind::kJitBootImageAddress: {
7194 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7195 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
7196 DCHECK_NE(address, 0u);
7197 __ movl(out, Immediate(address));
7198 break;
7199 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007200 case HLoadClass::LoadKind::kJitTableAddress: {
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007201 Address address = Address::Absolute(CodeGeneratorX86::kPlaceholder32BitOffset);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007202 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007203 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007204 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00007205 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007206 break;
7207 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007208 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007209 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007210 LOG(FATAL) << "UNREACHABLE";
7211 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007212 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007213
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007214 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7215 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007216 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007217 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007218
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007219 if (generate_null_check) {
7220 __ testl(out, out);
7221 __ j(kEqual, slow_path->GetEntryLabel());
7222 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007223
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007224 if (cls->MustGenerateClinitCheck()) {
7225 GenerateClassInitializationCheck(slow_path, out);
7226 } else {
7227 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007228 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007229 }
7230}
7231
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007232void LocationsBuilderX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7233 InvokeRuntimeCallingConvention calling_convention;
7234 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7235 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
7236}
7237
7238void InstructionCodeGeneratorX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7239 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
7240}
7241
Orion Hodson18259d72018-04-12 11:18:23 +01007242void LocationsBuilderX86::VisitLoadMethodType(HLoadMethodType* load) {
7243 InvokeRuntimeCallingConvention calling_convention;
7244 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7245 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
7246}
7247
7248void InstructionCodeGeneratorX86::VisitLoadMethodType(HLoadMethodType* load) {
7249 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
7250}
7251
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007252void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
7253 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007254 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007255 locations->SetInAt(0, Location::RequiresRegister());
7256 if (check->HasUses()) {
7257 locations->SetOut(Location::SameAsFirstInput());
7258 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007259 // Rely on the type initialization to save everything we need.
7260 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007261}
7262
7263void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007264 // We assume the class to not be null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007265 SlowPathCode* slow_path =
7266 new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(check->GetLoadClass(), check);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007267 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00007268 GenerateClassInitializationCheck(slow_path,
7269 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007270}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007271
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007272void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07007273 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markobf121912019-06-04 13:49:05 +01007274 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_visibly_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00007275 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007276 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007277}
7278
Vladimir Marko175e7862018-03-27 09:03:13 +00007279void InstructionCodeGeneratorX86::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
7280 Register temp) {
7281 uint32_t path_to_root = check->GetBitstringPathToRoot();
7282 uint32_t mask = check->GetBitstringMask();
7283 DCHECK(IsPowerOfTwo(mask + 1));
7284 size_t mask_bits = WhichPowerOf2(mask + 1);
7285
7286 if (mask_bits == 16u) {
7287 // Compare the bitstring in memory.
7288 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
7289 } else {
7290 // /* uint32_t */ temp = temp->status_
7291 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
7292 // Compare the bitstring bits using SUB.
7293 __ subl(temp, Immediate(path_to_root));
7294 // Shift out bits that do not contribute to the comparison.
7295 __ shll(temp, Immediate(32u - mask_bits));
7296 }
7297}
7298
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007299HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
7300 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007301 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007302 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007303 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007304 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01007305 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007306 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007307 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007308 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01007309 DCHECK(GetCompilerOptions().IsJitCompiler());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007310 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007311 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007312 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007313 }
7314 return desired_string_load_kind;
7315}
7316
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007317void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007318 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007319 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007320 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007321 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007322 load_kind == HLoadString::LoadKind::kBootImageRelRo ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00007323 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007324 locations->SetInAt(0, Location::RequiresRegister());
7325 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007326 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007327 locations->SetOut(Location::RegisterLocation(EAX));
7328 } else {
7329 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007330 if (load_kind == HLoadString::LoadKind::kBssEntry) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00007331 if (!gUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007332 // Rely on the pResolveString to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007333 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007334 } else {
7335 // For non-Baker read barrier we have a temp-clobbering call.
7336 }
7337 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007338 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007339}
7340
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007341Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01007342 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007343 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007344 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007345 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007346 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007347 PatchInfo<Label>* info = &jit_string_patches_.back();
7348 return &info->label;
7349}
7350
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007351// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7352// move.
7353void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01007354 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007355 Location out_loc = locations->Out();
7356 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007357
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007358 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007359 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007360 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7361 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007362 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007363 __ leal(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007364 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007365 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007366 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007367 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007368 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7369 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007370 __ movl(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007371 codegen_->RecordBootImageRelRoPatch(load->InputAt(0)->AsX86ComputeBaseMethodAddress(),
Vladimir Markode91ca92020-10-27 13:41:40 +00007372 CodeGenerator::GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007373 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007374 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007375 case HLoadString::LoadKind::kBssEntry: {
7376 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007377 Address address = Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007378 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007379 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00007380 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, gCompilerReadBarrierOption);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007381 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007382 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007383 codegen_->AddSlowPath(slow_path);
7384 __ testl(out, out);
7385 __ j(kEqual, slow_path->GetEntryLabel());
7386 __ Bind(slow_path->GetExitLabel());
7387 return;
7388 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007389 case HLoadString::LoadKind::kJitBootImageAddress: {
7390 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
7391 DCHECK_NE(address, 0u);
7392 __ movl(out, Immediate(address));
7393 return;
7394 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007395 case HLoadString::LoadKind::kJitTableAddress: {
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007396 Address address = Address::Absolute(CodeGeneratorX86::kPlaceholder32BitOffset);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007397 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007398 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007399 // /* GcRoot<mirror::String> */ out = *address
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00007400 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, gCompilerReadBarrierOption);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007401 return;
7402 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007403 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007404 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007405 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007406
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007407 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007408 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007409 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007410 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007411 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7412 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007413}
7414
David Brazdilcb1c0552015-08-04 16:22:25 +01007415static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007416 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01007417}
7418
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007419void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
7420 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007421 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007422 locations->SetOut(Location::RequiresRegister());
7423}
7424
7425void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01007426 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
7427}
7428
7429void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007430 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01007431}
7432
7433void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7434 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007435}
7436
7437void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007438 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7439 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007440 InvokeRuntimeCallingConvention calling_convention;
7441 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7442}
7443
7444void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007445 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007446 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007447}
7448
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007449// Temp is used for read barrier.
7450static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00007451 if (gUseReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00007452 !kUseBakerReadBarrier &&
7453 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00007454 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007455 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7456 return 1;
7457 }
7458 return 0;
7459}
7460
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007461// Interface case has 2 temps, one for holding the number of interfaces, one for the current
7462// interface pointer, the current interface is compared in memory.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007463// The other checks have one temp for loading the object's class.
7464static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007465 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007466 return 2;
7467 }
7468 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007469}
7470
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007471void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007472 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007473 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01007474 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007475 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007476 case TypeCheckKind::kExactCheck:
7477 case TypeCheckKind::kAbstractClassCheck:
7478 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007479 case TypeCheckKind::kArrayObjectCheck: {
7480 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7481 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7482 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007483 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007484 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007485 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007486 case TypeCheckKind::kUnresolvedCheck:
7487 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007488 call_kind = LocationSummary::kCallOnSlowPath;
7489 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007490 case TypeCheckKind::kBitstringCheck:
7491 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007492 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007493
Vladimir Markoca6fff82017-10-03 14:49:14 +01007494 LocationSummary* locations =
7495 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01007496 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007497 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007498 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007499 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007500 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7501 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7502 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7503 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7504 } else {
7505 locations->SetInAt(1, Location::Any());
7506 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007507 // Note that TypeCheckSlowPathX86 uses this "out" register too.
7508 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007509 // When read barriers are enabled, we need a temporary register for some cases.
7510 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007511}
7512
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007513void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007514 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007515 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007516 Location obj_loc = locations->InAt(0);
7517 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007518 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007519 Location out_loc = locations->Out();
7520 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007521 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7522 DCHECK_LE(num_temps, 1u);
7523 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007524 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007525 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7526 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7527 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07007528 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007529 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007530
7531 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007532 // Avoid null check if we know obj is not null.
7533 if (instruction->MustDoNullCheck()) {
7534 __ testl(obj, obj);
7535 __ j(kEqual, &zero);
7536 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007537
Roland Levillain7c1559a2015-12-15 10:55:36 +00007538 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007539 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007540 ReadBarrierOption read_barrier_option =
7541 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007542 // /* HeapReference<Class> */ out = obj->klass_
7543 GenerateReferenceLoadTwoRegisters(instruction,
7544 out_loc,
7545 obj_loc,
7546 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007547 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007548 if (cls.IsRegister()) {
7549 __ cmpl(out, cls.AsRegister<Register>());
7550 } else {
7551 DCHECK(cls.IsStackSlot()) << cls;
7552 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7553 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007554
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007555 // Classes must be equal for the instanceof to succeed.
7556 __ j(kNotEqual, &zero);
7557 __ movl(out, Immediate(1));
7558 __ jmp(&done);
7559 break;
7560 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007561
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007562 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007563 ReadBarrierOption read_barrier_option =
7564 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007565 // /* HeapReference<Class> */ out = obj->klass_
7566 GenerateReferenceLoadTwoRegisters(instruction,
7567 out_loc,
7568 obj_loc,
7569 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007570 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007571 // If the class is abstract, we eagerly fetch the super class of the
7572 // object to avoid doing a comparison we know will fail.
7573 NearLabel loop;
7574 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007575 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007576 GenerateReferenceLoadOneRegister(instruction,
7577 out_loc,
7578 super_offset,
7579 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007580 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007581 __ testl(out, out);
7582 // If `out` is null, we use it for the result, and jump to `done`.
7583 __ j(kEqual, &done);
7584 if (cls.IsRegister()) {
7585 __ cmpl(out, cls.AsRegister<Register>());
7586 } else {
7587 DCHECK(cls.IsStackSlot()) << cls;
7588 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7589 }
7590 __ j(kNotEqual, &loop);
7591 __ movl(out, Immediate(1));
7592 if (zero.IsLinked()) {
7593 __ jmp(&done);
7594 }
7595 break;
7596 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007597
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007598 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007599 ReadBarrierOption read_barrier_option =
7600 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007601 // /* HeapReference<Class> */ out = obj->klass_
7602 GenerateReferenceLoadTwoRegisters(instruction,
7603 out_loc,
7604 obj_loc,
7605 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007606 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007607 // Walk over the class hierarchy to find a match.
7608 NearLabel loop, success;
7609 __ Bind(&loop);
7610 if (cls.IsRegister()) {
7611 __ cmpl(out, cls.AsRegister<Register>());
7612 } else {
7613 DCHECK(cls.IsStackSlot()) << cls;
7614 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7615 }
7616 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007617 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007618 GenerateReferenceLoadOneRegister(instruction,
7619 out_loc,
7620 super_offset,
7621 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007622 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007623 __ testl(out, out);
7624 __ j(kNotEqual, &loop);
7625 // If `out` is null, we use it for the result, and jump to `done`.
7626 __ jmp(&done);
7627 __ Bind(&success);
7628 __ movl(out, Immediate(1));
7629 if (zero.IsLinked()) {
7630 __ jmp(&done);
7631 }
7632 break;
7633 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007634
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007635 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007636 ReadBarrierOption read_barrier_option =
7637 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007638 // /* HeapReference<Class> */ out = obj->klass_
7639 GenerateReferenceLoadTwoRegisters(instruction,
7640 out_loc,
7641 obj_loc,
7642 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007643 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007644 // Do an exact check.
7645 NearLabel exact_check;
7646 if (cls.IsRegister()) {
7647 __ cmpl(out, cls.AsRegister<Register>());
7648 } else {
7649 DCHECK(cls.IsStackSlot()) << cls;
7650 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7651 }
7652 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007653 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007654 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007655 GenerateReferenceLoadOneRegister(instruction,
7656 out_loc,
7657 component_offset,
7658 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007659 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007660 __ testl(out, out);
7661 // If `out` is null, we use it for the result, and jump to `done`.
7662 __ j(kEqual, &done);
7663 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
7664 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007665 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007666 __ movl(out, Immediate(1));
7667 __ jmp(&done);
7668 break;
7669 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007670
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007671 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007672 // No read barrier since the slow path will retry upon failure.
7673 // /* HeapReference<Class> */ out = obj->klass_
7674 GenerateReferenceLoadTwoRegisters(instruction,
7675 out_loc,
7676 obj_loc,
7677 class_offset,
7678 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007679 if (cls.IsRegister()) {
7680 __ cmpl(out, cls.AsRegister<Register>());
7681 } else {
7682 DCHECK(cls.IsStackSlot()) << cls;
7683 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7684 }
7685 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007686 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007687 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007688 codegen_->AddSlowPath(slow_path);
7689 __ j(kNotEqual, slow_path->GetEntryLabel());
7690 __ movl(out, Immediate(1));
7691 if (zero.IsLinked()) {
7692 __ jmp(&done);
7693 }
7694 break;
7695 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007696
Calin Juravle98893e12015-10-02 21:05:03 +01007697 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007698 case TypeCheckKind::kInterfaceCheck: {
7699 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007700 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00007701 // cases.
7702 //
7703 // We cannot directly call the InstanceofNonTrivial runtime
7704 // entry point without resorting to a type checking slow path
7705 // here (i.e. by calling InvokeRuntime directly), as it would
7706 // require to assign fixed registers for the inputs of this
7707 // HInstanceOf instruction (following the runtime calling
7708 // convention), which might be cluttered by the potential first
7709 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007710 //
7711 // TODO: Introduce a new runtime entry point taking the object
7712 // to test (instead of its class) as argument, and let it deal
7713 // with the read barrier issues. This will let us refactor this
7714 // case of the `switch` code as it was previously (with a direct
7715 // call to the runtime not using a type checking slow path).
7716 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007717 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007718 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007719 instruction, /* is_fatal= */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007720 codegen_->AddSlowPath(slow_path);
7721 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007722 if (zero.IsLinked()) {
7723 __ jmp(&done);
7724 }
7725 break;
7726 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007727
7728 case TypeCheckKind::kBitstringCheck: {
7729 // /* HeapReference<Class> */ temp = obj->klass_
7730 GenerateReferenceLoadTwoRegisters(instruction,
7731 out_loc,
7732 obj_loc,
7733 class_offset,
7734 kWithoutReadBarrier);
7735
7736 GenerateBitstringTypeCheckCompare(instruction, out);
7737 __ j(kNotEqual, &zero);
7738 __ movl(out, Immediate(1));
7739 __ jmp(&done);
7740 break;
7741 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007742 }
7743
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007744 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007745 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007746 __ xorl(out, out);
7747 }
7748
7749 if (done.IsLinked()) {
7750 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007751 }
7752
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007753 if (slow_path != nullptr) {
7754 __ Bind(slow_path->GetExitLabel());
7755 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007756}
7757
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007758void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007759 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007760 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007761 LocationSummary* locations =
7762 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007763 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007764 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7765 // Require a register for the interface check since there is a loop that compares the class to
7766 // a memory address.
7767 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007768 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7769 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7770 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7771 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007772 } else {
7773 locations->SetInAt(1, Location::Any());
7774 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007775 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007776 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
7777}
7778
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007779void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007780 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007781 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007782 Location obj_loc = locations->InAt(0);
7783 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007784 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007785 Location temp_loc = locations->GetTemp(0);
7786 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007787 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7788 DCHECK_GE(num_temps, 1u);
7789 DCHECK_LE(num_temps, 2u);
7790 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7791 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7792 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7793 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7794 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7795 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7796 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7797 const uint32_t object_array_data_offset =
7798 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007799
Vladimir Marko87584542017-12-12 17:47:52 +00007800 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007801 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007802 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7803 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007804 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007805
Roland Levillain0d5a2812015-11-13 10:07:31 +00007806 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007807 // Avoid null check if we know obj is not null.
7808 if (instruction->MustDoNullCheck()) {
7809 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007810 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007811 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007812
Roland Levillain0d5a2812015-11-13 10:07:31 +00007813 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007814 case TypeCheckKind::kExactCheck:
7815 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007816 // /* HeapReference<Class> */ temp = obj->klass_
7817 GenerateReferenceLoadTwoRegisters(instruction,
7818 temp_loc,
7819 obj_loc,
7820 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007821 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007822
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007823 if (cls.IsRegister()) {
7824 __ cmpl(temp, cls.AsRegister<Register>());
7825 } else {
7826 DCHECK(cls.IsStackSlot()) << cls;
7827 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7828 }
7829 // Jump to slow path for throwing the exception or doing a
7830 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007831 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007832 break;
7833 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007834
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007835 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007836 // /* HeapReference<Class> */ temp = obj->klass_
7837 GenerateReferenceLoadTwoRegisters(instruction,
7838 temp_loc,
7839 obj_loc,
7840 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007841 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007842
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007843 // If the class is abstract, we eagerly fetch the super class of the
7844 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007845 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007846 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007847 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007848 GenerateReferenceLoadOneRegister(instruction,
7849 temp_loc,
7850 super_offset,
7851 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007852 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007853
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007854 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7855 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007856 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007857 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007858
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007859 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007860 if (cls.IsRegister()) {
7861 __ cmpl(temp, cls.AsRegister<Register>());
7862 } else {
7863 DCHECK(cls.IsStackSlot()) << cls;
7864 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7865 }
7866 __ j(kNotEqual, &loop);
7867 break;
7868 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007869
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007870 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007871 // /* HeapReference<Class> */ temp = obj->klass_
7872 GenerateReferenceLoadTwoRegisters(instruction,
7873 temp_loc,
7874 obj_loc,
7875 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007876 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007877
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007878 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007879 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007880 __ Bind(&loop);
7881 if (cls.IsRegister()) {
7882 __ cmpl(temp, cls.AsRegister<Register>());
7883 } else {
7884 DCHECK(cls.IsStackSlot()) << cls;
7885 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7886 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007887 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007888
Roland Levillain0d5a2812015-11-13 10:07:31 +00007889 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007890 GenerateReferenceLoadOneRegister(instruction,
7891 temp_loc,
7892 super_offset,
7893 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007894 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007895
7896 // If the class reference currently in `temp` is not null, jump
7897 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007898 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007899 __ j(kNotZero, &loop);
7900 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007901 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007902 break;
7903 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007904
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007905 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007906 // /* HeapReference<Class> */ temp = obj->klass_
7907 GenerateReferenceLoadTwoRegisters(instruction,
7908 temp_loc,
7909 obj_loc,
7910 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007911 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007912
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007913 // Do an exact check.
7914 if (cls.IsRegister()) {
7915 __ cmpl(temp, cls.AsRegister<Register>());
7916 } else {
7917 DCHECK(cls.IsStackSlot()) << cls;
7918 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7919 }
7920 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007921
7922 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007923 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007924 GenerateReferenceLoadOneRegister(instruction,
7925 temp_loc,
7926 component_offset,
7927 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007928 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007929
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007930 // If the component type is null (i.e. the object not an array), jump to the slow path to
7931 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007932 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007933 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007934
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007935 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007936 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007937 break;
7938 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007939
Calin Juravle98893e12015-10-02 21:05:03 +01007940 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007941 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007942 // We cannot directly call the CheckCast runtime entry point
7943 // without resorting to a type checking slow path here (i.e. by
7944 // calling InvokeRuntime directly), as it would require to
7945 // assign fixed registers for the inputs of this HInstanceOf
7946 // instruction (following the runtime calling convention), which
7947 // might be cluttered by the potential first read barrier
7948 // emission at the beginning of this method.
7949 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007950 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007951
7952 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007953 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
7954 // We can not get false positives by doing this.
7955 // /* HeapReference<Class> */ temp = obj->klass_
7956 GenerateReferenceLoadTwoRegisters(instruction,
7957 temp_loc,
7958 obj_loc,
7959 class_offset,
7960 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007961
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007962 // /* HeapReference<Class> */ temp = temp->iftable_
7963 GenerateReferenceLoadTwoRegisters(instruction,
7964 temp_loc,
7965 temp_loc,
7966 iftable_offset,
7967 kWithoutReadBarrier);
7968 // Iftable is never null.
7969 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
7970 // Maybe poison the `cls` for direct comparison with memory.
7971 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
7972 // Loop through the iftable and check if any class matches.
7973 NearLabel start_loop;
7974 __ Bind(&start_loop);
7975 // Need to subtract first to handle the empty array case.
7976 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
7977 __ j(kNegative, type_check_slow_path->GetEntryLabel());
7978 // Go to next interface if the classes do not match.
7979 __ cmpl(cls.AsRegister<Register>(),
7980 CodeGeneratorX86::ArrayAddress(temp,
7981 maybe_temp2_loc,
7982 TIMES_4,
7983 object_array_data_offset));
7984 __ j(kNotEqual, &start_loop);
7985 // If `cls` was poisoned above, unpoison it.
7986 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007987 break;
7988 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007989
7990 case TypeCheckKind::kBitstringCheck: {
7991 // /* HeapReference<Class> */ temp = obj->klass_
7992 GenerateReferenceLoadTwoRegisters(instruction,
7993 temp_loc,
7994 obj_loc,
7995 class_offset,
7996 kWithoutReadBarrier);
7997
7998 GenerateBitstringTypeCheckCompare(instruction, temp);
7999 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
8000 break;
8001 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00008002 }
8003 __ Bind(&done);
8004
Roland Levillain0d5a2812015-11-13 10:07:31 +00008005 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00008006}
8007
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008008void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008009 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8010 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008011 InvokeRuntimeCallingConvention calling_convention;
8012 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8013}
8014
8015void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01008016 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
8017 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01008018 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01008019 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008020 if (instruction->IsEnter()) {
8021 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8022 } else {
8023 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8024 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008025}
8026
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05308027void LocationsBuilderX86::VisitX86AndNot(HX86AndNot* instruction) {
8028 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
8029 DCHECK(DataType::IsIntOrLongType(instruction->GetType())) << instruction->GetType();
8030 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
8031 locations->SetInAt(0, Location::RequiresRegister());
8032 locations->SetInAt(1, Location::RequiresRegister());
8033 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8034}
8035
8036void InstructionCodeGeneratorX86::VisitX86AndNot(HX86AndNot* instruction) {
8037 LocationSummary* locations = instruction->GetLocations();
8038 Location first = locations->InAt(0);
8039 Location second = locations->InAt(1);
8040 Location dest = locations->Out();
8041 if (instruction->GetResultType() == DataType::Type::kInt32) {
8042 __ andn(dest.AsRegister<Register>(),
8043 first.AsRegister<Register>(),
8044 second.AsRegister<Register>());
8045 } else {
8046 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
8047 __ andn(dest.AsRegisterPairLow<Register>(),
8048 first.AsRegisterPairLow<Register>(),
8049 second.AsRegisterPairLow<Register>());
8050 __ andn(dest.AsRegisterPairHigh<Register>(),
8051 first.AsRegisterPairHigh<Register>(),
8052 second.AsRegisterPairHigh<Register>());
8053 }
8054}
8055
8056void LocationsBuilderX86::VisitX86MaskOrResetLeastSetBit(HX86MaskOrResetLeastSetBit* instruction) {
8057 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
8058 DCHECK(instruction->GetType() == DataType::Type::kInt32) << instruction->GetType();
8059 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
8060 locations->SetInAt(0, Location::RequiresRegister());
8061 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8062}
8063
8064void InstructionCodeGeneratorX86::VisitX86MaskOrResetLeastSetBit(
8065 HX86MaskOrResetLeastSetBit* instruction) {
8066 LocationSummary* locations = instruction->GetLocations();
8067 Location src = locations->InAt(0);
8068 Location dest = locations->Out();
8069 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
8070 switch (instruction->GetOpKind()) {
8071 case HInstruction::kAnd:
8072 __ blsr(dest.AsRegister<Register>(), src.AsRegister<Register>());
8073 break;
8074 case HInstruction::kXor:
8075 __ blsmsk(dest.AsRegister<Register>(), src.AsRegister<Register>());
8076 break;
8077 default:
8078 LOG(FATAL) << "Unreachable";
8079 }
8080}
8081
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008082void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
8083void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
8084void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
8085
8086void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
8087 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008088 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008089 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
8090 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008091 locations->SetInAt(0, Location::RequiresRegister());
8092 locations->SetInAt(1, Location::Any());
8093 locations->SetOut(Location::SameAsFirstInput());
8094}
8095
8096void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
8097 HandleBitwiseOperation(instruction);
8098}
8099
8100void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
8101 HandleBitwiseOperation(instruction);
8102}
8103
8104void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
8105 HandleBitwiseOperation(instruction);
8106}
8107
8108void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
8109 LocationSummary* locations = instruction->GetLocations();
8110 Location first = locations->InAt(0);
8111 Location second = locations->InAt(1);
8112 DCHECK(first.Equals(locations->Out()));
8113
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008114 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008115 if (second.IsRegister()) {
8116 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00008117 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008118 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00008119 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008120 } else {
8121 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00008122 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008123 }
8124 } else if (second.IsConstant()) {
8125 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00008126 __ andl(first.AsRegister<Register>(),
8127 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008128 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00008129 __ orl(first.AsRegister<Register>(),
8130 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008131 } else {
8132 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00008133 __ xorl(first.AsRegister<Register>(),
8134 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008135 }
8136 } else {
8137 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00008138 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008139 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00008140 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008141 } else {
8142 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00008143 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008144 }
8145 }
8146 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008147 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008148 if (second.IsRegisterPair()) {
8149 if (instruction->IsAnd()) {
8150 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
8151 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
8152 } else if (instruction->IsOr()) {
8153 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
8154 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
8155 } else {
8156 DCHECK(instruction->IsXor());
8157 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
8158 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
8159 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008160 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008161 if (instruction->IsAnd()) {
8162 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
8163 __ andl(first.AsRegisterPairHigh<Register>(),
8164 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
8165 } else if (instruction->IsOr()) {
8166 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
8167 __ orl(first.AsRegisterPairHigh<Register>(),
8168 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
8169 } else {
8170 DCHECK(instruction->IsXor());
8171 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
8172 __ xorl(first.AsRegisterPairHigh<Register>(),
8173 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
8174 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008175 } else {
8176 DCHECK(second.IsConstant()) << second;
8177 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008178 int32_t low_value = Low32Bits(value);
8179 int32_t high_value = High32Bits(value);
8180 Immediate low(low_value);
8181 Immediate high(high_value);
8182 Register first_low = first.AsRegisterPairLow<Register>();
8183 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008184 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008185 if (low_value == 0) {
8186 __ xorl(first_low, first_low);
8187 } else if (low_value != -1) {
8188 __ andl(first_low, low);
8189 }
8190 if (high_value == 0) {
8191 __ xorl(first_high, first_high);
8192 } else if (high_value != -1) {
8193 __ andl(first_high, high);
8194 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008195 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008196 if (low_value != 0) {
8197 __ orl(first_low, low);
8198 }
8199 if (high_value != 0) {
8200 __ orl(first_high, high);
8201 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008202 } else {
8203 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008204 if (low_value != 0) {
8205 __ xorl(first_low, low);
8206 }
8207 if (high_value != 0) {
8208 __ xorl(first_high, high);
8209 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008210 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008211 }
8212 }
8213}
8214
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008215void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
8216 HInstruction* instruction,
8217 Location out,
8218 uint32_t offset,
8219 Location maybe_temp,
8220 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008221 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008222 if (read_barrier_option == kWithReadBarrier) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00008223 CHECK(gUseReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008224 if (kUseBakerReadBarrier) {
8225 // Load with fast path based Baker's read barrier.
8226 // /* HeapReference<Object> */ out = *(out + offset)
8227 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008228 instruction, out, out_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008229 } else {
8230 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008231 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00008232 // in the following move operation, as we will need it for the
8233 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00008234 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008235 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008236 // /* HeapReference<Object> */ out = *(out + offset)
8237 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008238 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008239 }
8240 } else {
8241 // Plain load with no read barrier.
8242 // /* HeapReference<Object> */ out = *(out + offset)
8243 __ movl(out_reg, Address(out_reg, offset));
8244 __ MaybeUnpoisonHeapReference(out_reg);
8245 }
8246}
8247
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008248void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
8249 HInstruction* instruction,
8250 Location out,
8251 Location obj,
8252 uint32_t offset,
8253 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008254 Register out_reg = out.AsRegister<Register>();
8255 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008256 if (read_barrier_option == kWithReadBarrier) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00008257 CHECK(gUseReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008258 if (kUseBakerReadBarrier) {
8259 // Load with fast path based Baker's read barrier.
8260 // /* HeapReference<Object> */ out = *(obj + offset)
8261 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008262 instruction, out, obj_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008263 } else {
8264 // Load with slow path based read barrier.
8265 // /* HeapReference<Object> */ out = *(obj + offset)
8266 __ movl(out_reg, Address(obj_reg, offset));
8267 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8268 }
8269 } else {
8270 // Plain load with no read barrier.
8271 // /* HeapReference<Object> */ out = *(obj + offset)
8272 __ movl(out_reg, Address(obj_reg, offset));
8273 __ MaybeUnpoisonHeapReference(out_reg);
8274 }
8275}
8276
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008277void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
8278 HInstruction* instruction,
8279 Location root,
8280 const Address& address,
8281 Label* fixup_label,
8282 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008283 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008284 if (read_barrier_option == kWithReadBarrier) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00008285 DCHECK(gUseReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008286 if (kUseBakerReadBarrier) {
8287 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
8288 // Baker's read barrier are used:
8289 //
Roland Levillaind966ce72017-02-09 16:20:14 +00008290 // root = obj.field;
8291 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8292 // if (temp != null) {
8293 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00008294 // }
8295
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008296 // /* GcRoot<mirror::Object> */ root = *address
8297 __ movl(root_reg, address);
8298 if (fixup_label != nullptr) {
8299 __ Bind(fixup_label);
8300 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008301 static_assert(
8302 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8303 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8304 "have different sizes.");
8305 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8306 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8307 "have different sizes.");
8308
Vladimir Marko953437b2016-08-24 08:30:46 +00008309 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008310 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008311 instruction, root, /* unpoison_ref_before_marking= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008312 codegen_->AddSlowPath(slow_path);
8313
Roland Levillaind966ce72017-02-09 16:20:14 +00008314 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
8315 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01008316 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00008317 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
8318 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008319 __ j(kNotEqual, slow_path->GetEntryLabel());
8320 __ Bind(slow_path->GetExitLabel());
8321 } else {
8322 // GC root loaded through a slow path for read barriers other
8323 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008324 // /* GcRoot<mirror::Object>* */ root = address
8325 __ leal(root_reg, address);
8326 if (fixup_label != nullptr) {
8327 __ Bind(fixup_label);
8328 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008329 // /* mirror::Object* */ root = root->Read()
8330 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8331 }
8332 } else {
8333 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008334 // /* GcRoot<mirror::Object> */ root = *address
8335 __ movl(root_reg, address);
8336 if (fixup_label != nullptr) {
8337 __ Bind(fixup_label);
8338 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00008339 // Note that GC roots are not affected by heap poisoning, thus we
8340 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008341 }
8342}
8343
8344void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8345 Location ref,
8346 Register obj,
8347 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00008348 bool needs_null_check) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00008349 DCHECK(gUseReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008350 DCHECK(kUseBakerReadBarrier);
8351
8352 // /* HeapReference<Object> */ ref = *(obj + offset)
8353 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00008354 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008355}
8356
8357void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8358 Location ref,
8359 Register obj,
8360 uint32_t data_offset,
8361 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00008362 bool needs_null_check) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00008363 DCHECK(gUseReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008364 DCHECK(kUseBakerReadBarrier);
8365
Roland Levillain3d312422016-06-23 13:53:42 +01008366 static_assert(
8367 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8368 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00008369 // /* HeapReference<Object> */ ref =
8370 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008371 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00008372 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008373}
8374
8375void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8376 Location ref,
8377 Register obj,
8378 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008379 bool needs_null_check,
8380 bool always_update_field,
8381 Register* temp) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00008382 DCHECK(gUseReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008383 DCHECK(kUseBakerReadBarrier);
8384
8385 // In slow path based read barriers, the read barrier call is
8386 // inserted after the original load. However, in fast path based
8387 // Baker's read barriers, we need to perform the load of
8388 // mirror::Object::monitor_ *before* the original reference load.
8389 // This load-load ordering is required by the read barrier.
8390 // The fast path/slow path (for Baker's algorithm) should look like:
8391 //
8392 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8393 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8394 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008395 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00008396 // if (is_gray) {
8397 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
8398 // }
8399 //
8400 // Note: the original implementation in ReadBarrier::Barrier is
8401 // slightly more complex as:
8402 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00008403 // the high-bits of rb_state, which are expected to be all zeroes
8404 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
8405 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008406 // - it performs additional checks that we do not do here for
8407 // performance reasons.
8408
8409 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00008410 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
8411
Vladimir Marko953437b2016-08-24 08:30:46 +00008412 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01008413 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008414 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00008415 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
8416 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
8417 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
8418
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008419 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00008420 // ref = ReadBarrier::Mark(ref);
8421 // At this point, just do the "if" and make sure that flags are preserved until the branch.
8422 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00008423 if (needs_null_check) {
8424 MaybeRecordImplicitNullCheck(instruction);
8425 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008426
8427 // Load fence to prevent load-load reordering.
8428 // Note that this is a no-op, thanks to the x86 memory model.
8429 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
8430
8431 // The actual reference load.
8432 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00008433 __ movl(ref_reg, src); // Flags are unaffected.
8434
8435 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
8436 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008437 SlowPathCode* slow_path;
8438 if (always_update_field) {
8439 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01008440 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008441 instruction, ref, obj, src, /* unpoison_ref_before_marking= */ true, *temp);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008442 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008443 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008444 instruction, ref, /* unpoison_ref_before_marking= */ true);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008445 }
Vladimir Marko953437b2016-08-24 08:30:46 +00008446 AddSlowPath(slow_path);
8447
8448 // We have done the "if" of the gray bit check above, now branch based on the flags.
8449 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00008450
8451 // Object* ref = ref_addr->AsMirrorPtr()
8452 __ MaybeUnpoisonHeapReference(ref_reg);
8453
Roland Levillain7c1559a2015-12-15 10:55:36 +00008454 __ Bind(slow_path->GetExitLabel());
8455}
8456
8457void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
8458 Location out,
8459 Location ref,
8460 Location obj,
8461 uint32_t offset,
8462 Location index) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00008463 DCHECK(gUseReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008464
Roland Levillain7c1559a2015-12-15 10:55:36 +00008465 // Insert a slow path based read barrier *after* the reference load.
8466 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008467 // If heap poisoning is enabled, the unpoisoning of the loaded
8468 // reference will be carried out by the runtime within the slow
8469 // path.
8470 //
8471 // Note that `ref` currently does not get unpoisoned (when heap
8472 // poisoning is enabled), which is alright as the `ref` argument is
8473 // not used by the artReadBarrierSlow entry point.
8474 //
8475 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008476 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00008477 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
8478 AddSlowPath(slow_path);
8479
Roland Levillain0d5a2812015-11-13 10:07:31 +00008480 __ jmp(slow_path->GetEntryLabel());
8481 __ Bind(slow_path->GetExitLabel());
8482}
8483
Roland Levillain7c1559a2015-12-15 10:55:36 +00008484void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
8485 Location out,
8486 Location ref,
8487 Location obj,
8488 uint32_t offset,
8489 Location index) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00008490 if (gUseReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008491 // Baker's read barriers shall be handled by the fast path
8492 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
8493 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008494 // If heap poisoning is enabled, unpoisoning will be taken care of
8495 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008496 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008497 } else if (kPoisonHeapReferences) {
8498 __ UnpoisonHeapReference(out.AsRegister<Register>());
8499 }
8500}
8501
Roland Levillain7c1559a2015-12-15 10:55:36 +00008502void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8503 Location out,
8504 Location root) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00008505 DCHECK(gUseReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008506
Roland Levillain7c1559a2015-12-15 10:55:36 +00008507 // Insert a slow path based read barrier *after* the GC root load.
8508 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008509 // Note that GC roots are not affected by heap poisoning, so we do
8510 // not need to do anything special for this here.
8511 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008512 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008513 AddSlowPath(slow_path);
8514
Roland Levillain0d5a2812015-11-13 10:07:31 +00008515 __ jmp(slow_path->GetEntryLabel());
8516 __ Bind(slow_path->GetExitLabel());
8517}
8518
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008519void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008520 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008521 LOG(FATAL) << "Unreachable";
8522}
8523
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008524void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008525 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008526 LOG(FATAL) << "Unreachable";
8527}
8528
Mark Mendellfe57faa2015-09-18 09:26:15 -04008529// Simple implementation of packed switch - generate cascaded compare/jumps.
8530void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8531 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008532 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04008533 locations->SetInAt(0, Location::RequiresRegister());
8534}
8535
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008536void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
8537 int32_t lower_bound,
8538 uint32_t num_entries,
8539 HBasicBlock* switch_block,
8540 HBasicBlock* default_block) {
8541 // Figure out the correct compare values and jump conditions.
8542 // Handle the first compare/branch as a special case because it might
8543 // jump to the default case.
8544 DCHECK_GT(num_entries, 2u);
8545 Condition first_condition;
8546 uint32_t index;
8547 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
8548 if (lower_bound != 0) {
8549 first_condition = kLess;
8550 __ cmpl(value_reg, Immediate(lower_bound));
8551 __ j(first_condition, codegen_->GetLabelOf(default_block));
8552 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008553
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008554 index = 1;
8555 } else {
8556 // Handle all the compare/jumps below.
8557 first_condition = kBelow;
8558 index = 0;
8559 }
8560
8561 // Handle the rest of the compare/jumps.
8562 for (; index + 1 < num_entries; index += 2) {
8563 int32_t compare_to_value = lower_bound + index + 1;
8564 __ cmpl(value_reg, Immediate(compare_to_value));
8565 // Jump to successors[index] if value < case_value[index].
8566 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
8567 // Jump to successors[index + 1] if value == case_value[index + 1].
8568 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
8569 }
8570
8571 if (index != num_entries) {
8572 // There are an odd number of entries. Handle the last one.
8573 DCHECK_EQ(index + 1, num_entries);
8574 __ cmpl(value_reg, Immediate(lower_bound + index));
8575 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008576 }
8577
8578 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008579 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
8580 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008581 }
8582}
8583
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008584void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8585 int32_t lower_bound = switch_instr->GetStartValue();
8586 uint32_t num_entries = switch_instr->GetNumEntries();
8587 LocationSummary* locations = switch_instr->GetLocations();
8588 Register value_reg = locations->InAt(0).AsRegister<Register>();
8589
8590 GenPackedSwitchWithCompares(value_reg,
8591 lower_bound,
8592 num_entries,
8593 switch_instr->GetBlock(),
8594 switch_instr->GetDefaultBlock());
8595}
8596
Mark Mendell805b3b52015-09-18 14:10:29 -04008597void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8598 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008599 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04008600 locations->SetInAt(0, Location::RequiresRegister());
8601
8602 // Constant area pointer.
8603 locations->SetInAt(1, Location::RequiresRegister());
8604
8605 // And the temporary we need.
8606 locations->AddTemp(Location::RequiresRegister());
8607}
8608
8609void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8610 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008611 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04008612 LocationSummary* locations = switch_instr->GetLocations();
8613 Register value_reg = locations->InAt(0).AsRegister<Register>();
8614 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8615
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008616 if (num_entries <= kPackedSwitchJumpTableThreshold) {
8617 GenPackedSwitchWithCompares(value_reg,
8618 lower_bound,
8619 num_entries,
8620 switch_instr->GetBlock(),
8621 default_block);
8622 return;
8623 }
8624
Mark Mendell805b3b52015-09-18 14:10:29 -04008625 // Optimizing has a jump area.
8626 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
8627 Register constant_area = locations->InAt(1).AsRegister<Register>();
8628
8629 // Remove the bias, if needed.
8630 if (lower_bound != 0) {
8631 __ leal(temp_reg, Address(value_reg, -lower_bound));
8632 value_reg = temp_reg;
8633 }
8634
8635 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008636 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04008637 __ cmpl(value_reg, Immediate(num_entries - 1));
8638 __ j(kAbove, codegen_->GetLabelOf(default_block));
8639
8640 // We are in the range of the table.
8641 // Load (target-constant_area) from the jump table, indexing by the value.
8642 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
8643
8644 // Compute the actual target address by adding in constant_area.
8645 __ addl(temp_reg, constant_area);
8646
8647 // And jump.
8648 __ jmp(temp_reg);
8649}
8650
Mark Mendell0616ae02015-04-17 12:49:27 -04008651void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
8652 HX86ComputeBaseMethodAddress* insn) {
8653 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008654 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008655 locations->SetOut(Location::RequiresRegister());
8656}
8657
8658void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
8659 HX86ComputeBaseMethodAddress* insn) {
8660 LocationSummary* locations = insn->GetLocations();
8661 Register reg = locations->Out().AsRegister<Register>();
8662
8663 // Generate call to next instruction.
8664 Label next_instruction;
8665 __ call(&next_instruction);
8666 __ Bind(&next_instruction);
8667
8668 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008669 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04008670
8671 // Grab the return address off the stack.
8672 __ popl(reg);
8673}
8674
8675void LocationsBuilderX86::VisitX86LoadFromConstantTable(
8676 HX86LoadFromConstantTable* insn) {
8677 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008678 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008679
8680 locations->SetInAt(0, Location::RequiresRegister());
8681 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
8682
8683 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00008684 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008685 return;
8686 }
8687
8688 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008689 case DataType::Type::kFloat32:
8690 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008691 locations->SetOut(Location::RequiresFpuRegister());
8692 break;
8693
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008694 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008695 locations->SetOut(Location::RequiresRegister());
8696 break;
8697
8698 default:
8699 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8700 }
8701}
8702
8703void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00008704 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008705 return;
8706 }
8707
8708 LocationSummary* locations = insn->GetLocations();
8709 Location out = locations->Out();
8710 Register const_area = locations->InAt(0).AsRegister<Register>();
8711 HConstant *value = insn->GetConstant();
8712
8713 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008714 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008715 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008716 codegen_->LiteralFloatAddress(
8717 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008718 break;
8719
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008720 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008721 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008722 codegen_->LiteralDoubleAddress(
8723 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008724 break;
8725
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008726 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008727 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008728 codegen_->LiteralInt32Address(
8729 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008730 break;
8731
8732 default:
8733 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8734 }
8735}
8736
Mark Mendell0616ae02015-04-17 12:49:27 -04008737/**
8738 * Class to handle late fixup of offsets into constant area.
8739 */
Vladimir Marko5233f932015-09-29 19:01:15 +01008740class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04008741 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008742 RIPFixup(CodeGeneratorX86& codegen,
8743 HX86ComputeBaseMethodAddress* base_method_address,
8744 size_t offset)
8745 : codegen_(&codegen),
8746 base_method_address_(base_method_address),
8747 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008748
8749 protected:
8750 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
8751
8752 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008753 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008754
8755 private:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008756 void Process(const MemoryRegion& region, int pos) override {
Mark Mendell0616ae02015-04-17 12:49:27 -04008757 // Patch the correct offset for the instruction. The place to patch is the
8758 // last 4 bytes of the instruction.
8759 // The value to patch is the distance from the offset in the constant area
8760 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04008761 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008762 int32_t relative_position =
8763 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04008764
8765 // Patch in the right value.
8766 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
8767 }
8768
Mark Mendell0616ae02015-04-17 12:49:27 -04008769 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04008770 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008771};
8772
Mark Mendell805b3b52015-09-18 14:10:29 -04008773/**
8774 * Class to handle late fixup of offsets to a jump table that will be created in the
8775 * constant area.
8776 */
8777class JumpTableRIPFixup : public RIPFixup {
8778 public:
8779 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008780 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
8781 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008782
8783 void CreateJumpTable() {
8784 X86Assembler* assembler = codegen_->GetAssembler();
8785
8786 // Ensure that the reference to the jump table has the correct offset.
8787 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
8788 SetOffset(offset_in_constant_table);
8789
8790 // The label values in the jump table are computed relative to the
8791 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008792 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04008793
8794 // Populate the jump table with the correct values for the jump table.
8795 int32_t num_entries = switch_instr_->GetNumEntries();
8796 HBasicBlock* block = switch_instr_->GetBlock();
8797 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
8798 // The value that we want is the target offset - the position of the table.
8799 for (int32_t i = 0; i < num_entries; i++) {
8800 HBasicBlock* b = successors[i];
8801 Label* l = codegen_->GetLabelOf(b);
8802 DCHECK(l->IsBound());
8803 int32_t offset_to_block = l->Position() - relative_offset;
8804 assembler->AppendInt32(offset_to_block);
8805 }
8806 }
8807
8808 private:
8809 const HX86PackedSwitch* switch_instr_;
8810};
8811
8812void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
8813 // Generate the constant area if needed.
8814 X86Assembler* assembler = GetAssembler();
jaishank20d1c942019-03-08 15:08:17 +05308815
Mark Mendell805b3b52015-09-18 14:10:29 -04008816 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
8817 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
8818 // byte values.
8819 assembler->Align(4, 0);
8820 constant_area_start_ = assembler->CodeSize();
8821
8822 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008823 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04008824 jump_table->CreateJumpTable();
8825 }
8826
8827 // And now add the constant area to the generated code.
8828 assembler->AddConstantArea();
8829 }
8830
8831 // And finish up.
8832 CodeGenerator::Finalize(allocator);
8833}
8834
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008835Address CodeGeneratorX86::LiteralDoubleAddress(double v,
8836 HX86ComputeBaseMethodAddress* method_base,
8837 Register reg) {
8838 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008839 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008840 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008841}
8842
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008843Address CodeGeneratorX86::LiteralFloatAddress(float v,
8844 HX86ComputeBaseMethodAddress* method_base,
8845 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008846 AssemblerFixup* fixup =
8847 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008848 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008849}
8850
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008851Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
8852 HX86ComputeBaseMethodAddress* method_base,
8853 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008854 AssemblerFixup* fixup =
8855 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008856 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008857}
8858
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008859Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
8860 HX86ComputeBaseMethodAddress* method_base,
8861 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008862 AssemblerFixup* fixup =
8863 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008864 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008865}
8866
Aart Bika19616e2016-02-01 18:57:58 -08008867void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
8868 if (value == 0) {
8869 __ xorl(dest, dest);
8870 } else {
8871 __ movl(dest, Immediate(value));
8872 }
8873}
8874
8875void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
8876 if (value == 0) {
8877 __ testl(dest, dest);
8878 } else {
8879 __ cmpl(dest, Immediate(value));
8880 }
8881}
8882
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008883void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
8884 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07008885 GenerateIntCompare(lhs_reg, rhs);
8886}
8887
8888void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008889 if (rhs.IsConstant()) {
8890 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07008891 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008892 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07008893 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008894 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07008895 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008896 }
8897}
8898
8899Address CodeGeneratorX86::ArrayAddress(Register obj,
8900 Location index,
8901 ScaleFactor scale,
8902 uint32_t data_offset) {
8903 return index.IsConstant() ?
8904 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
8905 Address(obj, index.AsRegister<Register>(), scale, data_offset);
8906}
8907
Mark Mendell805b3b52015-09-18 14:10:29 -04008908Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
8909 Register reg,
8910 Register value) {
8911 // Create a fixup to be used to create and address the jump table.
8912 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008913 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04008914
8915 // We have to populate the jump tables.
8916 fixups_to_jump_tables_.push_back(table_fixup);
8917
8918 // We want a scaled address, as we are extracting the correct offset from the table.
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008919 return Address(reg, value, TIMES_4, kPlaceholder32BitOffset, table_fixup);
Mark Mendell805b3b52015-09-18 14:10:29 -04008920}
8921
Andreas Gampe85b62f22015-09-09 13:15:38 -07008922// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008923void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07008924 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008925 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008926 return;
8927 }
8928
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008929 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008930
8931 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
8932 if (target.Equals(return_loc)) {
8933 return;
8934 }
8935
8936 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8937 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008938 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008939 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008940 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
8941 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008942 GetMoveResolver()->EmitNativeCode(&parallel_move);
8943 } else {
8944 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01008945 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07008946 parallel_move.AddMove(return_loc, target, type, nullptr);
8947 GetMoveResolver()->EmitNativeCode(&parallel_move);
8948 }
8949}
8950
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008951void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
8952 const uint8_t* roots_data,
8953 const PatchInfo<Label>& info,
8954 uint64_t index_in_table) const {
8955 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
8956 uintptr_t address =
8957 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
Andreas Gampec55bb392018-09-21 00:02:02 +00008958 using unaligned_uint32_t __attribute__((__aligned__(1))) = uint32_t;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008959 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
8960 dchecked_integral_cast<uint32_t>(address);
8961}
8962
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008963void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8964 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008965 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008966 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008967 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008968 }
8969
8970 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008971 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008972 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008973 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008974 }
8975}
8976
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008977void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8978 ATTRIBUTE_UNUSED) {
8979 LOG(FATAL) << "Unreachable";
8980}
8981
8982void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8983 ATTRIBUTE_UNUSED) {
8984 LOG(FATAL) << "Unreachable";
8985}
8986
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05308987bool LocationsBuilderX86::CpuHasAvxFeatureFlag() {
8988 return codegen_->GetInstructionSetFeatures().HasAVX();
8989}
8990bool LocationsBuilderX86::CpuHasAvx2FeatureFlag() {
8991 return codegen_->GetInstructionSetFeatures().HasAVX2();
8992}
8993bool InstructionCodeGeneratorX86::CpuHasAvxFeatureFlag() {
8994 return codegen_->GetInstructionSetFeatures().HasAVX();
8995}
8996bool InstructionCodeGeneratorX86::CpuHasAvx2FeatureFlag() {
8997 return codegen_->GetInstructionSetFeatures().HasAVX2();
8998}
8999
Roland Levillain4d027112015-07-01 15:41:14 +01009000#undef __
9001
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00009002} // namespace x86
9003} // namespace art