blob: 58cb56d243bb9b633821a556cbcb4343a792bf78 [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"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000024#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010025#include "gc/accounting/card_table.h"
Vladimir Markoeebb8212018-06-05 14:57:24 +010026#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070027#include "heap_poisoning.h"
Nicolas Geoffray8b8d93d2020-09-17 14:30:01 +010028#include "interpreter/mterp/nterp.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040029#include "intrinsics.h"
Ulya Trafimovichec696e52022-01-26 10:21:32 +000030#include "intrinsics_utils.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040031#include "intrinsics_x86.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000032#include "jit/profiling_info.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010033#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070034#include "lock_word.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070035#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070036#include "mirror/class-inl.h"
Andra Danciu52d2c0c2020-09-15 14:27:21 +000037#include "mirror/var_handle.h"
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +000038#include "optimizing/nodes.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
VladimĂ­r Marko434d9682022-11-04 14:04:17 +000046namespace art HIDDEN {
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);
Mythri Allebab6beb2022-10-21 13:28:05 +0000970 if (instruction_->IsMethodExitHook()) {
971 __ movl(EBX, Immediate(codegen->GetFrameSize()));
972 }
Mythri Alle5097f832021-11-02 14:52:30 +0000973 x86_codegen->InvokeRuntime(entry_point, instruction_, instruction_->GetDexPc(), this);
974 RestoreLiveRegisters(codegen, locations);
975 __ jmp(GetExitLabel());
976 }
977
978 const char* GetDescription() const override {
979 return "MethodEntryExitHooksSlowPath";
980 }
981
982 private:
983 DISALLOW_COPY_AND_ASSIGN(MethodEntryExitHooksSlowPathX86);
984};
985
Nicolas Geoffray9e598902021-11-19 14:53:07 +0000986class CompileOptimizedSlowPathX86 : public SlowPathCode {
987 public:
988 CompileOptimizedSlowPathX86() : SlowPathCode(/* instruction= */ nullptr) {}
989
990 void EmitNativeCode(CodeGenerator* codegen) override {
991 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
992 __ Bind(GetEntryLabel());
993 x86_codegen->GenerateInvokeRuntime(
994 GetThreadOffset<kX86PointerSize>(kQuickCompileOptimized).Int32Value());
995 __ jmp(GetExitLabel());
996 }
997
998 const char* GetDescription() const override {
999 return "CompileOptimizedSlowPath";
1000 }
1001
1002 private:
1003 DISALLOW_COPY_AND_ASSIGN(CompileOptimizedSlowPathX86);
1004};
1005
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001006#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001007// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1008#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001009
Aart Bike9f37602015-10-09 11:15:55 -07001010inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001011 switch (cond) {
1012 case kCondEQ: return kEqual;
1013 case kCondNE: return kNotEqual;
1014 case kCondLT: return kLess;
1015 case kCondLE: return kLessEqual;
1016 case kCondGT: return kGreater;
1017 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -07001018 case kCondB: return kBelow;
1019 case kCondBE: return kBelowEqual;
1020 case kCondA: return kAbove;
1021 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -07001022 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001023 LOG(FATAL) << "Unreachable";
1024 UNREACHABLE();
1025}
1026
Aart Bike9f37602015-10-09 11:15:55 -07001027// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001028inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
1029 switch (cond) {
1030 case kCondEQ: return kEqual;
1031 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -07001032 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001033 case kCondLT: return kBelow;
1034 case kCondLE: return kBelowEqual;
1035 case kCondGT: return kAbove;
1036 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -07001037 // Unsigned remain unchanged.
1038 case kCondB: return kBelow;
1039 case kCondBE: return kBelowEqual;
1040 case kCondA: return kAbove;
1041 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001042 }
1043 LOG(FATAL) << "Unreachable";
1044 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001045}
1046
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001047void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001048 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001049}
1050
1051void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001052 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001053}
1054
Vladimir Markoa0431112018-06-25 09:32:54 +01001055const X86InstructionSetFeatures& CodeGeneratorX86::GetInstructionSetFeatures() const {
1056 return *GetCompilerOptions().GetInstructionSetFeatures()->AsX86InstructionSetFeatures();
1057}
1058
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001059size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1060 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
1061 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001062}
1063
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001064size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1065 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
1066 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001067}
1068
Mark Mendell7c8d0092015-01-26 11:21:33 -05001069size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001070 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001071 __ movups(Address(ESP, stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -07001072 } else {
1073 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
1074 }
Artem Serov6a0b6572019-07-26 20:38:37 +01001075 return GetSlowPathFPWidth();
Mark Mendell7c8d0092015-01-26 11:21:33 -05001076}
1077
1078size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001079 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001080 __ movups(XmmRegister(reg_id), Address(ESP, stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -07001081 } else {
1082 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
1083 }
Artem Serov6a0b6572019-07-26 20:38:37 +01001084 return GetSlowPathFPWidth();
Mark Mendell7c8d0092015-01-26 11:21:33 -05001085}
1086
Calin Juravle175dc732015-08-25 15:42:32 +01001087void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
1088 HInstruction* instruction,
1089 uint32_t dex_pc,
1090 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001091 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001092 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
1093 if (EntrypointRequiresStackMap(entrypoint)) {
1094 RecordPcInfo(instruction, dex_pc, slow_path);
1095 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001096}
1097
Roland Levillaindec8f632016-07-22 17:10:06 +01001098void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1099 HInstruction* instruction,
1100 SlowPathCode* slow_path) {
1101 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001102 GenerateInvokeRuntime(entry_point_offset);
1103}
1104
1105void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001106 __ fs()->call(Address::Absolute(entry_point_offset));
1107}
1108
Mark Mendellfb8d2792015-03-31 22:16:59 -04001109CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001110 const CompilerOptions& compiler_options,
1111 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001112 : CodeGenerator(graph,
1113 kNumberOfCpuRegisters,
1114 kNumberOfXmmRegisters,
1115 kNumberOfRegisterPairs,
1116 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1117 arraysize(kCoreCalleeSaves))
1118 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001119 0,
1120 compiler_options,
1121 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001122 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001123 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001124 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001125 move_resolver_(graph->GetAllocator(), this),
Shalini Salomi Bodapati6545ee32021-11-02 20:01:06 +05301126 assembler_(graph->GetAllocator(),
1127 compiler_options.GetInstructionSetFeatures()->AsX86InstructionSetFeatures()),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001128 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1129 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1130 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1131 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko8f63f102020-09-28 12:10:28 +01001132 public_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1133 package_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001134 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001135 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoeb9eb002020-10-02 13:54:19 +01001136 boot_image_jni_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +01001137 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001138 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1139 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001140 constant_area_start_(-1),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001141 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001142 method_address_offset_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001143 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001144 // Use a fake return address register to mimic Quick.
1145 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001146}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001147
David Brazdil58282f42016-01-14 12:45:10 +00001148void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001149 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001150 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001151}
1152
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001153InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001154 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001155 assembler_(codegen->GetAssembler()),
1156 codegen_(codegen) {}
1157
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001158static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001159 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001160}
1161
Mythri Alle5097f832021-11-02 14:52:30 +00001162void SetInForReturnValue(HInstruction* ret, LocationSummary* locations) {
1163 switch (ret->InputAt(0)->GetType()) {
1164 case DataType::Type::kReference:
1165 case DataType::Type::kBool:
1166 case DataType::Type::kUint8:
1167 case DataType::Type::kInt8:
1168 case DataType::Type::kUint16:
1169 case DataType::Type::kInt16:
1170 case DataType::Type::kInt32:
1171 locations->SetInAt(0, Location::RegisterLocation(EAX));
1172 break;
1173
1174 case DataType::Type::kInt64:
1175 locations->SetInAt(0, Location::RegisterPairLocation(EAX, EDX));
1176 break;
1177
1178 case DataType::Type::kFloat32:
1179 case DataType::Type::kFloat64:
1180 locations->SetInAt(0, Location::FpuRegisterLocation(XMM0));
1181 break;
1182
1183 case DataType::Type::kVoid:
1184 locations->SetInAt(0, Location::NoLocation());
1185 break;
1186
1187 default:
1188 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
1189 }
1190}
1191
1192void LocationsBuilderX86::VisitMethodExitHook(HMethodExitHook* method_hook) {
1193 LocationSummary* locations = new (GetGraph()->GetAllocator())
1194 LocationSummary(method_hook, LocationSummary::kCallOnSlowPath);
1195 SetInForReturnValue(method_hook, locations);
1196}
1197
1198void InstructionCodeGeneratorX86::GenerateMethodEntryExitHook(HInstruction* instruction) {
1199 SlowPathCode* slow_path =
1200 new (codegen_->GetScopedAllocator()) MethodEntryExitHooksSlowPathX86(instruction);
1201 codegen_->AddSlowPath(slow_path);
1202
Mythri Alle5eff6b32022-11-04 10:57:53 +00001203 if (instruction->IsMethodExitHook()) {
1204 // Check if we are required to check if the caller needs a deoptimization. Strictly speaking it
1205 // would be sufficient to check if CheckCallerForDeopt bit is set. Though it is faster to check
1206 // if it is just non-zero. kCHA bit isn't used in debuggable runtimes as cha optimization is
1207 // disabled in debuggable runtime. The other bit is used when this method itself requires a
1208 // deoptimization due to redefinition. So it is safe to just check for non-zero value here.
1209 __ cmpl(Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
1210 __ j(kNotEqual, slow_path->GetEntryLabel());
1211 }
1212
Mythri Alle5097f832021-11-02 14:52:30 +00001213 uint64_t address = reinterpret_cast64<uint64_t>(Runtime::Current()->GetInstrumentation());
Mythri Alle5b263ae2022-10-21 15:07:44 +00001214 MemberOffset offset = instruction->IsMethodExitHook() ?
Mythri Alle5eff6b32022-11-04 10:57:53 +00001215 instrumentation::Instrumentation::HaveMethodExitListenersOffset() :
Mythri Alle5b263ae2022-10-21 15:07:44 +00001216 instrumentation::Instrumentation::HaveMethodEntryListenersOffset();
1217 __ cmpb(Address::Absolute(address + offset.Int32Value()), Immediate(0));
Mythri Alle9575c122021-11-12 12:04:41 +00001218 __ j(kNotEqual, slow_path->GetEntryLabel());
Mythri Alle5097f832021-11-02 14:52:30 +00001219 __ Bind(slow_path->GetExitLabel());
1220}
1221
1222void InstructionCodeGeneratorX86::VisitMethodExitHook(HMethodExitHook* instruction) {
1223 DCHECK(codegen_->GetCompilerOptions().IsJitCompiler() && GetGraph()->IsDebuggable());
1224 DCHECK(codegen_->RequiresCurrentMethod());
1225 GenerateMethodEntryExitHook(instruction);
1226}
1227
1228void LocationsBuilderX86::VisitMethodEntryHook(HMethodEntryHook* method_hook) {
1229 new (GetGraph()->GetAllocator()) LocationSummary(method_hook, LocationSummary::kCallOnSlowPath);
1230}
1231
1232void InstructionCodeGeneratorX86::VisitMethodEntryHook(HMethodEntryHook* instruction) {
1233 DCHECK(codegen_->GetCompilerOptions().IsJitCompiler() && GetGraph()->IsDebuggable());
1234 DCHECK(codegen_->RequiresCurrentMethod());
1235 GenerateMethodEntryExitHook(instruction);
1236}
1237
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001238void CodeGeneratorX86::MaybeIncrementHotness(bool is_frame_entry) {
1239 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1240 Register reg = EAX;
1241 if (is_frame_entry) {
1242 reg = kMethodRegisterArgument;
1243 } else {
1244 __ pushl(EAX);
Vladimir Markodec78172020-06-19 15:31:23 +01001245 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001246 __ movl(EAX, Address(ESP, kX86WordSize));
1247 }
1248 NearLabel overflow;
1249 __ cmpw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()),
Nicolas Geoffray61673dc2021-11-06 13:58:31 +00001250 Immediate(interpreter::kNterpHotnessValue));
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001251 __ j(kEqual, &overflow);
Nicolas Geoffray61673dc2021-11-06 13:58:31 +00001252 __ addw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()), Immediate(-1));
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001253 __ Bind(&overflow);
1254 if (!is_frame_entry) {
1255 __ popl(EAX);
Vladimir Markodec78172020-06-19 15:31:23 +01001256 __ cfi().AdjustCFAOffset(-4);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001257 }
1258 }
1259
1260 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffray9e598902021-11-19 14:53:07 +00001261 SlowPathCode* slow_path = new (GetScopedAllocator()) CompileOptimizedSlowPathX86();
1262 AddSlowPath(slow_path);
1263 ProfilingInfo* info = GetGraph()->GetProfilingInfo();
1264 DCHECK(info != nullptr);
1265 uint32_t address = reinterpret_cast32<uint32_t>(info) +
1266 ProfilingInfo::BaselineHotnessCountOffset().Int32Value();
1267 DCHECK(!HasEmptyFrame());
1268 // With multiple threads, this can overflow. This is OK, we will eventually get to see
1269 // it reaching 0. Also, at this point we have no register available to look
1270 // at the counter directly.
1271 __ addw(Address::Absolute(address), Immediate(-1));
1272 __ j(kEqual, slow_path->GetEntryLabel());
1273 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001274 }
1275}
1276
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001277void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001278 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffrayab3f8d22022-07-23 15:49:51 +00001279
1280 // Check if we need to generate the clinit check. We will jump to the
1281 // resolution stub if the class is not initialized and the executing thread is
1282 // not the thread initializing it.
1283 // We do this before constructing the frame to get the correct stack trace if
1284 // an exception is thrown.
1285 if (GetCompilerOptions().ShouldCompileWithClinitCheck(GetGraph()->GetArtMethod())) {
1286 NearLabel continue_execution, resolution;
1287 // We'll use EBP as temporary.
1288 __ pushl(EBP);
1289 // Check if we're visibly initialized.
1290
1291 // We don't emit a read barrier here to save on code size. We rely on the
1292 // resolution trampoline to do a suspend check before re-entering this code.
1293 __ movl(EBP, Address(kMethodRegisterArgument, ArtMethod::DeclaringClassOffset().Int32Value()));
1294 __ cmpb(Address(EBP, status_byte_offset), Immediate(shifted_visibly_initialized_value));
1295 __ j(kAboveEqual, &continue_execution);
1296
1297 // Check if we're initializing and the thread initializing is the one
1298 // executing the code.
1299 __ cmpb(Address(EBP, status_byte_offset), Immediate(shifted_initializing_value));
1300 __ j(kBelow, &resolution);
1301
1302 __ movl(EBP, Address(EBP, mirror::Class::ClinitThreadIdOffset().Int32Value()));
1303 __ fs()->cmpl(EBP, Address::Absolute(Thread::TidOffset<kX86PointerSize>().Int32Value()));
1304 __ j(kEqual, &continue_execution);
1305 __ Bind(&resolution);
1306
1307 __ popl(EBP);
1308 // Jump to the resolution stub.
1309 ThreadOffset32 entrypoint_offset =
1310 GetThreadOffset<kX86PointerSize>(kQuickQuickResolutionTrampoline);
1311 __ fs()->jmp(Address::Absolute(entrypoint_offset));
1312
1313 __ Bind(&continue_execution);
1314 __ popl(EBP);
1315 }
1316
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001317 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001318 bool skip_overflow_check =
1319 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001320 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001321
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001322 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001323 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86);
1324 __ testl(EAX, Address(ESP, -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001325 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001326 }
1327
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001328 if (!HasEmptyFrame()) {
1329 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1330 Register reg = kCoreCalleeSaves[i];
1331 if (allocated_registers_.ContainsCoreRegister(reg)) {
1332 __ pushl(reg);
1333 __ cfi().AdjustCFAOffset(kX86WordSize);
1334 __ cfi().RelOffset(DWARFReg(reg), 0);
1335 }
1336 }
Mark Mendell5f874182015-03-04 15:42:45 -05001337
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001338 int adjust = GetFrameSize() - FrameEntrySpillSize();
Vladimir Markodec78172020-06-19 15:31:23 +01001339 IncreaseFrame(adjust);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001340 // Save the current method if we need it. Note that we do not
1341 // do this in HCurrentMethod, as the instruction might have been removed
1342 // in the SSA graph.
1343 if (RequiresCurrentMethod()) {
1344 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1345 }
1346
1347 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1348 // Initialize should_deoptimize flag to 0.
1349 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
Mark Mendell5f874182015-03-04 15:42:45 -05001350 }
1351 }
1352
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001353 MaybeIncrementHotness(/* is_frame_entry= */ true);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001354}
1355
1356void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001357 __ cfi().RememberState();
1358 if (!HasEmptyFrame()) {
1359 int adjust = GetFrameSize() - FrameEntrySpillSize();
Vladimir Markodec78172020-06-19 15:31:23 +01001360 DecreaseFrame(adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001361
David Srbeckyc34dc932015-04-12 09:27:43 +01001362 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1363 Register reg = kCoreCalleeSaves[i];
1364 if (allocated_registers_.ContainsCoreRegister(reg)) {
1365 __ popl(reg);
1366 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1367 __ cfi().Restore(DWARFReg(reg));
1368 }
Mark Mendell5f874182015-03-04 15:42:45 -05001369 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001370 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001371 __ ret();
1372 __ cfi().RestoreState();
1373 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001374}
1375
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001376void CodeGeneratorX86::Bind(HBasicBlock* block) {
1377 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001378}
1379
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001380Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001381 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001382 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001383 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001384 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001385 case DataType::Type::kInt8:
1386 case DataType::Type::kUint16:
1387 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08001388 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001389 case DataType::Type::kInt32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001390 return Location::RegisterLocation(EAX);
1391
Aart Bik66c158e2018-01-31 12:55:04 -08001392 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001393 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001394 return Location::RegisterPairLocation(EAX, EDX);
1395
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001396 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001397 return Location::NoLocation();
1398
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001399 case DataType::Type::kFloat64:
1400 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001401 return Location::FpuRegisterLocation(XMM0);
1402 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001403
1404 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001405}
1406
1407Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1408 return Location::RegisterLocation(kMethodRegisterArgument);
1409}
1410
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001411Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001412 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001413 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001414 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001415 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001416 case DataType::Type::kInt8:
1417 case DataType::Type::kUint16:
1418 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001419 case DataType::Type::kInt32: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001420 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001421 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001422 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001423 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001424 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001425 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001426 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001427 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001428
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001429 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001430 uint32_t index = gp_index_;
1431 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001432 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001433 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001434 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1435 calling_convention.GetRegisterPairAt(index));
1436 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001437 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001438 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1439 }
1440 }
1441
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001442 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001443 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001444 stack_index_++;
1445 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1446 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1447 } else {
1448 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1449 }
1450 }
1451
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001452 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001453 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001454 stack_index_ += 2;
1455 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1456 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1457 } else {
1458 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001459 }
1460 }
1461
Aart Bik66c158e2018-01-31 12:55:04 -08001462 case DataType::Type::kUint32:
1463 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001464 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001465 LOG(FATAL) << "Unexpected parameter type " << type;
Elliott Hughesc1896c92018-11-29 11:33:18 -08001466 UNREACHABLE();
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001467 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001468 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001469}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001470
Vladimir Marko86c87522020-05-11 16:55:55 +01001471Location CriticalNativeCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
1472 DCHECK_NE(type, DataType::Type::kReference);
1473
1474 Location location;
1475 if (DataType::Is64BitType(type)) {
1476 location = Location::DoubleStackSlot(stack_offset_);
1477 stack_offset_ += 2 * kFramePointerSize;
1478 } else {
1479 location = Location::StackSlot(stack_offset_);
1480 stack_offset_ += kFramePointerSize;
1481 }
1482 if (for_register_allocation_) {
1483 location = Location::Any();
1484 }
1485 return location;
1486}
1487
1488Location CriticalNativeCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
1489 // We perform conversion to the managed ABI return register after the call if needed.
1490 InvokeDexCallingConventionVisitorX86 dex_calling_convention;
1491 return dex_calling_convention.GetReturnLocation(type);
1492}
1493
1494Location CriticalNativeCallingConventionVisitorX86::GetMethodLocation() const {
1495 // Pass the method in the hidden argument EAX.
1496 return Location::RegisterLocation(EAX);
1497}
1498
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001499void CodeGeneratorX86::Move32(Location destination, Location source) {
1500 if (source.Equals(destination)) {
1501 return;
1502 }
1503 if (destination.IsRegister()) {
1504 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001505 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001506 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001507 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Andra Danciu5e13d452020-09-08 14:35:09 +00001508 } else if (source.IsConstant()) {
1509 int32_t value = GetInt32ValueOf(source.GetConstant());
1510 __ movl(destination.AsRegister<Register>(), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001511 } else {
1512 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001513 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001514 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001515 } else if (destination.IsFpuRegister()) {
1516 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001517 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001518 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001519 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001520 } else {
1521 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001522 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001523 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001524 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001525 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001526 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001527 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001528 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001529 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001530 } else if (source.IsConstant()) {
1531 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001532 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001533 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001534 } else {
1535 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001536 __ pushl(Address(ESP, source.GetStackIndex()));
1537 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001538 }
1539 }
1540}
1541
1542void CodeGeneratorX86::Move64(Location destination, Location source) {
1543 if (source.Equals(destination)) {
1544 return;
1545 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001546 if (destination.IsRegisterPair()) {
1547 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001548 EmitParallelMoves(
1549 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1550 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001551 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001552 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001553 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001554 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001555 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001556 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1557 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1558 __ psrlq(src_reg, Immediate(32));
1559 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001560 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001561 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001562 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001563 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1564 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001565 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1566 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001567 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001568 if (source.IsFpuRegister()) {
1569 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1570 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001571 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001572 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001573 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Vladimir Markodec78172020-06-19 15:31:23 +01001574 // Push the 2 source registers to the stack.
1575 __ pushl(source.AsRegisterPairHigh<Register>());
1576 __ cfi().AdjustCFAOffset(elem_size);
1577 __ pushl(source.AsRegisterPairLow<Register>());
1578 __ cfi().AdjustCFAOffset(elem_size);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001579 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1580 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01001581 DecreaseFrame(2 * elem_size);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001582 } else {
1583 LOG(FATAL) << "Unimplemented";
1584 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001585 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001586 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001587 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001588 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001589 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001590 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001591 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001592 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001593 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001594 } else if (source.IsConstant()) {
1595 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001596 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1597 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001598 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001599 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1600 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001601 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001602 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001603 EmitParallelMoves(
1604 Location::StackSlot(source.GetStackIndex()),
1605 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001606 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001607 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001608 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001609 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001610 }
1611 }
1612}
1613
Andra Danciu1ca6f322020-08-12 08:58:07 +00001614static Address CreateAddress(Register base,
1615 Register index = Register::kNoRegister,
1616 ScaleFactor scale = TIMES_1,
1617 int32_t disp = 0) {
1618 if (index == Register::kNoRegister) {
1619 return Address(base, disp);
1620 }
1621
1622 return Address(base, index, scale, disp);
1623}
1624
Andra Danciud0f71f22020-09-17 09:00:15 +00001625void CodeGeneratorX86::LoadFromMemoryNoBarrier(DataType::Type dst_type,
1626 Location dst,
1627 Address src,
Ulya Trafimovich322eced2021-06-02 15:39:36 +01001628 HInstruction* instr,
Andra Danciud0f71f22020-09-17 09:00:15 +00001629 XmmRegister temp,
1630 bool is_atomic_load) {
Andra Danciu1ca6f322020-08-12 08:58:07 +00001631 switch (dst_type) {
1632 case DataType::Type::kBool:
1633 case DataType::Type::kUint8:
1634 __ movzxb(dst.AsRegister<Register>(), src);
1635 break;
1636 case DataType::Type::kInt8:
1637 __ movsxb(dst.AsRegister<Register>(), src);
1638 break;
1639 case DataType::Type::kInt16:
1640 __ movsxw(dst.AsRegister<Register>(), src);
1641 break;
1642 case DataType::Type::kUint16:
1643 __ movzxw(dst.AsRegister<Register>(), src);
1644 break;
1645 case DataType::Type::kInt32:
Andra Danciu1ca6f322020-08-12 08:58:07 +00001646 __ movl(dst.AsRegister<Register>(), src);
1647 break;
Andra Danciud0f71f22020-09-17 09:00:15 +00001648 case DataType::Type::kInt64: {
1649 if (is_atomic_load) {
1650 __ movsd(temp, src);
Ulya Trafimovich322eced2021-06-02 15:39:36 +01001651 if (instr != nullptr) {
1652 MaybeRecordImplicitNullCheck(instr);
1653 }
Andra Danciud0f71f22020-09-17 09:00:15 +00001654 __ movd(dst.AsRegisterPairLow<Register>(), temp);
1655 __ psrlq(temp, Immediate(32));
1656 __ movd(dst.AsRegisterPairHigh<Register>(), temp);
1657 } else {
1658 DCHECK_NE(src.GetBaseRegister(), dst.AsRegisterPairLow<Register>());
Ulya Trafimovich893e2ed2021-06-10 16:18:12 +01001659 Address src_high = Address::displace(src, kX86WordSize);
Andra Danciud0f71f22020-09-17 09:00:15 +00001660 __ movl(dst.AsRegisterPairLow<Register>(), src);
Ulya Trafimovich322eced2021-06-02 15:39:36 +01001661 if (instr != nullptr) {
1662 MaybeRecordImplicitNullCheck(instr);
1663 }
Andra Danciud0f71f22020-09-17 09:00:15 +00001664 __ movl(dst.AsRegisterPairHigh<Register>(), src_high);
1665 }
Andra Danciu1ca6f322020-08-12 08:58:07 +00001666 break;
1667 }
1668 case DataType::Type::kFloat32:
1669 __ movss(dst.AsFpuRegister<XmmRegister>(), src);
1670 break;
1671 case DataType::Type::kFloat64:
1672 __ movsd(dst.AsFpuRegister<XmmRegister>(), src);
1673 break;
Andra Danciu1ca6f322020-08-12 08:58:07 +00001674 case DataType::Type::kReference:
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00001675 DCHECK(!gUseReadBarrier);
Andra Danciud0f71f22020-09-17 09:00:15 +00001676 __ movl(dst.AsRegister<Register>(), src);
1677 __ MaybeUnpoisonHeapReference(dst.AsRegister<Register>());
1678 break;
1679 default:
Andra Danciu1ca6f322020-08-12 08:58:07 +00001680 LOG(FATAL) << "Unreachable type " << dst_type;
1681 }
Ulya Trafimovich322eced2021-06-02 15:39:36 +01001682 if (instr != nullptr && dst_type != DataType::Type::kInt64) {
1683 // kInt64 needs special handling that is done in the above switch.
1684 MaybeRecordImplicitNullCheck(instr);
1685 }
Andra Danciu1ca6f322020-08-12 08:58:07 +00001686}
1687
Andra Danciu73c31802020-09-01 13:17:05 +00001688void CodeGeneratorX86::MoveToMemory(DataType::Type src_type,
1689 Location src,
1690 Register dst_base,
1691 Register dst_index,
1692 ScaleFactor dst_scale,
1693 int32_t dst_disp) {
1694 DCHECK(dst_base != Register::kNoRegister);
1695 Address dst = CreateAddress(dst_base, dst_index, dst_scale, dst_disp);
1696
1697 switch (src_type) {
1698 case DataType::Type::kBool:
1699 case DataType::Type::kUint8:
1700 case DataType::Type::kInt8: {
1701 if (src.IsConstant()) {
1702 __ movb(dst, Immediate(CodeGenerator::GetInt8ValueOf(src.GetConstant())));
1703 } else {
1704 __ movb(dst, src.AsRegister<ByteRegister>());
1705 }
1706 break;
1707 }
1708 case DataType::Type::kUint16:
1709 case DataType::Type::kInt16: {
1710 if (src.IsConstant()) {
1711 __ movw(dst, Immediate(CodeGenerator::GetInt16ValueOf(src.GetConstant())));
1712 } else {
1713 __ movw(dst, src.AsRegister<Register>());
1714 }
1715 break;
1716 }
1717 case DataType::Type::kUint32:
1718 case DataType::Type::kInt32: {
1719 if (src.IsConstant()) {
1720 int32_t v = CodeGenerator::GetInt32ValueOf(src.GetConstant());
1721 __ movl(dst, Immediate(v));
1722 } else {
1723 __ movl(dst, src.AsRegister<Register>());
1724 }
1725 break;
1726 }
1727 case DataType::Type::kUint64:
1728 case DataType::Type::kInt64: {
1729 Address dst_next_4_bytes = CreateAddress(dst_base, dst_index, dst_scale, dst_disp + 4);
1730 if (src.IsConstant()) {
1731 int64_t v = CodeGenerator::GetInt64ValueOf(src.GetConstant());
1732 __ movl(dst, Immediate(Low32Bits(v)));
1733 __ movl(dst_next_4_bytes, Immediate(High32Bits(v)));
1734 } else {
1735 __ movl(dst, src.AsRegisterPairLow<Register>());
1736 __ movl(dst_next_4_bytes, src.AsRegisterPairHigh<Register>());
1737 }
1738 break;
1739 }
1740 case DataType::Type::kFloat32: {
1741 if (src.IsConstant()) {
1742 int32_t v = CodeGenerator::GetInt32ValueOf(src.GetConstant());
1743 __ movl(dst, Immediate(v));
1744 } else {
1745 __ movss(dst, src.AsFpuRegister<XmmRegister>());
1746 }
1747 break;
1748 }
1749 case DataType::Type::kFloat64: {
1750 Address dst_next_4_bytes = CreateAddress(dst_base, dst_index, dst_scale, dst_disp + 4);
1751 if (src.IsConstant()) {
1752 int64_t v = CodeGenerator::GetInt64ValueOf(src.GetConstant());
1753 __ movl(dst, Immediate(Low32Bits(v)));
1754 __ movl(dst_next_4_bytes, Immediate(High32Bits(v)));
1755 } else {
1756 __ movsd(dst, src.AsFpuRegister<XmmRegister>());
1757 }
1758 break;
1759 }
1760 case DataType::Type::kVoid:
1761 case DataType::Type::kReference:
1762 LOG(FATAL) << "Unreachable type " << src_type;
1763 }
1764}
1765
Calin Juravle175dc732015-08-25 15:42:32 +01001766void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1767 DCHECK(location.IsRegister());
1768 __ movl(location.AsRegister<Register>(), Immediate(value));
1769}
1770
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001771void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001772 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001773 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1774 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1775 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001776 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001777 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001778 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001779 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001780}
1781
1782void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1783 if (location.IsRegister()) {
1784 locations->AddTemp(location);
1785 } else if (location.IsRegisterPair()) {
1786 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1787 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1788 } else {
1789 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1790 }
1791}
1792
David Brazdilfc6a86a2015-06-26 10:33:45 +00001793void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001794 if (successor->IsExitBlock()) {
1795 DCHECK(got->GetPrevious()->AlwaysThrows());
1796 return; // no code needed
1797 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001798
1799 HBasicBlock* block = got->GetBlock();
1800 HInstruction* previous = got->GetPrevious();
1801
1802 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001803 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001804 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001805 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1806 return;
1807 }
1808
1809 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1810 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1811 }
1812 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001813 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001814 }
1815}
1816
David Brazdilfc6a86a2015-06-26 10:33:45 +00001817void LocationsBuilderX86::VisitGoto(HGoto* got) {
1818 got->SetLocations(nullptr);
1819}
1820
1821void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1822 HandleGoto(got, got->GetSuccessor());
1823}
1824
1825void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1826 try_boundary->SetLocations(nullptr);
1827}
1828
1829void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1830 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1831 if (!successor->IsExitBlock()) {
1832 HandleGoto(try_boundary, successor);
1833 }
1834}
1835
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001836void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001837 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001838}
1839
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001840void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001841}
1842
Mark Mendell152408f2015-12-31 12:28:50 -05001843template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001844void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001845 LabelType* true_label,
1846 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001847 if (cond->IsFPConditionTrueIfNaN()) {
1848 __ j(kUnordered, true_label);
1849 } else if (cond->IsFPConditionFalseIfNaN()) {
1850 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001851 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001852 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001853}
1854
Mark Mendell152408f2015-12-31 12:28:50 -05001855template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001856void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001857 LabelType* true_label,
1858 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001859 LocationSummary* locations = cond->GetLocations();
1860 Location left = locations->InAt(0);
1861 Location right = locations->InAt(1);
1862 IfCondition if_cond = cond->GetCondition();
1863
Mark Mendellc4701932015-04-10 13:18:51 -04001864 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001865 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001866 IfCondition true_high_cond = if_cond;
1867 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001868 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001869
1870 // Set the conditions for the test, remembering that == needs to be
1871 // decided using the low words.
1872 switch (if_cond) {
1873 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001874 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001875 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001876 break;
1877 case kCondLT:
1878 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001879 break;
1880 case kCondLE:
1881 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001882 break;
1883 case kCondGT:
1884 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001885 break;
1886 case kCondGE:
1887 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001888 break;
Aart Bike9f37602015-10-09 11:15:55 -07001889 case kCondB:
1890 false_high_cond = kCondA;
1891 break;
1892 case kCondBE:
1893 true_high_cond = kCondB;
1894 break;
1895 case kCondA:
1896 false_high_cond = kCondB;
1897 break;
1898 case kCondAE:
1899 true_high_cond = kCondA;
1900 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001901 }
1902
1903 if (right.IsConstant()) {
1904 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001905 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001906 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001907
Aart Bika19616e2016-02-01 18:57:58 -08001908 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001909 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.
Aart Bika19616e2016-02-01 18:57:58 -08001918 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001919 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001920 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001921 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001922
1923 __ cmpl(left_high, right_high);
1924 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001925 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001926 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001927 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001928 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001929 __ j(X86Condition(true_high_cond), true_label);
1930 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001931 }
1932 // Must be equal high, so compare the lows.
1933 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001934 } else {
1935 DCHECK(right.IsDoubleStackSlot());
1936 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1937 if (if_cond == kCondNE) {
1938 __ j(X86Condition(true_high_cond), true_label);
1939 } else if (if_cond == kCondEQ) {
1940 __ j(X86Condition(false_high_cond), false_label);
1941 } else {
1942 __ j(X86Condition(true_high_cond), true_label);
1943 __ j(X86Condition(false_high_cond), false_label);
1944 }
1945 // Must be equal high, so compare the lows.
1946 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001947 }
1948 // The last comparison might be unsigned.
1949 __ j(final_condition, true_label);
1950}
1951
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001952void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1953 Location rhs,
1954 HInstruction* insn,
1955 bool is_double) {
1956 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1957 if (is_double) {
1958 if (rhs.IsFpuRegister()) {
1959 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1960 } else if (const_area != nullptr) {
1961 DCHECK(const_area->IsEmittedAtUseSite());
1962 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1963 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001964 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1965 const_area->GetBaseMethodAddress(),
1966 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001967 } else {
1968 DCHECK(rhs.IsDoubleStackSlot());
1969 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1970 }
1971 } else {
1972 if (rhs.IsFpuRegister()) {
1973 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1974 } else if (const_area != nullptr) {
1975 DCHECK(const_area->IsEmittedAtUseSite());
1976 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1977 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001978 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1979 const_area->GetBaseMethodAddress(),
1980 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001981 } else {
1982 DCHECK(rhs.IsStackSlot());
1983 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1984 }
1985 }
1986}
1987
Mark Mendell152408f2015-12-31 12:28:50 -05001988template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001989void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001990 LabelType* true_target_in,
1991 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001992 // Generated branching requires both targets to be explicit. If either of the
1993 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001994 LabelType fallthrough_target;
1995 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1996 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001997
Mark Mendellc4701932015-04-10 13:18:51 -04001998 LocationSummary* locations = condition->GetLocations();
1999 Location left = locations->InAt(0);
2000 Location right = locations->InAt(1);
2001
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002002 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04002003 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002004 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04002005 GenerateLongComparesAndJumps(condition, true_target, false_target);
2006 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002007 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002008 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04002009 GenerateFPJumps(condition, true_target, false_target);
2010 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002011 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002012 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04002013 GenerateFPJumps(condition, true_target, false_target);
2014 break;
2015 default:
2016 LOG(FATAL) << "Unexpected compare type " << type;
2017 }
2018
David Brazdil0debae72015-11-12 18:37:00 +00002019 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04002020 __ jmp(false_target);
2021 }
David Brazdil0debae72015-11-12 18:37:00 +00002022
2023 if (fallthrough_target.IsLinked()) {
2024 __ Bind(&fallthrough_target);
2025 }
Mark Mendellc4701932015-04-10 13:18:51 -04002026}
2027
David Brazdil0debae72015-11-12 18:37:00 +00002028static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
2029 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
2030 // are set only strictly before `branch`. We can't use the eflags on long/FP
2031 // conditions if they are materialized due to the complex branching.
2032 return cond->IsCondition() &&
2033 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002034 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
2035 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00002036}
2037
Mark Mendell152408f2015-12-31 12:28:50 -05002038template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002039void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002040 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05002041 LabelType* true_target,
2042 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00002043 HInstruction* cond = instruction->InputAt(condition_input_index);
2044
2045 if (true_target == nullptr && false_target == nullptr) {
2046 // Nothing to do. The code always falls through.
2047 return;
2048 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002049 // Constant condition, statically compared against "true" (integer value 1).
2050 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002051 if (true_target != nullptr) {
2052 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01002053 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002054 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002055 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002056 if (false_target != nullptr) {
2057 __ jmp(false_target);
2058 }
2059 }
2060 return;
2061 }
2062
2063 // The following code generates these patterns:
2064 // (1) true_target == nullptr && false_target != nullptr
2065 // - opposite condition true => branch to false_target
2066 // (2) true_target != nullptr && false_target == nullptr
2067 // - condition true => branch to true_target
2068 // (3) true_target != nullptr && false_target != nullptr
2069 // - condition true => branch to true_target
2070 // - branch to false_target
2071 if (IsBooleanValueOrMaterializedCondition(cond)) {
2072 if (AreEflagsSetFrom(cond, instruction)) {
2073 if (true_target == nullptr) {
2074 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
2075 } else {
2076 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
2077 }
2078 } else {
2079 // Materialized condition, compare against 0.
2080 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
2081 if (lhs.IsRegister()) {
2082 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
2083 } else {
2084 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
2085 }
2086 if (true_target == nullptr) {
2087 __ j(kEqual, false_target);
2088 } else {
2089 __ j(kNotEqual, true_target);
2090 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002091 }
2092 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002093 // Condition has not been materialized, use its inputs as the comparison and
2094 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04002095 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00002096
2097 // If this is a long or FP comparison that has been folded into
2098 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002099 DataType::Type type = condition->InputAt(0)->GetType();
2100 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00002101 GenerateCompareTestAndBranch(condition, true_target, false_target);
2102 return;
2103 }
2104
2105 Location lhs = condition->GetLocations()->InAt(0);
2106 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002107 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01002108 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00002109 if (true_target == nullptr) {
2110 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
2111 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04002112 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07002113 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002114 }
David Brazdil0debae72015-11-12 18:37:00 +00002115
2116 // If neither branch falls through (case 3), the conditional branch to `true_target`
2117 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2118 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002119 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002120 }
2121}
2122
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002123void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002124 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00002125 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002126 locations->SetInAt(0, Location::Any());
2127 }
2128}
2129
2130void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002131 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2132 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2133 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2134 nullptr : codegen_->GetLabelOf(true_successor);
2135 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2136 nullptr : codegen_->GetLabelOf(false_successor);
Andreas Gampe3db70682018-12-26 15:12:03 -08002137 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002138}
2139
2140void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002141 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002142 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01002143 InvokeRuntimeCallingConvention calling_convention;
2144 RegisterSet caller_saves = RegisterSet::Empty();
2145 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2146 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00002147 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002148 locations->SetInAt(0, Location::Any());
2149 }
2150}
2151
2152void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002153 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00002154 GenerateTestAndBranch<Label>(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08002155 /* condition_input_index= */ 0,
David Brazdil74eb1b22015-12-14 11:44:01 +00002156 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08002157 /* false_target= */ nullptr);
David Brazdil74eb1b22015-12-14 11:44:01 +00002158}
2159
Mingyao Yang063fc772016-08-02 11:02:54 -07002160void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002161 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07002162 LocationSummary(flag, LocationSummary::kNoCall);
2163 locations->SetOut(Location::RequiresRegister());
2164}
2165
2166void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2167 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
2168 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
2169}
2170
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002171static bool SelectCanUseCMOV(HSelect* select) {
2172 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002173 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002174 return false;
2175 }
2176
2177 // A FP condition doesn't generate the single CC that we need.
2178 // In 32 bit mode, a long condition doesn't generate a single CC either.
2179 HInstruction* condition = select->GetCondition();
2180 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002181 DataType::Type compare_type = condition->InputAt(0)->GetType();
2182 if (compare_type == DataType::Type::kInt64 ||
2183 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002184 return false;
2185 }
2186 }
2187
2188 // We can generate a CMOV for this Select.
2189 return true;
2190}
2191
David Brazdil74eb1b22015-12-14 11:44:01 +00002192void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002193 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002194 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00002195 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002196 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00002197 } else {
2198 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002199 if (SelectCanUseCMOV(select)) {
2200 if (select->InputAt(1)->IsConstant()) {
2201 // Cmov can't handle a constant value.
2202 locations->SetInAt(1, Location::RequiresRegister());
2203 } else {
2204 locations->SetInAt(1, Location::Any());
2205 }
2206 } else {
2207 locations->SetInAt(1, Location::Any());
2208 }
David Brazdil74eb1b22015-12-14 11:44:01 +00002209 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002210 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2211 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00002212 }
2213 locations->SetOut(Location::SameAsFirstInput());
2214}
2215
2216void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
2217 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002218 DCHECK(locations->InAt(0).Equals(locations->Out()));
2219 if (SelectCanUseCMOV(select)) {
2220 // If both the condition and the source types are integer, we can generate
2221 // a CMOV to implement Select.
2222
2223 HInstruction* select_condition = select->GetCondition();
2224 Condition cond = kNotEqual;
2225
2226 // Figure out how to test the 'condition'.
2227 if (select_condition->IsCondition()) {
2228 HCondition* condition = select_condition->AsCondition();
2229 if (!condition->IsEmittedAtUseSite()) {
2230 // This was a previously materialized condition.
2231 // Can we use the existing condition code?
2232 if (AreEflagsSetFrom(condition, select)) {
2233 // Materialization was the previous instruction. Condition codes are right.
2234 cond = X86Condition(condition->GetCondition());
2235 } else {
2236 // No, we have to recreate the condition code.
2237 Register cond_reg = locations->InAt(2).AsRegister<Register>();
2238 __ testl(cond_reg, cond_reg);
2239 }
2240 } else {
2241 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002242 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
2243 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002244 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01002245 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002246 cond = X86Condition(condition->GetCondition());
2247 }
2248 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002249 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002250 Register cond_reg = locations->InAt(2).AsRegister<Register>();
2251 __ testl(cond_reg, cond_reg);
2252 }
2253
2254 // If the condition is true, overwrite the output, which already contains false.
2255 Location false_loc = locations->InAt(0);
2256 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002257 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002258 // 64 bit conditional move.
2259 Register false_high = false_loc.AsRegisterPairHigh<Register>();
2260 Register false_low = false_loc.AsRegisterPairLow<Register>();
2261 if (true_loc.IsRegisterPair()) {
2262 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
2263 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
2264 } else {
2265 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
2266 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
2267 }
2268 } else {
2269 // 32 bit conditional move.
2270 Register false_reg = false_loc.AsRegister<Register>();
2271 if (true_loc.IsRegister()) {
2272 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
2273 } else {
2274 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
2275 }
2276 }
2277 } else {
2278 NearLabel false_target;
2279 GenerateTestAndBranch<NearLabel>(
Andreas Gampe3db70682018-12-26 15:12:03 -08002280 select, /* condition_input_index= */ 2, /* true_target= */ nullptr, &false_target);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002281 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2282 __ Bind(&false_target);
2283 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002284}
2285
Santiago Aboy Solanescaf9b652022-06-24 10:03:30 +01002286void LocationsBuilderX86::VisitNop(HNop* nop) {
2287 new (GetGraph()->GetAllocator()) LocationSummary(nop);
David Srbecky0cf44932015-12-09 14:09:59 +00002288}
2289
Santiago Aboy Solanescaf9b652022-06-24 10:03:30 +01002290void InstructionCodeGeneratorX86::VisitNop(HNop*) {
2291 // The environment recording already happened in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002292}
2293
Vladimir Markodec78172020-06-19 15:31:23 +01002294void CodeGeneratorX86::IncreaseFrame(size_t adjustment) {
2295 __ subl(ESP, Immediate(adjustment));
2296 __ cfi().AdjustCFAOffset(adjustment);
2297}
2298
2299void CodeGeneratorX86::DecreaseFrame(size_t adjustment) {
2300 __ addl(ESP, Immediate(adjustment));
2301 __ cfi().AdjustCFAOffset(-adjustment);
2302}
2303
David Srbeckyc7098ff2016-02-09 14:30:11 +00002304void CodeGeneratorX86::GenerateNop() {
2305 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002306}
2307
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002308void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002309 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002310 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04002311 // Handle the long/FP comparisons made in instruction simplification.
2312 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002313 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04002314 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05002315 locations->SetInAt(1, Location::Any());
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 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002321 case DataType::Type::kFloat32:
2322 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04002323 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002324 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
2325 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
2326 } else if (cond->InputAt(1)->IsConstant()) {
2327 locations->SetInAt(1, Location::RequiresFpuRegister());
2328 } else {
2329 locations->SetInAt(1, Location::Any());
2330 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002331 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002332 locations->SetOut(Location::RequiresRegister());
2333 }
2334 break;
2335 }
2336 default:
2337 locations->SetInAt(0, Location::RequiresRegister());
2338 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00002339 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002340 // We need a byte register.
2341 locations->SetOut(Location::RegisterLocation(ECX));
2342 }
2343 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002344 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002345}
2346
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002347void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002348 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002349 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002350 }
Mark Mendellc4701932015-04-10 13:18:51 -04002351
2352 LocationSummary* locations = cond->GetLocations();
2353 Location lhs = locations->InAt(0);
2354 Location rhs = locations->InAt(1);
2355 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05002356 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002357
2358 switch (cond->InputAt(0)->GetType()) {
2359 default: {
2360 // Integer case.
2361
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002362 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04002363 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01002364 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07002365 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04002366 return;
2367 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002368 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04002369 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2370 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002371 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002372 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04002373 GenerateFPJumps(cond, &true_label, &false_label);
2374 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002375 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002376 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04002377 GenerateFPJumps(cond, &true_label, &false_label);
2378 break;
2379 }
2380
2381 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002382 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002383
Roland Levillain4fa13f62015-07-06 18:11:54 +01002384 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04002385 __ Bind(&false_label);
2386 __ xorl(reg, reg);
2387 __ jmp(&done_label);
2388
Roland Levillain4fa13f62015-07-06 18:11:54 +01002389 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04002390 __ Bind(&true_label);
2391 __ movl(reg, Immediate(1));
2392 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002393}
2394
2395void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002396 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002397}
2398
2399void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002400 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002401}
2402
2403void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002404 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002405}
2406
2407void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002408 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002409}
2410
2411void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002412 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002413}
2414
2415void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002416 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002417}
2418
2419void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002420 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002421}
2422
2423void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002424 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002425}
2426
2427void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002428 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002429}
2430
2431void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002432 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002433}
2434
2435void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002436 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002437}
2438
2439void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002440 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002441}
2442
Aart Bike9f37602015-10-09 11:15:55 -07002443void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002444 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002445}
2446
2447void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002448 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002449}
2450
2451void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002452 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002453}
2454
2455void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002456 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002457}
2458
2459void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002460 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002461}
2462
2463void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002464 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002465}
2466
2467void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002468 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002469}
2470
2471void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002472 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002473}
2474
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002475void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002476 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002477 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002478 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002479}
2480
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002481void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002482 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002483}
2484
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002485void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2486 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002487 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002488 locations->SetOut(Location::ConstantLocation(constant));
2489}
2490
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002491void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002492 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002493}
2494
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002495void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002496 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002497 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002498 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002499}
2500
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002501void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002502 // Will be generated at use site.
2503}
2504
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002505void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2506 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002507 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002508 locations->SetOut(Location::ConstantLocation(constant));
2509}
2510
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002511void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002512 // Will be generated at use site.
2513}
2514
2515void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2516 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002517 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002518 locations->SetOut(Location::ConstantLocation(constant));
2519}
2520
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002521void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002522 // Will be generated at use site.
2523}
2524
Igor Murashkind01745e2017-04-05 16:40:31 -07002525void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2526 constructor_fence->SetLocations(nullptr);
2527}
2528
2529void InstructionCodeGeneratorX86::VisitConstructorFence(
2530 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2531 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2532}
2533
Calin Juravle27df7582015-04-17 19:12:31 +01002534void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2535 memory_barrier->SetLocations(nullptr);
2536}
2537
2538void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002539 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002540}
2541
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002542void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002543 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002544}
2545
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002546void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002547 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002548}
2549
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002550void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002551 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002552 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Mythri Alle5097f832021-11-02 14:52:30 +00002553 SetInForReturnValue(ret, locations);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002554}
2555
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002556void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002557 switch (ret->InputAt(0)->GetType()) {
2558 case DataType::Type::kReference:
2559 case DataType::Type::kBool:
2560 case DataType::Type::kUint8:
2561 case DataType::Type::kInt8:
2562 case DataType::Type::kUint16:
2563 case DataType::Type::kInt16:
2564 case DataType::Type::kInt32:
2565 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
2566 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002567
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002568 case DataType::Type::kInt64:
2569 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2570 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
2571 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002572
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002573 case DataType::Type::kFloat32:
2574 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2575 if (GetGraph()->IsCompilingOsr()) {
2576 // To simplify callers of an OSR method, we put the return value in both
2577 // floating point and core registers.
2578 __ movd(EAX, XMM0);
2579 }
2580 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002581
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002582 case DataType::Type::kFloat64:
2583 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2584 if (GetGraph()->IsCompilingOsr()) {
2585 // To simplify callers of an OSR method, we put the return value in both
2586 // floating point and core registers.
2587 __ movd(EAX, XMM0);
2588 // Use XMM1 as temporary register to not clobber XMM0.
2589 __ movaps(XMM1, XMM0);
2590 __ psrlq(XMM1, Immediate(32));
2591 __ movd(EDX, XMM1);
2592 }
2593 break;
2594
2595 default:
2596 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002597 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002598 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002599}
2600
Calin Juravle175dc732015-08-25 15:42:32 +01002601void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2602 // The trampoline uses the same calling convention as dex calling conventions,
2603 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2604 // the method_idx.
2605 HandleInvoke(invoke);
2606}
2607
2608void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2609 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2610}
2611
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002612void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002613 // Explicit clinit checks triggered by static invokes must have been pruned by
2614 // art::PrepareForRegisterAllocation.
2615 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002616
Mark Mendellfb8d2792015-03-31 22:16:59 -04002617 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002618 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002619 if (invoke->GetLocations()->CanCall() &&
2620 invoke->HasPcRelativeMethodLoadKind() &&
2621 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).IsInvalid()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002622 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002623 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002624 return;
2625 }
2626
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01002627 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
Vladimir Marko86c87522020-05-11 16:55:55 +01002628 CriticalNativeCallingConventionVisitorX86 calling_convention_visitor(
2629 /*for_register_allocation=*/ true);
2630 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2631 } else {
2632 HandleInvoke(invoke);
2633 }
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002634
Vladimir Marko86c87522020-05-11 16:55:55 +01002635 // For PC-relative load kinds the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002636 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002637 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002638 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002639}
2640
Mark Mendell09ed1a32015-03-25 08:30:06 -04002641static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2642 if (invoke->GetLocations()->Intrinsified()) {
2643 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2644 intrinsic.Dispatch(invoke);
2645 return true;
2646 }
2647 return false;
2648}
2649
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002650void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002651 // Explicit clinit checks triggered by static invokes must have been pruned by
2652 // art::PrepareForRegisterAllocation.
2653 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002654
Mark Mendell09ed1a32015-03-25 08:30:06 -04002655 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2656 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002657 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002658
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002659 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002660 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002661 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002662}
2663
2664void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002665 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2666 if (intrinsic.TryDispatch(invoke)) {
2667 return;
2668 }
2669
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002670 HandleInvoke(invoke);
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002671
2672 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002673 // Add one temporary for inline cache update.
2674 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2675 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002676}
2677
2678void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002679 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002680 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002681}
2682
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002683void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002684 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2685 return;
2686 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002687
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002688 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002689 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002690}
2691
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002692void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002693 // This call to HandleInvoke allocates a temporary (core) register
2694 // which is also used to transfer the hidden argument from FP to
2695 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002696 HandleInvoke(invoke);
2697 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002698 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002699
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002700 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002701 // Add one temporary for inline cache update.
2702 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2703 }
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01002704
2705 // For PC-relative load kinds the invoke has an extra input, the PC-relative address base.
2706 if (IsPcRelativeMethodLoadKind(invoke->GetHiddenArgumentLoadKind())) {
2707 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2708 }
2709
2710 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
2711 invoke->GetLocations()->SetInAt(invoke->GetNumberOfArguments() - 1,
2712 Location::RequiresRegister());
2713 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002714}
2715
2716void CodeGeneratorX86::MaybeGenerateInlineCacheCheck(HInstruction* instruction, Register klass) {
2717 DCHECK_EQ(EAX, klass);
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002718 // We know the destination of an intrinsic, so no need to record inline
2719 // caches (also the intrinsic location builder doesn't request an additional
2720 // temporary).
2721 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002722 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002723 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002724 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
Nicolas Geoffray9e598902021-11-19 14:53:07 +00002725 ProfilingInfo* info = GetGraph()->GetProfilingInfo();
2726 DCHECK(info != nullptr);
2727 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
2728 uint32_t address = reinterpret_cast32<uint32_t>(cache);
2729 if (kIsDebugBuild) {
2730 uint32_t temp_index = instruction->GetLocations()->GetTempCount() - 1u;
2731 CHECK_EQ(EBP, instruction->GetLocations()->GetTemp(temp_index).AsRegister<Register>());
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002732 }
Nicolas Geoffray9e598902021-11-19 14:53:07 +00002733 Register temp = EBP;
2734 NearLabel done;
2735 __ movl(temp, Immediate(address));
2736 // Fast path for a monomorphic cache.
2737 __ cmpl(klass, Address(temp, InlineCache::ClassesOffset().Int32Value()));
2738 __ j(kEqual, &done);
2739 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(kQuickUpdateInlineCache).Int32Value());
2740 __ Bind(&done);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002741 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002742}
2743
2744void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2745 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002746 LocationSummary* locations = invoke->GetLocations();
2747 Register temp = locations->GetTemp(0).AsRegister<Register>();
2748 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002749 Location receiver = locations->InAt(0);
2750 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2751
Roland Levillain0d5a2812015-11-13 10:07:31 +00002752 // Set the hidden argument. This is safe to do this here, as XMM7
2753 // won't be modified thereafter, before the `call` instruction.
2754 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01002755 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
2756 __ movd(hidden_reg, locations->InAt(invoke->GetNumberOfArguments() - 1).AsRegister<Register>());
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00002757 } else if (invoke->GetHiddenArgumentLoadKind() != MethodLoadKind::kRuntimeCall) {
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01002758 codegen_->LoadMethod(invoke->GetHiddenArgumentLoadKind(), locations->GetTemp(0), invoke);
2759 __ movd(hidden_reg, temp);
2760 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002761
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002762 if (receiver.IsStackSlot()) {
2763 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002764 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002765 __ movl(temp, Address(temp, class_offset));
2766 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002767 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002768 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002769 }
Roland Levillain4d027112015-07-01 15:41:14 +01002770 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002771 // Instead of simply (possibly) unpoisoning `temp` here, we should
2772 // emit a read barrier for the previous class reference load.
2773 // However this is not required in practice, as this is an
2774 // intermediate/temporary reference and because the current
2775 // concurrent copying collector keeps the from-space memory
2776 // intact/accessible until the end of the marking phase (the
2777 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002778 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002779
2780 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
2781
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002782 // temp = temp->GetAddressOfIMT()
2783 __ movl(temp,
2784 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002785 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002786 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002787 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002788 __ movl(temp, Address(temp, method_offset));
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00002789 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRuntimeCall) {
2790 // We pass the method from the IMT in case of a conflict. This will ensure
2791 // we go into the runtime to resolve the actual method.
2792 __ movd(hidden_reg, temp);
2793 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002794 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002795 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002796 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002797
2798 DCHECK(!codegen_->IsLeafMethod());
2799 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2800}
2801
Orion Hodsonac141392017-01-13 11:53:47 +00002802void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00002803 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2804 if (intrinsic.TryDispatch(invoke)) {
2805 return;
2806 }
Orion Hodsonac141392017-01-13 11:53:47 +00002807 HandleInvoke(invoke);
2808}
2809
2810void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00002811 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2812 return;
2813 }
Orion Hodsonac141392017-01-13 11:53:47 +00002814 codegen_->GenerateInvokePolymorphicCall(invoke);
2815}
2816
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002817void LocationsBuilderX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2818 HandleInvoke(invoke);
2819}
2820
2821void InstructionCodeGeneratorX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2822 codegen_->GenerateInvokeCustomCall(invoke);
2823}
2824
Roland Levillain88cb1752014-10-20 16:36:47 +01002825void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2826 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002827 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002828 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002829 case DataType::Type::kInt32:
2830 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002831 locations->SetInAt(0, Location::RequiresRegister());
2832 locations->SetOut(Location::SameAsFirstInput());
2833 break;
2834
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002835 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002836 locations->SetInAt(0, Location::RequiresFpuRegister());
2837 locations->SetOut(Location::SameAsFirstInput());
2838 locations->AddTemp(Location::RequiresRegister());
2839 locations->AddTemp(Location::RequiresFpuRegister());
2840 break;
2841
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002842 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002843 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002844 locations->SetOut(Location::SameAsFirstInput());
2845 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002846 break;
2847
2848 default:
2849 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2850 }
2851}
2852
2853void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2854 LocationSummary* locations = neg->GetLocations();
2855 Location out = locations->Out();
2856 Location in = locations->InAt(0);
2857 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002858 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002859 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002860 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002861 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002862 break;
2863
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002864 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002865 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002866 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002867 __ negl(out.AsRegisterPairLow<Register>());
2868 // Negation is similar to subtraction from zero. The least
2869 // significant byte triggers a borrow when it is different from
2870 // zero; to take it into account, add 1 to the most significant
2871 // byte if the carry flag (CF) is set to 1 after the first NEGL
2872 // operation.
2873 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2874 __ negl(out.AsRegisterPairHigh<Register>());
2875 break;
2876
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002877 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002878 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002879 Register constant = locations->GetTemp(0).AsRegister<Register>();
2880 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002881 // Implement float negation with an exclusive or with value
2882 // 0x80000000 (mask for bit 31, representing the sign of a
2883 // single-precision floating-point number).
2884 __ movl(constant, Immediate(INT32_C(0x80000000)));
2885 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002886 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002887 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002888 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002889
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002890 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002891 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002892 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002893 // Implement double negation with an exclusive or with value
2894 // 0x8000000000000000 (mask for bit 63, representing the sign of
2895 // a double-precision floating-point number).
2896 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002897 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002898 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002899 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002900
2901 default:
2902 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2903 }
2904}
2905
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002906void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2907 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002908 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002909 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002910 locations->SetInAt(0, Location::RequiresFpuRegister());
2911 locations->SetInAt(1, Location::RequiresRegister());
2912 locations->SetOut(Location::SameAsFirstInput());
2913 locations->AddTemp(Location::RequiresFpuRegister());
2914}
2915
2916void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2917 LocationSummary* locations = neg->GetLocations();
2918 Location out = locations->Out();
2919 DCHECK(locations->InAt(0).Equals(out));
2920
2921 Register constant_area = locations->InAt(1).AsRegister<Register>();
2922 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002923 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002924 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2925 neg->GetBaseMethodAddress(),
2926 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002927 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2928 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002929 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2930 neg->GetBaseMethodAddress(),
2931 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002932 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2933 }
2934}
2935
Roland Levillaindff1f282014-11-05 14:15:05 +00002936void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002937 DataType::Type result_type = conversion->GetResultType();
2938 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002939 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2940 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002941
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002942 // The float-to-long and double-to-long type conversions rely on a
2943 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002944 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002945 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2946 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002947 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002948 : LocationSummary::kNoCall;
2949 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002950 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002951
Roland Levillaindff1f282014-11-05 14:15:05 +00002952 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002953 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002954 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002955 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002956 case DataType::Type::kUint8:
2957 case DataType::Type::kInt8:
2958 case DataType::Type::kUint16:
2959 case DataType::Type::kInt16:
2960 case DataType::Type::kInt32:
2961 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2962 // Make the output overlap to please the register allocator. This greatly simplifies
2963 // the validation of the linear scan implementation
2964 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2965 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002966 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002967 HInstruction* input = conversion->InputAt(0);
2968 Location input_location = input->IsConstant()
2969 ? Location::ConstantLocation(input->AsConstant())
2970 : Location::RegisterPairLocation(EAX, EDX);
2971 locations->SetInAt(0, input_location);
2972 // Make the output overlap to please the register allocator. This greatly simplifies
2973 // the validation of the linear scan implementation
2974 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2975 break;
2976 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002977
2978 default:
2979 LOG(FATAL) << "Unexpected type conversion from " << input_type
2980 << " to " << result_type;
2981 }
2982 break;
2983
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002984 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002985 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002986 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2987 locations->SetInAt(0, Location::Any());
2988 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002989 break;
2990
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002991 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002992 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002993 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002994 locations->SetInAt(0, Location::Any());
2995 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2996 break;
2997
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002998 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002999 locations->SetInAt(0, Location::RequiresFpuRegister());
3000 locations->SetOut(Location::RequiresRegister());
3001 locations->AddTemp(Location::RequiresFpuRegister());
3002 break;
3003
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003004 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003005 locations->SetInAt(0, Location::RequiresFpuRegister());
3006 locations->SetOut(Location::RequiresRegister());
3007 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00003008 break;
3009
3010 default:
3011 LOG(FATAL) << "Unexpected type conversion from " << input_type
3012 << " to " << result_type;
3013 }
3014 break;
3015
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003016 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00003017 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003018 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003019 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003020 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003021 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003022 case DataType::Type::kInt16:
3023 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00003024 locations->SetInAt(0, Location::RegisterLocation(EAX));
3025 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3026 break;
3027
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003028 case DataType::Type::kFloat32:
3029 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00003030 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00003031 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
3032 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
3033
Vladimir Marko949c91f2015-01-27 10:48:44 +00003034 // The runtime helper puts the result in EAX, EDX.
3035 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00003036 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00003037 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00003038
3039 default:
3040 LOG(FATAL) << "Unexpected type conversion from " << input_type
3041 << " to " << result_type;
3042 }
3043 break;
3044
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003045 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00003046 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003047 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003048 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003049 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003050 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003051 case DataType::Type::kInt16:
3052 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00003053 locations->SetInAt(0, Location::RequiresRegister());
3054 locations->SetOut(Location::RequiresFpuRegister());
3055 break;
3056
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003057 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01003058 locations->SetInAt(0, Location::Any());
3059 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00003060 break;
3061
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003062 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003063 locations->SetInAt(0, Location::RequiresFpuRegister());
3064 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00003065 break;
3066
3067 default:
3068 LOG(FATAL) << "Unexpected type conversion from " << input_type
3069 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003070 }
Roland Levillaincff13742014-11-17 14:32:17 +00003071 break;
3072
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003073 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00003074 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003075 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003076 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003077 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003078 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003079 case DataType::Type::kInt16:
3080 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00003081 locations->SetInAt(0, Location::RequiresRegister());
3082 locations->SetOut(Location::RequiresFpuRegister());
3083 break;
3084
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003085 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01003086 locations->SetInAt(0, Location::Any());
3087 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00003088 break;
3089
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003090 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003091 locations->SetInAt(0, Location::RequiresFpuRegister());
3092 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00003093 break;
3094
3095 default:
3096 LOG(FATAL) << "Unexpected type conversion from " << input_type
3097 << " to " << result_type;
3098 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003099 break;
3100
3101 default:
3102 LOG(FATAL) << "Unexpected type conversion from " << input_type
3103 << " to " << result_type;
3104 }
3105}
3106
3107void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
3108 LocationSummary* locations = conversion->GetLocations();
3109 Location out = locations->Out();
3110 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003111 DataType::Type result_type = conversion->GetResultType();
3112 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003113 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
3114 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00003115 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003116 case DataType::Type::kUint8:
3117 switch (input_type) {
3118 case DataType::Type::kInt8:
3119 case DataType::Type::kUint16:
3120 case DataType::Type::kInt16:
3121 case DataType::Type::kInt32:
3122 if (in.IsRegister()) {
3123 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
3124 } else {
3125 DCHECK(in.GetConstant()->IsIntConstant());
3126 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
3127 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
3128 }
3129 break;
3130 case DataType::Type::kInt64:
3131 if (in.IsRegisterPair()) {
3132 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
3133 } else {
3134 DCHECK(in.GetConstant()->IsLongConstant());
3135 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3136 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
3137 }
3138 break;
3139
3140 default:
3141 LOG(FATAL) << "Unexpected type conversion from " << input_type
3142 << " to " << result_type;
3143 }
3144 break;
3145
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003146 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00003147 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003148 case DataType::Type::kUint8:
3149 case DataType::Type::kUint16:
3150 case DataType::Type::kInt16:
3151 case DataType::Type::kInt32:
3152 if (in.IsRegister()) {
3153 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
3154 } else {
3155 DCHECK(in.GetConstant()->IsIntConstant());
3156 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
3157 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
3158 }
3159 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003160 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003161 if (in.IsRegisterPair()) {
3162 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
3163 } else {
3164 DCHECK(in.GetConstant()->IsLongConstant());
3165 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3166 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
3167 }
3168 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003169
3170 default:
3171 LOG(FATAL) << "Unexpected type conversion from " << input_type
3172 << " to " << result_type;
3173 }
3174 break;
3175
3176 case DataType::Type::kUint16:
3177 switch (input_type) {
3178 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003179 case DataType::Type::kInt16:
3180 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003181 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003182 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
3183 } else if (in.IsStackSlot()) {
3184 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003185 } else {
3186 DCHECK(in.GetConstant()->IsIntConstant());
3187 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003188 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
3189 }
3190 break;
3191 case DataType::Type::kInt64:
3192 if (in.IsRegisterPair()) {
3193 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
3194 } else if (in.IsDoubleStackSlot()) {
3195 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
3196 } else {
3197 DCHECK(in.GetConstant()->IsLongConstant());
3198 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3199 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003200 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00003201 break;
3202
3203 default:
3204 LOG(FATAL) << "Unexpected type conversion from " << input_type
3205 << " to " << result_type;
3206 }
3207 break;
3208
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003209 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00003210 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003211 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003212 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00003213 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003214 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00003215 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003216 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00003217 } else {
3218 DCHECK(in.GetConstant()->IsIntConstant());
3219 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003220 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00003221 }
3222 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003223 case DataType::Type::kInt64:
3224 if (in.IsRegisterPair()) {
3225 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
3226 } else if (in.IsDoubleStackSlot()) {
3227 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
3228 } else {
3229 DCHECK(in.GetConstant()->IsLongConstant());
3230 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3231 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
3232 }
3233 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00003234
3235 default:
3236 LOG(FATAL) << "Unexpected type conversion from " << input_type
3237 << " to " << result_type;
3238 }
3239 break;
3240
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003241 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00003242 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003243 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00003244 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003245 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00003246 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003247 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00003248 } else {
3249 DCHECK(in.IsConstant());
3250 DCHECK(in.GetConstant()->IsLongConstant());
3251 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003252 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00003253 }
3254 break;
3255
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003256 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00003257 XmmRegister input = in.AsFpuRegister<XmmRegister>();
3258 Register output = out.AsRegister<Register>();
3259 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04003260 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00003261
3262 __ movl(output, Immediate(kPrimIntMax));
3263 // temp = int-to-float(output)
3264 __ cvtsi2ss(temp, output);
3265 // if input >= temp goto done
3266 __ comiss(input, temp);
3267 __ j(kAboveEqual, &done);
3268 // if input == NaN goto nan
3269 __ j(kUnordered, &nan);
3270 // output = float-to-int-truncate(input)
3271 __ cvttss2si(output, input);
3272 __ jmp(&done);
3273 __ Bind(&nan);
3274 // output = 0
3275 __ xorl(output, output);
3276 __ Bind(&done);
3277 break;
3278 }
3279
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003280 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003281 XmmRegister input = in.AsFpuRegister<XmmRegister>();
3282 Register output = out.AsRegister<Register>();
3283 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04003284 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003285
3286 __ movl(output, Immediate(kPrimIntMax));
3287 // temp = int-to-double(output)
3288 __ cvtsi2sd(temp, output);
3289 // if input >= temp goto done
3290 __ comisd(input, temp);
3291 __ j(kAboveEqual, &done);
3292 // if input == NaN goto nan
3293 __ j(kUnordered, &nan);
3294 // output = double-to-int-truncate(input)
3295 __ cvttsd2si(output, input);
3296 __ jmp(&done);
3297 __ Bind(&nan);
3298 // output = 0
3299 __ xorl(output, output);
3300 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00003301 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003302 }
Roland Levillain946e1432014-11-11 17:35:19 +00003303
3304 default:
3305 LOG(FATAL) << "Unexpected type conversion from " << input_type
3306 << " to " << result_type;
3307 }
3308 break;
3309
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003310 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00003311 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003312 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003313 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003314 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003315 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003316 case DataType::Type::kInt16:
3317 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00003318 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
3319 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003320 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00003321 __ cdq();
3322 break;
3323
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003324 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01003325 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003326 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00003327 break;
3328
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003329 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01003330 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003331 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00003332 break;
3333
3334 default:
3335 LOG(FATAL) << "Unexpected type conversion from " << input_type
3336 << " to " << result_type;
3337 }
3338 break;
3339
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003340 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00003341 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003342 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003343 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003344 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003345 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003346 case DataType::Type::kInt16:
3347 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003348 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003349 break;
3350
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003351 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003352 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00003353
Roland Levillain232ade02015-04-20 15:14:36 +01003354 // Create stack space for the call to
3355 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
3356 // TODO: enhance register allocator to ask for stack temporaries.
3357 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003358 adjustment = DataType::Size(DataType::Type::kInt64);
Vladimir Markodec78172020-06-19 15:31:23 +01003359 codegen_->IncreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003360 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003361
Roland Levillain232ade02015-04-20 15:14:36 +01003362 // Load the value to the FP stack, using temporaries if needed.
3363 PushOntoFPStack(in, 0, adjustment, false, true);
3364
3365 if (out.IsStackSlot()) {
3366 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
3367 } else {
3368 __ fstps(Address(ESP, 0));
3369 Location stack_temp = Location::StackSlot(0);
3370 codegen_->Move32(out, stack_temp);
3371 }
3372
3373 // Remove the temporary stack space we allocated.
3374 if (adjustment != 0) {
Vladimir Markodec78172020-06-19 15:31:23 +01003375 codegen_->DecreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003376 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003377 break;
3378 }
3379
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003380 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003381 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003382 break;
3383
3384 default:
3385 LOG(FATAL) << "Unexpected type conversion from " << input_type
3386 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003387 }
Roland Levillaincff13742014-11-17 14:32:17 +00003388 break;
3389
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003390 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00003391 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003392 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003393 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003394 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003395 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003396 case DataType::Type::kInt16:
3397 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003398 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003399 break;
3400
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003401 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003402 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00003403
Roland Levillain232ade02015-04-20 15:14:36 +01003404 // Create stack space for the call to
3405 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
3406 // TODO: enhance register allocator to ask for stack temporaries.
3407 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003408 adjustment = DataType::Size(DataType::Type::kInt64);
Vladimir Markodec78172020-06-19 15:31:23 +01003409 codegen_->IncreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003410 }
3411
3412 // Load the value to the FP stack, using temporaries if needed.
3413 PushOntoFPStack(in, 0, adjustment, false, true);
3414
3415 if (out.IsDoubleStackSlot()) {
3416 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
3417 } else {
3418 __ fstpl(Address(ESP, 0));
3419 Location stack_temp = Location::DoubleStackSlot(0);
3420 codegen_->Move64(out, stack_temp);
3421 }
3422
3423 // Remove the temporary stack space we allocated.
3424 if (adjustment != 0) {
Vladimir Markodec78172020-06-19 15:31:23 +01003425 codegen_->DecreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003426 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003427 break;
3428 }
3429
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003430 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003431 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003432 break;
3433
3434 default:
3435 LOG(FATAL) << "Unexpected type conversion from " << input_type
3436 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003437 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003438 break;
3439
3440 default:
3441 LOG(FATAL) << "Unexpected type conversion from " << input_type
3442 << " to " << result_type;
3443 }
3444}
3445
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003446void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003447 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003448 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003449 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003450 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05003451 locations->SetInAt(0, Location::RequiresRegister());
3452 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3453 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3454 break;
3455 }
3456
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003457 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003458 locations->SetInAt(0, Location::RequiresRegister());
3459 locations->SetInAt(1, Location::Any());
3460 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003461 break;
3462 }
3463
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003464 case DataType::Type::kFloat32:
3465 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003466 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003467 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3468 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003469 } else if (add->InputAt(1)->IsConstant()) {
3470 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003471 } else {
3472 locations->SetInAt(1, Location::Any());
3473 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003474 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003475 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003476 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003477
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003478 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003479 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Elliott Hughesc1896c92018-11-29 11:33:18 -08003480 UNREACHABLE();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003481 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003482}
3483
3484void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
3485 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003486 Location first = locations->InAt(0);
3487 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05003488 Location out = locations->Out();
3489
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003490 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003491 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003492 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003493 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3494 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003495 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3496 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05003497 } else {
3498 __ leal(out.AsRegister<Register>(), Address(
3499 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3500 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003501 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003502 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3503 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3504 __ addl(out.AsRegister<Register>(), Immediate(value));
3505 } else {
3506 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3507 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003508 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003509 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003510 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003511 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003512 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003513 }
3514
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003515 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003516 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003517 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3518 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003519 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003520 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3521 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003522 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003523 } else {
3524 DCHECK(second.IsConstant()) << second;
3525 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3526 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3527 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003528 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003529 break;
3530 }
3531
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003532 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003533 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003534 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003535 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3536 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003537 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003538 __ addss(first.AsFpuRegister<XmmRegister>(),
3539 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003540 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3541 const_area->GetBaseMethodAddress(),
3542 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003543 } else {
3544 DCHECK(second.IsStackSlot());
3545 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003546 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003547 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003548 }
3549
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003550 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003551 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003552 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003553 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3554 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003555 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003556 __ addsd(first.AsFpuRegister<XmmRegister>(),
3557 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003558 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3559 const_area->GetBaseMethodAddress(),
3560 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003561 } else {
3562 DCHECK(second.IsDoubleStackSlot());
3563 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003564 }
3565 break;
3566 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003567
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003568 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003569 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003570 }
3571}
3572
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003573void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003574 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003575 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003576 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003577 case DataType::Type::kInt32:
3578 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003579 locations->SetInAt(0, Location::RequiresRegister());
3580 locations->SetInAt(1, Location::Any());
3581 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003582 break;
3583 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003584 case DataType::Type::kFloat32:
3585 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003586 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003587 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3588 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003589 } else if (sub->InputAt(1)->IsConstant()) {
3590 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003591 } else {
3592 locations->SetInAt(1, Location::Any());
3593 }
Calin Juravle11351682014-10-23 15:38:15 +01003594 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003595 break;
Calin Juravle11351682014-10-23 15:38:15 +01003596 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003597
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003598 default:
Calin Juravle11351682014-10-23 15:38:15 +01003599 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003600 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003601}
3602
3603void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3604 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003605 Location first = locations->InAt(0);
3606 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003607 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003608 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003609 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003610 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003611 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003612 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003613 __ subl(first.AsRegister<Register>(),
3614 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003615 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003616 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003617 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003618 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003619 }
3620
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003621 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003622 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003623 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3624 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003625 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003626 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003627 __ sbbl(first.AsRegisterPairHigh<Register>(),
3628 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003629 } else {
3630 DCHECK(second.IsConstant()) << second;
3631 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3632 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3633 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003634 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003635 break;
3636 }
3637
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003638 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003639 if (second.IsFpuRegister()) {
3640 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3641 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3642 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003643 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003644 __ subss(first.AsFpuRegister<XmmRegister>(),
3645 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003646 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3647 const_area->GetBaseMethodAddress(),
3648 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003649 } else {
3650 DCHECK(second.IsStackSlot());
3651 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3652 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003653 break;
Calin Juravle11351682014-10-23 15:38:15 +01003654 }
3655
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003656 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003657 if (second.IsFpuRegister()) {
3658 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3659 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3660 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003661 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003662 __ subsd(first.AsFpuRegister<XmmRegister>(),
3663 codegen_->LiteralDoubleAddress(
3664 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003665 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003666 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3667 } else {
3668 DCHECK(second.IsDoubleStackSlot());
3669 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3670 }
Calin Juravle11351682014-10-23 15:38:15 +01003671 break;
3672 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003673
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003674 default:
Calin Juravle11351682014-10-23 15:38:15 +01003675 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003676 }
3677}
3678
Calin Juravle34bacdf2014-10-07 20:23:36 +01003679void LocationsBuilderX86::VisitMul(HMul* mul) {
3680 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003681 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003682 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003683 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003684 locations->SetInAt(0, Location::RequiresRegister());
3685 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003686 if (mul->InputAt(1)->IsIntConstant()) {
3687 // Can use 3 operand multiply.
3688 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3689 } else {
3690 locations->SetOut(Location::SameAsFirstInput());
3691 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003692 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003693 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003694 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003695 locations->SetInAt(1, Location::Any());
3696 locations->SetOut(Location::SameAsFirstInput());
3697 // Needed for imul on 32bits with 64bits output.
3698 locations->AddTemp(Location::RegisterLocation(EAX));
3699 locations->AddTemp(Location::RegisterLocation(EDX));
3700 break;
3701 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003702 case DataType::Type::kFloat32:
3703 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003704 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003705 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3706 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003707 } else if (mul->InputAt(1)->IsConstant()) {
3708 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003709 } else {
3710 locations->SetInAt(1, Location::Any());
3711 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003712 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003713 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003714 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003715
3716 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003717 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003718 }
3719}
3720
3721void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3722 LocationSummary* locations = mul->GetLocations();
3723 Location first = locations->InAt(0);
3724 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003725 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003726
3727 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003728 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003729 // The constant may have ended up in a register, so test explicitly to avoid
3730 // problems where the output may not be the same as the first operand.
3731 if (mul->InputAt(1)->IsIntConstant()) {
3732 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3733 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3734 } else if (second.IsRegister()) {
3735 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003736 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003737 } else {
3738 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003739 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003740 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003741 }
3742 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003743
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003744 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003745 Register in1_hi = first.AsRegisterPairHigh<Register>();
3746 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003747 Register eax = locations->GetTemp(0).AsRegister<Register>();
3748 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003749
3750 DCHECK_EQ(EAX, eax);
3751 DCHECK_EQ(EDX, edx);
3752
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003753 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003754 // output: in1
3755 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3756 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3757 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003758 if (second.IsConstant()) {
3759 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003760
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003761 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3762 int32_t low_value = Low32Bits(value);
3763 int32_t high_value = High32Bits(value);
3764 Immediate low(low_value);
3765 Immediate high(high_value);
3766
3767 __ movl(eax, high);
3768 // eax <- in1.lo * in2.hi
3769 __ imull(eax, in1_lo);
3770 // in1.hi <- in1.hi * in2.lo
3771 __ imull(in1_hi, low);
3772 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3773 __ addl(in1_hi, eax);
3774 // move in2_lo to eax to prepare for double precision
3775 __ movl(eax, low);
3776 // edx:eax <- in1.lo * in2.lo
3777 __ mull(in1_lo);
3778 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3779 __ addl(in1_hi, edx);
3780 // in1.lo <- (in1.lo * in2.lo)[31:0];
3781 __ movl(in1_lo, eax);
3782 } else if (second.IsRegisterPair()) {
3783 Register in2_hi = second.AsRegisterPairHigh<Register>();
3784 Register in2_lo = second.AsRegisterPairLow<Register>();
3785
3786 __ movl(eax, in2_hi);
3787 // eax <- in1.lo * in2.hi
3788 __ imull(eax, in1_lo);
3789 // in1.hi <- in1.hi * in2.lo
3790 __ imull(in1_hi, in2_lo);
3791 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3792 __ addl(in1_hi, eax);
3793 // move in1_lo to eax to prepare for double precision
3794 __ movl(eax, in1_lo);
3795 // edx:eax <- in1.lo * in2.lo
3796 __ mull(in2_lo);
3797 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3798 __ addl(in1_hi, edx);
3799 // in1.lo <- (in1.lo * in2.lo)[31:0];
3800 __ movl(in1_lo, eax);
3801 } else {
3802 DCHECK(second.IsDoubleStackSlot()) << second;
3803 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3804 Address in2_lo(ESP, second.GetStackIndex());
3805
3806 __ movl(eax, in2_hi);
3807 // eax <- in1.lo * in2.hi
3808 __ imull(eax, in1_lo);
3809 // in1.hi <- in1.hi * in2.lo
3810 __ imull(in1_hi, in2_lo);
3811 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3812 __ addl(in1_hi, eax);
3813 // move in1_lo to eax to prepare for double precision
3814 __ movl(eax, in1_lo);
3815 // edx:eax <- in1.lo * in2.lo
3816 __ mull(in2_lo);
3817 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3818 __ addl(in1_hi, edx);
3819 // in1.lo <- (in1.lo * in2.lo)[31:0];
3820 __ movl(in1_lo, eax);
3821 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003822
3823 break;
3824 }
3825
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003826 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003827 DCHECK(first.Equals(locations->Out()));
3828 if (second.IsFpuRegister()) {
3829 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3830 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3831 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003832 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003833 __ mulss(first.AsFpuRegister<XmmRegister>(),
3834 codegen_->LiteralFloatAddress(
3835 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003836 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003837 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3838 } else {
3839 DCHECK(second.IsStackSlot());
3840 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3841 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003842 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003843 }
3844
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003845 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003846 DCHECK(first.Equals(locations->Out()));
3847 if (second.IsFpuRegister()) {
3848 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3849 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3850 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003851 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003852 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3853 codegen_->LiteralDoubleAddress(
3854 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003855 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003856 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3857 } else {
3858 DCHECK(second.IsDoubleStackSlot());
3859 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3860 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003861 break;
3862 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003863
3864 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003865 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003866 }
3867}
3868
Roland Levillain232ade02015-04-20 15:14:36 +01003869void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3870 uint32_t temp_offset,
3871 uint32_t stack_adjustment,
3872 bool is_fp,
3873 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003874 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003875 DCHECK(!is_wide);
3876 if (is_fp) {
3877 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3878 } else {
3879 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3880 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003881 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003882 DCHECK(is_wide);
3883 if (is_fp) {
3884 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3885 } else {
3886 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3887 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003888 } else {
3889 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003890 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003891 Location stack_temp = Location::StackSlot(temp_offset);
3892 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003893 if (is_fp) {
3894 __ flds(Address(ESP, temp_offset));
3895 } else {
3896 __ filds(Address(ESP, temp_offset));
3897 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003898 } else {
3899 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3900 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003901 if (is_fp) {
3902 __ fldl(Address(ESP, temp_offset));
3903 } else {
3904 __ fildl(Address(ESP, temp_offset));
3905 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003906 }
3907 }
3908}
3909
3910void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003911 DataType::Type type = rem->GetResultType();
3912 bool is_float = type == DataType::Type::kFloat32;
3913 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003914 LocationSummary* locations = rem->GetLocations();
3915 Location first = locations->InAt(0);
3916 Location second = locations->InAt(1);
3917 Location out = locations->Out();
3918
3919 // Create stack space for 2 elements.
3920 // TODO: enhance register allocator to ask for stack temporaries.
Vladimir Markodec78172020-06-19 15:31:23 +01003921 codegen_->IncreaseFrame(2 * elem_size);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003922
3923 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003924 const bool is_wide = !is_float;
Andreas Gampe3db70682018-12-26 15:12:03 -08003925 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp= */ true, is_wide);
3926 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp= */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003927
3928 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003929 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003930 __ Bind(&retry);
3931 __ fprem();
3932
3933 // Move FP status to AX.
3934 __ fstsw();
3935
3936 // And see if the argument reduction is complete. This is signaled by the
3937 // C2 FPU flag bit set to 0.
3938 __ andl(EAX, Immediate(kC2ConditionMask));
3939 __ j(kNotEqual, &retry);
3940
3941 // We have settled on the final value. Retrieve it into an XMM register.
3942 // Store FP top of stack to real stack.
3943 if (is_float) {
3944 __ fsts(Address(ESP, 0));
3945 } else {
3946 __ fstl(Address(ESP, 0));
3947 }
3948
3949 // Pop the 2 items from the FP stack.
3950 __ fucompp();
3951
3952 // Load the value from the stack into an XMM register.
3953 DCHECK(out.IsFpuRegister()) << out;
3954 if (is_float) {
3955 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3956 } else {
3957 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3958 }
3959
3960 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01003961 codegen_->DecreaseFrame(2 * elem_size);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003962}
3963
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003964
3965void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3966 DCHECK(instruction->IsDiv() || instruction->IsRem());
3967
3968 LocationSummary* locations = instruction->GetLocations();
3969 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003970 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003971
3972 Register out_register = locations->Out().AsRegister<Register>();
3973 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003974 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003975
3976 DCHECK(imm == 1 || imm == -1);
3977
3978 if (instruction->IsRem()) {
3979 __ xorl(out_register, out_register);
3980 } else {
3981 __ movl(out_register, input_register);
3982 if (imm == -1) {
3983 __ negl(out_register);
3984 }
3985 }
3986}
3987
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303988void InstructionCodeGeneratorX86::RemByPowerOfTwo(HRem* instruction) {
3989 LocationSummary* locations = instruction->GetLocations();
3990 Location second = locations->InAt(1);
3991
3992 Register out = locations->Out().AsRegister<Register>();
3993 Register numerator = locations->InAt(0).AsRegister<Register>();
3994
3995 int32_t imm = Int64FromConstant(second.GetConstant());
3996 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3997 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3998
3999 Register tmp = locations->GetTemp(0).AsRegister<Register>();
4000 NearLabel done;
4001 __ movl(out, numerator);
4002 __ andl(out, Immediate(abs_imm-1));
4003 __ j(Condition::kZero, &done);
4004 __ leal(tmp, Address(out, static_cast<int32_t>(~(abs_imm-1))));
4005 __ testl(numerator, numerator);
4006 __ cmovl(Condition::kLess, out, tmp);
4007 __ Bind(&done);
4008}
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004009
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01004010void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004011 LocationSummary* locations = instruction->GetLocations();
4012
4013 Register out_register = locations->Out().AsRegister<Register>();
4014 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01004015 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004016 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
4017 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004018
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004019 Register num = locations->GetTemp(0).AsRegister<Register>();
4020
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004021 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004022 __ testl(input_register, input_register);
4023 __ cmovl(kGreaterEqual, num, input_register);
4024 int shift = CTZ(imm);
4025 __ sarl(num, Immediate(shift));
4026
4027 if (imm < 0) {
4028 __ negl(num);
4029 }
4030
4031 __ movl(out_register, num);
4032}
4033
4034void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4035 DCHECK(instruction->IsDiv() || instruction->IsRem());
4036
4037 LocationSummary* locations = instruction->GetLocations();
4038 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
4039
4040 Register eax = locations->InAt(0).AsRegister<Register>();
4041 Register out = locations->Out().AsRegister<Register>();
4042 Register num;
4043 Register edx;
4044
4045 if (instruction->IsDiv()) {
4046 edx = locations->GetTemp(0).AsRegister<Register>();
4047 num = locations->GetTemp(1).AsRegister<Register>();
4048 } else {
4049 edx = locations->Out().AsRegister<Register>();
4050 num = locations->GetTemp(0).AsRegister<Register>();
4051 }
4052
4053 DCHECK_EQ(EAX, eax);
4054 DCHECK_EQ(EDX, edx);
4055 if (instruction->IsDiv()) {
4056 DCHECK_EQ(EAX, out);
4057 } else {
4058 DCHECK_EQ(EDX, out);
4059 }
4060
4061 int64_t magic;
4062 int shift;
Andreas Gampe3db70682018-12-26 15:12:03 -08004063 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004064
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004065 // Save the numerator.
4066 __ movl(num, eax);
4067
4068 // EAX = magic
4069 __ movl(eax, Immediate(magic));
4070
4071 // EDX:EAX = magic * numerator
4072 __ imull(num);
4073
4074 if (imm > 0 && magic < 0) {
4075 // EDX += num
4076 __ addl(edx, num);
4077 } else if (imm < 0 && magic > 0) {
4078 __ subl(edx, num);
4079 }
4080
4081 // Shift if needed.
4082 if (shift != 0) {
4083 __ sarl(edx, Immediate(shift));
4084 }
4085
4086 // EDX += 1 if EDX < 0
4087 __ movl(eax, edx);
4088 __ shrl(edx, Immediate(31));
4089 __ addl(edx, eax);
4090
4091 if (instruction->IsRem()) {
4092 __ movl(eax, num);
4093 __ imull(edx, Immediate(imm));
4094 __ subl(eax, edx);
4095 __ movl(edx, eax);
4096 } else {
4097 __ movl(eax, edx);
4098 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004099}
4100
Calin Juravlebacfec32014-11-14 15:54:36 +00004101void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
4102 DCHECK(instruction->IsDiv() || instruction->IsRem());
4103
4104 LocationSummary* locations = instruction->GetLocations();
4105 Location out = locations->Out();
4106 Location first = locations->InAt(0);
4107 Location second = locations->InAt(1);
4108 bool is_div = instruction->IsDiv();
4109
4110 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004111 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004112 DCHECK_EQ(EAX, first.AsRegister<Register>());
4113 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00004114
Vladimir Marko13c86fd2015-11-11 12:37:46 +00004115 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01004116 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004117
4118 if (imm == 0) {
4119 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
4120 } else if (imm == 1 || imm == -1) {
4121 DivRemOneOrMinusOne(instruction);
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05304122 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
4123 if (is_div) {
4124 DivByPowerOfTwo(instruction->AsDiv());
4125 } else {
4126 RemByPowerOfTwo(instruction->AsRem());
4127 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004128 } else {
4129 DCHECK(imm <= -2 || imm >= 2);
4130 GenerateDivRemWithAnyConstant(instruction);
4131 }
4132 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004133 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00004134 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004135 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00004136
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004137 Register second_reg = second.AsRegister<Register>();
4138 // 0x80000000/-1 triggers an arithmetic exception!
4139 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
4140 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00004141
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004142 __ cmpl(second_reg, Immediate(-1));
4143 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00004144
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004145 // edx:eax <- sign-extended of eax
4146 __ cdq();
4147 // eax = quotient, edx = remainder
4148 __ idivl(second_reg);
4149 __ Bind(slow_path->GetExitLabel());
4150 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004151 break;
4152 }
4153
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004154 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004155 InvokeRuntimeCallingConvention calling_convention;
4156 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
4157 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
4158 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
4159 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
4160 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
4161 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
4162
4163 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004164 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004165 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004166 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004167 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004168 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004169 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004170 break;
4171 }
4172
4173 default:
4174 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
4175 }
4176}
4177
Calin Juravle7c4954d2014-10-28 16:57:40 +00004178void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004179 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004180 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004181 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01004182 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004183
Calin Juravle7c4954d2014-10-28 16:57:40 +00004184 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004185 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00004186 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004187 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00004188 locations->SetOut(Location::SameAsFirstInput());
4189 // Intel uses edx:eax as the dividend.
4190 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004191 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
4192 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
4193 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01004194 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004195 locations->AddTemp(Location::RequiresRegister());
4196 }
Calin Juravled0d48522014-11-04 16:40:20 +00004197 break;
4198 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004199 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004200 InvokeRuntimeCallingConvention calling_convention;
4201 locations->SetInAt(0, Location::RegisterPairLocation(
4202 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4203 locations->SetInAt(1, Location::RegisterPairLocation(
4204 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4205 // Runtime helper puts the result in EAX, EDX.
4206 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00004207 break;
4208 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004209 case DataType::Type::kFloat32:
4210 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004211 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00004212 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
4213 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00004214 } else if (div->InputAt(1)->IsConstant()) {
4215 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00004216 } else {
4217 locations->SetInAt(1, Location::Any());
4218 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004219 locations->SetOut(Location::SameAsFirstInput());
4220 break;
4221 }
4222
4223 default:
4224 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4225 }
4226}
4227
4228void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
4229 LocationSummary* locations = div->GetLocations();
4230 Location first = locations->InAt(0);
4231 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004232
4233 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004234 case DataType::Type::kInt32:
4235 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004236 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004237 break;
4238 }
4239
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004240 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04004241 if (second.IsFpuRegister()) {
4242 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
4243 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
4244 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00004245 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04004246 __ divss(first.AsFpuRegister<XmmRegister>(),
4247 codegen_->LiteralFloatAddress(
4248 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004249 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04004250 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
4251 } else {
4252 DCHECK(second.IsStackSlot());
4253 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
4254 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004255 break;
4256 }
4257
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004258 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04004259 if (second.IsFpuRegister()) {
4260 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
4261 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
4262 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00004263 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04004264 __ divsd(first.AsFpuRegister<XmmRegister>(),
4265 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004266 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
4267 const_area->GetBaseMethodAddress(),
4268 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04004269 } else {
4270 DCHECK(second.IsDoubleStackSlot());
4271 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
4272 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004273 break;
4274 }
4275
4276 default:
4277 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4278 }
4279}
4280
Calin Juravlebacfec32014-11-14 15:54:36 +00004281void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004282 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004283
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004284 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004285 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004286 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01004287 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00004288
Calin Juravled2ec87d2014-12-08 14:24:46 +00004289 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004290 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004291 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004292 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00004293 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004294 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
4295 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
4296 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01004297 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004298 locations->AddTemp(Location::RequiresRegister());
4299 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004300 break;
4301 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004302 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004303 InvokeRuntimeCallingConvention calling_convention;
4304 locations->SetInAt(0, Location::RegisterPairLocation(
4305 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4306 locations->SetInAt(1, Location::RegisterPairLocation(
4307 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4308 // Runtime helper puts the result in EAX, EDX.
4309 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
4310 break;
4311 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004312 case DataType::Type::kFloat64:
4313 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05004314 locations->SetInAt(0, Location::Any());
4315 locations->SetInAt(1, Location::Any());
4316 locations->SetOut(Location::RequiresFpuRegister());
4317 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00004318 break;
4319 }
4320
4321 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004322 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004323 }
4324}
4325
4326void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004327 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00004328 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004329 case DataType::Type::kInt32:
4330 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004331 GenerateDivRemIntegral(rem);
4332 break;
4333 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004334 case DataType::Type::kFloat32:
4335 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05004336 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00004337 break;
4338 }
4339 default:
4340 LOG(FATAL) << "Unexpected rem type " << type;
4341 }
4342}
4343
Aart Bik1f8d51b2018-02-15 10:42:37 -08004344static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
4345 LocationSummary* locations = new (allocator) LocationSummary(minmax);
4346 switch (minmax->GetResultType()) {
4347 case DataType::Type::kInt32:
4348 locations->SetInAt(0, Location::RequiresRegister());
4349 locations->SetInAt(1, Location::RequiresRegister());
4350 locations->SetOut(Location::SameAsFirstInput());
4351 break;
4352 case DataType::Type::kInt64:
4353 locations->SetInAt(0, Location::RequiresRegister());
4354 locations->SetInAt(1, Location::RequiresRegister());
4355 locations->SetOut(Location::SameAsFirstInput());
4356 // Register to use to perform a long subtract to set cc.
4357 locations->AddTemp(Location::RequiresRegister());
4358 break;
4359 case DataType::Type::kFloat32:
4360 locations->SetInAt(0, Location::RequiresFpuRegister());
4361 locations->SetInAt(1, Location::RequiresFpuRegister());
4362 locations->SetOut(Location::SameAsFirstInput());
4363 locations->AddTemp(Location::RequiresRegister());
4364 break;
4365 case DataType::Type::kFloat64:
4366 locations->SetInAt(0, Location::RequiresFpuRegister());
4367 locations->SetInAt(1, Location::RequiresFpuRegister());
4368 locations->SetOut(Location::SameAsFirstInput());
4369 break;
4370 default:
4371 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
4372 }
4373}
4374
Aart Bik351df3e2018-03-07 11:54:57 -08004375void InstructionCodeGeneratorX86::GenerateMinMaxInt(LocationSummary* locations,
4376 bool is_min,
4377 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08004378 Location op1_loc = locations->InAt(0);
4379 Location op2_loc = locations->InAt(1);
4380
4381 // Shortcut for same input locations.
4382 if (op1_loc.Equals(op2_loc)) {
4383 // Can return immediately, as op1_loc == out_loc.
4384 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
4385 // a copy here.
4386 DCHECK(locations->Out().Equals(op1_loc));
4387 return;
4388 }
4389
4390 if (type == DataType::Type::kInt64) {
4391 // Need to perform a subtract to get the sign right.
4392 // op1 is already in the same location as the output.
4393 Location output = locations->Out();
4394 Register output_lo = output.AsRegisterPairLow<Register>();
4395 Register output_hi = output.AsRegisterPairHigh<Register>();
4396
4397 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
4398 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
4399
4400 // The comparison is performed by subtracting the second operand from
4401 // the first operand and then setting the status flags in the same
4402 // manner as the SUB instruction."
4403 __ cmpl(output_lo, op2_lo);
4404
4405 // Now use a temp and the borrow to finish the subtraction of op2_hi.
4406 Register temp = locations->GetTemp(0).AsRegister<Register>();
4407 __ movl(temp, output_hi);
4408 __ sbbl(temp, op2_hi);
4409
4410 // Now the condition code is correct.
4411 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
4412 __ cmovl(cond, output_lo, op2_lo);
4413 __ cmovl(cond, output_hi, op2_hi);
4414 } else {
4415 DCHECK_EQ(type, DataType::Type::kInt32);
4416 Register out = locations->Out().AsRegister<Register>();
4417 Register op2 = op2_loc.AsRegister<Register>();
4418
4419 // (out := op1)
4420 // out <=? op2
4421 // if out is min jmp done
4422 // out := op2
4423 // done:
4424
4425 __ cmpl(out, op2);
4426 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
4427 __ cmovl(cond, out, op2);
4428 }
4429}
4430
4431void InstructionCodeGeneratorX86::GenerateMinMaxFP(LocationSummary* locations,
4432 bool is_min,
4433 DataType::Type type) {
4434 Location op1_loc = locations->InAt(0);
4435 Location op2_loc = locations->InAt(1);
4436 Location out_loc = locations->Out();
4437 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
4438
4439 // Shortcut for same input locations.
4440 if (op1_loc.Equals(op2_loc)) {
4441 DCHECK(out_loc.Equals(op1_loc));
4442 return;
4443 }
4444
4445 // (out := op1)
4446 // out <=? op2
4447 // if Nan jmp Nan_label
4448 // if out is min jmp done
4449 // if op2 is min jmp op2_label
4450 // handle -0/+0
4451 // jmp done
4452 // Nan_label:
4453 // out := NaN
4454 // op2_label:
4455 // out := op2
4456 // done:
4457 //
4458 // This removes one jmp, but needs to copy one input (op1) to out.
4459 //
4460 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
4461
4462 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
4463
4464 NearLabel nan, done, op2_label;
4465 if (type == DataType::Type::kFloat64) {
4466 __ ucomisd(out, op2);
4467 } else {
4468 DCHECK_EQ(type, DataType::Type::kFloat32);
4469 __ ucomiss(out, op2);
4470 }
4471
4472 __ j(Condition::kParityEven, &nan);
4473
4474 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
4475 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
4476
4477 // Handle 0.0/-0.0.
4478 if (is_min) {
4479 if (type == DataType::Type::kFloat64) {
4480 __ orpd(out, op2);
4481 } else {
4482 __ orps(out, op2);
4483 }
4484 } else {
4485 if (type == DataType::Type::kFloat64) {
4486 __ andpd(out, op2);
4487 } else {
4488 __ andps(out, op2);
4489 }
4490 }
4491 __ jmp(&done);
4492
4493 // NaN handling.
4494 __ Bind(&nan);
4495 if (type == DataType::Type::kFloat64) {
4496 // TODO: Use a constant from the constant table (requires extra input).
4497 __ LoadLongConstant(out, kDoubleNaN);
4498 } else {
4499 Register constant = locations->GetTemp(0).AsRegister<Register>();
4500 __ movl(constant, Immediate(kFloatNaN));
4501 __ movd(out, constant);
4502 }
4503 __ jmp(&done);
4504
4505 // out := op2;
4506 __ Bind(&op2_label);
4507 if (type == DataType::Type::kFloat64) {
4508 __ movsd(out, op2);
4509 } else {
4510 __ movss(out, op2);
4511 }
4512
4513 // Done.
4514 __ Bind(&done);
4515}
4516
Aart Bik351df3e2018-03-07 11:54:57 -08004517void InstructionCodeGeneratorX86::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4518 DataType::Type type = minmax->GetResultType();
4519 switch (type) {
4520 case DataType::Type::kInt32:
4521 case DataType::Type::kInt64:
4522 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
4523 break;
4524 case DataType::Type::kFloat32:
4525 case DataType::Type::kFloat64:
4526 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
4527 break;
4528 default:
4529 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4530 }
4531}
4532
Aart Bik1f8d51b2018-02-15 10:42:37 -08004533void LocationsBuilderX86::VisitMin(HMin* min) {
4534 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4535}
4536
4537void InstructionCodeGeneratorX86::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004538 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004539}
4540
4541void LocationsBuilderX86::VisitMax(HMax* max) {
4542 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4543}
4544
4545void InstructionCodeGeneratorX86::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004546 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004547}
4548
Aart Bik3dad3412018-02-28 12:01:46 -08004549void LocationsBuilderX86::VisitAbs(HAbs* abs) {
4550 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4551 switch (abs->GetResultType()) {
4552 case DataType::Type::kInt32:
4553 locations->SetInAt(0, Location::RegisterLocation(EAX));
4554 locations->SetOut(Location::SameAsFirstInput());
4555 locations->AddTemp(Location::RegisterLocation(EDX));
4556 break;
4557 case DataType::Type::kInt64:
4558 locations->SetInAt(0, Location::RequiresRegister());
4559 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4560 locations->AddTemp(Location::RequiresRegister());
4561 break;
4562 case DataType::Type::kFloat32:
4563 locations->SetInAt(0, Location::RequiresFpuRegister());
4564 locations->SetOut(Location::SameAsFirstInput());
4565 locations->AddTemp(Location::RequiresFpuRegister());
4566 locations->AddTemp(Location::RequiresRegister());
4567 break;
4568 case DataType::Type::kFloat64:
4569 locations->SetInAt(0, Location::RequiresFpuRegister());
4570 locations->SetOut(Location::SameAsFirstInput());
4571 locations->AddTemp(Location::RequiresFpuRegister());
4572 break;
4573 default:
4574 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4575 }
4576}
4577
4578void InstructionCodeGeneratorX86::VisitAbs(HAbs* abs) {
4579 LocationSummary* locations = abs->GetLocations();
4580 switch (abs->GetResultType()) {
4581 case DataType::Type::kInt32: {
4582 Register out = locations->Out().AsRegister<Register>();
4583 DCHECK_EQ(out, EAX);
4584 Register temp = locations->GetTemp(0).AsRegister<Register>();
4585 DCHECK_EQ(temp, EDX);
4586 // Sign extend EAX into EDX.
4587 __ cdq();
4588 // XOR EAX with sign.
4589 __ xorl(EAX, EDX);
4590 // Subtract out sign to correct.
4591 __ subl(EAX, EDX);
4592 // The result is in EAX.
4593 break;
4594 }
4595 case DataType::Type::kInt64: {
4596 Location input = locations->InAt(0);
4597 Register input_lo = input.AsRegisterPairLow<Register>();
4598 Register input_hi = input.AsRegisterPairHigh<Register>();
4599 Location output = locations->Out();
4600 Register output_lo = output.AsRegisterPairLow<Register>();
4601 Register output_hi = output.AsRegisterPairHigh<Register>();
4602 Register temp = locations->GetTemp(0).AsRegister<Register>();
4603 // Compute the sign into the temporary.
4604 __ movl(temp, input_hi);
4605 __ sarl(temp, Immediate(31));
4606 // Store the sign into the output.
4607 __ movl(output_lo, temp);
4608 __ movl(output_hi, temp);
4609 // XOR the input to the output.
4610 __ xorl(output_lo, input_lo);
4611 __ xorl(output_hi, input_hi);
4612 // Subtract the sign.
4613 __ subl(output_lo, temp);
4614 __ sbbl(output_hi, temp);
4615 break;
4616 }
4617 case DataType::Type::kFloat32: {
4618 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4619 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4620 Register constant = locations->GetTemp(1).AsRegister<Register>();
4621 __ movl(constant, Immediate(INT32_C(0x7FFFFFFF)));
4622 __ movd(temp, constant);
4623 __ andps(out, temp);
4624 break;
4625 }
4626 case DataType::Type::kFloat64: {
4627 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4628 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4629 // TODO: Use a constant from the constant table (requires extra input).
4630 __ LoadLongConstant(temp, INT64_C(0x7FFFFFFFFFFFFFFF));
4631 __ andpd(out, temp);
4632 break;
4633 }
4634 default:
4635 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4636 }
4637}
4638
Calin Juravled0d48522014-11-04 16:40:20 +00004639void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004640 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004641 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004642 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004643 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004644 case DataType::Type::kInt8:
4645 case DataType::Type::kUint16:
4646 case DataType::Type::kInt16:
4647 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004648 locations->SetInAt(0, Location::Any());
4649 break;
4650 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004651 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004652 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
4653 if (!instruction->IsConstant()) {
4654 locations->AddTemp(Location::RequiresRegister());
4655 }
4656 break;
4657 }
4658 default:
4659 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4660 }
Calin Juravled0d48522014-11-04 16:40:20 +00004661}
4662
4663void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004664 SlowPathCode* slow_path =
4665 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004666 codegen_->AddSlowPath(slow_path);
4667
4668 LocationSummary* locations = instruction->GetLocations();
4669 Location value = locations->InAt(0);
4670
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004671 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004672 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004673 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004674 case DataType::Type::kInt8:
4675 case DataType::Type::kUint16:
4676 case DataType::Type::kInt16:
4677 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004678 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004679 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004680 __ j(kEqual, slow_path->GetEntryLabel());
4681 } else if (value.IsStackSlot()) {
4682 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
4683 __ j(kEqual, slow_path->GetEntryLabel());
4684 } else {
4685 DCHECK(value.IsConstant()) << value;
4686 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004687 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004688 }
4689 }
4690 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004691 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004692 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004693 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004694 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004695 __ movl(temp, value.AsRegisterPairLow<Register>());
4696 __ orl(temp, value.AsRegisterPairHigh<Register>());
4697 __ j(kEqual, slow_path->GetEntryLabel());
4698 } else {
4699 DCHECK(value.IsConstant()) << value;
4700 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4701 __ jmp(slow_path->GetEntryLabel());
4702 }
4703 }
4704 break;
4705 }
4706 default:
4707 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004708 }
Calin Juravled0d48522014-11-04 16:40:20 +00004709}
4710
Calin Juravle9aec02f2014-11-18 23:06:35 +00004711void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
4712 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4713
4714 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004715 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004716
4717 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004718 case DataType::Type::kInt32:
4719 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004720 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00004721 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00004722 // The shift count needs to be in CL or a constant.
4723 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004724 locations->SetOut(Location::SameAsFirstInput());
4725 break;
4726 }
4727 default:
4728 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4729 }
4730}
4731
4732void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
4733 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4734
4735 LocationSummary* locations = op->GetLocations();
4736 Location first = locations->InAt(0);
4737 Location second = locations->InAt(1);
4738 DCHECK(first.Equals(locations->Out()));
4739
4740 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004741 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00004742 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004743 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004744 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004745 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004746 DCHECK_EQ(ECX, second_reg);
4747 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004748 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004749 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004750 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004751 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004752 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004753 }
4754 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004755 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004756 if (shift == 0) {
4757 return;
4758 }
4759 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004760 if (op->IsShl()) {
4761 __ shll(first_reg, imm);
4762 } else if (op->IsShr()) {
4763 __ sarl(first_reg, imm);
4764 } else {
4765 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004766 }
4767 }
4768 break;
4769 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004770 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004771 if (second.IsRegister()) {
4772 Register second_reg = second.AsRegister<Register>();
4773 DCHECK_EQ(ECX, second_reg);
4774 if (op->IsShl()) {
4775 GenerateShlLong(first, second_reg);
4776 } else if (op->IsShr()) {
4777 GenerateShrLong(first, second_reg);
4778 } else {
4779 GenerateUShrLong(first, second_reg);
4780 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004781 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00004782 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004783 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004784 // Nothing to do if the shift is 0, as the input is already the output.
4785 if (shift != 0) {
4786 if (op->IsShl()) {
4787 GenerateShlLong(first, shift);
4788 } else if (op->IsShr()) {
4789 GenerateShrLong(first, shift);
4790 } else {
4791 GenerateUShrLong(first, shift);
4792 }
4793 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004794 }
4795 break;
4796 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004797 default:
4798 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4799 }
4800}
4801
Mark P Mendell73945692015-04-29 14:56:17 +00004802void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
4803 Register low = loc.AsRegisterPairLow<Register>();
4804 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04004805 if (shift == 1) {
4806 // This is just an addition.
4807 __ addl(low, low);
4808 __ adcl(high, high);
4809 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00004810 // Shift by 32 is easy. High gets low, and low gets 0.
4811 codegen_->EmitParallelMoves(
4812 loc.ToLow(),
4813 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004814 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004815 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4816 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004817 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004818 } else if (shift > 32) {
4819 // Low part becomes 0. High part is low part << (shift-32).
4820 __ movl(high, low);
4821 __ shll(high, Immediate(shift - 32));
4822 __ xorl(low, low);
4823 } else {
4824 // Between 1 and 31.
4825 __ shld(high, low, Immediate(shift));
4826 __ shll(low, Immediate(shift));
4827 }
4828}
4829
Calin Juravle9aec02f2014-11-18 23:06:35 +00004830void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004831 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004832 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4833 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4834 __ testl(shifter, Immediate(32));
4835 __ j(kEqual, &done);
4836 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4837 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4838 __ Bind(&done);
4839}
4840
Mark P Mendell73945692015-04-29 14:56:17 +00004841void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4842 Register low = loc.AsRegisterPairLow<Register>();
4843 Register high = loc.AsRegisterPairHigh<Register>();
4844 if (shift == 32) {
4845 // Need to copy the sign.
4846 DCHECK_NE(low, high);
4847 __ movl(low, high);
4848 __ sarl(high, Immediate(31));
4849 } else if (shift > 32) {
4850 DCHECK_NE(low, high);
4851 // High part becomes sign. Low part is shifted by shift - 32.
4852 __ movl(low, high);
4853 __ sarl(high, Immediate(31));
4854 __ sarl(low, Immediate(shift - 32));
4855 } else {
4856 // Between 1 and 31.
4857 __ shrd(low, high, Immediate(shift));
4858 __ sarl(high, Immediate(shift));
4859 }
4860}
4861
Calin Juravle9aec02f2014-11-18 23:06:35 +00004862void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004863 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004864 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4865 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4866 __ testl(shifter, Immediate(32));
4867 __ j(kEqual, &done);
4868 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4869 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4870 __ Bind(&done);
4871}
4872
Mark P Mendell73945692015-04-29 14:56:17 +00004873void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4874 Register low = loc.AsRegisterPairLow<Register>();
4875 Register high = loc.AsRegisterPairHigh<Register>();
4876 if (shift == 32) {
4877 // Shift by 32 is easy. Low gets high, and high gets 0.
4878 codegen_->EmitParallelMoves(
4879 loc.ToHigh(),
4880 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004881 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004882 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4883 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004884 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004885 } else if (shift > 32) {
4886 // Low part is high >> (shift - 32). High part becomes 0.
4887 __ movl(low, high);
4888 __ shrl(low, Immediate(shift - 32));
4889 __ xorl(high, high);
4890 } else {
4891 // Between 1 and 31.
4892 __ shrd(low, high, Immediate(shift));
4893 __ shrl(high, Immediate(shift));
4894 }
4895}
4896
Calin Juravle9aec02f2014-11-18 23:06:35 +00004897void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004898 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004899 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4900 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4901 __ testl(shifter, Immediate(32));
4902 __ j(kEqual, &done);
4903 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4904 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4905 __ Bind(&done);
4906}
4907
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004908void LocationsBuilderX86::VisitRor(HRor* ror) {
4909 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004910 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004911
4912 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004913 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004914 // Add the temporary needed.
4915 locations->AddTemp(Location::RequiresRegister());
4916 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004917 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004918 locations->SetInAt(0, Location::RequiresRegister());
4919 // The shift count needs to be in CL (unless it is a constant).
4920 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4921 locations->SetOut(Location::SameAsFirstInput());
4922 break;
4923 default:
4924 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4925 UNREACHABLE();
4926 }
4927}
4928
4929void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4930 LocationSummary* locations = ror->GetLocations();
4931 Location first = locations->InAt(0);
4932 Location second = locations->InAt(1);
4933
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004934 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004935 Register first_reg = first.AsRegister<Register>();
4936 if (second.IsRegister()) {
4937 Register second_reg = second.AsRegister<Register>();
4938 __ rorl(first_reg, second_reg);
4939 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004940 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004941 __ rorl(first_reg, imm);
4942 }
4943 return;
4944 }
4945
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004946 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004947 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4948 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4949 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4950 if (second.IsRegister()) {
4951 Register second_reg = second.AsRegister<Register>();
4952 DCHECK_EQ(second_reg, ECX);
4953 __ movl(temp_reg, first_reg_hi);
4954 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4955 __ shrd(first_reg_lo, temp_reg, second_reg);
4956 __ movl(temp_reg, first_reg_hi);
4957 __ testl(second_reg, Immediate(32));
4958 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4959 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4960 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004961 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004962 if (shift_amt == 0) {
4963 // Already fine.
4964 return;
4965 }
4966 if (shift_amt == 32) {
4967 // Just swap.
4968 __ movl(temp_reg, first_reg_lo);
4969 __ movl(first_reg_lo, first_reg_hi);
4970 __ movl(first_reg_hi, temp_reg);
4971 return;
4972 }
4973
4974 Immediate imm(shift_amt);
4975 // Save the constents of the low value.
4976 __ movl(temp_reg, first_reg_lo);
4977
4978 // Shift right into low, feeding bits from high.
4979 __ shrd(first_reg_lo, first_reg_hi, imm);
4980
4981 // Shift right into high, feeding bits from the original low.
4982 __ shrd(first_reg_hi, temp_reg, imm);
4983
4984 // Swap if needed.
4985 if (shift_amt > 32) {
4986 __ movl(temp_reg, first_reg_lo);
4987 __ movl(first_reg_lo, first_reg_hi);
4988 __ movl(first_reg_hi, temp_reg);
4989 }
4990 }
4991}
4992
Calin Juravle9aec02f2014-11-18 23:06:35 +00004993void LocationsBuilderX86::VisitShl(HShl* shl) {
4994 HandleShift(shl);
4995}
4996
4997void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4998 HandleShift(shl);
4999}
5000
5001void LocationsBuilderX86::VisitShr(HShr* shr) {
5002 HandleShift(shr);
5003}
5004
5005void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
5006 HandleShift(shr);
5007}
5008
5009void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
5010 HandleShift(ushr);
5011}
5012
5013void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
5014 HandleShift(ushr);
5015}
5016
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01005017void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005018 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5019 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005020 locations->SetOut(Location::RegisterLocation(EAX));
Alex Lightd109e302018-06-27 10:25:41 -07005021 InvokeRuntimeCallingConvention calling_convention;
5022 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01005023}
5024
5025void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07005026 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
5027 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
5028 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01005029}
5030
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005031void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005032 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
5033 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005034 locations->SetOut(Location::RegisterLocation(EAX));
5035 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005036 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5037 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005038}
5039
5040void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01005041 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
5042 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00005043 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005044 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005045 DCHECK(!codegen_->IsLeafMethod());
5046}
5047
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005048void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005049 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005050 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01005051 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5052 if (location.IsStackSlot()) {
5053 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5054 } else if (location.IsDoubleStackSlot()) {
5055 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005056 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01005057 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005058}
5059
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005060void InstructionCodeGeneratorX86::VisitParameterValue(
5061 HParameterValue* instruction ATTRIBUTE_UNUSED) {
5062}
5063
5064void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
5065 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005066 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005067 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
5068}
5069
5070void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005071}
5072
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005073void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
5074 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005075 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005076 locations->SetInAt(0, Location::RequiresRegister());
5077 locations->SetOut(Location::RequiresRegister());
5078}
5079
5080void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
5081 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00005082 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005083 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005084 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005085 __ movl(locations->Out().AsRegister<Register>(),
5086 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005087 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005088 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00005089 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01005090 __ movl(locations->Out().AsRegister<Register>(),
5091 Address(locations->InAt(0).AsRegister<Register>(),
5092 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
5093 // temp = temp->GetImtEntryAt(method_offset);
5094 __ movl(locations->Out().AsRegister<Register>(),
5095 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005096 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00005097}
5098
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005099void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005100 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005101 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01005102 locations->SetInAt(0, Location::RequiresRegister());
5103 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005104}
5105
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005106void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
5107 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01005108 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01005109 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01005110 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005111 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005112 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00005113 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005114 break;
5115
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005116 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01005117 __ notl(out.AsRegisterPairLow<Register>());
5118 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005119 break;
5120
5121 default:
5122 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
5123 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005124}
5125
David Brazdil66d126e2015-04-03 16:02:44 +01005126void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
5127 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005128 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01005129 locations->SetInAt(0, Location::RequiresRegister());
5130 locations->SetOut(Location::SameAsFirstInput());
5131}
5132
5133void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01005134 LocationSummary* locations = bool_not->GetLocations();
5135 Location in = locations->InAt(0);
5136 Location out = locations->Out();
5137 DCHECK(in.Equals(out));
5138 __ xorl(out.AsRegister<Register>(), Immediate(1));
5139}
5140
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005141void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005142 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005143 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00005144 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005145 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005146 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005147 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005148 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005149 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005150 case DataType::Type::kInt32:
5151 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00005152 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00005153 locations->SetInAt(1, Location::Any());
5154 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5155 break;
5156 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005157 case DataType::Type::kFloat32:
5158 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00005159 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00005160 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
5161 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
5162 } else if (compare->InputAt(1)->IsConstant()) {
5163 locations->SetInAt(1, Location::RequiresFpuRegister());
5164 } else {
5165 locations->SetInAt(1, Location::Any());
5166 }
Calin Juravleddb7df22014-11-25 20:56:51 +00005167 locations->SetOut(Location::RequiresRegister());
5168 break;
5169 }
5170 default:
5171 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5172 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005173}
5174
5175void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005176 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005177 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00005178 Location left = locations->InAt(0);
5179 Location right = locations->InAt(1);
5180
Mark Mendell0c9497d2015-08-21 09:30:05 -04005181 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08005182 Condition less_cond = kLess;
5183
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005184 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005185 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005186 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005187 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005188 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005189 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005190 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01005191 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08005192 break;
5193 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005194 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005195 Register left_low = left.AsRegisterPairLow<Register>();
5196 Register left_high = left.AsRegisterPairHigh<Register>();
5197 int32_t val_low = 0;
5198 int32_t val_high = 0;
5199 bool right_is_const = false;
5200
5201 if (right.IsConstant()) {
5202 DCHECK(right.GetConstant()->IsLongConstant());
5203 right_is_const = true;
5204 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
5205 val_low = Low32Bits(val);
5206 val_high = High32Bits(val);
5207 }
5208
Calin Juravleddb7df22014-11-25 20:56:51 +00005209 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005210 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005211 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005212 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005213 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005214 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08005215 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005216 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005217 __ j(kLess, &less); // Signed compare.
5218 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005219 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005220 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005221 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005222 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005223 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005224 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08005225 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005226 }
Aart Bika19616e2016-02-01 18:57:58 -08005227 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00005228 break;
5229 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005230 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00005231 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00005232 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08005233 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00005234 break;
5235 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005236 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00005237 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00005238 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08005239 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005240 break;
5241 }
5242 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00005243 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005244 }
Aart Bika19616e2016-02-01 18:57:58 -08005245
Calin Juravleddb7df22014-11-25 20:56:51 +00005246 __ movl(out, Immediate(0));
5247 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08005248 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00005249
5250 __ Bind(&greater);
5251 __ movl(out, Immediate(1));
5252 __ jmp(&done);
5253
5254 __ Bind(&less);
5255 __ movl(out, Immediate(-1));
5256
5257 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005258}
5259
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005260void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005261 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005262 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005263 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01005264 locations->SetInAt(i, Location::Any());
5265 }
5266 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005267}
5268
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005269void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005270 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005271}
5272
Roland Levillain7c1559a2015-12-15 10:55:36 +00005273void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00005274 /*
5275 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
5276 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
5277 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
5278 */
5279 switch (kind) {
5280 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00005281 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00005282 break;
5283 }
5284 case MemBarrierKind::kAnyStore:
5285 case MemBarrierKind::kLoadAny:
5286 case MemBarrierKind::kStoreStore: {
5287 // nop
5288 break;
5289 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05005290 case MemBarrierKind::kNTStoreStore:
5291 // Non-Temporal Store/Store needs an explicit fence.
Andreas Gampe3db70682018-12-26 15:12:03 -08005292 MemoryFence(/* non-temporal= */ true);
Mark Mendell7aa04a12016-01-27 22:39:07 -05005293 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005294 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005295}
5296
Vladimir Markodc151b22015-10-15 18:02:30 +01005297HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
5298 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01005299 ArtMethod* method ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005300 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005301}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005302
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005303Register CodeGeneratorX86::GetInvokeExtraParameter(HInvoke* invoke, Register temp) {
5304 if (invoke->IsInvokeStaticOrDirect()) {
5305 return GetInvokeStaticOrDirectExtraParameter(invoke->AsInvokeStaticOrDirect(), temp);
5306 }
5307 DCHECK(invoke->IsInvokeInterface());
5308 Location location =
5309 invoke->GetLocations()->InAt(invoke->AsInvokeInterface()->GetSpecialInputIndex());
5310 return location.AsRegister<Register>();
5311}
5312
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005313Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
5314 Register temp) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00005315 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005316 if (!invoke->GetLocations()->Intrinsified()) {
5317 return location.AsRegister<Register>();
5318 }
5319 // For intrinsics we allow any location, so it may be on the stack.
5320 if (!location.IsRegister()) {
5321 __ movl(temp, Address(ESP, location.GetStackIndex()));
5322 return temp;
5323 }
5324 // For register locations, check if the register was saved. If so, get it from the stack.
5325 // Note: There is a chance that the register was saved but not overwritten, so we could
5326 // save one load. However, since this is just an intrinsic slow path we prefer this
5327 // simple and more robust approach rather that trying to determine if that's the case.
5328 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00005329 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
5330 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
5331 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
5332 __ movl(temp, Address(ESP, stack_offset));
5333 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005334 }
5335 return location.AsRegister<Register>();
5336}
5337
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005338void CodeGeneratorX86::LoadMethod(MethodLoadKind load_kind, Location temp, HInvoke* invoke) {
5339 switch (load_kind) {
5340 case MethodLoadKind::kBootImageLinkTimePcRelative: {
5341 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
5342 Register base_reg = GetInvokeExtraParameter(invoke, temp.AsRegister<Register>());
5343 __ leal(temp.AsRegister<Register>(),
5344 Address(base_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
5345 RecordBootImageMethodPatch(invoke);
5346 break;
5347 }
5348 case MethodLoadKind::kBootImageRelRo: {
5349 size_t index = invoke->IsInvokeInterface()
5350 ? invoke->AsInvokeInterface()->GetSpecialInputIndex()
5351 : invoke->AsInvokeStaticOrDirect()->GetSpecialInputIndex();
5352 Register base_reg = GetInvokeExtraParameter(invoke, temp.AsRegister<Register>());
5353 __ movl(temp.AsRegister<Register>(), Address(base_reg, kPlaceholder32BitOffset));
5354 RecordBootImageRelRoPatch(
5355 invoke->InputAt(index)->AsX86ComputeBaseMethodAddress(),
5356 GetBootImageOffset(invoke));
5357 break;
5358 }
5359 case MethodLoadKind::kBssEntry: {
5360 Register base_reg = GetInvokeExtraParameter(invoke, temp.AsRegister<Register>());
5361 __ movl(temp.AsRegister<Register>(), Address(base_reg, kPlaceholder32BitOffset));
5362 RecordMethodBssEntryPatch(invoke);
5363 // No need for memory fence, thanks to the x86 memory model.
5364 break;
5365 }
5366 case MethodLoadKind::kJitDirectAddress: {
5367 __ movl(temp.AsRegister<Register>(),
5368 Immediate(reinterpret_cast32<uint32_t>(invoke->GetResolvedMethod())));
5369 break;
5370 }
5371 case MethodLoadKind::kRuntimeCall: {
5372 // Test situation, don't do anything.
5373 break;
5374 }
5375 default: {
5376 LOG(FATAL) << "Load kind should have already been handled " << load_kind;
5377 UNREACHABLE();
5378 }
5379 }
5380}
5381
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005382void CodeGeneratorX86::GenerateStaticOrDirectCall(
5383 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00005384 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
5385 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005386 case MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005387 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005388 uint32_t offset =
5389 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
5390 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00005391 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005392 }
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005393 case MethodLoadKind::kRecursive: {
Vladimir Marko86c87522020-05-11 16:55:55 +01005394 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005395 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005396 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005397 case MethodLoadKind::kRuntimeCall: {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005398 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5399 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01005400 }
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005401 case MethodLoadKind::kBootImageLinkTimePcRelative:
5402 // For kCallCriticalNative we skip loading the method and do the call directly.
5403 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
5404 break;
5405 }
5406 FALLTHROUGH_INTENDED;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005407 default: {
5408 LoadMethod(invoke->GetMethodLoadKind(), callee_method, invoke);
5409 }
Vladimir Marko58155012015-08-19 12:49:41 +00005410 }
5411
5412 switch (invoke->GetCodePtrLocation()) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005413 case CodePtrLocation::kCallSelf:
Nicolas Geoffray282795c2021-09-24 18:16:41 +01005414 DCHECK(!GetGraph()->HasShouldDeoptimizeFlag());
Vladimir Marko58155012015-08-19 12:49:41 +00005415 __ call(GetFrameEntryLabel());
Vladimir Marko86c87522020-05-11 16:55:55 +01005416 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko58155012015-08-19 12:49:41 +00005417 break;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005418 case CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01005419 size_t out_frame_size =
5420 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorX86,
5421 kNativeStackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01005422 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005423 if (invoke->GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative) {
5424 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
5425 Register base_reg = GetInvokeExtraParameter(invoke, temp.AsRegister<Register>());
5426 __ call(Address(base_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
5427 RecordBootImageJniEntrypointPatch(invoke);
5428 } else {
5429 // (callee_method + offset_of_jni_entry_point)()
5430 __ call(Address(callee_method.AsRegister<Register>(),
5431 ArtMethod::EntryPointFromJniOffset(kX86PointerSize).Int32Value()));
5432 }
Vladimir Marko86c87522020-05-11 16:55:55 +01005433 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5434 if (out_frame_size == 0u && DataType::IsFloatingPointType(invoke->GetType())) {
5435 // Create space for conversion.
5436 out_frame_size = 8u;
Vladimir Markodec78172020-06-19 15:31:23 +01005437 IncreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01005438 }
5439 // Zero-/sign-extend or move the result when needed due to native and managed ABI mismatch.
5440 switch (invoke->GetType()) {
5441 case DataType::Type::kBool:
5442 __ movzxb(EAX, AL);
5443 break;
5444 case DataType::Type::kInt8:
5445 __ movsxb(EAX, AL);
5446 break;
5447 case DataType::Type::kUint16:
5448 __ movzxw(EAX, EAX);
5449 break;
5450 case DataType::Type::kInt16:
5451 __ movsxw(EAX, EAX);
5452 break;
5453 case DataType::Type::kFloat32:
5454 __ fstps(Address(ESP, 0));
5455 __ movss(XMM0, Address(ESP, 0));
5456 break;
5457 case DataType::Type::kFloat64:
5458 __ fstpl(Address(ESP, 0));
5459 __ movsd(XMM0, Address(ESP, 0));
5460 break;
5461 case DataType::Type::kInt32:
5462 case DataType::Type::kInt64:
5463 case DataType::Type::kVoid:
5464 break;
5465 default:
5466 DCHECK(false) << invoke->GetType();
5467 break;
5468 }
5469 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01005470 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01005471 }
5472 break;
5473 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005474 case CodePtrLocation::kCallArtMethod:
Vladimir Marko58155012015-08-19 12:49:41 +00005475 // (callee_method + offset_of_quick_compiled_code)()
5476 __ call(Address(callee_method.AsRegister<Register>(),
5477 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005478 kX86PointerSize).Int32Value()));
Vladimir Marko86c87522020-05-11 16:55:55 +01005479 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko58155012015-08-19 12:49:41 +00005480 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04005481 }
5482
5483 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04005484}
5485
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005486void CodeGeneratorX86::GenerateVirtualCall(
5487 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005488 Register temp = temp_in.AsRegister<Register>();
5489 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5490 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005491
5492 // Use the calling convention instead of the location of the receiver, as
5493 // intrinsics may have put the receiver in a different register. In the intrinsics
5494 // slow path, the arguments have been moved to the right place, so here we are
5495 // guaranteed that the receiver is the first register of the calling convention.
5496 InvokeDexCallingConvention calling_convention;
5497 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005498 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005499 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005500 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005501 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005502 // Instead of simply (possibly) unpoisoning `temp` here, we should
5503 // emit a read barrier for the previous class reference load.
5504 // However this is not required in practice, as this is an
5505 // intermediate/temporary reference and because the current
5506 // concurrent copying collector keeps the from-space memory
5507 // intact/accessible until the end of the marking phase (the
5508 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005509 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00005510
5511 MaybeGenerateInlineCacheCheck(invoke, temp);
5512
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005513 // temp = temp->GetMethodAt(method_offset);
5514 __ movl(temp, Address(temp, method_offset));
5515 // call temp->GetEntryPoint();
5516 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07005517 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005518 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005519}
5520
Vladimir Marko6fd16062018-06-26 11:02:04 +01005521void CodeGeneratorX86::RecordBootImageIntrinsicPatch(HX86ComputeBaseMethodAddress* method_address,
5522 uint32_t intrinsic_data) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005523 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005524 method_address, /* target_dex_file= */ nullptr, intrinsic_data);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005525 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005526}
5527
Vladimir Markob066d432018-01-03 13:14:37 +00005528void CodeGeneratorX86::RecordBootImageRelRoPatch(HX86ComputeBaseMethodAddress* method_address,
5529 uint32_t boot_image_offset) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005530 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005531 method_address, /* target_dex_file= */ nullptr, boot_image_offset);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005532 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Markob066d432018-01-03 13:14:37 +00005533}
5534
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005535void CodeGeneratorX86::RecordBootImageMethodPatch(HInvoke* invoke) {
5536 size_t index = invoke->IsInvokeInterface()
5537 ? invoke->AsInvokeInterface()->GetSpecialInputIndex()
5538 : invoke->AsInvokeStaticOrDirect()->GetSpecialInputIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005539 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005540 invoke->InputAt(index)->AsX86ComputeBaseMethodAddress();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005541 boot_image_method_patches_.emplace_back(
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005542 method_address,
5543 invoke->GetResolvedMethodReference().dex_file,
5544 invoke->GetResolvedMethodReference().index);
Vladimir Marko65979462017-05-19 17:25:12 +01005545 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005546}
5547
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005548void CodeGeneratorX86::RecordMethodBssEntryPatch(HInvoke* invoke) {
5549 size_t index = invoke->IsInvokeInterface()
5550 ? invoke->AsInvokeInterface()->GetSpecialInputIndex()
5551 : invoke->AsInvokeStaticOrDirect()->GetSpecialInputIndex();
Santiago Aboy Solanesa0232ad2021-11-08 17:00:06 +00005552 DCHECK(IsSameDexFile(GetGraph()->GetDexFile(), *invoke->GetMethodReference().dex_file) ||
Santiago Aboy Solanes69a87e32022-03-08 16:43:54 +00005553 GetCompilerOptions().WithinOatFile(invoke->GetMethodReference().dex_file) ||
5554 ContainsElement(Runtime::Current()->GetClassLinker()->GetBootClassPath(),
5555 invoke->GetMethodReference().dex_file));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005556 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005557 invoke->InputAt(index)->AsX86ComputeBaseMethodAddress();
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005558 // Add the patch entry and bind its label at the end of the instruction.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005559 method_bss_entry_patches_.emplace_back(
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005560 method_address,
5561 invoke->GetMethodReference().dex_file,
5562 invoke->GetMethodReference().index);
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005563 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005564}
5565
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005566void CodeGeneratorX86::RecordBootImageTypePatch(HLoadClass* load_class) {
5567 HX86ComputeBaseMethodAddress* method_address =
5568 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5569 boot_image_type_patches_.emplace_back(
5570 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005571 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005572}
5573
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005574Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005575 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005576 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko8f63f102020-09-28 12:10:28 +01005577 ArenaDeque<X86PcRelativePatchInfo>* patches = nullptr;
5578 switch (load_class->GetLoadKind()) {
5579 case HLoadClass::LoadKind::kBssEntry:
5580 patches = &type_bss_entry_patches_;
5581 break;
5582 case HLoadClass::LoadKind::kBssEntryPublic:
5583 patches = &public_type_bss_entry_patches_;
5584 break;
5585 case HLoadClass::LoadKind::kBssEntryPackage:
5586 patches = &package_type_bss_entry_patches_;
5587 break;
5588 default:
5589 LOG(FATAL) << "Unexpected load kind: " << load_class->GetLoadKind();
5590 UNREACHABLE();
5591 }
5592 patches->emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005593 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005594 return &patches->back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005595}
5596
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005597void CodeGeneratorX86::RecordBootImageStringPatch(HLoadString* load_string) {
5598 HX86ComputeBaseMethodAddress* method_address =
5599 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
5600 boot_image_string_patches_.emplace_back(
5601 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
5602 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01005603}
5604
Vladimir Markoaad75c62016-10-03 08:46:48 +00005605Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005606 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005607 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005608 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005609 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005610 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005611}
5612
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005613void CodeGeneratorX86::RecordBootImageJniEntrypointPatch(HInvokeStaticOrDirect* invoke) {
5614 HX86ComputeBaseMethodAddress* method_address =
5615 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5616 boot_image_jni_entrypoint_patches_.emplace_back(
5617 method_address,
5618 invoke->GetResolvedMethodReference().dex_file,
5619 invoke->GetResolvedMethodReference().index);
5620 __ Bind(&boot_image_jni_entrypoint_patches_.back().label);
5621}
5622
Vladimir Markoeebb8212018-06-05 14:57:24 +01005623void CodeGeneratorX86::LoadBootImageAddress(Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01005624 uint32_t boot_image_reference,
Vladimir Markoeebb8212018-06-05 14:57:24 +01005625 HInvokeStaticOrDirect* invoke) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005626 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005627 HX86ComputeBaseMethodAddress* method_address =
5628 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5629 DCHECK(method_address != nullptr);
5630 Register method_address_reg =
5631 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005632 __ leal(reg, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005633 RecordBootImageIntrinsicPatch(method_address, boot_image_reference);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01005634 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01005635 HX86ComputeBaseMethodAddress* method_address =
5636 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5637 DCHECK(method_address != nullptr);
5638 Register method_address_reg =
5639 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005640 __ movl(reg, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005641 RecordBootImageRelRoPatch(method_address, boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01005642 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01005643 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01005644 gc::Heap* heap = Runtime::Current()->GetHeap();
5645 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01005646 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01005647 __ movl(reg, Immediate(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address))));
5648 }
5649}
5650
Vladimir Markode91ca92020-10-27 13:41:40 +00005651void CodeGeneratorX86::LoadIntrinsicDeclaringClass(Register reg, HInvokeStaticOrDirect* invoke) {
5652 DCHECK_NE(invoke->GetIntrinsic(), Intrinsics::kNone);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005653 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005654 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
Vladimir Marko6fd16062018-06-26 11:02:04 +01005655 HX86ComputeBaseMethodAddress* method_address =
5656 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5657 DCHECK(method_address != nullptr);
5658 Register method_address_reg =
5659 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Markode91ca92020-10-27 13:41:40 +00005660 __ leal(reg, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005661 MethodReference target_method = invoke->GetResolvedMethodReference();
Vladimir Marko6fd16062018-06-26 11:02:04 +01005662 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
5663 boot_image_type_patches_.emplace_back(method_address, target_method.dex_file, type_idx.index_);
5664 __ Bind(&boot_image_type_patches_.back().label);
5665 } else {
Vladimir Markode91ca92020-10-27 13:41:40 +00005666 uint32_t boot_image_offset = GetBootImageOffsetOfIntrinsicDeclaringClass(invoke);
5667 LoadBootImageAddress(reg, boot_image_offset, invoke);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005668 }
Vladimir Marko6fd16062018-06-26 11:02:04 +01005669}
5670
Vladimir Markoaad75c62016-10-03 08:46:48 +00005671// The label points to the end of the "movl" or another instruction but the literal offset
5672// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
5673constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
5674
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005675template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005676inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005677 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005678 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005679 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005680 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005681 linker_patches->push_back(Factory(literal_offset,
5682 info.target_dex_file,
5683 GetMethodAddressOffset(info.method_address),
5684 info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005685 }
5686}
5687
Vladimir Marko6fd16062018-06-26 11:02:04 +01005688template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
5689linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
5690 const DexFile* target_dex_file,
5691 uint32_t pc_insn_offset,
5692 uint32_t boot_image_offset) {
5693 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5694 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005695}
5696
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005697void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005698 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005699 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01005700 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005701 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00005702 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01005703 type_bss_entry_patches_.size() +
Vladimir Marko8f63f102020-09-28 12:10:28 +01005704 public_type_bss_entry_patches_.size() +
5705 package_type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005706 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01005707 string_bss_entry_patches_.size() +
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005708 boot_image_jni_entrypoint_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01005709 boot_image_other_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005710 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01005711 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005712 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
5713 boot_image_method_patches_, linker_patches);
5714 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
5715 boot_image_type_patches_, linker_patches);
5716 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005717 boot_image_string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005718 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005719 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005720 DCHECK(boot_image_type_patches_.empty());
5721 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01005722 }
5723 if (GetCompilerOptions().IsBootImage()) {
5724 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5725 boot_image_other_patches_, linker_patches);
5726 } else {
5727 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
5728 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005729 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005730 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5731 method_bss_entry_patches_, linker_patches);
5732 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5733 type_bss_entry_patches_, linker_patches);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005734 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PublicTypeBssEntryPatch>(
5735 public_type_bss_entry_patches_, linker_patches);
5736 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PackageTypeBssEntryPatch>(
5737 package_type_bss_entry_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005738 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5739 string_bss_entry_patches_, linker_patches);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005740 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeJniEntrypointPatch>(
5741 boot_image_jni_entrypoint_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005742 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005743}
5744
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00005745void CodeGeneratorX86::MarkGCCard(
5746 Register temp, Register card, Register object, Register value, bool emit_null_check) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005747 NearLabel is_null;
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00005748 if (emit_null_check) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005749 __ testl(value, value);
5750 __ j(kEqual, &is_null);
5751 }
Roland Levillainc73f0522018-08-14 15:16:50 +01005752 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07005753 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01005754 // Calculate the offset (in the card table) of the card corresponding to
5755 // `object`.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005756 __ movl(temp, object);
5757 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01005758 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
5759 // `object`'s card.
5760 //
5761 // Register `card` contains the address of the card table. Note that the card
5762 // table's base is biased during its creation so that it always starts at an
5763 // address whose least-significant byte is equal to `kCardDirty` (see
5764 // art::gc::accounting::CardTable::Create). Therefore the MOVB instruction
5765 // below writes the `kCardDirty` (byte) value into the `object`'s card
5766 // (located at `card + object >> kCardShift`).
5767 //
5768 // This dual use of the value in register `card` (1. to calculate the location
5769 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
5770 // (no need to explicitly load `kCardDirty` as an immediate value).
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00005771 __ movb(Address(temp, card, TIMES_1, 0),
5772 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00005773 if (emit_null_check) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005774 __ Bind(&is_null);
5775 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005776}
5777
Calin Juravle52c48962014-12-16 17:02:57 +00005778void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Alex Light3a73ffb2021-01-25 14:11:05 +00005779 DCHECK(instruction->IsInstanceFieldGet() ||
5780 instruction->IsStaticFieldGet() ||
5781 instruction->IsPredicatedInstanceFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005782
5783 bool object_field_get_with_read_barrier =
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00005784 gUseReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alex Light3a73ffb2021-01-25 14:11:05 +00005785 bool is_predicated = instruction->IsPredicatedInstanceFieldGet();
Nicolas Geoffray39468442014-09-02 15:17:15 +01005786 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005787 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00005788 gUseReadBarrier
Vladimir Markoca6fff82017-10-03 14:49:14 +01005789 ? LocationSummary::kCallOnSlowPath
5790 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005791 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005792 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005793 }
Alex Light3a73ffb2021-01-25 14:11:05 +00005794 // receiver_input
5795 locations->SetInAt(is_predicated ? 1 : 0, Location::RequiresRegister());
5796 if (is_predicated) {
5797 if (DataType::IsFloatingPointType(instruction->GetType())) {
5798 locations->SetInAt(0, Location::RequiresFpuRegister());
5799 } else {
5800 locations->SetInAt(0, Location::RequiresRegister());
5801 }
5802 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005803 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alex Light3a73ffb2021-01-25 14:11:05 +00005804 locations->SetOut(is_predicated ? Location::SameAsFirstInput()
5805 : Location::RequiresFpuRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005806 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005807 // The output overlaps in case of long: we don't want the low move
5808 // to overwrite the object's location. Likewise, in the case of
5809 // an object field get with read barriers enabled, we do not want
5810 // the move to overwrite the object's location, as we need it to emit
5811 // the read barrier.
Alex Light3a73ffb2021-01-25 14:11:05 +00005812 locations->SetOut(is_predicated ? Location::SameAsFirstInput() : Location::RequiresRegister(),
5813 (object_field_get_with_read_barrier ||
5814 instruction->GetType() == DataType::Type::kInt64 ||
5815 is_predicated)
5816 ? Location::kOutputOverlap
5817 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005818 }
Calin Juravle52c48962014-12-16 17:02:57 +00005819
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005820 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00005821 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005822 // So we use an XMM register as a temp to achieve atomicity (first
5823 // load the temp into the XMM and then copy the XMM into the
5824 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00005825 locations->AddTemp(Location::RequiresFpuRegister());
5826 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005827}
5828
Calin Juravle52c48962014-12-16 17:02:57 +00005829void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
5830 const FieldInfo& field_info) {
Alex Light3a73ffb2021-01-25 14:11:05 +00005831 DCHECK(instruction->IsInstanceFieldGet() ||
5832 instruction->IsStaticFieldGet() ||
5833 instruction->IsPredicatedInstanceFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005834
Calin Juravle52c48962014-12-16 17:02:57 +00005835 LocationSummary* locations = instruction->GetLocations();
Alex Light3a73ffb2021-01-25 14:11:05 +00005836 Location base_loc = locations->InAt(instruction->IsPredicatedInstanceFieldGet() ? 1 : 0);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005837 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005838 Location out = locations->Out();
5839 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01005840 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5841 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00005842 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5843
Ulya Trafimovich322eced2021-06-02 15:39:36 +01005844 if (load_type == DataType::Type::kReference) {
5845 // /* HeapReference<Object> */ out = *(base + offset)
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00005846 if (gUseReadBarrier && kUseBakerReadBarrier) {
Ulya Trafimovich322eced2021-06-02 15:39:36 +01005847 // Note that a potential implicit null check is handled in this
5848 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
5849 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5850 instruction, out, base, offset, /* needs_null_check= */ true);
Calin Juravle52c48962014-12-16 17:02:57 +00005851 if (is_volatile) {
Ulya Trafimovich322eced2021-06-02 15:39:36 +01005852 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005853 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00005854 } else {
Ulya Trafimovich322eced2021-06-02 15:39:36 +01005855 __ movl(out.AsRegister<Register>(), Address(base, offset));
5856 codegen_->MaybeRecordImplicitNullCheck(instruction);
5857 if (is_volatile) {
5858 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5859 }
5860 // If read barriers are enabled, emit read barriers other than
5861 // Baker's using a slow path (and also unpoison the loaded
5862 // reference, if heap poisoning is enabled).
5863 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5864 }
5865 } else {
5866 Address src(base, offset);
5867 XmmRegister temp = (load_type == DataType::Type::kInt64 && is_volatile)
5868 ? locations->GetTemp(0).AsFpuRegister<XmmRegister>()
5869 : kNoXmmRegister;
5870 codegen_->LoadFromMemoryNoBarrier(load_type, out, src, instruction, temp, is_volatile);
5871 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005872 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5873 }
Roland Levillain4d027112015-07-01 15:41:14 +01005874 }
Calin Juravle52c48962014-12-16 17:02:57 +00005875}
5876
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00005877void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction,
5878 const FieldInfo& field_info,
5879 WriteBarrierKind write_barrier_kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00005880 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5881
5882 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005883 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00005884 locations->SetInAt(0, Location::RequiresRegister());
5885 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005886 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005887 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00005888
5889 // The register allocator does not support multiple
5890 // inputs that die at entry with one in a specific register.
5891 if (is_byte_type) {
5892 // Ensure the value is in a byte register.
5893 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005894 } else if (DataType::IsFloatingPointType(field_type)) {
5895 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05005896 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5897 locations->SetInAt(1, Location::RequiresFpuRegister());
5898 } else {
5899 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
5900 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005901 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05005902 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00005903 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005904
Calin Juravle52c48962014-12-16 17:02:57 +00005905 // 64bits value can be atomically written to an address with movsd and an XMM register.
5906 // We need two XMM registers because there's no easier way to (bit) copy a register pair
5907 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
5908 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
5909 // isolated cases when we need this it isn't worth adding the extra complexity.
5910 locations->AddTemp(Location::RequiresFpuRegister());
5911 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005912 } else {
5913 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5914
5915 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00005916 if (write_barrier_kind != WriteBarrierKind::kDontEmit) {
5917 locations->AddTemp(Location::RequiresRegister());
5918 // Ensure the card is in a byte register.
5919 locations->AddTemp(Location::RegisterLocation(ECX));
5920 } else if (kPoisonHeapReferences) {
5921 locations->AddTemp(Location::RequiresRegister());
5922 }
Mark Mendell81489372015-11-04 11:30:41 -05005923 }
Calin Juravle52c48962014-12-16 17:02:57 +00005924 }
5925}
5926
5927void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Andra Danciucde98192020-09-13 12:32:09 +00005928 uint32_t value_index,
5929 DataType::Type field_type,
5930 Address field_addr,
5931 Register base,
5932 bool is_volatile,
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00005933 bool value_can_be_null,
5934 WriteBarrierKind write_barrier_kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00005935 LocationSummary* locations = instruction->GetLocations();
Andra Danciucde98192020-09-13 12:32:09 +00005936 Location value = locations->InAt(value_index);
Roland Levillain4d027112015-07-01 15:41:14 +01005937 bool needs_write_barrier =
Andra Danciucde98192020-09-13 12:32:09 +00005938 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(value_index));
Calin Juravle52c48962014-12-16 17:02:57 +00005939
5940 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005941 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005942 }
5943
Mark Mendell81489372015-11-04 11:30:41 -05005944 bool maybe_record_implicit_null_check_done = false;
5945
Calin Juravle52c48962014-12-16 17:02:57 +00005946 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005947 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005948 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005949 case DataType::Type::kInt8: {
Andra Danciucde98192020-09-13 12:32:09 +00005950 if (value.IsConstant()) {
5951 __ movb(field_addr, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
5952 } else {
5953 __ movb(field_addr, value.AsRegister<ByteRegister>());
5954 }
Calin Juravle52c48962014-12-16 17:02:57 +00005955 break;
5956 }
5957
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005958 case DataType::Type::kUint16:
5959 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05005960 if (value.IsConstant()) {
Andra Danciucde98192020-09-13 12:32:09 +00005961 __ movw(field_addr, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05005962 } else {
Andra Danciucde98192020-09-13 12:32:09 +00005963 __ movw(field_addr, value.AsRegister<Register>());
Mark Mendell81489372015-11-04 11:30:41 -05005964 }
Calin Juravle52c48962014-12-16 17:02:57 +00005965 break;
5966 }
5967
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005968 case DataType::Type::kInt32:
5969 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01005970 if (kPoisonHeapReferences && needs_write_barrier) {
5971 // Note that in the case where `value` is a null reference,
5972 // we do not enter this block, as the reference does not
5973 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005974 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01005975 Register temp = locations->GetTemp(0).AsRegister<Register>();
5976 __ movl(temp, value.AsRegister<Register>());
5977 __ PoisonHeapReference(temp);
Andra Danciucde98192020-09-13 12:32:09 +00005978 __ movl(field_addr, temp);
Mark Mendell81489372015-11-04 11:30:41 -05005979 } else if (value.IsConstant()) {
5980 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Andra Danciucde98192020-09-13 12:32:09 +00005981 __ movl(field_addr, Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005982 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005983 DCHECK(value.IsRegister()) << value;
Andra Danciucde98192020-09-13 12:32:09 +00005984 __ movl(field_addr, value.AsRegister<Register>());
Roland Levillain4d027112015-07-01 15:41:14 +01005985 }
Calin Juravle52c48962014-12-16 17:02:57 +00005986 break;
5987 }
5988
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005989 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005990 if (is_volatile) {
5991 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5992 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5993 __ movd(temp1, value.AsRegisterPairLow<Register>());
5994 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5995 __ punpckldq(temp1, temp2);
Andra Danciucde98192020-09-13 12:32:09 +00005996 __ movsd(field_addr, temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005997 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005998 } else if (value.IsConstant()) {
5999 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
Andra Danciucde98192020-09-13 12:32:09 +00006000 __ movl(field_addr, Immediate(Low32Bits(v)));
Mark Mendell81489372015-11-04 11:30:41 -05006001 codegen_->MaybeRecordImplicitNullCheck(instruction);
Ulya Trafimovich893e2ed2021-06-10 16:18:12 +01006002 __ movl(Address::displace(field_addr, kX86WordSize), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00006003 } else {
Andra Danciucde98192020-09-13 12:32:09 +00006004 __ movl(field_addr, value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00006005 codegen_->MaybeRecordImplicitNullCheck(instruction);
Ulya Trafimovich893e2ed2021-06-10 16:18:12 +01006006 __ movl(Address::displace(field_addr, kX86WordSize), value.AsRegisterPairHigh<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00006007 }
Mark Mendell81489372015-11-04 11:30:41 -05006008 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00006009 break;
6010 }
6011
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006012 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05006013 if (value.IsConstant()) {
6014 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Andra Danciucde98192020-09-13 12:32:09 +00006015 __ movl(field_addr, Immediate(v));
Mark Mendell81489372015-11-04 11:30:41 -05006016 } else {
Andra Danciucde98192020-09-13 12:32:09 +00006017 __ movss(field_addr, value.AsFpuRegister<XmmRegister>());
Mark Mendell81489372015-11-04 11:30:41 -05006018 }
Calin Juravle52c48962014-12-16 17:02:57 +00006019 break;
6020 }
6021
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006022 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05006023 if (value.IsConstant()) {
Andra Danciuc992e422020-09-16 08:12:02 +00006024 DCHECK(!is_volatile);
Mark Mendell81489372015-11-04 11:30:41 -05006025 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
Andra Danciucde98192020-09-13 12:32:09 +00006026 __ movl(field_addr, Immediate(Low32Bits(v)));
Mark Mendell81489372015-11-04 11:30:41 -05006027 codegen_->MaybeRecordImplicitNullCheck(instruction);
Ulya Trafimovich893e2ed2021-06-10 16:18:12 +01006028 __ movl(Address::displace(field_addr, kX86WordSize), Immediate(High32Bits(v)));
Mark Mendell81489372015-11-04 11:30:41 -05006029 maybe_record_implicit_null_check_done = true;
6030 } else {
Andra Danciucde98192020-09-13 12:32:09 +00006031 __ movsd(field_addr, value.AsFpuRegister<XmmRegister>());
Mark Mendell81489372015-11-04 11:30:41 -05006032 }
Calin Juravle52c48962014-12-16 17:02:57 +00006033 break;
6034 }
6035
Aart Bik66c158e2018-01-31 12:55:04 -08006036 case DataType::Type::kUint32:
6037 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006038 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00006039 LOG(FATAL) << "Unreachable type " << field_type;
6040 UNREACHABLE();
6041 }
6042
Mark Mendell81489372015-11-04 11:30:41 -05006043 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00006044 codegen_->MaybeRecordImplicitNullCheck(instruction);
6045 }
6046
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00006047 if (needs_write_barrier && write_barrier_kind != WriteBarrierKind::kDontEmit) {
Calin Juravle77520bc2015-01-12 18:45:46 +00006048 Register temp = locations->GetTemp(0).AsRegister<Register>();
6049 Register card = locations->GetTemp(1).AsRegister<Register>();
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00006050 codegen_->MarkGCCard(
6051 temp,
6052 card,
6053 base,
6054 value.AsRegister<Register>(),
6055 value_can_be_null && write_barrier_kind == WriteBarrierKind::kEmitWithNullCheck);
Calin Juravle77520bc2015-01-12 18:45:46 +00006056 }
6057
Calin Juravle52c48962014-12-16 17:02:57 +00006058 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006059 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00006060 }
6061}
6062
Andra Danciucde98192020-09-13 12:32:09 +00006063void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
6064 const FieldInfo& field_info,
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00006065 bool value_can_be_null,
6066 WriteBarrierKind write_barrier_kind) {
Andra Danciucde98192020-09-13 12:32:09 +00006067 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
6068
6069 LocationSummary* locations = instruction->GetLocations();
6070 Register base = locations->InAt(0).AsRegister<Register>();
6071 bool is_volatile = field_info.IsVolatile();
6072 DataType::Type field_type = field_info.GetFieldType();
6073 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alex Light3a73ffb2021-01-25 14:11:05 +00006074 bool is_predicated =
6075 instruction->IsInstanceFieldSet() && instruction->AsInstanceFieldSet()->GetIsPredicatedSet();
Andra Danciucde98192020-09-13 12:32:09 +00006076
6077 Address field_addr(base, offset);
6078
Alex Light3a73ffb2021-01-25 14:11:05 +00006079 NearLabel pred_is_null;
6080 if (is_predicated) {
6081 __ testl(base, base);
6082 __ j(kEqual, &pred_is_null);
6083 }
6084
Andra Danciucde98192020-09-13 12:32:09 +00006085 HandleFieldSet(instruction,
6086 /* value_index= */ 1,
6087 field_type,
6088 field_addr,
6089 base,
6090 is_volatile,
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00006091 value_can_be_null,
6092 write_barrier_kind);
Alex Light3a73ffb2021-01-25 14:11:05 +00006093
6094 if (is_predicated) {
6095 __ Bind(&pred_is_null);
6096 }
Andra Danciucde98192020-09-13 12:32:09 +00006097}
6098
Calin Juravle52c48962014-12-16 17:02:57 +00006099void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6100 HandleFieldGet(instruction, instruction->GetFieldInfo());
6101}
6102
6103void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6104 HandleFieldGet(instruction, instruction->GetFieldInfo());
6105}
6106
6107void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00006108 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetWriteBarrierKind());
Calin Juravle52c48962014-12-16 17:02:57 +00006109}
6110
6111void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00006112 HandleFieldSet(instruction,
6113 instruction->GetFieldInfo(),
6114 instruction->GetValueCanBeNull(),
6115 instruction->GetWriteBarrierKind());
Calin Juravle52c48962014-12-16 17:02:57 +00006116}
6117
6118void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00006119 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetWriteBarrierKind());
Calin Juravle52c48962014-12-16 17:02:57 +00006120}
6121
6122void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00006123 HandleFieldSet(instruction,
6124 instruction->GetFieldInfo(),
6125 instruction->GetValueCanBeNull(),
6126 instruction->GetWriteBarrierKind());
Calin Juravle52c48962014-12-16 17:02:57 +00006127}
6128
Alex Light3a73ffb2021-01-25 14:11:05 +00006129void LocationsBuilderX86::VisitPredicatedInstanceFieldGet(
6130 HPredicatedInstanceFieldGet* instruction) {
6131 HandleFieldGet(instruction, instruction->GetFieldInfo());
6132}
6133
Calin Juravle52c48962014-12-16 17:02:57 +00006134void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6135 HandleFieldGet(instruction, instruction->GetFieldInfo());
6136}
6137
Alex Light3a73ffb2021-01-25 14:11:05 +00006138void InstructionCodeGeneratorX86::VisitPredicatedInstanceFieldGet(
6139 HPredicatedInstanceFieldGet* instruction) {
6140 NearLabel finish;
6141 LocationSummary* locations = instruction->GetLocations();
6142 Register recv = locations->InAt(1).AsRegister<Register>();
6143 __ testl(recv, recv);
6144 __ j(kZero, &finish);
6145 HandleFieldGet(instruction, instruction->GetFieldInfo());
6146 __ Bind(&finish);
6147}
Calin Juravle52c48962014-12-16 17:02:57 +00006148void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6149 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006150}
6151
Vladimir Marko552a1342017-10-31 10:56:47 +00006152void LocationsBuilderX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6153 codegen_->CreateStringBuilderAppendLocations(instruction, Location::RegisterLocation(EAX));
6154}
6155
6156void InstructionCodeGeneratorX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6157 __ movl(EAX, Immediate(instruction->GetFormat()->GetValue()));
6158 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
6159}
6160
Calin Juravlee460d1d2015-09-29 04:52:17 +01006161void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
6162 HUnresolvedInstanceFieldGet* instruction) {
6163 FieldAccessCallingConventionX86 calling_convention;
6164 codegen_->CreateUnresolvedFieldLocationSummary(
6165 instruction, instruction->GetFieldType(), calling_convention);
6166}
6167
6168void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
6169 HUnresolvedInstanceFieldGet* instruction) {
6170 FieldAccessCallingConventionX86 calling_convention;
6171 codegen_->GenerateUnresolvedFieldAccess(instruction,
6172 instruction->GetFieldType(),
6173 instruction->GetFieldIndex(),
6174 instruction->GetDexPc(),
6175 calling_convention);
6176}
6177
6178void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
6179 HUnresolvedInstanceFieldSet* instruction) {
6180 FieldAccessCallingConventionX86 calling_convention;
6181 codegen_->CreateUnresolvedFieldLocationSummary(
6182 instruction, instruction->GetFieldType(), calling_convention);
6183}
6184
6185void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
6186 HUnresolvedInstanceFieldSet* instruction) {
6187 FieldAccessCallingConventionX86 calling_convention;
6188 codegen_->GenerateUnresolvedFieldAccess(instruction,
6189 instruction->GetFieldType(),
6190 instruction->GetFieldIndex(),
6191 instruction->GetDexPc(),
6192 calling_convention);
6193}
6194
6195void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
6196 HUnresolvedStaticFieldGet* instruction) {
6197 FieldAccessCallingConventionX86 calling_convention;
6198 codegen_->CreateUnresolvedFieldLocationSummary(
6199 instruction, instruction->GetFieldType(), calling_convention);
6200}
6201
6202void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
6203 HUnresolvedStaticFieldGet* instruction) {
6204 FieldAccessCallingConventionX86 calling_convention;
6205 codegen_->GenerateUnresolvedFieldAccess(instruction,
6206 instruction->GetFieldType(),
6207 instruction->GetFieldIndex(),
6208 instruction->GetDexPc(),
6209 calling_convention);
6210}
6211
6212void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
6213 HUnresolvedStaticFieldSet* instruction) {
6214 FieldAccessCallingConventionX86 calling_convention;
6215 codegen_->CreateUnresolvedFieldLocationSummary(
6216 instruction, instruction->GetFieldType(), calling_convention);
6217}
6218
6219void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
6220 HUnresolvedStaticFieldSet* instruction) {
6221 FieldAccessCallingConventionX86 calling_convention;
6222 codegen_->GenerateUnresolvedFieldAccess(instruction,
6223 instruction->GetFieldType(),
6224 instruction->GetFieldIndex(),
6225 instruction->GetDexPc(),
6226 calling_convention);
6227}
6228
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006229void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006230 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6231 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
6232 ? Location::RequiresRegister()
6233 : Location::Any();
6234 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006235}
6236
Calin Juravle2ae48182016-03-16 14:05:09 +00006237void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
6238 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00006239 return;
6240 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006241 LocationSummary* locations = instruction->GetLocations();
6242 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00006243
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006244 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00006245 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006246}
6247
Calin Juravle2ae48182016-03-16 14:05:09 +00006248void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006249 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006250 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006251
6252 LocationSummary* locations = instruction->GetLocations();
6253 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006254
6255 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04006256 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006257 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006258 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006259 } else {
6260 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00006261 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006262 __ jmp(slow_path->GetEntryLabel());
6263 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006264 }
6265 __ j(kEqual, slow_path->GetEntryLabel());
6266}
6267
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006268void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006269 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006270}
6271
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006272void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006273 bool object_array_get_with_read_barrier =
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006274 gUseReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006275 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006276 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
6277 object_array_get_with_read_barrier
6278 ? LocationSummary::kCallOnSlowPath
6279 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01006280 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006281 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006282 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006283 locations->SetInAt(0, Location::RequiresRegister());
6284 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006285 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01006286 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6287 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006288 // The output overlaps in case of long: we don't want the low move
6289 // to overwrite the array's location. Likewise, in the case of an
6290 // object array get with read barriers enabled, we do not want the
6291 // move to overwrite the array's location, as we need it to emit
6292 // the read barrier.
6293 locations->SetOut(
6294 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006295 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
6296 ? Location::kOutputOverlap
6297 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01006298 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006299}
6300
6301void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
6302 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006303 Location obj_loc = locations->InAt(0);
6304 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006305 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006306 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006307 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006308
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006309 DataType::Type type = instruction->GetType();
Ulya Trafimovich322eced2021-06-02 15:39:36 +01006310 if (type == DataType::Type::kReference) {
6311 static_assert(
6312 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6313 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6314 // /* HeapReference<Object> */ out =
6315 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00006316 if (gUseReadBarrier && kUseBakerReadBarrier) {
Ulya Trafimovich322eced2021-06-02 15:39:36 +01006317 // Note that a potential implicit null check is handled in this
6318 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
6319 codegen_->GenerateArrayLoadWithBakerReadBarrier(
6320 instruction, out_loc, obj, data_offset, index, /* needs_null_check= */ true);
6321 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006322 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006323 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006324 codegen_->MaybeRecordImplicitNullCheck(instruction);
Ulya Trafimovich322eced2021-06-02 15:39:36 +01006325 // If read barriers are enabled, emit read barriers other than
6326 // Baker's using a slow path (and also unpoison the loaded
6327 // reference, if heap poisoning is enabled).
6328 if (index.IsConstant()) {
6329 uint32_t offset =
6330 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
6331 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6332 } else {
6333 codegen_->MaybeGenerateReadBarrierSlow(
6334 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6335 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006336 }
Ulya Trafimovich322eced2021-06-02 15:39:36 +01006337 } else if (type == DataType::Type::kUint16
6338 && mirror::kUseStringCompression
6339 && instruction->IsStringCharAt()) {
6340 // Branch cases into compressed and uncompressed for each index's type.
6341 Register out = out_loc.AsRegister<Register>();
6342 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
6343 NearLabel done, not_compressed;
6344 __ testb(Address(obj, count_offset), Immediate(1));
Calin Juravle77520bc2015-01-12 18:45:46 +00006345 codegen_->MaybeRecordImplicitNullCheck(instruction);
Ulya Trafimovich322eced2021-06-02 15:39:36 +01006346 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6347 "Expecting 0=compressed, 1=uncompressed");
6348 __ j(kNotZero, &not_compressed);
6349 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
6350 __ jmp(&done);
6351 __ Bind(&not_compressed);
6352 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
6353 __ Bind(&done);
6354 } else {
Ulya Trafimovichc8451cb2021-06-02 17:35:16 +01006355 ScaleFactor scale = CodeGenerator::ScaleFactorForType(type);
6356 Address src = CodeGeneratorX86::ArrayAddress(obj, index, scale, data_offset);
Ulya Trafimovich322eced2021-06-02 15:39:36 +01006357 codegen_->LoadFromMemoryNoBarrier(type, out_loc, src, instruction);
Calin Juravle77520bc2015-01-12 18:45:46 +00006358 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006359}
6360
6361void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006362 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006363
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006364 bool needs_write_barrier =
6365 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006366 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006367
Vladimir Markoca6fff82017-10-03 14:49:14 +01006368 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01006369 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006370 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006371
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006372 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006373 // We need the inputs to be different than the output in case of long operation.
6374 // In case of a byte operation, the register allocator does not support multiple
6375 // inputs that die at entry with one in a specific register.
6376 locations->SetInAt(0, Location::RequiresRegister());
6377 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6378 if (is_byte_type) {
6379 // Ensure the value is in a byte register.
6380 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006381 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05006382 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006383 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006384 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
6385 }
6386 if (needs_write_barrier) {
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00006387 // Used by reference poisoning or emitting write barrier.
6388 locations->AddTemp(Location::RequiresRegister());
6389 if (instruction->GetWriteBarrierKind() != WriteBarrierKind::kDontEmit) {
6390 // Only used when emitting a write barrier. Ensure the card is in a byte register.
6391 locations->AddTemp(Location::RegisterLocation(ECX));
6392 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006393 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006394}
6395
6396void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
6397 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006398 Location array_loc = locations->InAt(0);
6399 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006400 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006401 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006402 DataType::Type value_type = instruction->GetComponentType();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006403 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006404 bool needs_write_barrier =
6405 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006406
6407 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006408 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006409 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006410 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006411 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006412 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006413 if (value.IsRegister()) {
6414 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006415 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01006416 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006417 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006418 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006419 break;
6420 }
6421
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006422 case DataType::Type::kUint16:
6423 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006424 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006425 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006426 if (value.IsRegister()) {
6427 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006428 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01006429 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006430 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006431 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006432 break;
6433 }
6434
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006435 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006436 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006437 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006438
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006439 if (!value.IsRegister()) {
6440 // Just setting null.
6441 DCHECK(instruction->InputAt(2)->IsNullConstant());
6442 DCHECK(value.IsConstant()) << value;
6443 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00006444 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006445 DCHECK(!needs_write_barrier);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006446 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006447 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006448 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006449
6450 DCHECK(needs_write_barrier);
6451 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01006452 Location temp_loc = locations->GetTemp(0);
6453 Register temp = temp_loc.AsRegister<Register>();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006454
6455 bool can_value_be_null = instruction->GetValueCanBeNull();
6456 NearLabel do_store;
6457 if (can_value_be_null) {
6458 __ testl(register_value, register_value);
6459 __ j(kEqual, &do_store);
6460 }
6461
6462 SlowPathCode* slow_path = nullptr;
6463 if (needs_type_check) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006464 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006465 codegen_->AddSlowPath(slow_path);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006466
6467 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6468 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6469 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006470
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006471 // Note that when Baker read barriers are enabled, the type
6472 // checks are performed without read barriers. This is fine,
6473 // even in the case where a class object is in the from-space
6474 // after the flip, as a comparison involving such a type would
6475 // not produce a false positive; it may of course produce a
6476 // false negative, in which case we would take the ArraySet
6477 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01006478
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006479 // /* HeapReference<Class> */ temp = array->klass_
6480 __ movl(temp, Address(array, class_offset));
6481 codegen_->MaybeRecordImplicitNullCheck(instruction);
6482 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01006483
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006484 // /* HeapReference<Class> */ temp = temp->component_type_
6485 __ movl(temp, Address(temp, component_offset));
6486 // If heap poisoning is enabled, no need to unpoison `temp`
6487 // nor the object reference in `register_value->klass`, as
6488 // we are comparing two poisoned references.
6489 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01006490
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006491 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006492 NearLabel do_put;
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006493 __ j(kEqual, &do_put);
6494 // If heap poisoning is enabled, the `temp` reference has
6495 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006496 __ MaybeUnpoisonHeapReference(temp);
6497
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006498 // If heap poisoning is enabled, no need to unpoison the
6499 // heap reference loaded below, as it is only used for a
6500 // comparison with null.
6501 __ cmpl(Address(temp, super_offset), Immediate(0));
6502 __ j(kNotEqual, slow_path->GetEntryLabel());
6503 __ Bind(&do_put);
6504 } else {
6505 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006506 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006507 }
6508
Santiago Aboy Solanesd4229602023-01-03 16:20:50 +00006509 if (instruction->GetWriteBarrierKind() != WriteBarrierKind::kDontEmit) {
6510 DCHECK_EQ(instruction->GetWriteBarrierKind(), WriteBarrierKind::kEmitNoNullCheck)
6511 << " Already null checked so we shouldn't do it again.";
6512 Register card = locations->GetTemp(1).AsRegister<Register>();
6513 codegen_->MarkGCCard(temp,
6514 card,
6515 array,
6516 value.AsRegister<Register>(),
6517 /* emit_null_check= */ false);
6518 }
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006519
6520 if (can_value_be_null) {
6521 DCHECK(do_store.IsLinked());
6522 __ Bind(&do_store);
6523 }
6524
6525 Register source = register_value;
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006526 if (kPoisonHeapReferences) {
6527 __ movl(temp, register_value);
6528 __ PoisonHeapReference(temp);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006529 source = temp;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006530 }
6531
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006532 __ movl(address, source);
6533
6534 if (can_value_be_null || !needs_type_check) {
6535 codegen_->MaybeRecordImplicitNullCheck(instruction);
6536 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006537
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006538 if (slow_path != nullptr) {
6539 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006540 }
6541
6542 break;
6543 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006544
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006545 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006546 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006547 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006548 if (value.IsRegister()) {
6549 __ movl(address, value.AsRegister<Register>());
6550 } else {
6551 DCHECK(value.IsConstant()) << value;
6552 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
6553 __ movl(address, Immediate(v));
6554 }
6555 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006556 break;
6557 }
6558
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006559 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006560 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006561 if (value.IsRegisterPair()) {
6562 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6563 value.AsRegisterPairLow<Register>());
6564 codegen_->MaybeRecordImplicitNullCheck(instruction);
6565 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6566 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006567 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006568 DCHECK(value.IsConstant());
6569 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
6570 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6571 Immediate(Low32Bits(val)));
6572 codegen_->MaybeRecordImplicitNullCheck(instruction);
6573 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6574 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006575 }
6576 break;
6577 }
6578
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006579 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006580 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006581 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006582 if (value.IsFpuRegister()) {
6583 __ movss(address, value.AsFpuRegister<XmmRegister>());
6584 } else {
6585 DCHECK(value.IsConstant());
6586 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
6587 __ movl(address, Immediate(v));
6588 }
6589 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006590 break;
6591 }
6592
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006593 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006594 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006595 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006596 if (value.IsFpuRegister()) {
6597 __ movsd(address, value.AsFpuRegister<XmmRegister>());
6598 } else {
6599 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006600 Address address_hi =
6601 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05006602 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
6603 __ movl(address, Immediate(Low32Bits(v)));
6604 codegen_->MaybeRecordImplicitNullCheck(instruction);
6605 __ movl(address_hi, Immediate(High32Bits(v)));
6606 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006607 break;
6608 }
6609
Aart Bik66c158e2018-01-31 12:55:04 -08006610 case DataType::Type::kUint32:
6611 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006612 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006613 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07006614 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006615 }
6616}
6617
6618void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006619 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006620 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04006621 if (!instruction->IsEmittedAtUseSite()) {
6622 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6623 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006624}
6625
6626void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04006627 if (instruction->IsEmittedAtUseSite()) {
6628 return;
6629 }
6630
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006631 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006632 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006633 Register obj = locations->InAt(0).AsRegister<Register>();
6634 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006635 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00006636 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07006637 // Mask out most significant bit in case the array is String's array of char.
6638 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006639 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006640 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006641}
6642
6643void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006644 RegisterSet caller_saves = RegisterSet::Empty();
6645 InvokeRuntimeCallingConvention calling_convention;
6646 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6647 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6648 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05006649 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04006650 HInstruction* length = instruction->InputAt(1);
6651 if (!length->IsEmittedAtUseSite()) {
6652 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6653 }
jessicahandojo4877b792016-09-08 19:49:13 -07006654 // Need register to see array's length.
6655 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6656 locations->AddTemp(Location::RequiresRegister());
6657 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006658}
6659
6660void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07006661 const bool is_string_compressed_char_at =
6662 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006663 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05006664 Location index_loc = locations->InAt(0);
6665 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006666 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006667 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006668
Mark Mendell99dbd682015-04-22 16:18:52 -04006669 if (length_loc.IsConstant()) {
6670 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
6671 if (index_loc.IsConstant()) {
6672 // BCE will remove the bounds check if we are guarenteed to pass.
6673 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6674 if (index < 0 || index >= length) {
6675 codegen_->AddSlowPath(slow_path);
6676 __ jmp(slow_path->GetEntryLabel());
6677 } else {
6678 // Some optimization after BCE may have generated this, and we should not
6679 // generate a bounds check if it is a valid range.
6680 }
6681 return;
6682 }
6683
6684 // We have to reverse the jump condition because the length is the constant.
6685 Register index_reg = index_loc.AsRegister<Register>();
6686 __ cmpl(index_reg, Immediate(length));
6687 codegen_->AddSlowPath(slow_path);
6688 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006689 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04006690 HInstruction* array_length = instruction->InputAt(1);
6691 if (array_length->IsEmittedAtUseSite()) {
6692 // Address the length field in the array.
6693 DCHECK(array_length->IsArrayLength());
6694 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
6695 Location array_loc = array_length->GetLocations()->InAt(0);
6696 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07006697 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006698 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
6699 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07006700 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
6701 __ movl(length_reg, array_len);
6702 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006703 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006704 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04006705 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006706 // Checking bounds for general case:
6707 // Array of char or string's array with feature compression off.
6708 if (index_loc.IsConstant()) {
6709 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6710 __ cmpl(array_len, Immediate(value));
6711 } else {
6712 __ cmpl(array_len, index_loc.AsRegister<Register>());
6713 }
6714 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04006715 }
Mark Mendell99dbd682015-04-22 16:18:52 -04006716 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006717 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04006718 }
6719 codegen_->AddSlowPath(slow_path);
6720 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006721 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006722}
6723
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006724void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006725 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006726}
6727
6728void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006729 if (instruction->GetNext()->IsSuspendCheck() &&
6730 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6731 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6732 // The back edge will generate the suspend check.
6733 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6734 }
6735
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006736 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6737}
6738
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006739void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006740 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6741 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07006742 // In suspend check slow path, usually there are no caller-save registers at all.
6743 // If SIMD instructions are present, however, we force spilling all live SIMD
6744 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07006745 locations->SetCustomSlowPathCallerSaves(
6746 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006747}
6748
6749void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006750 HBasicBlock* block = instruction->GetBlock();
6751 if (block->GetLoopInformation() != nullptr) {
6752 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6753 // The back edge will generate the suspend check.
6754 return;
6755 }
6756 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6757 // The goto will generate the suspend check.
6758 return;
6759 }
6760 GenerateSuspendCheck(instruction, nullptr);
6761}
6762
6763void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
6764 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006765 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006766 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
6767 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006768 slow_path =
6769 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006770 instruction->SetSlowPath(slow_path);
6771 codegen_->AddSlowPath(slow_path);
6772 if (successor != nullptr) {
6773 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006774 }
6775 } else {
6776 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6777 }
6778
Vladimir Marko254a8582021-11-29 14:08:37 +00006779 __ fs()->testl(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
6780 Immediate(Thread::SuspendOrCheckpointRequestFlags()));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006781 if (successor == nullptr) {
Vladimir Marko254a8582021-11-29 14:08:37 +00006782 __ j(kNotZero, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006783 __ Bind(slow_path->GetReturnLabel());
6784 } else {
Vladimir Marko254a8582021-11-29 14:08:37 +00006785 __ j(kZero, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006786 __ jmp(slow_path->GetEntryLabel());
6787 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006788}
6789
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006790X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
6791 return codegen_->GetAssembler();
6792}
6793
Aart Bikcfe50bb2017-12-12 14:54:12 -08006794void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006795 ScratchRegisterScope ensure_scratch(
6796 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6797 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6798 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05006799
Aart Bikcfe50bb2017-12-12 14:54:12 -08006800 // Now that temp register is available (possibly spilled), move blocks of memory.
6801 for (int i = 0; i < number_of_words; i++) {
6802 __ movl(temp_reg, Address(ESP, src + stack_offset));
6803 __ movl(Address(ESP, dst + stack_offset), temp_reg);
6804 stack_offset += kX86WordSize;
6805 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006806}
6807
6808void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006809 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006810 Location source = move->GetSource();
6811 Location destination = move->GetDestination();
6812
6813 if (source.IsRegister()) {
6814 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006815 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006816 } else if (destination.IsFpuRegister()) {
6817 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006818 } else {
6819 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006820 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006821 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006822 } else if (source.IsRegisterPair()) {
Vladimir Marko86c87522020-05-11 16:55:55 +01006823 if (destination.IsRegisterPair()) {
6824 __ movl(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
6825 DCHECK_NE(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
6826 __ movl(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
6827 } else if (destination.IsFpuRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006828 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Vladimir Markodec78172020-06-19 15:31:23 +01006829 // Push the 2 source registers to the stack.
Vladimir Marko86c87522020-05-11 16:55:55 +01006830 __ pushl(source.AsRegisterPairHigh<Register>());
6831 __ cfi().AdjustCFAOffset(elem_size);
6832 __ pushl(source.AsRegisterPairLow<Register>());
6833 __ cfi().AdjustCFAOffset(elem_size);
6834 // Load the destination register.
David Brazdil74eb1b22015-12-14 11:44:01 +00006835 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
6836 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01006837 codegen_->DecreaseFrame(2 * elem_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01006838 } else {
6839 DCHECK(destination.IsDoubleStackSlot());
6840 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
6841 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
6842 source.AsRegisterPairHigh<Register>());
6843 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006844 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006845 if (destination.IsRegister()) {
6846 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
6847 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006848 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006849 } else if (destination.IsRegisterPair()) {
Vladimir Marko86c87522020-05-11 16:55:55 +01006850 size_t elem_size = DataType::Size(DataType::Type::kInt32);
6851 // Create stack space for 2 elements.
Vladimir Markodec78172020-06-19 15:31:23 +01006852 codegen_->IncreaseFrame(2 * elem_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01006853 // Store the source register.
6854 __ movsd(Address(ESP, 0), source.AsFpuRegister<XmmRegister>());
6855 // And pop the values into destination registers.
6856 __ popl(destination.AsRegisterPairLow<Register>());
6857 __ cfi().AdjustCFAOffset(-elem_size);
6858 __ popl(destination.AsRegisterPairHigh<Register>());
6859 __ cfi().AdjustCFAOffset(-elem_size);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006860 } else if (destination.IsStackSlot()) {
6861 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006862 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006863 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006864 } else {
6865 DCHECK(destination.IsSIMDStackSlot());
6866 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05006867 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006868 } else if (source.IsStackSlot()) {
6869 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006870 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006871 } else if (destination.IsFpuRegister()) {
6872 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006873 } else {
6874 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08006875 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006876 }
6877 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006878 if (destination.IsRegisterPair()) {
6879 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
6880 __ movl(destination.AsRegisterPairHigh<Register>(),
6881 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
6882 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006883 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6884 } else {
6885 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006886 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006887 }
Aart Bik5576f372017-03-23 16:17:37 -07006888 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006889 if (destination.IsFpuRegister()) {
6890 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6891 } else {
6892 DCHECK(destination.IsSIMDStackSlot());
6893 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6894 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006895 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006896 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006897 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006898 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006899 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006900 if (value == 0) {
6901 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
6902 } else {
6903 __ movl(destination.AsRegister<Register>(), Immediate(value));
6904 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006905 } else {
6906 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05006907 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006908 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006909 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006910 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006911 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006912 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006913 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006914 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6915 if (value == 0) {
6916 // Easy handling of 0.0.
6917 __ xorps(dest, dest);
6918 } else {
6919 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006920 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6921 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
6922 __ movl(temp, Immediate(value));
6923 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006924 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006925 } else {
6926 DCHECK(destination.IsStackSlot()) << destination;
6927 __ movl(Address(ESP, destination.GetStackIndex()), imm);
6928 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006929 } else if (constant->IsLongConstant()) {
6930 int64_t value = constant->AsLongConstant()->GetValue();
6931 int32_t low_value = Low32Bits(value);
6932 int32_t high_value = High32Bits(value);
6933 Immediate low(low_value);
6934 Immediate high(high_value);
6935 if (destination.IsDoubleStackSlot()) {
6936 __ movl(Address(ESP, destination.GetStackIndex()), low);
6937 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6938 } else {
6939 __ movl(destination.AsRegisterPairLow<Register>(), low);
6940 __ movl(destination.AsRegisterPairHigh<Register>(), high);
6941 }
6942 } else {
6943 DCHECK(constant->IsDoubleConstant());
6944 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006945 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006946 int32_t low_value = Low32Bits(value);
6947 int32_t high_value = High32Bits(value);
6948 Immediate low(low_value);
6949 Immediate high(high_value);
6950 if (destination.IsFpuRegister()) {
6951 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6952 if (value == 0) {
6953 // Easy handling of 0.0.
6954 __ xorpd(dest, dest);
6955 } else {
6956 __ pushl(high);
Vladimir Marko86c87522020-05-11 16:55:55 +01006957 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006958 __ pushl(low);
Vladimir Marko86c87522020-05-11 16:55:55 +01006959 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006960 __ movsd(dest, Address(ESP, 0));
Vladimir Markodec78172020-06-19 15:31:23 +01006961 codegen_->DecreaseFrame(8);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006962 }
6963 } else {
6964 DCHECK(destination.IsDoubleStackSlot()) << destination;
6965 __ movl(Address(ESP, destination.GetStackIndex()), low);
6966 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6967 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006968 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006969 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006970 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006971 }
6972}
6973
Mark Mendella5c19ce2015-04-01 12:51:05 -04006974void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006975 Register suggested_scratch = reg == EAX ? EBX : EAX;
6976 ScratchRegisterScope ensure_scratch(
6977 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
6978
6979 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6980 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
6981 __ movl(Address(ESP, mem + stack_offset), reg);
6982 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006983}
6984
Mark Mendell7c8d0092015-01-26 11:21:33 -05006985void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006986 ScratchRegisterScope ensure_scratch(
6987 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6988
6989 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6990 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6991 __ movl(temp_reg, Address(ESP, mem + stack_offset));
6992 __ movss(Address(ESP, mem + stack_offset), reg);
6993 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006994}
6995
Aart Bikcfe50bb2017-12-12 14:54:12 -08006996void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
6997 size_t extra_slot = 4 * kX86WordSize;
Vladimir Markodec78172020-06-19 15:31:23 +01006998 codegen_->IncreaseFrame(extra_slot);
Aart Bikcfe50bb2017-12-12 14:54:12 -08006999 __ movups(Address(ESP, 0), XmmRegister(reg));
7000 ExchangeMemory(0, mem + extra_slot, 4);
7001 __ movups(XmmRegister(reg), Address(ESP, 0));
Vladimir Markodec78172020-06-19 15:31:23 +01007002 codegen_->DecreaseFrame(extra_slot);
Aart Bikcfe50bb2017-12-12 14:54:12 -08007003}
7004
7005void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00007006 ScratchRegisterScope ensure_scratch1(
7007 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01007008
Guillaume Sancheze14590b2015-04-15 18:57:27 +00007009 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
7010 ScratchRegisterScope ensure_scratch2(
7011 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01007012
Guillaume Sancheze14590b2015-04-15 18:57:27 +00007013 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
7014 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08007015
7016 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
7017 for (int i = 0; i < number_of_words; i++) {
7018 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
7019 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
7020 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
7021 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
7022 stack_offset += kX86WordSize;
7023 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01007024}
7025
7026void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007027 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01007028 Location source = move->GetSource();
7029 Location destination = move->GetDestination();
7030
7031 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04007032 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
7033 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
7034 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
7035 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
7036 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01007037 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007038 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01007039 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007040 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01007041 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08007042 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05007043 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
7044 // Use XOR Swap algorithm to avoid a temporary.
7045 DCHECK_NE(source.reg(), destination.reg());
7046 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
7047 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
7048 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
7049 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
7050 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
7051 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
7052 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007053 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
7054 // Take advantage of the 16 bytes in the XMM register.
7055 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
7056 Address stack(ESP, destination.GetStackIndex());
7057 // Load the double into the high doubleword.
7058 __ movhpd(reg, stack);
7059
7060 // Store the low double into the destination.
7061 __ movsd(stack, reg);
7062
7063 // Move the high double to the low double.
7064 __ psrldq(reg, Immediate(8));
7065 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
7066 // Take advantage of the 16 bytes in the XMM register.
7067 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
7068 Address stack(ESP, source.GetStackIndex());
7069 // Load the double into the high doubleword.
7070 __ movhpd(reg, stack);
7071
7072 // Store the low double into the destination.
7073 __ movsd(stack, reg);
7074
7075 // Move the high double to the low double.
7076 __ psrldq(reg, Immediate(8));
7077 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08007078 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
7079 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
7080 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
7081 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
7082 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
7083 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
7084 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01007085 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05007086 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01007087 }
7088}
7089
7090void ParallelMoveResolverX86::SpillScratch(int reg) {
7091 __ pushl(static_cast<Register>(reg));
7092}
7093
7094void ParallelMoveResolverX86::RestoreScratch(int reg) {
7095 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007096}
7097
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007098HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
7099 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007100 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007101 case HLoadClass::LoadKind::kInvalid:
7102 LOG(FATAL) << "UNREACHABLE";
7103 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007104 case HLoadClass::LoadKind::kReferrersClass:
7105 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007106 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007107 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007108 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko8f63f102020-09-28 12:10:28 +01007109 case HLoadClass::LoadKind::kBssEntryPublic:
7110 case HLoadClass::LoadKind::kBssEntryPackage:
Vladimir Marko695348f2020-05-19 14:42:02 +01007111 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007112 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007113 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007114 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01007115 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007116 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007117 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007118 break;
7119 }
7120 return desired_class_load_kind;
7121}
7122
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007123void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007124 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007125 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007126 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00007127 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007128 cls,
7129 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00007130 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00007131 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007132 return;
7133 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01007134 DCHECK_EQ(cls->NeedsAccessCheck(),
7135 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
7136 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007137
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00007138 const bool requires_read_barrier = gUseReadBarrier && !cls->IsInBootImage();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007139 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007140 ? LocationSummary::kCallOnSlowPath
7141 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007142 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007143 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007144 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007145 }
7146
Vladimir Marko8f63f102020-09-28 12:10:28 +01007147 if (load_kind == HLoadClass::LoadKind::kReferrersClass || cls->HasPcRelativeLoadKind()) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007148 locations->SetInAt(0, Location::RequiresRegister());
7149 }
7150 locations->SetOut(Location::RequiresRegister());
Vladimir Marko8f63f102020-09-28 12:10:28 +01007151 if (call_kind == LocationSummary::kCallOnSlowPath && cls->HasPcRelativeLoadKind()) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00007152 if (!gUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007153 // Rely on the type resolution and/or initialization to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007154 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007155 } else {
7156 // For non-Baker read barrier we have a temp-clobbering call.
7157 }
7158 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007159}
7160
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007161Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01007162 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007163 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007164 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007165 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007166 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007167 PatchInfo<Label>* info = &jit_class_patches_.back();
7168 return &info->label;
7169}
7170
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007171// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7172// move.
7173void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007174 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007175 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007176 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01007177 return;
7178 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01007179 DCHECK_EQ(cls->NeedsAccessCheck(),
7180 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
7181 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Calin Juravle580b6092015-10-06 17:35:58 +01007182
Vladimir Marko41559982017-01-06 14:04:23 +00007183 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007184 Location out_loc = locations->Out();
7185 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007186
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007187 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007188 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7189 ? kWithoutReadBarrier
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00007190 : gCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00007191 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007192 case HLoadClass::LoadKind::kReferrersClass: {
7193 DCHECK(!cls->CanCallRuntime());
7194 DCHECK(!cls->MustGenerateClinitCheck());
7195 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7196 Register current_method = locations->InAt(0).AsRegister<Register>();
7197 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007198 cls,
7199 out_loc,
7200 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Andreas Gampe3db70682018-12-26 15:12:03 -08007201 /* fixup_label= */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007202 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007203 break;
7204 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007205 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007206 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7207 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007208 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007209 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007210 __ leal(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007211 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007212 break;
7213 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007214 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007215 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7216 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007217 __ movl(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007218 codegen_->RecordBootImageRelRoPatch(cls->InputAt(0)->AsX86ComputeBaseMethodAddress(),
Vladimir Markode91ca92020-10-27 13:41:40 +00007219 CodeGenerator::GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007220 break;
7221 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01007222 case HLoadClass::LoadKind::kBssEntry:
7223 case HLoadClass::LoadKind::kBssEntryPublic:
7224 case HLoadClass::LoadKind::kBssEntryPackage: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007225 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007226 Address address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007227 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
7228 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007229 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007230 generate_null_check = true;
7231 break;
7232 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007233 case HLoadClass::LoadKind::kJitBootImageAddress: {
7234 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7235 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
7236 DCHECK_NE(address, 0u);
7237 __ movl(out, Immediate(address));
7238 break;
7239 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007240 case HLoadClass::LoadKind::kJitTableAddress: {
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007241 Address address = Address::Absolute(CodeGeneratorX86::kPlaceholder32BitOffset);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007242 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007243 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007244 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00007245 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007246 break;
7247 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007248 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007249 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007250 LOG(FATAL) << "UNREACHABLE";
7251 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007252 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007253
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007254 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7255 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007256 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007257 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007258
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007259 if (generate_null_check) {
7260 __ testl(out, out);
7261 __ j(kEqual, slow_path->GetEntryLabel());
7262 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007263
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007264 if (cls->MustGenerateClinitCheck()) {
7265 GenerateClassInitializationCheck(slow_path, out);
7266 } else {
7267 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007268 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007269 }
7270}
7271
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007272void LocationsBuilderX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7273 InvokeRuntimeCallingConvention calling_convention;
7274 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7275 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
7276}
7277
7278void InstructionCodeGeneratorX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7279 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
7280}
7281
Orion Hodson18259d72018-04-12 11:18:23 +01007282void LocationsBuilderX86::VisitLoadMethodType(HLoadMethodType* load) {
7283 InvokeRuntimeCallingConvention calling_convention;
7284 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7285 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
7286}
7287
7288void InstructionCodeGeneratorX86::VisitLoadMethodType(HLoadMethodType* load) {
7289 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
7290}
7291
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007292void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
7293 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007294 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007295 locations->SetInAt(0, Location::RequiresRegister());
7296 if (check->HasUses()) {
7297 locations->SetOut(Location::SameAsFirstInput());
7298 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007299 // Rely on the type initialization to save everything we need.
7300 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007301}
7302
7303void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007304 // We assume the class to not be null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007305 SlowPathCode* slow_path =
7306 new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(check->GetLoadClass(), check);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007307 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00007308 GenerateClassInitializationCheck(slow_path,
7309 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007310}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007311
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007312void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07007313 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markobf121912019-06-04 13:49:05 +01007314 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_visibly_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00007315 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007316 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007317}
7318
Vladimir Marko175e7862018-03-27 09:03:13 +00007319void InstructionCodeGeneratorX86::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
7320 Register temp) {
7321 uint32_t path_to_root = check->GetBitstringPathToRoot();
7322 uint32_t mask = check->GetBitstringMask();
7323 DCHECK(IsPowerOfTwo(mask + 1));
7324 size_t mask_bits = WhichPowerOf2(mask + 1);
7325
7326 if (mask_bits == 16u) {
7327 // Compare the bitstring in memory.
7328 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
7329 } else {
7330 // /* uint32_t */ temp = temp->status_
7331 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
7332 // Compare the bitstring bits using SUB.
7333 __ subl(temp, Immediate(path_to_root));
7334 // Shift out bits that do not contribute to the comparison.
7335 __ shll(temp, Immediate(32u - mask_bits));
7336 }
7337}
7338
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007339HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
7340 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007341 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007342 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007343 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007344 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01007345 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007346 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007347 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007348 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01007349 DCHECK(GetCompilerOptions().IsJitCompiler());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007350 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007351 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007352 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007353 }
7354 return desired_string_load_kind;
7355}
7356
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007357void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007358 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007359 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007360 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007361 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007362 load_kind == HLoadString::LoadKind::kBootImageRelRo ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00007363 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007364 locations->SetInAt(0, Location::RequiresRegister());
7365 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007366 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007367 locations->SetOut(Location::RegisterLocation(EAX));
7368 } else {
7369 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007370 if (load_kind == HLoadString::LoadKind::kBssEntry) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00007371 if (!gUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007372 // Rely on the pResolveString to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007373 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007374 } else {
7375 // For non-Baker read barrier we have a temp-clobbering call.
7376 }
7377 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007378 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007379}
7380
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007381Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01007382 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007383 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007384 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007385 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007386 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007387 PatchInfo<Label>* info = &jit_string_patches_.back();
7388 return &info->label;
7389}
7390
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007391// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7392// move.
7393void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01007394 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007395 Location out_loc = locations->Out();
7396 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007397
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007398 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007399 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007400 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7401 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007402 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007403 __ leal(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007404 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007405 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007406 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007407 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007408 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7409 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007410 __ movl(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007411 codegen_->RecordBootImageRelRoPatch(load->InputAt(0)->AsX86ComputeBaseMethodAddress(),
Vladimir Markode91ca92020-10-27 13:41:40 +00007412 CodeGenerator::GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007413 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007414 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007415 case HLoadString::LoadKind::kBssEntry: {
7416 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007417 Address address = Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007418 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007419 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00007420 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, gCompilerReadBarrierOption);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007421 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007422 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007423 codegen_->AddSlowPath(slow_path);
7424 __ testl(out, out);
7425 __ j(kEqual, slow_path->GetEntryLabel());
7426 __ Bind(slow_path->GetExitLabel());
7427 return;
7428 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007429 case HLoadString::LoadKind::kJitBootImageAddress: {
7430 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
7431 DCHECK_NE(address, 0u);
7432 __ movl(out, Immediate(address));
7433 return;
7434 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007435 case HLoadString::LoadKind::kJitTableAddress: {
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007436 Address address = Address::Absolute(CodeGeneratorX86::kPlaceholder32BitOffset);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007437 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007438 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007439 // /* GcRoot<mirror::String> */ out = *address
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00007440 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, gCompilerReadBarrierOption);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007441 return;
7442 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007443 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007444 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007445 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007446
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007447 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007448 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007449 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007450 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007451 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7452 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007453}
7454
David Brazdilcb1c0552015-08-04 16:22:25 +01007455static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007456 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01007457}
7458
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007459void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
7460 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007461 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007462 locations->SetOut(Location::RequiresRegister());
7463}
7464
7465void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01007466 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
7467}
7468
7469void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007470 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01007471}
7472
7473void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7474 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007475}
7476
7477void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007478 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7479 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007480 InvokeRuntimeCallingConvention calling_convention;
7481 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7482}
7483
7484void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007485 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007486 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007487}
7488
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007489// Temp is used for read barrier.
7490static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00007491 if (gUseReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00007492 !kUseBakerReadBarrier &&
7493 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00007494 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007495 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7496 return 1;
7497 }
7498 return 0;
7499}
7500
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007501// Interface case has 2 temps, one for holding the number of interfaces, one for the current
7502// interface pointer, the current interface is compared in memory.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007503// The other checks have one temp for loading the object's class.
7504static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007505 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007506 return 2;
7507 }
7508 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007509}
7510
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007511void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007512 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007513 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01007514 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007515 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007516 case TypeCheckKind::kExactCheck:
7517 case TypeCheckKind::kAbstractClassCheck:
7518 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007519 case TypeCheckKind::kArrayObjectCheck: {
7520 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7521 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7522 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007523 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007524 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007525 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007526 case TypeCheckKind::kUnresolvedCheck:
7527 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007528 call_kind = LocationSummary::kCallOnSlowPath;
7529 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007530 case TypeCheckKind::kBitstringCheck:
7531 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007532 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007533
Vladimir Markoca6fff82017-10-03 14:49:14 +01007534 LocationSummary* locations =
7535 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01007536 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007537 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007538 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007539 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007540 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7541 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7542 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7543 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7544 } else {
7545 locations->SetInAt(1, Location::Any());
7546 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007547 // Note that TypeCheckSlowPathX86 uses this "out" register too.
7548 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007549 // When read barriers are enabled, we need a temporary register for some cases.
7550 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007551}
7552
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007553void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007554 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007555 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007556 Location obj_loc = locations->InAt(0);
7557 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007558 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007559 Location out_loc = locations->Out();
7560 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007561 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7562 DCHECK_LE(num_temps, 1u);
7563 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007564 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007565 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7566 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7567 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07007568 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007569 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007570
7571 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007572 // Avoid null check if we know obj is not null.
7573 if (instruction->MustDoNullCheck()) {
7574 __ testl(obj, obj);
7575 __ j(kEqual, &zero);
7576 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007577
Roland Levillain7c1559a2015-12-15 10:55:36 +00007578 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007579 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007580 ReadBarrierOption read_barrier_option =
7581 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007582 // /* HeapReference<Class> */ out = obj->klass_
7583 GenerateReferenceLoadTwoRegisters(instruction,
7584 out_loc,
7585 obj_loc,
7586 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007587 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007588 if (cls.IsRegister()) {
7589 __ cmpl(out, cls.AsRegister<Register>());
7590 } else {
7591 DCHECK(cls.IsStackSlot()) << cls;
7592 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7593 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007594
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007595 // Classes must be equal for the instanceof to succeed.
7596 __ j(kNotEqual, &zero);
7597 __ movl(out, Immediate(1));
7598 __ jmp(&done);
7599 break;
7600 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007601
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007602 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007603 ReadBarrierOption read_barrier_option =
7604 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007605 // /* HeapReference<Class> */ out = obj->klass_
7606 GenerateReferenceLoadTwoRegisters(instruction,
7607 out_loc,
7608 obj_loc,
7609 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007610 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007611 // If the class is abstract, we eagerly fetch the super class of the
7612 // object to avoid doing a comparison we know will fail.
7613 NearLabel loop;
7614 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007615 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007616 GenerateReferenceLoadOneRegister(instruction,
7617 out_loc,
7618 super_offset,
7619 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007620 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007621 __ testl(out, out);
7622 // If `out` is null, we use it for the result, and jump to `done`.
7623 __ j(kEqual, &done);
7624 if (cls.IsRegister()) {
7625 __ cmpl(out, cls.AsRegister<Register>());
7626 } else {
7627 DCHECK(cls.IsStackSlot()) << cls;
7628 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7629 }
7630 __ j(kNotEqual, &loop);
7631 __ movl(out, Immediate(1));
7632 if (zero.IsLinked()) {
7633 __ jmp(&done);
7634 }
7635 break;
7636 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007637
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007638 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007639 ReadBarrierOption read_barrier_option =
7640 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007641 // /* HeapReference<Class> */ out = obj->klass_
7642 GenerateReferenceLoadTwoRegisters(instruction,
7643 out_loc,
7644 obj_loc,
7645 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007646 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007647 // Walk over the class hierarchy to find a match.
7648 NearLabel loop, success;
7649 __ Bind(&loop);
7650 if (cls.IsRegister()) {
7651 __ cmpl(out, cls.AsRegister<Register>());
7652 } else {
7653 DCHECK(cls.IsStackSlot()) << cls;
7654 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7655 }
7656 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007657 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007658 GenerateReferenceLoadOneRegister(instruction,
7659 out_loc,
7660 super_offset,
7661 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007662 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007663 __ testl(out, out);
7664 __ j(kNotEqual, &loop);
7665 // If `out` is null, we use it for the result, and jump to `done`.
7666 __ jmp(&done);
7667 __ Bind(&success);
7668 __ movl(out, Immediate(1));
7669 if (zero.IsLinked()) {
7670 __ jmp(&done);
7671 }
7672 break;
7673 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007674
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007675 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007676 ReadBarrierOption read_barrier_option =
7677 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007678 // /* HeapReference<Class> */ out = obj->klass_
7679 GenerateReferenceLoadTwoRegisters(instruction,
7680 out_loc,
7681 obj_loc,
7682 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007683 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007684 // Do an exact check.
7685 NearLabel exact_check;
7686 if (cls.IsRegister()) {
7687 __ cmpl(out, cls.AsRegister<Register>());
7688 } else {
7689 DCHECK(cls.IsStackSlot()) << cls;
7690 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7691 }
7692 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007693 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007694 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007695 GenerateReferenceLoadOneRegister(instruction,
7696 out_loc,
7697 component_offset,
7698 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007699 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007700 __ testl(out, out);
7701 // If `out` is null, we use it for the result, and jump to `done`.
7702 __ j(kEqual, &done);
7703 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
7704 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007705 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007706 __ movl(out, Immediate(1));
7707 __ jmp(&done);
7708 break;
7709 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007710
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007711 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007712 // No read barrier since the slow path will retry upon failure.
7713 // /* HeapReference<Class> */ out = obj->klass_
7714 GenerateReferenceLoadTwoRegisters(instruction,
7715 out_loc,
7716 obj_loc,
7717 class_offset,
7718 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007719 if (cls.IsRegister()) {
7720 __ cmpl(out, cls.AsRegister<Register>());
7721 } else {
7722 DCHECK(cls.IsStackSlot()) << cls;
7723 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7724 }
7725 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007726 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007727 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007728 codegen_->AddSlowPath(slow_path);
7729 __ j(kNotEqual, slow_path->GetEntryLabel());
7730 __ movl(out, Immediate(1));
7731 if (zero.IsLinked()) {
7732 __ jmp(&done);
7733 }
7734 break;
7735 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007736
Calin Juravle98893e12015-10-02 21:05:03 +01007737 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007738 case TypeCheckKind::kInterfaceCheck: {
7739 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007740 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00007741 // cases.
7742 //
7743 // We cannot directly call the InstanceofNonTrivial runtime
7744 // entry point without resorting to a type checking slow path
7745 // here (i.e. by calling InvokeRuntime directly), as it would
7746 // require to assign fixed registers for the inputs of this
7747 // HInstanceOf instruction (following the runtime calling
7748 // convention), which might be cluttered by the potential first
7749 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007750 //
7751 // TODO: Introduce a new runtime entry point taking the object
7752 // to test (instead of its class) as argument, and let it deal
7753 // with the read barrier issues. This will let us refactor this
7754 // case of the `switch` code as it was previously (with a direct
7755 // call to the runtime not using a type checking slow path).
7756 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007757 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007758 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007759 instruction, /* is_fatal= */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007760 codegen_->AddSlowPath(slow_path);
7761 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007762 if (zero.IsLinked()) {
7763 __ jmp(&done);
7764 }
7765 break;
7766 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007767
7768 case TypeCheckKind::kBitstringCheck: {
7769 // /* HeapReference<Class> */ temp = obj->klass_
7770 GenerateReferenceLoadTwoRegisters(instruction,
7771 out_loc,
7772 obj_loc,
7773 class_offset,
7774 kWithoutReadBarrier);
7775
7776 GenerateBitstringTypeCheckCompare(instruction, out);
7777 __ j(kNotEqual, &zero);
7778 __ movl(out, Immediate(1));
7779 __ jmp(&done);
7780 break;
7781 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007782 }
7783
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007784 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007785 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007786 __ xorl(out, out);
7787 }
7788
7789 if (done.IsLinked()) {
7790 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007791 }
7792
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007793 if (slow_path != nullptr) {
7794 __ Bind(slow_path->GetExitLabel());
7795 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007796}
7797
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007798void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007799 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007800 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007801 LocationSummary* locations =
7802 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007803 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007804 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7805 // Require a register for the interface check since there is a loop that compares the class to
7806 // a memory address.
7807 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007808 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7809 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7810 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7811 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007812 } else {
7813 locations->SetInAt(1, Location::Any());
7814 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007815 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007816 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
7817}
7818
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007819void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007820 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007821 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007822 Location obj_loc = locations->InAt(0);
7823 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007824 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007825 Location temp_loc = locations->GetTemp(0);
7826 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007827 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7828 DCHECK_GE(num_temps, 1u);
7829 DCHECK_LE(num_temps, 2u);
7830 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7831 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7832 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7833 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7834 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7835 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7836 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7837 const uint32_t object_array_data_offset =
7838 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007839
Vladimir Marko87584542017-12-12 17:47:52 +00007840 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007841 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007842 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7843 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007844 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007845
Roland Levillain0d5a2812015-11-13 10:07:31 +00007846 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007847 // Avoid null check if we know obj is not null.
7848 if (instruction->MustDoNullCheck()) {
7849 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007850 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007851 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007852
Roland Levillain0d5a2812015-11-13 10:07:31 +00007853 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007854 case TypeCheckKind::kExactCheck:
7855 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007856 // /* HeapReference<Class> */ temp = obj->klass_
7857 GenerateReferenceLoadTwoRegisters(instruction,
7858 temp_loc,
7859 obj_loc,
7860 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007861 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007862
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007863 if (cls.IsRegister()) {
7864 __ cmpl(temp, cls.AsRegister<Register>());
7865 } else {
7866 DCHECK(cls.IsStackSlot()) << cls;
7867 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7868 }
7869 // Jump to slow path for throwing the exception or doing a
7870 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007871 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007872 break;
7873 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007874
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007875 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007876 // /* HeapReference<Class> */ temp = obj->klass_
7877 GenerateReferenceLoadTwoRegisters(instruction,
7878 temp_loc,
7879 obj_loc,
7880 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007881 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007882
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007883 // If the class is abstract, we eagerly fetch the super class of the
7884 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007885 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007886 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007887 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007888 GenerateReferenceLoadOneRegister(instruction,
7889 temp_loc,
7890 super_offset,
7891 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007892 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007893
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007894 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7895 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007896 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007897 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007898
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007899 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007900 if (cls.IsRegister()) {
7901 __ cmpl(temp, cls.AsRegister<Register>());
7902 } else {
7903 DCHECK(cls.IsStackSlot()) << cls;
7904 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7905 }
7906 __ j(kNotEqual, &loop);
7907 break;
7908 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007909
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007910 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007911 // /* HeapReference<Class> */ temp = obj->klass_
7912 GenerateReferenceLoadTwoRegisters(instruction,
7913 temp_loc,
7914 obj_loc,
7915 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007916 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007917
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007918 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007919 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007920 __ Bind(&loop);
7921 if (cls.IsRegister()) {
7922 __ cmpl(temp, cls.AsRegister<Register>());
7923 } else {
7924 DCHECK(cls.IsStackSlot()) << cls;
7925 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7926 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007927 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007928
Roland Levillain0d5a2812015-11-13 10:07:31 +00007929 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007930 GenerateReferenceLoadOneRegister(instruction,
7931 temp_loc,
7932 super_offset,
7933 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007934 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007935
7936 // If the class reference currently in `temp` is not null, jump
7937 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007938 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007939 __ j(kNotZero, &loop);
7940 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007941 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007942 break;
7943 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007944
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007945 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007946 // /* HeapReference<Class> */ temp = obj->klass_
7947 GenerateReferenceLoadTwoRegisters(instruction,
7948 temp_loc,
7949 obj_loc,
7950 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007951 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007952
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007953 // Do an exact check.
7954 if (cls.IsRegister()) {
7955 __ cmpl(temp, cls.AsRegister<Register>());
7956 } else {
7957 DCHECK(cls.IsStackSlot()) << cls;
7958 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7959 }
7960 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007961
7962 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007963 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007964 GenerateReferenceLoadOneRegister(instruction,
7965 temp_loc,
7966 component_offset,
7967 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007968 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007969
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007970 // If the component type is null (i.e. the object not an array), jump to the slow path to
7971 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007972 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007973 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007974
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007975 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007976 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007977 break;
7978 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007979
Calin Juravle98893e12015-10-02 21:05:03 +01007980 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007981 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007982 // We cannot directly call the CheckCast runtime entry point
7983 // without resorting to a type checking slow path here (i.e. by
7984 // calling InvokeRuntime directly), as it would require to
7985 // assign fixed registers for the inputs of this HInstanceOf
7986 // instruction (following the runtime calling convention), which
7987 // might be cluttered by the potential first read barrier
7988 // emission at the beginning of this method.
7989 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007990 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007991
7992 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007993 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
7994 // We can not get false positives by doing this.
7995 // /* HeapReference<Class> */ temp = obj->klass_
7996 GenerateReferenceLoadTwoRegisters(instruction,
7997 temp_loc,
7998 obj_loc,
7999 class_offset,
8000 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07008001
Vladimir Markoe619f6c2017-12-12 16:00:01 +00008002 // /* HeapReference<Class> */ temp = temp->iftable_
8003 GenerateReferenceLoadTwoRegisters(instruction,
8004 temp_loc,
8005 temp_loc,
8006 iftable_offset,
8007 kWithoutReadBarrier);
8008 // Iftable is never null.
8009 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
8010 // Maybe poison the `cls` for direct comparison with memory.
8011 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
8012 // Loop through the iftable and check if any class matches.
8013 NearLabel start_loop;
8014 __ Bind(&start_loop);
8015 // Need to subtract first to handle the empty array case.
8016 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
8017 __ j(kNegative, type_check_slow_path->GetEntryLabel());
8018 // Go to next interface if the classes do not match.
8019 __ cmpl(cls.AsRegister<Register>(),
8020 CodeGeneratorX86::ArrayAddress(temp,
8021 maybe_temp2_loc,
8022 TIMES_4,
8023 object_array_data_offset));
8024 __ j(kNotEqual, &start_loop);
8025 // If `cls` was poisoned above, unpoison it.
8026 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07008027 break;
8028 }
Vladimir Marko175e7862018-03-27 09:03:13 +00008029
8030 case TypeCheckKind::kBitstringCheck: {
8031 // /* HeapReference<Class> */ temp = obj->klass_
8032 GenerateReferenceLoadTwoRegisters(instruction,
8033 temp_loc,
8034 obj_loc,
8035 class_offset,
8036 kWithoutReadBarrier);
8037
8038 GenerateBitstringTypeCheckCompare(instruction, temp);
8039 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
8040 break;
8041 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00008042 }
8043 __ Bind(&done);
8044
Roland Levillain0d5a2812015-11-13 10:07:31 +00008045 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00008046}
8047
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008048void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008049 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8050 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008051 InvokeRuntimeCallingConvention calling_convention;
8052 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8053}
8054
8055void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01008056 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
8057 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01008058 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01008059 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008060 if (instruction->IsEnter()) {
8061 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8062 } else {
8063 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8064 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008065}
8066
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05308067void LocationsBuilderX86::VisitX86AndNot(HX86AndNot* instruction) {
8068 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
8069 DCHECK(DataType::IsIntOrLongType(instruction->GetType())) << instruction->GetType();
8070 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
8071 locations->SetInAt(0, Location::RequiresRegister());
8072 locations->SetInAt(1, Location::RequiresRegister());
8073 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8074}
8075
8076void InstructionCodeGeneratorX86::VisitX86AndNot(HX86AndNot* instruction) {
8077 LocationSummary* locations = instruction->GetLocations();
8078 Location first = locations->InAt(0);
8079 Location second = locations->InAt(1);
8080 Location dest = locations->Out();
8081 if (instruction->GetResultType() == DataType::Type::kInt32) {
8082 __ andn(dest.AsRegister<Register>(),
8083 first.AsRegister<Register>(),
8084 second.AsRegister<Register>());
8085 } else {
8086 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
8087 __ andn(dest.AsRegisterPairLow<Register>(),
8088 first.AsRegisterPairLow<Register>(),
8089 second.AsRegisterPairLow<Register>());
8090 __ andn(dest.AsRegisterPairHigh<Register>(),
8091 first.AsRegisterPairHigh<Register>(),
8092 second.AsRegisterPairHigh<Register>());
8093 }
8094}
8095
8096void LocationsBuilderX86::VisitX86MaskOrResetLeastSetBit(HX86MaskOrResetLeastSetBit* instruction) {
8097 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
8098 DCHECK(instruction->GetType() == DataType::Type::kInt32) << instruction->GetType();
8099 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
8100 locations->SetInAt(0, Location::RequiresRegister());
8101 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8102}
8103
8104void InstructionCodeGeneratorX86::VisitX86MaskOrResetLeastSetBit(
8105 HX86MaskOrResetLeastSetBit* instruction) {
8106 LocationSummary* locations = instruction->GetLocations();
8107 Location src = locations->InAt(0);
8108 Location dest = locations->Out();
8109 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
8110 switch (instruction->GetOpKind()) {
8111 case HInstruction::kAnd:
8112 __ blsr(dest.AsRegister<Register>(), src.AsRegister<Register>());
8113 break;
8114 case HInstruction::kXor:
8115 __ blsmsk(dest.AsRegister<Register>(), src.AsRegister<Register>());
8116 break;
8117 default:
8118 LOG(FATAL) << "Unreachable";
8119 }
8120}
8121
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008122void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
8123void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
8124void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
8125
8126void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
8127 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008128 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008129 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
8130 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008131 locations->SetInAt(0, Location::RequiresRegister());
8132 locations->SetInAt(1, Location::Any());
8133 locations->SetOut(Location::SameAsFirstInput());
8134}
8135
8136void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
8137 HandleBitwiseOperation(instruction);
8138}
8139
8140void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
8141 HandleBitwiseOperation(instruction);
8142}
8143
8144void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
8145 HandleBitwiseOperation(instruction);
8146}
8147
8148void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
8149 LocationSummary* locations = instruction->GetLocations();
8150 Location first = locations->InAt(0);
8151 Location second = locations->InAt(1);
8152 DCHECK(first.Equals(locations->Out()));
8153
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008154 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008155 if (second.IsRegister()) {
8156 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00008157 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008158 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00008159 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008160 } else {
8161 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00008162 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008163 }
8164 } else if (second.IsConstant()) {
8165 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00008166 __ andl(first.AsRegister<Register>(),
8167 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008168 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00008169 __ orl(first.AsRegister<Register>(),
8170 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008171 } else {
8172 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00008173 __ xorl(first.AsRegister<Register>(),
8174 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008175 }
8176 } else {
8177 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00008178 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008179 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00008180 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008181 } else {
8182 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00008183 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008184 }
8185 }
8186 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008187 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008188 if (second.IsRegisterPair()) {
8189 if (instruction->IsAnd()) {
8190 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
8191 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
8192 } else if (instruction->IsOr()) {
8193 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
8194 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
8195 } else {
8196 DCHECK(instruction->IsXor());
8197 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
8198 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
8199 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008200 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008201 if (instruction->IsAnd()) {
8202 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
8203 __ andl(first.AsRegisterPairHigh<Register>(),
8204 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
8205 } else if (instruction->IsOr()) {
8206 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
8207 __ orl(first.AsRegisterPairHigh<Register>(),
8208 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
8209 } else {
8210 DCHECK(instruction->IsXor());
8211 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
8212 __ xorl(first.AsRegisterPairHigh<Register>(),
8213 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
8214 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008215 } else {
8216 DCHECK(second.IsConstant()) << second;
8217 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008218 int32_t low_value = Low32Bits(value);
8219 int32_t high_value = High32Bits(value);
8220 Immediate low(low_value);
8221 Immediate high(high_value);
8222 Register first_low = first.AsRegisterPairLow<Register>();
8223 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008224 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008225 if (low_value == 0) {
8226 __ xorl(first_low, first_low);
8227 } else if (low_value != -1) {
8228 __ andl(first_low, low);
8229 }
8230 if (high_value == 0) {
8231 __ xorl(first_high, first_high);
8232 } else if (high_value != -1) {
8233 __ andl(first_high, high);
8234 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008235 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008236 if (low_value != 0) {
8237 __ orl(first_low, low);
8238 }
8239 if (high_value != 0) {
8240 __ orl(first_high, high);
8241 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008242 } else {
8243 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008244 if (low_value != 0) {
8245 __ xorl(first_low, low);
8246 }
8247 if (high_value != 0) {
8248 __ xorl(first_high, high);
8249 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008250 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008251 }
8252 }
8253}
8254
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008255void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
8256 HInstruction* instruction,
8257 Location out,
8258 uint32_t offset,
8259 Location maybe_temp,
8260 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008261 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008262 if (read_barrier_option == kWithReadBarrier) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00008263 CHECK(gUseReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008264 if (kUseBakerReadBarrier) {
8265 // Load with fast path based Baker's read barrier.
8266 // /* HeapReference<Object> */ out = *(out + offset)
8267 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008268 instruction, out, out_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008269 } else {
8270 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008271 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00008272 // in the following move operation, as we will need it for the
8273 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00008274 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008275 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008276 // /* HeapReference<Object> */ out = *(out + offset)
8277 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008278 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008279 }
8280 } else {
8281 // Plain load with no read barrier.
8282 // /* HeapReference<Object> */ out = *(out + offset)
8283 __ movl(out_reg, Address(out_reg, offset));
8284 __ MaybeUnpoisonHeapReference(out_reg);
8285 }
8286}
8287
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008288void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
8289 HInstruction* instruction,
8290 Location out,
8291 Location obj,
8292 uint32_t offset,
8293 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008294 Register out_reg = out.AsRegister<Register>();
8295 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008296 if (read_barrier_option == kWithReadBarrier) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00008297 CHECK(gUseReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008298 if (kUseBakerReadBarrier) {
8299 // Load with fast path based Baker's read barrier.
8300 // /* HeapReference<Object> */ out = *(obj + offset)
8301 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008302 instruction, out, obj_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008303 } else {
8304 // Load with slow path based read barrier.
8305 // /* HeapReference<Object> */ out = *(obj + offset)
8306 __ movl(out_reg, Address(obj_reg, offset));
8307 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8308 }
8309 } else {
8310 // Plain load with no read barrier.
8311 // /* HeapReference<Object> */ out = *(obj + offset)
8312 __ movl(out_reg, Address(obj_reg, offset));
8313 __ MaybeUnpoisonHeapReference(out_reg);
8314 }
8315}
8316
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008317void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
8318 HInstruction* instruction,
8319 Location root,
8320 const Address& address,
8321 Label* fixup_label,
8322 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008323 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008324 if (read_barrier_option == kWithReadBarrier) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00008325 DCHECK(gUseReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008326 if (kUseBakerReadBarrier) {
8327 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
8328 // Baker's read barrier are used:
8329 //
Roland Levillaind966ce72017-02-09 16:20:14 +00008330 // root = obj.field;
8331 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8332 // if (temp != null) {
8333 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00008334 // }
8335
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008336 // /* GcRoot<mirror::Object> */ root = *address
8337 __ movl(root_reg, address);
8338 if (fixup_label != nullptr) {
8339 __ Bind(fixup_label);
8340 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008341 static_assert(
8342 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8343 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8344 "have different sizes.");
8345 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8346 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8347 "have different sizes.");
8348
Vladimir Marko953437b2016-08-24 08:30:46 +00008349 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008350 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008351 instruction, root, /* unpoison_ref_before_marking= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008352 codegen_->AddSlowPath(slow_path);
8353
Roland Levillaind966ce72017-02-09 16:20:14 +00008354 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
8355 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01008356 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00008357 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
8358 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008359 __ j(kNotEqual, slow_path->GetEntryLabel());
8360 __ Bind(slow_path->GetExitLabel());
8361 } else {
8362 // GC root loaded through a slow path for read barriers other
8363 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008364 // /* GcRoot<mirror::Object>* */ root = address
8365 __ leal(root_reg, address);
8366 if (fixup_label != nullptr) {
8367 __ Bind(fixup_label);
8368 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008369 // /* mirror::Object* */ root = root->Read()
8370 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8371 }
8372 } else {
8373 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008374 // /* GcRoot<mirror::Object> */ root = *address
8375 __ movl(root_reg, address);
8376 if (fixup_label != nullptr) {
8377 __ Bind(fixup_label);
8378 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00008379 // Note that GC roots are not affected by heap poisoning, thus we
8380 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008381 }
8382}
8383
8384void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8385 Location ref,
8386 Register obj,
8387 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00008388 bool needs_null_check) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00008389 DCHECK(gUseReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008390 DCHECK(kUseBakerReadBarrier);
8391
8392 // /* HeapReference<Object> */ ref = *(obj + offset)
8393 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00008394 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008395}
8396
8397void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8398 Location ref,
8399 Register obj,
8400 uint32_t data_offset,
8401 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00008402 bool needs_null_check) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00008403 DCHECK(gUseReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008404 DCHECK(kUseBakerReadBarrier);
8405
Roland Levillain3d312422016-06-23 13:53:42 +01008406 static_assert(
8407 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8408 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00008409 // /* HeapReference<Object> */ ref =
8410 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008411 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00008412 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008413}
8414
8415void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8416 Location ref,
8417 Register obj,
8418 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008419 bool needs_null_check,
8420 bool always_update_field,
8421 Register* temp) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00008422 DCHECK(gUseReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008423 DCHECK(kUseBakerReadBarrier);
8424
8425 // In slow path based read barriers, the read barrier call is
8426 // inserted after the original load. However, in fast path based
8427 // Baker's read barriers, we need to perform the load of
8428 // mirror::Object::monitor_ *before* the original reference load.
8429 // This load-load ordering is required by the read barrier.
8430 // The fast path/slow path (for Baker's algorithm) should look like:
8431 //
8432 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8433 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8434 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008435 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00008436 // if (is_gray) {
8437 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
8438 // }
8439 //
8440 // Note: the original implementation in ReadBarrier::Barrier is
8441 // slightly more complex as:
8442 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00008443 // the high-bits of rb_state, which are expected to be all zeroes
8444 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
8445 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008446 // - it performs additional checks that we do not do here for
8447 // performance reasons.
8448
8449 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00008450 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
8451
Vladimir Marko953437b2016-08-24 08:30:46 +00008452 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01008453 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008454 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00008455 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
8456 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
8457 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
8458
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008459 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00008460 // ref = ReadBarrier::Mark(ref);
8461 // At this point, just do the "if" and make sure that flags are preserved until the branch.
8462 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00008463 if (needs_null_check) {
8464 MaybeRecordImplicitNullCheck(instruction);
8465 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008466
8467 // Load fence to prevent load-load reordering.
8468 // Note that this is a no-op, thanks to the x86 memory model.
8469 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
8470
8471 // The actual reference load.
8472 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00008473 __ movl(ref_reg, src); // Flags are unaffected.
8474
8475 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
8476 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008477 SlowPathCode* slow_path;
8478 if (always_update_field) {
8479 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01008480 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008481 instruction, ref, obj, src, /* unpoison_ref_before_marking= */ true, *temp);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008482 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008483 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008484 instruction, ref, /* unpoison_ref_before_marking= */ true);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008485 }
Vladimir Marko953437b2016-08-24 08:30:46 +00008486 AddSlowPath(slow_path);
8487
8488 // We have done the "if" of the gray bit check above, now branch based on the flags.
8489 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00008490
8491 // Object* ref = ref_addr->AsMirrorPtr()
8492 __ MaybeUnpoisonHeapReference(ref_reg);
8493
Roland Levillain7c1559a2015-12-15 10:55:36 +00008494 __ Bind(slow_path->GetExitLabel());
8495}
8496
8497void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
8498 Location out,
8499 Location ref,
8500 Location obj,
8501 uint32_t offset,
8502 Location index) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00008503 DCHECK(gUseReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008504
Roland Levillain7c1559a2015-12-15 10:55:36 +00008505 // Insert a slow path based read barrier *after* the reference load.
8506 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008507 // If heap poisoning is enabled, the unpoisoning of the loaded
8508 // reference will be carried out by the runtime within the slow
8509 // path.
8510 //
8511 // Note that `ref` currently does not get unpoisoned (when heap
8512 // poisoning is enabled), which is alright as the `ref` argument is
8513 // not used by the artReadBarrierSlow entry point.
8514 //
8515 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008516 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00008517 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
8518 AddSlowPath(slow_path);
8519
Roland Levillain0d5a2812015-11-13 10:07:31 +00008520 __ jmp(slow_path->GetEntryLabel());
8521 __ Bind(slow_path->GetExitLabel());
8522}
8523
Roland Levillain7c1559a2015-12-15 10:55:36 +00008524void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
8525 Location out,
8526 Location ref,
8527 Location obj,
8528 uint32_t offset,
8529 Location index) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00008530 if (gUseReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008531 // Baker's read barriers shall be handled by the fast path
8532 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
8533 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008534 // If heap poisoning is enabled, unpoisoning will be taken care of
8535 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008536 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008537 } else if (kPoisonHeapReferences) {
8538 __ UnpoisonHeapReference(out.AsRegister<Register>());
8539 }
8540}
8541
Roland Levillain7c1559a2015-12-15 10:55:36 +00008542void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8543 Location out,
8544 Location root) {
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +00008545 DCHECK(gUseReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008546
Roland Levillain7c1559a2015-12-15 10:55:36 +00008547 // Insert a slow path based read barrier *after* the GC root load.
8548 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008549 // Note that GC roots are not affected by heap poisoning, so we do
8550 // not need to do anything special for this here.
8551 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008552 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008553 AddSlowPath(slow_path);
8554
Roland Levillain0d5a2812015-11-13 10:07:31 +00008555 __ jmp(slow_path->GetEntryLabel());
8556 __ Bind(slow_path->GetExitLabel());
8557}
8558
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008559void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008560 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008561 LOG(FATAL) << "Unreachable";
8562}
8563
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008564void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008565 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008566 LOG(FATAL) << "Unreachable";
8567}
8568
Mark Mendellfe57faa2015-09-18 09:26:15 -04008569// Simple implementation of packed switch - generate cascaded compare/jumps.
8570void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8571 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008572 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04008573 locations->SetInAt(0, Location::RequiresRegister());
8574}
8575
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008576void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
8577 int32_t lower_bound,
8578 uint32_t num_entries,
8579 HBasicBlock* switch_block,
8580 HBasicBlock* default_block) {
8581 // Figure out the correct compare values and jump conditions.
8582 // Handle the first compare/branch as a special case because it might
8583 // jump to the default case.
8584 DCHECK_GT(num_entries, 2u);
8585 Condition first_condition;
8586 uint32_t index;
8587 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
8588 if (lower_bound != 0) {
8589 first_condition = kLess;
8590 __ cmpl(value_reg, Immediate(lower_bound));
8591 __ j(first_condition, codegen_->GetLabelOf(default_block));
8592 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008593
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008594 index = 1;
8595 } else {
8596 // Handle all the compare/jumps below.
8597 first_condition = kBelow;
8598 index = 0;
8599 }
8600
8601 // Handle the rest of the compare/jumps.
8602 for (; index + 1 < num_entries; index += 2) {
8603 int32_t compare_to_value = lower_bound + index + 1;
8604 __ cmpl(value_reg, Immediate(compare_to_value));
8605 // Jump to successors[index] if value < case_value[index].
8606 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
8607 // Jump to successors[index + 1] if value == case_value[index + 1].
8608 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
8609 }
8610
8611 if (index != num_entries) {
8612 // There are an odd number of entries. Handle the last one.
8613 DCHECK_EQ(index + 1, num_entries);
8614 __ cmpl(value_reg, Immediate(lower_bound + index));
8615 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008616 }
8617
8618 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008619 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
8620 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008621 }
8622}
8623
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008624void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8625 int32_t lower_bound = switch_instr->GetStartValue();
8626 uint32_t num_entries = switch_instr->GetNumEntries();
8627 LocationSummary* locations = switch_instr->GetLocations();
8628 Register value_reg = locations->InAt(0).AsRegister<Register>();
8629
8630 GenPackedSwitchWithCompares(value_reg,
8631 lower_bound,
8632 num_entries,
8633 switch_instr->GetBlock(),
8634 switch_instr->GetDefaultBlock());
8635}
8636
Mark Mendell805b3b52015-09-18 14:10:29 -04008637void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8638 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008639 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04008640 locations->SetInAt(0, Location::RequiresRegister());
8641
8642 // Constant area pointer.
8643 locations->SetInAt(1, Location::RequiresRegister());
8644
8645 // And the temporary we need.
8646 locations->AddTemp(Location::RequiresRegister());
8647}
8648
8649void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8650 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008651 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04008652 LocationSummary* locations = switch_instr->GetLocations();
8653 Register value_reg = locations->InAt(0).AsRegister<Register>();
8654 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8655
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008656 if (num_entries <= kPackedSwitchJumpTableThreshold) {
8657 GenPackedSwitchWithCompares(value_reg,
8658 lower_bound,
8659 num_entries,
8660 switch_instr->GetBlock(),
8661 default_block);
8662 return;
8663 }
8664
Mark Mendell805b3b52015-09-18 14:10:29 -04008665 // Optimizing has a jump area.
8666 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
8667 Register constant_area = locations->InAt(1).AsRegister<Register>();
8668
8669 // Remove the bias, if needed.
8670 if (lower_bound != 0) {
8671 __ leal(temp_reg, Address(value_reg, -lower_bound));
8672 value_reg = temp_reg;
8673 }
8674
8675 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008676 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04008677 __ cmpl(value_reg, Immediate(num_entries - 1));
8678 __ j(kAbove, codegen_->GetLabelOf(default_block));
8679
8680 // We are in the range of the table.
8681 // Load (target-constant_area) from the jump table, indexing by the value.
8682 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
8683
8684 // Compute the actual target address by adding in constant_area.
8685 __ addl(temp_reg, constant_area);
8686
8687 // And jump.
8688 __ jmp(temp_reg);
8689}
8690
Mark Mendell0616ae02015-04-17 12:49:27 -04008691void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
8692 HX86ComputeBaseMethodAddress* insn) {
8693 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008694 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008695 locations->SetOut(Location::RequiresRegister());
8696}
8697
8698void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
8699 HX86ComputeBaseMethodAddress* insn) {
8700 LocationSummary* locations = insn->GetLocations();
8701 Register reg = locations->Out().AsRegister<Register>();
8702
8703 // Generate call to next instruction.
8704 Label next_instruction;
8705 __ call(&next_instruction);
8706 __ Bind(&next_instruction);
8707
8708 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008709 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04008710
8711 // Grab the return address off the stack.
8712 __ popl(reg);
8713}
8714
8715void LocationsBuilderX86::VisitX86LoadFromConstantTable(
8716 HX86LoadFromConstantTable* insn) {
8717 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008718 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008719
8720 locations->SetInAt(0, Location::RequiresRegister());
8721 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
8722
8723 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00008724 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008725 return;
8726 }
8727
8728 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008729 case DataType::Type::kFloat32:
8730 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008731 locations->SetOut(Location::RequiresFpuRegister());
8732 break;
8733
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008734 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008735 locations->SetOut(Location::RequiresRegister());
8736 break;
8737
8738 default:
8739 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8740 }
8741}
8742
8743void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00008744 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008745 return;
8746 }
8747
8748 LocationSummary* locations = insn->GetLocations();
8749 Location out = locations->Out();
8750 Register const_area = locations->InAt(0).AsRegister<Register>();
8751 HConstant *value = insn->GetConstant();
8752
8753 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008754 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008755 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008756 codegen_->LiteralFloatAddress(
8757 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008758 break;
8759
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008760 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008761 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008762 codegen_->LiteralDoubleAddress(
8763 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008764 break;
8765
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008766 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008767 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008768 codegen_->LiteralInt32Address(
8769 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008770 break;
8771
8772 default:
8773 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8774 }
8775}
8776
Mark Mendell0616ae02015-04-17 12:49:27 -04008777/**
8778 * Class to handle late fixup of offsets into constant area.
8779 */
Vladimir Marko5233f932015-09-29 19:01:15 +01008780class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04008781 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008782 RIPFixup(CodeGeneratorX86& codegen,
8783 HX86ComputeBaseMethodAddress* base_method_address,
8784 size_t offset)
8785 : codegen_(&codegen),
8786 base_method_address_(base_method_address),
8787 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008788
8789 protected:
8790 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
8791
8792 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008793 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008794
8795 private:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008796 void Process(const MemoryRegion& region, int pos) override {
Mark Mendell0616ae02015-04-17 12:49:27 -04008797 // Patch the correct offset for the instruction. The place to patch is the
8798 // last 4 bytes of the instruction.
8799 // The value to patch is the distance from the offset in the constant area
8800 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04008801 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008802 int32_t relative_position =
8803 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04008804
8805 // Patch in the right value.
8806 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
8807 }
8808
Mark Mendell0616ae02015-04-17 12:49:27 -04008809 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04008810 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008811};
8812
Mark Mendell805b3b52015-09-18 14:10:29 -04008813/**
8814 * Class to handle late fixup of offsets to a jump table that will be created in the
8815 * constant area.
8816 */
8817class JumpTableRIPFixup : public RIPFixup {
8818 public:
8819 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008820 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
8821 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008822
8823 void CreateJumpTable() {
8824 X86Assembler* assembler = codegen_->GetAssembler();
8825
8826 // Ensure that the reference to the jump table has the correct offset.
8827 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
8828 SetOffset(offset_in_constant_table);
8829
8830 // The label values in the jump table are computed relative to the
8831 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008832 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04008833
8834 // Populate the jump table with the correct values for the jump table.
8835 int32_t num_entries = switch_instr_->GetNumEntries();
8836 HBasicBlock* block = switch_instr_->GetBlock();
8837 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
8838 // The value that we want is the target offset - the position of the table.
8839 for (int32_t i = 0; i < num_entries; i++) {
8840 HBasicBlock* b = successors[i];
8841 Label* l = codegen_->GetLabelOf(b);
8842 DCHECK(l->IsBound());
8843 int32_t offset_to_block = l->Position() - relative_offset;
8844 assembler->AppendInt32(offset_to_block);
8845 }
8846 }
8847
8848 private:
8849 const HX86PackedSwitch* switch_instr_;
8850};
8851
8852void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
8853 // Generate the constant area if needed.
8854 X86Assembler* assembler = GetAssembler();
jaishank20d1c942019-03-08 15:08:17 +05308855
Mark Mendell805b3b52015-09-18 14:10:29 -04008856 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
8857 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
8858 // byte values.
8859 assembler->Align(4, 0);
8860 constant_area_start_ = assembler->CodeSize();
8861
8862 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008863 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04008864 jump_table->CreateJumpTable();
8865 }
8866
8867 // And now add the constant area to the generated code.
8868 assembler->AddConstantArea();
8869 }
8870
8871 // And finish up.
8872 CodeGenerator::Finalize(allocator);
8873}
8874
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008875Address CodeGeneratorX86::LiteralDoubleAddress(double v,
8876 HX86ComputeBaseMethodAddress* method_base,
8877 Register reg) {
8878 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008879 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008880 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008881}
8882
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008883Address CodeGeneratorX86::LiteralFloatAddress(float v,
8884 HX86ComputeBaseMethodAddress* method_base,
8885 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008886 AssemblerFixup* fixup =
8887 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008888 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008889}
8890
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008891Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
8892 HX86ComputeBaseMethodAddress* method_base,
8893 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008894 AssemblerFixup* fixup =
8895 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008896 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008897}
8898
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008899Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
8900 HX86ComputeBaseMethodAddress* method_base,
8901 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008902 AssemblerFixup* fixup =
8903 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008904 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008905}
8906
Aart Bika19616e2016-02-01 18:57:58 -08008907void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
8908 if (value == 0) {
8909 __ xorl(dest, dest);
8910 } else {
8911 __ movl(dest, Immediate(value));
8912 }
8913}
8914
8915void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
8916 if (value == 0) {
8917 __ testl(dest, dest);
8918 } else {
8919 __ cmpl(dest, Immediate(value));
8920 }
8921}
8922
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008923void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
8924 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07008925 GenerateIntCompare(lhs_reg, rhs);
8926}
8927
8928void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008929 if (rhs.IsConstant()) {
8930 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07008931 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008932 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07008933 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008934 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07008935 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008936 }
8937}
8938
8939Address CodeGeneratorX86::ArrayAddress(Register obj,
8940 Location index,
8941 ScaleFactor scale,
8942 uint32_t data_offset) {
8943 return index.IsConstant() ?
8944 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
8945 Address(obj, index.AsRegister<Register>(), scale, data_offset);
8946}
8947
Mark Mendell805b3b52015-09-18 14:10:29 -04008948Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
8949 Register reg,
8950 Register value) {
8951 // Create a fixup to be used to create and address the jump table.
8952 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008953 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04008954
8955 // We have to populate the jump tables.
8956 fixups_to_jump_tables_.push_back(table_fixup);
8957
8958 // We want a scaled address, as we are extracting the correct offset from the table.
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008959 return Address(reg, value, TIMES_4, kPlaceholder32BitOffset, table_fixup);
Mark Mendell805b3b52015-09-18 14:10:29 -04008960}
8961
Andreas Gampe85b62f22015-09-09 13:15:38 -07008962// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008963void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07008964 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008965 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008966 return;
8967 }
8968
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008969 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008970
8971 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
8972 if (target.Equals(return_loc)) {
8973 return;
8974 }
8975
8976 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8977 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008978 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008979 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008980 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
8981 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008982 GetMoveResolver()->EmitNativeCode(&parallel_move);
8983 } else {
8984 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01008985 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07008986 parallel_move.AddMove(return_loc, target, type, nullptr);
8987 GetMoveResolver()->EmitNativeCode(&parallel_move);
8988 }
8989}
8990
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008991void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
8992 const uint8_t* roots_data,
8993 const PatchInfo<Label>& info,
8994 uint64_t index_in_table) const {
8995 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
8996 uintptr_t address =
8997 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
Andreas Gampec55bb392018-09-21 00:02:02 +00008998 using unaligned_uint32_t __attribute__((__aligned__(1))) = uint32_t;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008999 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
9000 dchecked_integral_cast<uint32_t>(address);
9001}
9002
Nicolas Geoffray132d8362016-11-16 09:19:42 +00009003void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
9004 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009005 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01009006 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009007 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00009008 }
9009
9010 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00009011 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01009012 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009013 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00009014 }
9015}
9016
xueliang.zhonge0eb4832017-10-30 13:43:14 +00009017void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
9018 ATTRIBUTE_UNUSED) {
9019 LOG(FATAL) << "Unreachable";
9020}
9021
9022void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
9023 ATTRIBUTE_UNUSED) {
9024 LOG(FATAL) << "Unreachable";
9025}
9026
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05309027bool LocationsBuilderX86::CpuHasAvxFeatureFlag() {
9028 return codegen_->GetInstructionSetFeatures().HasAVX();
9029}
9030bool LocationsBuilderX86::CpuHasAvx2FeatureFlag() {
9031 return codegen_->GetInstructionSetFeatures().HasAVX2();
9032}
9033bool InstructionCodeGeneratorX86::CpuHasAvxFeatureFlag() {
9034 return codegen_->GetInstructionSetFeatures().HasAVX();
9035}
9036bool InstructionCodeGeneratorX86::CpuHasAvx2FeatureFlag() {
9037 return codegen_->GetInstructionSetFeatures().HasAVX2();
9038}
9039
Roland Levillain4d027112015-07-01 15:41:14 +01009040#undef __
9041
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00009042} // namespace x86
9043} // namespace art