blob: dfed2f76e3ebf082a1dc0979718f5bb6004d34f8 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Vladimir Marko86c87522020-05-11 16:55:55 +010019#include "arch/x86/jni_frame_x86.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000020#include "art_method-inl.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010021#include "class_table.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010022#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000023#include "compiled_method.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010024#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000025#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010026#include "gc/accounting/card_table.h"
Vladimir Markoeebb8212018-06-05 14:57:24 +010027#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070028#include "heap_poisoning.h"
Nicolas Geoffray8b8d93d2020-09-17 14:30:01 +010029#include "interpreter/mterp/nterp.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040030#include "intrinsics.h"
31#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"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000038#include "scoped_thread_state_change-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010039#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010041#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000042#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010043#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000044
Vladimir Marko0a516052019-10-14 13:00:44 +000045namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010046
Roland Levillain0d5a2812015-11-13 10:07:31 +000047template<class MirrorType>
48class GcRoot;
49
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000050namespace x86 {
51
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010052static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010053static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050054static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010055
Mark Mendell24f2dfa2015-01-14 19:51:45 -050056static constexpr int kC2ConditionMask = 0x400;
57
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000058static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000059
Aart Bik1f8d51b2018-02-15 10:42:37 -080060static constexpr int64_t kDoubleNaN = INT64_C(0x7FF8000000000000);
61static constexpr int32_t kFloatNaN = INT32_C(0x7FC00000);
62
Vladimir Marko3232dbb2018-07-25 15:42:46 +010063static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
64 InvokeRuntimeCallingConvention calling_convention;
65 RegisterSet caller_saves = RegisterSet::Empty();
66 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
67 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
68 // that the the kPrimNot result register is the same as the first argument register.
69 return caller_saves;
70}
71
Roland Levillain7cbd27f2016-08-11 23:53:33 +010072// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
73#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070074#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010075
Andreas Gampe85b62f22015-09-09 13:15:38 -070076class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000078 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010080 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +010081 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010082 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000083 if (instruction_->CanThrowIntoCatchBlock()) {
84 // Live registers will be restored in the catch block if caught.
85 SaveLiveRegisters(codegen, instruction_->GetLocations());
86 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010087 x86_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexandre Rames8158f282015-08-07 10:26:17 +010088 instruction_,
89 instruction_->GetDexPc(),
90 this);
Roland Levillain888d0672015-11-23 18:53:50 +000091 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010092 }
93
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010094 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +010095
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010096 const char* GetDescription() const override { return "NullCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +010097
Nicolas Geoffraye5038322014-07-04 09:41:32 +010098 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010099 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
100};
101
Andreas Gampe85b62f22015-09-09 13:15:38 -0700102class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000103 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000104 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000105
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100106 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +0100107 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +0000108 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100109 x86_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000110 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000111 }
112
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100113 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100114
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100115 const char* GetDescription() const override { return "DivZeroCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100116
Calin Juravled0d48522014-11-04 16:40:20 +0000117 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000118 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
119};
120
Andreas Gampe85b62f22015-09-09 13:15:38 -0700121class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000122 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000123 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
124 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000125
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100126 void EmitNativeCode(CodeGenerator* codegen) override {
Calin Juravled0d48522014-11-04 16:40:20 +0000127 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000128 if (is_div_) {
129 __ negl(reg_);
130 } else {
131 __ movl(reg_, Immediate(0));
132 }
Calin Juravled0d48522014-11-04 16:40:20 +0000133 __ jmp(GetExitLabel());
134 }
135
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100136 const char* GetDescription() const override { return "DivRemMinusOneSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100137
Calin Juravled0d48522014-11-04 16:40:20 +0000138 private:
139 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000140 bool is_div_;
141 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000142};
143
Andreas Gampe85b62f22015-09-09 13:15:38 -0700144class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100145 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000146 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100147
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100148 void EmitNativeCode(CodeGenerator* codegen) override {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100149 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100150 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100151 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000152 // We're moving two locations to locations that could overlap, so we need a parallel
153 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000154 if (instruction_->CanThrowIntoCatchBlock()) {
155 // Live registers will be restored in the catch block if caught.
156 SaveLiveRegisters(codegen, instruction_->GetLocations());
157 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400158
159 // Are we using an array length from memory?
160 HInstruction* array_length = instruction_->InputAt(1);
161 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100162 InvokeRuntimeCallingConvention calling_convention;
Mark Mendellee8d9712016-07-12 11:13:15 -0400163 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
164 // Load the array length into our temporary.
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100165 HArrayLength* length = array_length->AsArrayLength();
166 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(length);
Mark Mendellee8d9712016-07-12 11:13:15 -0400167 Location array_loc = array_length->GetLocations()->InAt(0);
168 Address array_len(array_loc.AsRegister<Register>(), len_offset);
169 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
170 // Check for conflicts with index.
171 if (length_loc.Equals(locations->InAt(0))) {
172 // We know we aren't using parameter 2.
173 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
174 }
175 __ movl(length_loc.AsRegister<Register>(), array_len);
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100176 if (mirror::kUseStringCompression && length->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100177 __ shrl(length_loc.AsRegister<Register>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700178 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400179 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000180 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100181 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000182 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100183 DataType::Type::kInt32,
Mark Mendellee8d9712016-07-12 11:13:15 -0400184 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100185 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100186 DataType::Type::kInt32);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100187 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
188 ? kQuickThrowStringBounds
189 : kQuickThrowArrayBounds;
190 x86_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100191 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000192 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100193 }
194
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100195 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100196
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100197 const char* GetDescription() const override { return "BoundsCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100198
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100199 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100200 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
201};
202
Andreas Gampe85b62f22015-09-09 13:15:38 -0700203class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000204 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000205 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000206 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000207
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100208 void EmitNativeCode(CodeGenerator* codegen) override {
Aart Bikb13c65b2017-03-21 20:14:07 -0700209 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100210 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000211 __ Bind(GetEntryLabel());
Aart Bik24b905f2017-04-06 09:59:06 -0700212 SaveLiveRegisters(codegen, locations); // Only saves full width XMM for SIMD.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100213 x86_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000214 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Aart Bik24b905f2017-04-06 09:59:06 -0700215 RestoreLiveRegisters(codegen, locations); // Only restores full width XMM for SIMD.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100216 if (successor_ == nullptr) {
217 __ jmp(GetReturnLabel());
218 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100219 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100220 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000221 }
222
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100223 Label* GetReturnLabel() {
224 DCHECK(successor_ == nullptr);
225 return &return_label_;
226 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000227
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100228 HBasicBlock* GetSuccessor() const {
229 return successor_;
230 }
231
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100232 const char* GetDescription() const override { return "SuspendCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100233
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000234 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100235 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000236 Label return_label_;
237
238 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
239};
240
Vladimir Markoaad75c62016-10-03 08:46:48 +0000241class LoadStringSlowPathX86 : public SlowPathCode {
242 public:
243 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
244
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100245 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000246 LocationSummary* locations = instruction_->GetLocations();
247 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
248
249 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
250 __ Bind(GetEntryLabel());
251 SaveLiveRegisters(codegen, locations);
252
253 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000254 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
255 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index.index_));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000256 x86_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
257 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
258 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
259 RestoreLiveRegisters(codegen, locations);
260
Vladimir Markoaad75c62016-10-03 08:46:48 +0000261 __ jmp(GetExitLabel());
262 }
263
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100264 const char* GetDescription() const override { return "LoadStringSlowPathX86"; }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000265
266 private:
267 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
268};
269
Andreas Gampe85b62f22015-09-09 13:15:38 -0700270class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000271 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100272 LoadClassSlowPathX86(HLoadClass* cls, HInstruction* at)
273 : SlowPathCode(at), cls_(cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000274 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100275 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000276 }
277
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100278 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000279 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100280 Location out = locations->Out();
281 const uint32_t dex_pc = instruction_->GetDexPc();
282 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
283 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
284
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000285 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
286 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000287 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000288
289 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100290 if (must_resolve_type) {
291 DCHECK(IsSameDexFile(cls_->GetDexFile(), x86_codegen->GetGraph()->GetDexFile()));
292 dex::TypeIndex type_index = cls_->GetTypeIndex();
293 __ movl(calling_convention.GetRegisterAt(0), Immediate(type_index.index_));
Vladimir Marko8f63f102020-09-28 12:10:28 +0100294 if (cls_->NeedsAccessCheck()) {
295 CheckEntrypointTypes<kQuickResolveTypeAndVerifyAccess, void*, uint32_t>();
296 x86_codegen->InvokeRuntime(kQuickResolveTypeAndVerifyAccess, instruction_, dex_pc, this);
297 } else {
298 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
299 x86_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
300 }
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100301 // If we also must_do_clinit, the resolved type is now in the correct register.
302 } else {
303 DCHECK(must_do_clinit);
304 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
305 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), source);
306 }
307 if (must_do_clinit) {
308 x86_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
309 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Roland Levillain888d0672015-11-23 18:53:50 +0000310 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000311
312 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000313 if (out.IsValid()) {
314 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
315 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000316 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000317 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000318 __ jmp(GetExitLabel());
319 }
320
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100321 const char* GetDescription() const override { return "LoadClassSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100322
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000323 private:
324 // The class this slow path will load.
325 HLoadClass* const cls_;
326
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000327 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
328};
329
Andreas Gampe85b62f22015-09-09 13:15:38 -0700330class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000331 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000332 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000333 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000334
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100335 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000336 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000337 DCHECK(instruction_->IsCheckCast()
338 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000339
340 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
341 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000342
Vladimir Markoe619f6c2017-12-12 16:00:01 +0000343 if (kPoisonHeapReferences &&
344 instruction_->IsCheckCast() &&
345 instruction_->AsCheckCast()->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) {
346 // First, unpoison the `cls` reference that was poisoned for direct memory comparison.
347 __ UnpoisonHeapReference(locations->InAt(1).AsRegister<Register>());
348 }
349
Vladimir Marko87584542017-12-12 17:47:52 +0000350 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000351 SaveLiveRegisters(codegen, locations);
352 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000353
354 // We're moving two locations to locations that could overlap, so we need a parallel
355 // move resolver.
356 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800357 x86_codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800358 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100359 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800360 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800361 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100362 DataType::Type::kReference);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000363 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100364 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100365 instruction_,
366 instruction_->GetDexPc(),
367 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800368 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000369 } else {
370 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800371 x86_codegen->InvokeRuntime(kQuickCheckInstanceOf,
372 instruction_,
373 instruction_->GetDexPc(),
374 this);
375 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000376 }
377
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000378 if (!is_fatal_) {
379 if (instruction_->IsInstanceOf()) {
380 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
381 }
382 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000383
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000384 __ jmp(GetExitLabel());
385 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000386 }
387
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100388 const char* GetDescription() const override { return "TypeCheckSlowPathX86"; }
389 bool IsFatal() const override { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100390
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000391 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000392 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000393
394 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
395};
396
Andreas Gampe85b62f22015-09-09 13:15:38 -0700397class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700398 public:
Aart Bik42249c32016-01-07 15:33:50 -0800399 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000400 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700401
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100402 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +0100403 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700404 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100405 LocationSummary* locations = instruction_->GetLocations();
406 SaveLiveRegisters(codegen, locations);
407 InvokeRuntimeCallingConvention calling_convention;
408 x86_codegen->Load32BitValue(
409 calling_convention.GetRegisterAt(0),
410 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100411 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100412 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700413 }
414
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100415 const char* GetDescription() const override { return "DeoptimizationSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100416
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700417 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700418 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
419};
420
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100421class ArraySetSlowPathX86 : public SlowPathCode {
422 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000423 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100424
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100425 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100426 LocationSummary* locations = instruction_->GetLocations();
427 __ Bind(GetEntryLabel());
428 SaveLiveRegisters(codegen, locations);
429
430 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100431 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100432 parallel_move.AddMove(
433 locations->InAt(0),
434 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100435 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100436 nullptr);
437 parallel_move.AddMove(
438 locations->InAt(1),
439 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100440 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100441 nullptr);
442 parallel_move.AddMove(
443 locations->InAt(2),
444 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100445 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100446 nullptr);
447 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
448
449 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100450 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000451 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100452 RestoreLiveRegisters(codegen, locations);
453 __ jmp(GetExitLabel());
454 }
455
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100456 const char* GetDescription() const override { return "ArraySetSlowPathX86"; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100457
458 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100459 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
460};
461
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100462// Slow path marking an object reference `ref` during a read
463// barrier. The field `obj.field` in the object `obj` holding this
464// reference does not get updated by this slow path after marking (see
465// ReadBarrierMarkAndUpdateFieldSlowPathX86 below for that).
466//
467// This means that after the execution of this slow path, `ref` will
468// always be up-to-date, but `obj.field` may not; i.e., after the
469// flip, `ref` will be a to-space reference, but `obj.field` will
470// probably still be a from-space reference (unless it gets updated by
471// another thread, or if another thread installed another object
472// reference (different from `ref`) in `obj.field`).
Roland Levillain7c1559a2015-12-15 10:55:36 +0000473class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
474 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100475 ReadBarrierMarkSlowPathX86(HInstruction* instruction,
476 Location ref,
477 bool unpoison_ref_before_marking)
478 : SlowPathCode(instruction),
479 ref_(ref),
480 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000481 DCHECK(kEmitCompilerReadBarrier);
482 }
483
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100484 const char* GetDescription() const override { return "ReadBarrierMarkSlowPathX86"; }
Roland Levillain7c1559a2015-12-15 10:55:36 +0000485
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100486 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000487 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100488 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000489 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100490 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000491 DCHECK(instruction_->IsInstanceFieldGet() ||
Alex Light3a73ffb2021-01-25 14:11:05 +0000492 instruction_->IsPredicatedInstanceFieldGet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000493 instruction_->IsStaticFieldGet() ||
494 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100495 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000496 instruction_->IsLoadClass() ||
497 instruction_->IsLoadString() ||
498 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100499 instruction_->IsCheckCast() ||
Andra Danciu1ca6f322020-08-12 08:58:07 +0000500 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000501 << "Unexpected instruction in read barrier marking slow path: "
502 << instruction_->DebugName();
503
504 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100505 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000506 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100507 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000508 }
Roland Levillain4359e612016-07-20 11:32:19 +0100509 // No need to save live registers; it's taken care of by the
510 // entrypoint. Also, there is no need to update the stack mask,
511 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000512 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100513 DCHECK_NE(ref_reg, ESP);
514 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100515 // "Compact" slow path, saving two moves.
516 //
517 // Instead of using the standard runtime calling convention (input
518 // and output in EAX):
519 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100520 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100521 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100522 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100523 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100524 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100525 // of a dedicated entrypoint:
526 //
527 // rX <- ReadBarrierMarkRegX(rX)
528 //
Roland Levillain97c46462017-05-11 14:04:03 +0100529 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100530 // This runtime call does not require a stack map.
531 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000532 __ jmp(GetExitLabel());
533 }
534
535 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100536 // The location (register) of the marked object reference.
537 const Location ref_;
538 // Should the reference in `ref_` be unpoisoned prior to marking it?
539 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000540
541 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
542};
543
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100544// Slow path marking an object reference `ref` during a read barrier,
545// and if needed, atomically updating the field `obj.field` in the
546// object `obj` holding this reference after marking (contrary to
547// ReadBarrierMarkSlowPathX86 above, which never tries to update
548// `obj.field`).
549//
550// This means that after the execution of this slow path, both `ref`
551// and `obj.field` will be up-to-date; i.e., after the flip, both will
552// hold the same to-space reference (unless another thread installed
553// another object reference (different from `ref`) in `obj.field`).
554class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
555 public:
556 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
557 Location ref,
558 Register obj,
559 const Address& field_addr,
560 bool unpoison_ref_before_marking,
561 Register temp)
562 : SlowPathCode(instruction),
563 ref_(ref),
564 obj_(obj),
565 field_addr_(field_addr),
566 unpoison_ref_before_marking_(unpoison_ref_before_marking),
567 temp_(temp) {
568 DCHECK(kEmitCompilerReadBarrier);
569 }
570
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100571 const char* GetDescription() const override { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100572
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100573 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100574 LocationSummary* locations = instruction_->GetLocations();
575 Register ref_reg = ref_.AsRegister<Register>();
576 DCHECK(locations->CanCall());
577 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
578 // This slow path is only used by the UnsafeCASObject intrinsic.
Andra Danciu5e13d452020-09-08 14:35:09 +0000579 DCHECK((instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100580 << "Unexpected instruction in read barrier marking and field updating slow path: "
581 << instruction_->DebugName();
582 DCHECK(instruction_->GetLocations()->Intrinsified());
Andra Danciu52d2c0c2020-09-15 14:27:21 +0000583 Intrinsics intrinsic = instruction_->AsInvoke()->GetIntrinsic();
584 static constexpr auto kVarHandleCAS = mirror::VarHandle::AccessModeTemplate::kCompareAndSet;
Andra Danciu9dfb1a92020-09-22 13:27:18 +0000585 static constexpr auto kVarHandleGetAndSet =
586 mirror::VarHandle::AccessModeTemplate::kGetAndUpdate;
Andra Danciu370948e2020-09-23 08:07:25 +0000587 static constexpr auto kVarHandleCAX =
588 mirror::VarHandle::AccessModeTemplate::kCompareAndExchange;
Andra Danciu52d2c0c2020-09-15 14:27:21 +0000589 DCHECK(intrinsic == Intrinsics::kUnsafeCASObject ||
Andra Danciu9dfb1a92020-09-22 13:27:18 +0000590 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) == kVarHandleCAS ||
Andra Danciu370948e2020-09-23 08:07:25 +0000591 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) == kVarHandleGetAndSet ||
592 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) == kVarHandleCAX);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100593
594 __ Bind(GetEntryLabel());
595 if (unpoison_ref_before_marking_) {
596 // Object* ref = ref_addr->AsMirrorPtr()
597 __ MaybeUnpoisonHeapReference(ref_reg);
598 }
599
600 // Save the old (unpoisoned) reference.
601 __ movl(temp_, ref_reg);
602
603 // No need to save live registers; it's taken care of by the
604 // entrypoint. Also, there is no need to update the stack mask,
605 // as this runtime call will not trigger a garbage collection.
606 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
607 DCHECK_NE(ref_reg, ESP);
608 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
609 // "Compact" slow path, saving two moves.
610 //
611 // Instead of using the standard runtime calling convention (input
612 // and output in EAX):
613 //
614 // EAX <- ref
615 // EAX <- ReadBarrierMark(EAX)
616 // ref <- EAX
617 //
618 // we just use rX (the register containing `ref`) as input and output
619 // of a dedicated entrypoint:
620 //
621 // rX <- ReadBarrierMarkRegX(rX)
622 //
Roland Levillain97c46462017-05-11 14:04:03 +0100623 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100624 // This runtime call does not require a stack map.
625 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
626
627 // If the new reference is different from the old reference,
628 // update the field in the holder (`*field_addr`).
629 //
630 // Note that this field could also hold a different object, if
631 // another thread had concurrently changed it. In that case, the
632 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
633 // operation below would abort the CAS, leaving the field as-is.
634 NearLabel done;
635 __ cmpl(temp_, ref_reg);
636 __ j(kEqual, &done);
637
638 // Update the the holder's field atomically. This may fail if
639 // mutator updates before us, but it's OK. This is achieved
640 // using a strong compare-and-set (CAS) operation with relaxed
641 // memory synchronization ordering, where the expected value is
642 // the old reference and the desired value is the new reference.
643 // This operation is implemented with a 32-bit LOCK CMPXLCHG
644 // instruction, which requires the expected value (the old
645 // reference) to be in EAX. Save EAX beforehand, and move the
646 // expected value (stored in `temp_`) into EAX.
647 __ pushl(EAX);
648 __ movl(EAX, temp_);
649
650 // Convenience aliases.
651 Register base = obj_;
652 Register expected = EAX;
653 Register value = ref_reg;
654
655 bool base_equals_value = (base == value);
656 if (kPoisonHeapReferences) {
657 if (base_equals_value) {
658 // If `base` and `value` are the same register location, move
659 // `value` to a temporary register. This way, poisoning
660 // `value` won't invalidate `base`.
661 value = temp_;
662 __ movl(value, base);
663 }
664
665 // Check that the register allocator did not assign the location
666 // of `expected` (EAX) to `value` nor to `base`, so that heap
667 // poisoning (when enabled) works as intended below.
668 // - If `value` were equal to `expected`, both references would
669 // be poisoned twice, meaning they would not be poisoned at
670 // all, as heap poisoning uses address negation.
671 // - If `base` were equal to `expected`, poisoning `expected`
672 // would invalidate `base`.
673 DCHECK_NE(value, expected);
674 DCHECK_NE(base, expected);
675
676 __ PoisonHeapReference(expected);
677 __ PoisonHeapReference(value);
678 }
679
680 __ LockCmpxchgl(field_addr_, value);
681
682 // If heap poisoning is enabled, we need to unpoison the values
683 // that were poisoned earlier.
684 if (kPoisonHeapReferences) {
685 if (base_equals_value) {
686 // `value` has been moved to a temporary register, no need
687 // to unpoison it.
688 } else {
689 __ UnpoisonHeapReference(value);
690 }
691 // No need to unpoison `expected` (EAX), as it is be overwritten below.
692 }
693
694 // Restore EAX.
695 __ popl(EAX);
696
697 __ Bind(&done);
698 __ jmp(GetExitLabel());
699 }
700
701 private:
702 // The location (register) of the marked object reference.
703 const Location ref_;
704 // The register containing the object holding the marked object reference field.
705 const Register obj_;
706 // The address of the marked reference field. The base of this address must be `obj_`.
707 const Address field_addr_;
708
709 // Should the reference in `ref_` be unpoisoned prior to marking it?
710 const bool unpoison_ref_before_marking_;
711
712 const Register temp_;
713
714 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
715};
716
Roland Levillain0d5a2812015-11-13 10:07:31 +0000717// Slow path generating a read barrier for a heap reference.
718class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
719 public:
720 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
721 Location out,
722 Location ref,
723 Location obj,
724 uint32_t offset,
725 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000726 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000727 out_(out),
728 ref_(ref),
729 obj_(obj),
730 offset_(offset),
731 index_(index) {
732 DCHECK(kEmitCompilerReadBarrier);
733 // If `obj` is equal to `out` or `ref`, it means the initial object
734 // has been overwritten by (or after) the heap object reference load
735 // to be instrumented, e.g.:
736 //
737 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000738 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000739 //
740 // In that case, we have lost the information about the original
741 // object, and the emitted read barrier cannot work properly.
742 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
743 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
744 }
745
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100746 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000747 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
748 LocationSummary* locations = instruction_->GetLocations();
749 Register reg_out = out_.AsRegister<Register>();
750 DCHECK(locations->CanCall());
751 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100752 DCHECK(instruction_->IsInstanceFieldGet() ||
Alex Light3a73ffb2021-01-25 14:11:05 +0000753 instruction_->IsPredicatedInstanceFieldGet() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100754 instruction_->IsStaticFieldGet() ||
755 instruction_->IsArrayGet() ||
756 instruction_->IsInstanceOf() ||
757 instruction_->IsCheckCast() ||
Vladimir Marko94d2c812020-11-05 10:04:45 +0000758 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000759 << "Unexpected instruction in read barrier for heap reference slow path: "
760 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000761
762 __ Bind(GetEntryLabel());
763 SaveLiveRegisters(codegen, locations);
764
765 // We may have to change the index's value, but as `index_` is a
766 // constant member (like other "inputs" of this slow path),
767 // introduce a copy of it, `index`.
768 Location index = index_;
769 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100770 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000771 if (instruction_->IsArrayGet()) {
772 // Compute the actual memory offset and store it in `index`.
773 Register index_reg = index_.AsRegister<Register>();
774 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
775 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
776 // We are about to change the value of `index_reg` (see the
777 // calls to art::x86::X86Assembler::shll and
778 // art::x86::X86Assembler::AddImmediate below), but it has
779 // not been saved by the previous call to
780 // art::SlowPathCode::SaveLiveRegisters, as it is a
781 // callee-save register --
782 // art::SlowPathCode::SaveLiveRegisters does not consider
783 // callee-save registers, as it has been designed with the
784 // assumption that callee-save registers are supposed to be
785 // handled by the called function. So, as a callee-save
786 // register, `index_reg` _would_ eventually be saved onto
787 // the stack, but it would be too late: we would have
788 // changed its value earlier. Therefore, we manually save
789 // it here into another freely available register,
790 // `free_reg`, chosen of course among the caller-save
791 // registers (as a callee-save `free_reg` register would
792 // exhibit the same problem).
793 //
794 // Note we could have requested a temporary register from
795 // the register allocator instead; but we prefer not to, as
796 // this is a slow path, and we know we can find a
797 // caller-save register that is available.
798 Register free_reg = FindAvailableCallerSaveRegister(codegen);
799 __ movl(free_reg, index_reg);
800 index_reg = free_reg;
801 index = Location::RegisterLocation(index_reg);
802 } else {
803 // The initial register stored in `index_` has already been
804 // saved in the call to art::SlowPathCode::SaveLiveRegisters
805 // (as it is not a callee-save register), so we can freely
806 // use it.
807 }
808 // Shifting the index value contained in `index_reg` by the scale
809 // factor (2) cannot overflow in practice, as the runtime is
810 // unable to allocate object arrays with a size larger than
811 // 2^26 - 1 (that is, 2^28 - 4 bytes).
812 __ shll(index_reg, Immediate(TIMES_4));
813 static_assert(
814 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
815 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
816 __ AddImmediate(index_reg, Immediate(offset_));
817 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100818 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
819 // intrinsics, `index_` is not shifted by a scale factor of 2
820 // (as in the case of ArrayGet), as it is actually an offset
821 // to an object field within an object.
822 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000823 DCHECK(instruction_->GetLocations()->Intrinsified());
824 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
825 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
826 << instruction_->AsInvoke()->GetIntrinsic();
827 DCHECK_EQ(offset_, 0U);
828 DCHECK(index_.IsRegisterPair());
829 // UnsafeGet's offset location is a register pair, the low
830 // part contains the correct offset.
831 index = index_.ToLow();
832 }
833 }
834
835 // We're moving two or three locations to locations that could
836 // overlap, so we need a parallel move resolver.
837 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100838 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000839 parallel_move.AddMove(ref_,
840 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100841 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000842 nullptr);
843 parallel_move.AddMove(obj_,
844 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100845 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000846 nullptr);
847 if (index.IsValid()) {
848 parallel_move.AddMove(index,
849 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100850 DataType::Type::kInt32,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000851 nullptr);
852 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
853 } else {
854 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
855 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
856 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100857 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000858 CheckEntrypointTypes<
859 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
860 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
861
862 RestoreLiveRegisters(codegen, locations);
863 __ jmp(GetExitLabel());
864 }
865
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100866 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathX86"; }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000867
868 private:
869 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
870 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
871 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
872 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
873 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
874 return static_cast<Register>(i);
875 }
876 }
877 // We shall never fail to find a free caller-save register, as
878 // there are more than two core caller-save registers on x86
879 // (meaning it is possible to find one which is different from
880 // `ref` and `obj`).
881 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
882 LOG(FATAL) << "Could not find a free caller-save register";
883 UNREACHABLE();
884 }
885
Roland Levillain0d5a2812015-11-13 10:07:31 +0000886 const Location out_;
887 const Location ref_;
888 const Location obj_;
889 const uint32_t offset_;
890 // An additional location containing an index to an array.
891 // Only used for HArrayGet and the UnsafeGetObject &
892 // UnsafeGetObjectVolatile intrinsics.
893 const Location index_;
894
895 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
896};
897
898// Slow path generating a read barrier for a GC root.
899class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
900 public:
901 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000902 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000903 DCHECK(kEmitCompilerReadBarrier);
904 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000905
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100906 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000907 LocationSummary* locations = instruction_->GetLocations();
908 Register reg_out = out_.AsRegister<Register>();
909 DCHECK(locations->CanCall());
910 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000911 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
912 << "Unexpected instruction in read barrier for GC root slow path: "
913 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000914
915 __ Bind(GetEntryLabel());
916 SaveLiveRegisters(codegen, locations);
917
918 InvokeRuntimeCallingConvention calling_convention;
919 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
920 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100921 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000922 instruction_,
923 instruction_->GetDexPc(),
924 this);
925 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
926 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
927
928 RestoreLiveRegisters(codegen, locations);
929 __ jmp(GetExitLabel());
930 }
931
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100932 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathX86"; }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000933
934 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000935 const Location out_;
936 const Location root_;
937
938 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
939};
940
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100941#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100942// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
943#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100944
Aart Bike9f37602015-10-09 11:15:55 -0700945inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700946 switch (cond) {
947 case kCondEQ: return kEqual;
948 case kCondNE: return kNotEqual;
949 case kCondLT: return kLess;
950 case kCondLE: return kLessEqual;
951 case kCondGT: return kGreater;
952 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700953 case kCondB: return kBelow;
954 case kCondBE: return kBelowEqual;
955 case kCondA: return kAbove;
956 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700957 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100958 LOG(FATAL) << "Unreachable";
959 UNREACHABLE();
960}
961
Aart Bike9f37602015-10-09 11:15:55 -0700962// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100963inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
964 switch (cond) {
965 case kCondEQ: return kEqual;
966 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700967 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100968 case kCondLT: return kBelow;
969 case kCondLE: return kBelowEqual;
970 case kCondGT: return kAbove;
971 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700972 // Unsigned remain unchanged.
973 case kCondB: return kBelow;
974 case kCondBE: return kBelowEqual;
975 case kCondA: return kAbove;
976 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100977 }
978 LOG(FATAL) << "Unreachable";
979 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700980}
981
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100982void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100983 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100984}
985
986void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100987 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100988}
989
Vladimir Markoa0431112018-06-25 09:32:54 +0100990const X86InstructionSetFeatures& CodeGeneratorX86::GetInstructionSetFeatures() const {
991 return *GetCompilerOptions().GetInstructionSetFeatures()->AsX86InstructionSetFeatures();
992}
993
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100994size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
995 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
996 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100997}
998
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100999size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1000 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
1001 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001002}
1003
Mark Mendell7c8d0092015-01-26 11:21:33 -05001004size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001005 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001006 __ movups(Address(ESP, stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -07001007 } else {
1008 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
1009 }
Artem Serov6a0b6572019-07-26 20:38:37 +01001010 return GetSlowPathFPWidth();
Mark Mendell7c8d0092015-01-26 11:21:33 -05001011}
1012
1013size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001014 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001015 __ movups(XmmRegister(reg_id), Address(ESP, stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -07001016 } else {
1017 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
1018 }
Artem Serov6a0b6572019-07-26 20:38:37 +01001019 return GetSlowPathFPWidth();
Mark Mendell7c8d0092015-01-26 11:21:33 -05001020}
1021
Calin Juravle175dc732015-08-25 15:42:32 +01001022void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
1023 HInstruction* instruction,
1024 uint32_t dex_pc,
1025 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001026 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001027 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
1028 if (EntrypointRequiresStackMap(entrypoint)) {
1029 RecordPcInfo(instruction, dex_pc, slow_path);
1030 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001031}
1032
Roland Levillaindec8f632016-07-22 17:10:06 +01001033void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1034 HInstruction* instruction,
1035 SlowPathCode* slow_path) {
1036 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001037 GenerateInvokeRuntime(entry_point_offset);
1038}
1039
1040void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001041 __ fs()->call(Address::Absolute(entry_point_offset));
1042}
1043
Mark Mendellfb8d2792015-03-31 22:16:59 -04001044CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001045 const CompilerOptions& compiler_options,
1046 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001047 : CodeGenerator(graph,
1048 kNumberOfCpuRegisters,
1049 kNumberOfXmmRegisters,
1050 kNumberOfRegisterPairs,
1051 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1052 arraysize(kCoreCalleeSaves))
1053 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001054 0,
1055 compiler_options,
1056 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001057 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001058 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001059 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001060 move_resolver_(graph->GetAllocator(), this),
1061 assembler_(graph->GetAllocator()),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001062 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1063 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1064 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1065 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko8f63f102020-09-28 12:10:28 +01001066 public_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1067 package_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001068 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001069 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoeb9eb002020-10-02 13:54:19 +01001070 boot_image_jni_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +01001071 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001072 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1073 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001074 constant_area_start_(-1),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001075 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001076 method_address_offset_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001077 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001078 // Use a fake return address register to mimic Quick.
1079 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001080}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001081
David Brazdil58282f42016-01-14 12:45:10 +00001082void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001083 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001084 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001085}
1086
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001087InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001088 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001089 assembler_(codegen->GetAssembler()),
1090 codegen_(codegen) {}
1091
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001092static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001093 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001094}
1095
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001096void CodeGeneratorX86::MaybeIncrementHotness(bool is_frame_entry) {
1097 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1098 Register reg = EAX;
1099 if (is_frame_entry) {
1100 reg = kMethodRegisterArgument;
1101 } else {
1102 __ pushl(EAX);
Vladimir Markodec78172020-06-19 15:31:23 +01001103 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001104 __ movl(EAX, Address(ESP, kX86WordSize));
1105 }
1106 NearLabel overflow;
1107 __ cmpw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()),
1108 Immediate(ArtMethod::MaxCounter()));
1109 __ j(kEqual, &overflow);
1110 __ addw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()),
1111 Immediate(1));
1112 __ Bind(&overflow);
1113 if (!is_frame_entry) {
1114 __ popl(EAX);
Vladimir Markodec78172020-06-19 15:31:23 +01001115 __ cfi().AdjustCFAOffset(-4);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001116 }
1117 }
1118
1119 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffray095dc462020-08-17 16:40:28 +01001120 ScopedProfilingInfoUse spiu(
1121 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
1122 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001123 if (info != nullptr) {
1124 uint32_t address = reinterpret_cast32<uint32_t>(info);
1125 NearLabel done;
1126 if (HasEmptyFrame()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001127 CHECK(is_frame_entry);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001128 // Alignment
Vladimir Markodec78172020-06-19 15:31:23 +01001129 IncreaseFrame(8);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001130 // We need a temporary. The stub also expects the method at bottom of stack.
1131 __ pushl(EAX);
1132 __ cfi().AdjustCFAOffset(4);
1133 __ movl(EAX, Immediate(address));
1134 __ addw(Address(EAX, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()),
1135 Immediate(1));
Nicolas Geoffray8b8d93d2020-09-17 14:30:01 +01001136 __ andw(Address(EAX, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()),
1137 Immediate(interpreter::kTieredHotnessMask));
1138 __ j(kNotZero, &done);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001139 GenerateInvokeRuntime(
1140 GetThreadOffset<kX86PointerSize>(kQuickCompileOptimized).Int32Value());
1141 __ Bind(&done);
1142 // We don't strictly require to restore EAX, but this makes the generated
1143 // code easier to reason about.
1144 __ popl(EAX);
1145 __ cfi().AdjustCFAOffset(-4);
Vladimir Markodec78172020-06-19 15:31:23 +01001146 DecreaseFrame(8);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001147 } else {
1148 if (!RequiresCurrentMethod()) {
1149 CHECK(is_frame_entry);
1150 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1151 }
1152 // We need a temporary.
1153 __ pushl(EAX);
1154 __ cfi().AdjustCFAOffset(4);
1155 __ movl(EAX, Immediate(address));
1156 __ addw(Address(EAX, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()),
1157 Immediate(1));
1158 __ popl(EAX); // Put stack as expected before exiting or calling stub.
1159 __ cfi().AdjustCFAOffset(-4);
1160 __ j(kCarryClear, &done);
1161 GenerateInvokeRuntime(
1162 GetThreadOffset<kX86PointerSize>(kQuickCompileOptimized).Int32Value());
1163 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001164 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001165 }
1166 }
1167}
1168
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001169void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001170 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001171 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001172 bool skip_overflow_check =
1173 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001174 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001175
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001176 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001177 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86);
1178 __ testl(EAX, Address(ESP, -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001179 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001180 }
1181
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001182 if (!HasEmptyFrame()) {
1183 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1184 Register reg = kCoreCalleeSaves[i];
1185 if (allocated_registers_.ContainsCoreRegister(reg)) {
1186 __ pushl(reg);
1187 __ cfi().AdjustCFAOffset(kX86WordSize);
1188 __ cfi().RelOffset(DWARFReg(reg), 0);
1189 }
1190 }
Mark Mendell5f874182015-03-04 15:42:45 -05001191
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001192 int adjust = GetFrameSize() - FrameEntrySpillSize();
Vladimir Markodec78172020-06-19 15:31:23 +01001193 IncreaseFrame(adjust);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001194 // Save the current method if we need it. Note that we do not
1195 // do this in HCurrentMethod, as the instruction might have been removed
1196 // in the SSA graph.
1197 if (RequiresCurrentMethod()) {
1198 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1199 }
1200
1201 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1202 // Initialize should_deoptimize flag to 0.
1203 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
Mark Mendell5f874182015-03-04 15:42:45 -05001204 }
1205 }
1206
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001207 MaybeIncrementHotness(/* is_frame_entry= */ true);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001208}
1209
1210void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001211 __ cfi().RememberState();
1212 if (!HasEmptyFrame()) {
1213 int adjust = GetFrameSize() - FrameEntrySpillSize();
Vladimir Markodec78172020-06-19 15:31:23 +01001214 DecreaseFrame(adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001215
David Srbeckyc34dc932015-04-12 09:27:43 +01001216 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1217 Register reg = kCoreCalleeSaves[i];
1218 if (allocated_registers_.ContainsCoreRegister(reg)) {
1219 __ popl(reg);
1220 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1221 __ cfi().Restore(DWARFReg(reg));
1222 }
Mark Mendell5f874182015-03-04 15:42:45 -05001223 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001224 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001225 __ ret();
1226 __ cfi().RestoreState();
1227 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001228}
1229
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001230void CodeGeneratorX86::Bind(HBasicBlock* block) {
1231 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001232}
1233
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001234Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001235 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001236 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001237 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001238 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001239 case DataType::Type::kInt8:
1240 case DataType::Type::kUint16:
1241 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08001242 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001243 case DataType::Type::kInt32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001244 return Location::RegisterLocation(EAX);
1245
Aart Bik66c158e2018-01-31 12:55:04 -08001246 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001247 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001248 return Location::RegisterPairLocation(EAX, EDX);
1249
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001250 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001251 return Location::NoLocation();
1252
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001253 case DataType::Type::kFloat64:
1254 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001255 return Location::FpuRegisterLocation(XMM0);
1256 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001257
1258 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001259}
1260
1261Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1262 return Location::RegisterLocation(kMethodRegisterArgument);
1263}
1264
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001265Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001266 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001267 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001268 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001269 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001270 case DataType::Type::kInt8:
1271 case DataType::Type::kUint16:
1272 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001273 case DataType::Type::kInt32: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001274 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001275 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001276 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001277 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001278 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001279 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001280 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001281 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001282
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001283 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001284 uint32_t index = gp_index_;
1285 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001286 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001287 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001288 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1289 calling_convention.GetRegisterPairAt(index));
1290 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001291 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001292 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1293 }
1294 }
1295
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001296 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001297 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001298 stack_index_++;
1299 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1300 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1301 } else {
1302 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1303 }
1304 }
1305
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001306 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001307 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001308 stack_index_ += 2;
1309 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1310 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1311 } else {
1312 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001313 }
1314 }
1315
Aart Bik66c158e2018-01-31 12:55:04 -08001316 case DataType::Type::kUint32:
1317 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001318 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001319 LOG(FATAL) << "Unexpected parameter type " << type;
Elliott Hughesc1896c92018-11-29 11:33:18 -08001320 UNREACHABLE();
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001321 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001322 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001323}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001324
Vladimir Marko86c87522020-05-11 16:55:55 +01001325Location CriticalNativeCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
1326 DCHECK_NE(type, DataType::Type::kReference);
1327
1328 Location location;
1329 if (DataType::Is64BitType(type)) {
1330 location = Location::DoubleStackSlot(stack_offset_);
1331 stack_offset_ += 2 * kFramePointerSize;
1332 } else {
1333 location = Location::StackSlot(stack_offset_);
1334 stack_offset_ += kFramePointerSize;
1335 }
1336 if (for_register_allocation_) {
1337 location = Location::Any();
1338 }
1339 return location;
1340}
1341
1342Location CriticalNativeCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
1343 // We perform conversion to the managed ABI return register after the call if needed.
1344 InvokeDexCallingConventionVisitorX86 dex_calling_convention;
1345 return dex_calling_convention.GetReturnLocation(type);
1346}
1347
1348Location CriticalNativeCallingConventionVisitorX86::GetMethodLocation() const {
1349 // Pass the method in the hidden argument EAX.
1350 return Location::RegisterLocation(EAX);
1351}
1352
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001353void CodeGeneratorX86::Move32(Location destination, Location source) {
1354 if (source.Equals(destination)) {
1355 return;
1356 }
1357 if (destination.IsRegister()) {
1358 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001359 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001360 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001361 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Andra Danciu5e13d452020-09-08 14:35:09 +00001362 } else if (source.IsConstant()) {
1363 int32_t value = GetInt32ValueOf(source.GetConstant());
1364 __ movl(destination.AsRegister<Register>(), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001365 } else {
1366 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001367 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001368 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001369 } else if (destination.IsFpuRegister()) {
1370 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001371 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001372 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001373 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001374 } else {
1375 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001376 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001377 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001378 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001379 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001380 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001381 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001382 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001383 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001384 } else if (source.IsConstant()) {
1385 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001386 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001387 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001388 } else {
1389 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001390 __ pushl(Address(ESP, source.GetStackIndex()));
1391 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001392 }
1393 }
1394}
1395
1396void CodeGeneratorX86::Move64(Location destination, Location source) {
1397 if (source.Equals(destination)) {
1398 return;
1399 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001400 if (destination.IsRegisterPair()) {
1401 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001402 EmitParallelMoves(
1403 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1404 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001405 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001406 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001407 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001408 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001409 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001410 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1411 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1412 __ psrlq(src_reg, Immediate(32));
1413 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001414 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001415 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001416 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001417 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1418 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001419 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1420 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001421 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001422 if (source.IsFpuRegister()) {
1423 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1424 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001425 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001426 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001427 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Vladimir Markodec78172020-06-19 15:31:23 +01001428 // Push the 2 source registers to the stack.
1429 __ pushl(source.AsRegisterPairHigh<Register>());
1430 __ cfi().AdjustCFAOffset(elem_size);
1431 __ pushl(source.AsRegisterPairLow<Register>());
1432 __ cfi().AdjustCFAOffset(elem_size);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001433 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1434 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01001435 DecreaseFrame(2 * elem_size);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001436 } else {
1437 LOG(FATAL) << "Unimplemented";
1438 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001439 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001440 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001441 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001442 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001443 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001444 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001445 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001446 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001447 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001448 } else if (source.IsConstant()) {
1449 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001450 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1451 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001452 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001453 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1454 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001455 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001456 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001457 EmitParallelMoves(
1458 Location::StackSlot(source.GetStackIndex()),
1459 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001460 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001461 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001462 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001463 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001464 }
1465 }
1466}
1467
Andra Danciu1ca6f322020-08-12 08:58:07 +00001468static Address CreateAddress(Register base,
1469 Register index = Register::kNoRegister,
1470 ScaleFactor scale = TIMES_1,
1471 int32_t disp = 0) {
1472 if (index == Register::kNoRegister) {
1473 return Address(base, disp);
1474 }
1475
1476 return Address(base, index, scale, disp);
1477}
1478
Andra Danciud0f71f22020-09-17 09:00:15 +00001479void CodeGeneratorX86::LoadFromMemoryNoBarrier(DataType::Type dst_type,
1480 Location dst,
1481 Address src,
Ulya Trafimovich322eced2021-06-02 15:39:36 +01001482 HInstruction* instr,
Andra Danciud0f71f22020-09-17 09:00:15 +00001483 XmmRegister temp,
1484 bool is_atomic_load) {
Andra Danciu1ca6f322020-08-12 08:58:07 +00001485 switch (dst_type) {
1486 case DataType::Type::kBool:
1487 case DataType::Type::kUint8:
1488 __ movzxb(dst.AsRegister<Register>(), src);
1489 break;
1490 case DataType::Type::kInt8:
1491 __ movsxb(dst.AsRegister<Register>(), src);
1492 break;
1493 case DataType::Type::kInt16:
1494 __ movsxw(dst.AsRegister<Register>(), src);
1495 break;
1496 case DataType::Type::kUint16:
1497 __ movzxw(dst.AsRegister<Register>(), src);
1498 break;
1499 case DataType::Type::kInt32:
Andra Danciu1ca6f322020-08-12 08:58:07 +00001500 __ movl(dst.AsRegister<Register>(), src);
1501 break;
Andra Danciud0f71f22020-09-17 09:00:15 +00001502 case DataType::Type::kInt64: {
1503 if (is_atomic_load) {
1504 __ movsd(temp, src);
Ulya Trafimovich322eced2021-06-02 15:39:36 +01001505 if (instr != nullptr) {
1506 MaybeRecordImplicitNullCheck(instr);
1507 }
Andra Danciud0f71f22020-09-17 09:00:15 +00001508 __ movd(dst.AsRegisterPairLow<Register>(), temp);
1509 __ psrlq(temp, Immediate(32));
1510 __ movd(dst.AsRegisterPairHigh<Register>(), temp);
1511 } else {
1512 DCHECK_NE(src.GetBaseRegister(), dst.AsRegisterPairLow<Register>());
Ulya Trafimovich893e2ed2021-06-10 16:18:12 +01001513 Address src_high = Address::displace(src, kX86WordSize);
Andra Danciud0f71f22020-09-17 09:00:15 +00001514 __ movl(dst.AsRegisterPairLow<Register>(), src);
Ulya Trafimovich322eced2021-06-02 15:39:36 +01001515 if (instr != nullptr) {
1516 MaybeRecordImplicitNullCheck(instr);
1517 }
Andra Danciud0f71f22020-09-17 09:00:15 +00001518 __ movl(dst.AsRegisterPairHigh<Register>(), src_high);
1519 }
Andra Danciu1ca6f322020-08-12 08:58:07 +00001520 break;
1521 }
1522 case DataType::Type::kFloat32:
1523 __ movss(dst.AsFpuRegister<XmmRegister>(), src);
1524 break;
1525 case DataType::Type::kFloat64:
1526 __ movsd(dst.AsFpuRegister<XmmRegister>(), src);
1527 break;
Andra Danciu1ca6f322020-08-12 08:58:07 +00001528 case DataType::Type::kReference:
Ulya Trafimovich322eced2021-06-02 15:39:36 +01001529 DCHECK(!kEmitCompilerReadBarrier);
Andra Danciud0f71f22020-09-17 09:00:15 +00001530 __ movl(dst.AsRegister<Register>(), src);
1531 __ MaybeUnpoisonHeapReference(dst.AsRegister<Register>());
1532 break;
1533 default:
Andra Danciu1ca6f322020-08-12 08:58:07 +00001534 LOG(FATAL) << "Unreachable type " << dst_type;
1535 }
Ulya Trafimovich322eced2021-06-02 15:39:36 +01001536 if (instr != nullptr && dst_type != DataType::Type::kInt64) {
1537 // kInt64 needs special handling that is done in the above switch.
1538 MaybeRecordImplicitNullCheck(instr);
1539 }
Andra Danciu1ca6f322020-08-12 08:58:07 +00001540}
1541
Andra Danciu73c31802020-09-01 13:17:05 +00001542void CodeGeneratorX86::MoveToMemory(DataType::Type src_type,
1543 Location src,
1544 Register dst_base,
1545 Register dst_index,
1546 ScaleFactor dst_scale,
1547 int32_t dst_disp) {
1548 DCHECK(dst_base != Register::kNoRegister);
1549 Address dst = CreateAddress(dst_base, dst_index, dst_scale, dst_disp);
1550
1551 switch (src_type) {
1552 case DataType::Type::kBool:
1553 case DataType::Type::kUint8:
1554 case DataType::Type::kInt8: {
1555 if (src.IsConstant()) {
1556 __ movb(dst, Immediate(CodeGenerator::GetInt8ValueOf(src.GetConstant())));
1557 } else {
1558 __ movb(dst, src.AsRegister<ByteRegister>());
1559 }
1560 break;
1561 }
1562 case DataType::Type::kUint16:
1563 case DataType::Type::kInt16: {
1564 if (src.IsConstant()) {
1565 __ movw(dst, Immediate(CodeGenerator::GetInt16ValueOf(src.GetConstant())));
1566 } else {
1567 __ movw(dst, src.AsRegister<Register>());
1568 }
1569 break;
1570 }
1571 case DataType::Type::kUint32:
1572 case DataType::Type::kInt32: {
1573 if (src.IsConstant()) {
1574 int32_t v = CodeGenerator::GetInt32ValueOf(src.GetConstant());
1575 __ movl(dst, Immediate(v));
1576 } else {
1577 __ movl(dst, src.AsRegister<Register>());
1578 }
1579 break;
1580 }
1581 case DataType::Type::kUint64:
1582 case DataType::Type::kInt64: {
1583 Address dst_next_4_bytes = CreateAddress(dst_base, dst_index, dst_scale, dst_disp + 4);
1584 if (src.IsConstant()) {
1585 int64_t v = CodeGenerator::GetInt64ValueOf(src.GetConstant());
1586 __ movl(dst, Immediate(Low32Bits(v)));
1587 __ movl(dst_next_4_bytes, Immediate(High32Bits(v)));
1588 } else {
1589 __ movl(dst, src.AsRegisterPairLow<Register>());
1590 __ movl(dst_next_4_bytes, src.AsRegisterPairHigh<Register>());
1591 }
1592 break;
1593 }
1594 case DataType::Type::kFloat32: {
1595 if (src.IsConstant()) {
1596 int32_t v = CodeGenerator::GetInt32ValueOf(src.GetConstant());
1597 __ movl(dst, Immediate(v));
1598 } else {
1599 __ movss(dst, src.AsFpuRegister<XmmRegister>());
1600 }
1601 break;
1602 }
1603 case DataType::Type::kFloat64: {
1604 Address dst_next_4_bytes = CreateAddress(dst_base, dst_index, dst_scale, dst_disp + 4);
1605 if (src.IsConstant()) {
1606 int64_t v = CodeGenerator::GetInt64ValueOf(src.GetConstant());
1607 __ movl(dst, Immediate(Low32Bits(v)));
1608 __ movl(dst_next_4_bytes, Immediate(High32Bits(v)));
1609 } else {
1610 __ movsd(dst, src.AsFpuRegister<XmmRegister>());
1611 }
1612 break;
1613 }
1614 case DataType::Type::kVoid:
1615 case DataType::Type::kReference:
1616 LOG(FATAL) << "Unreachable type " << src_type;
1617 }
1618}
1619
Calin Juravle175dc732015-08-25 15:42:32 +01001620void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1621 DCHECK(location.IsRegister());
1622 __ movl(location.AsRegister<Register>(), Immediate(value));
1623}
1624
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001625void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001626 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001627 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1628 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1629 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001630 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001631 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001632 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001633 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001634}
1635
1636void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1637 if (location.IsRegister()) {
1638 locations->AddTemp(location);
1639 } else if (location.IsRegisterPair()) {
1640 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1641 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1642 } else {
1643 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1644 }
1645}
1646
David Brazdilfc6a86a2015-06-26 10:33:45 +00001647void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001648 if (successor->IsExitBlock()) {
1649 DCHECK(got->GetPrevious()->AlwaysThrows());
1650 return; // no code needed
1651 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001652
1653 HBasicBlock* block = got->GetBlock();
1654 HInstruction* previous = got->GetPrevious();
1655
1656 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001657 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001658 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001659 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1660 return;
1661 }
1662
1663 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1664 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1665 }
1666 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001667 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001668 }
1669}
1670
David Brazdilfc6a86a2015-06-26 10:33:45 +00001671void LocationsBuilderX86::VisitGoto(HGoto* got) {
1672 got->SetLocations(nullptr);
1673}
1674
1675void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1676 HandleGoto(got, got->GetSuccessor());
1677}
1678
1679void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1680 try_boundary->SetLocations(nullptr);
1681}
1682
1683void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1684 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1685 if (!successor->IsExitBlock()) {
1686 HandleGoto(try_boundary, successor);
1687 }
1688}
1689
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001690void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001691 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001692}
1693
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001694void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001695}
1696
Mark Mendell152408f2015-12-31 12:28:50 -05001697template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001698void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001699 LabelType* true_label,
1700 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001701 if (cond->IsFPConditionTrueIfNaN()) {
1702 __ j(kUnordered, true_label);
1703 } else if (cond->IsFPConditionFalseIfNaN()) {
1704 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001705 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001706 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001707}
1708
Mark Mendell152408f2015-12-31 12:28:50 -05001709template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001710void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001711 LabelType* true_label,
1712 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001713 LocationSummary* locations = cond->GetLocations();
1714 Location left = locations->InAt(0);
1715 Location right = locations->InAt(1);
1716 IfCondition if_cond = cond->GetCondition();
1717
Mark Mendellc4701932015-04-10 13:18:51 -04001718 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001719 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001720 IfCondition true_high_cond = if_cond;
1721 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001722 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001723
1724 // Set the conditions for the test, remembering that == needs to be
1725 // decided using the low words.
1726 switch (if_cond) {
1727 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001728 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001729 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001730 break;
1731 case kCondLT:
1732 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001733 break;
1734 case kCondLE:
1735 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001736 break;
1737 case kCondGT:
1738 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001739 break;
1740 case kCondGE:
1741 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001742 break;
Aart Bike9f37602015-10-09 11:15:55 -07001743 case kCondB:
1744 false_high_cond = kCondA;
1745 break;
1746 case kCondBE:
1747 true_high_cond = kCondB;
1748 break;
1749 case kCondA:
1750 false_high_cond = kCondB;
1751 break;
1752 case kCondAE:
1753 true_high_cond = kCondA;
1754 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001755 }
1756
1757 if (right.IsConstant()) {
1758 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001759 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001760 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001761
Aart Bika19616e2016-02-01 18:57:58 -08001762 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001763 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001764 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001765 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001766 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001767 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001768 __ j(X86Condition(true_high_cond), true_label);
1769 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001770 }
1771 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001772 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001773 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001774 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001775 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001776
1777 __ cmpl(left_high, right_high);
1778 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001779 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001780 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001781 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001782 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001783 __ j(X86Condition(true_high_cond), true_label);
1784 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001785 }
1786 // Must be equal high, so compare the lows.
1787 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001788 } else {
1789 DCHECK(right.IsDoubleStackSlot());
1790 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1791 if (if_cond == kCondNE) {
1792 __ j(X86Condition(true_high_cond), true_label);
1793 } else if (if_cond == kCondEQ) {
1794 __ j(X86Condition(false_high_cond), false_label);
1795 } else {
1796 __ j(X86Condition(true_high_cond), true_label);
1797 __ j(X86Condition(false_high_cond), false_label);
1798 }
1799 // Must be equal high, so compare the lows.
1800 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001801 }
1802 // The last comparison might be unsigned.
1803 __ j(final_condition, true_label);
1804}
1805
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001806void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1807 Location rhs,
1808 HInstruction* insn,
1809 bool is_double) {
1810 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1811 if (is_double) {
1812 if (rhs.IsFpuRegister()) {
1813 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1814 } else if (const_area != nullptr) {
1815 DCHECK(const_area->IsEmittedAtUseSite());
1816 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1817 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001818 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1819 const_area->GetBaseMethodAddress(),
1820 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001821 } else {
1822 DCHECK(rhs.IsDoubleStackSlot());
1823 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1824 }
1825 } else {
1826 if (rhs.IsFpuRegister()) {
1827 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1828 } else if (const_area != nullptr) {
1829 DCHECK(const_area->IsEmittedAtUseSite());
1830 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1831 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001832 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1833 const_area->GetBaseMethodAddress(),
1834 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001835 } else {
1836 DCHECK(rhs.IsStackSlot());
1837 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1838 }
1839 }
1840}
1841
Mark Mendell152408f2015-12-31 12:28:50 -05001842template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001843void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001844 LabelType* true_target_in,
1845 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001846 // Generated branching requires both targets to be explicit. If either of the
1847 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001848 LabelType fallthrough_target;
1849 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1850 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001851
Mark Mendellc4701932015-04-10 13:18:51 -04001852 LocationSummary* locations = condition->GetLocations();
1853 Location left = locations->InAt(0);
1854 Location right = locations->InAt(1);
1855
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001856 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001857 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001858 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001859 GenerateLongComparesAndJumps(condition, true_target, false_target);
1860 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001861 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001862 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001863 GenerateFPJumps(condition, true_target, false_target);
1864 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001865 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001866 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001867 GenerateFPJumps(condition, true_target, false_target);
1868 break;
1869 default:
1870 LOG(FATAL) << "Unexpected compare type " << type;
1871 }
1872
David Brazdil0debae72015-11-12 18:37:00 +00001873 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001874 __ jmp(false_target);
1875 }
David Brazdil0debae72015-11-12 18:37:00 +00001876
1877 if (fallthrough_target.IsLinked()) {
1878 __ Bind(&fallthrough_target);
1879 }
Mark Mendellc4701932015-04-10 13:18:51 -04001880}
1881
David Brazdil0debae72015-11-12 18:37:00 +00001882static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1883 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1884 // are set only strictly before `branch`. We can't use the eflags on long/FP
1885 // conditions if they are materialized due to the complex branching.
1886 return cond->IsCondition() &&
1887 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001888 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1889 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001890}
1891
Mark Mendell152408f2015-12-31 12:28:50 -05001892template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001893void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001894 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001895 LabelType* true_target,
1896 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001897 HInstruction* cond = instruction->InputAt(condition_input_index);
1898
1899 if (true_target == nullptr && false_target == nullptr) {
1900 // Nothing to do. The code always falls through.
1901 return;
1902 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001903 // Constant condition, statically compared against "true" (integer value 1).
1904 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001905 if (true_target != nullptr) {
1906 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001907 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001908 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001909 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001910 if (false_target != nullptr) {
1911 __ jmp(false_target);
1912 }
1913 }
1914 return;
1915 }
1916
1917 // The following code generates these patterns:
1918 // (1) true_target == nullptr && false_target != nullptr
1919 // - opposite condition true => branch to false_target
1920 // (2) true_target != nullptr && false_target == nullptr
1921 // - condition true => branch to true_target
1922 // (3) true_target != nullptr && false_target != nullptr
1923 // - condition true => branch to true_target
1924 // - branch to false_target
1925 if (IsBooleanValueOrMaterializedCondition(cond)) {
1926 if (AreEflagsSetFrom(cond, instruction)) {
1927 if (true_target == nullptr) {
1928 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1929 } else {
1930 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1931 }
1932 } else {
1933 // Materialized condition, compare against 0.
1934 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1935 if (lhs.IsRegister()) {
1936 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1937 } else {
1938 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1939 }
1940 if (true_target == nullptr) {
1941 __ j(kEqual, false_target);
1942 } else {
1943 __ j(kNotEqual, true_target);
1944 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001945 }
1946 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001947 // Condition has not been materialized, use its inputs as the comparison and
1948 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001949 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001950
1951 // If this is a long or FP comparison that has been folded into
1952 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001953 DataType::Type type = condition->InputAt(0)->GetType();
1954 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001955 GenerateCompareTestAndBranch(condition, true_target, false_target);
1956 return;
1957 }
1958
1959 Location lhs = condition->GetLocations()->InAt(0);
1960 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001961 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001962 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001963 if (true_target == nullptr) {
1964 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1965 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001966 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001967 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001968 }
David Brazdil0debae72015-11-12 18:37:00 +00001969
1970 // If neither branch falls through (case 3), the conditional branch to `true_target`
1971 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1972 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001973 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001974 }
1975}
1976
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001977void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001978 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001979 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001980 locations->SetInAt(0, Location::Any());
1981 }
1982}
1983
1984void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001985 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1986 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1987 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1988 nullptr : codegen_->GetLabelOf(true_successor);
1989 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1990 nullptr : codegen_->GetLabelOf(false_successor);
Andreas Gampe3db70682018-12-26 15:12:03 -08001991 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001992}
1993
1994void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001995 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001996 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001997 InvokeRuntimeCallingConvention calling_convention;
1998 RegisterSet caller_saves = RegisterSet::Empty();
1999 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2000 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00002001 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002002 locations->SetInAt(0, Location::Any());
2003 }
2004}
2005
2006void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002007 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00002008 GenerateTestAndBranch<Label>(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08002009 /* condition_input_index= */ 0,
David Brazdil74eb1b22015-12-14 11:44:01 +00002010 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08002011 /* false_target= */ nullptr);
David Brazdil74eb1b22015-12-14 11:44:01 +00002012}
2013
Mingyao Yang063fc772016-08-02 11:02:54 -07002014void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002015 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07002016 LocationSummary(flag, LocationSummary::kNoCall);
2017 locations->SetOut(Location::RequiresRegister());
2018}
2019
2020void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2021 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
2022 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
2023}
2024
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002025static bool SelectCanUseCMOV(HSelect* select) {
2026 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002027 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002028 return false;
2029 }
2030
2031 // A FP condition doesn't generate the single CC that we need.
2032 // In 32 bit mode, a long condition doesn't generate a single CC either.
2033 HInstruction* condition = select->GetCondition();
2034 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002035 DataType::Type compare_type = condition->InputAt(0)->GetType();
2036 if (compare_type == DataType::Type::kInt64 ||
2037 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002038 return false;
2039 }
2040 }
2041
2042 // We can generate a CMOV for this Select.
2043 return true;
2044}
2045
David Brazdil74eb1b22015-12-14 11:44:01 +00002046void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002047 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002048 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00002049 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002050 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00002051 } else {
2052 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002053 if (SelectCanUseCMOV(select)) {
2054 if (select->InputAt(1)->IsConstant()) {
2055 // Cmov can't handle a constant value.
2056 locations->SetInAt(1, Location::RequiresRegister());
2057 } else {
2058 locations->SetInAt(1, Location::Any());
2059 }
2060 } else {
2061 locations->SetInAt(1, Location::Any());
2062 }
David Brazdil74eb1b22015-12-14 11:44:01 +00002063 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002064 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2065 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00002066 }
2067 locations->SetOut(Location::SameAsFirstInput());
2068}
2069
2070void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
2071 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002072 DCHECK(locations->InAt(0).Equals(locations->Out()));
2073 if (SelectCanUseCMOV(select)) {
2074 // If both the condition and the source types are integer, we can generate
2075 // a CMOV to implement Select.
2076
2077 HInstruction* select_condition = select->GetCondition();
2078 Condition cond = kNotEqual;
2079
2080 // Figure out how to test the 'condition'.
2081 if (select_condition->IsCondition()) {
2082 HCondition* condition = select_condition->AsCondition();
2083 if (!condition->IsEmittedAtUseSite()) {
2084 // This was a previously materialized condition.
2085 // Can we use the existing condition code?
2086 if (AreEflagsSetFrom(condition, select)) {
2087 // Materialization was the previous instruction. Condition codes are right.
2088 cond = X86Condition(condition->GetCondition());
2089 } else {
2090 // No, we have to recreate the condition code.
2091 Register cond_reg = locations->InAt(2).AsRegister<Register>();
2092 __ testl(cond_reg, cond_reg);
2093 }
2094 } else {
2095 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002096 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
2097 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002098 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01002099 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002100 cond = X86Condition(condition->GetCondition());
2101 }
2102 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002103 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002104 Register cond_reg = locations->InAt(2).AsRegister<Register>();
2105 __ testl(cond_reg, cond_reg);
2106 }
2107
2108 // If the condition is true, overwrite the output, which already contains false.
2109 Location false_loc = locations->InAt(0);
2110 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002111 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002112 // 64 bit conditional move.
2113 Register false_high = false_loc.AsRegisterPairHigh<Register>();
2114 Register false_low = false_loc.AsRegisterPairLow<Register>();
2115 if (true_loc.IsRegisterPair()) {
2116 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
2117 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
2118 } else {
2119 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
2120 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
2121 }
2122 } else {
2123 // 32 bit conditional move.
2124 Register false_reg = false_loc.AsRegister<Register>();
2125 if (true_loc.IsRegister()) {
2126 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
2127 } else {
2128 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
2129 }
2130 }
2131 } else {
2132 NearLabel false_target;
2133 GenerateTestAndBranch<NearLabel>(
Andreas Gampe3db70682018-12-26 15:12:03 -08002134 select, /* condition_input_index= */ 2, /* true_target= */ nullptr, &false_target);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002135 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2136 __ Bind(&false_target);
2137 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002138}
2139
David Srbecky0cf44932015-12-09 14:09:59 +00002140void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002141 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00002142}
2143
David Srbeckyd28f4a02016-03-14 17:14:24 +00002144void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
2145 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002146}
2147
Vladimir Markodec78172020-06-19 15:31:23 +01002148void CodeGeneratorX86::IncreaseFrame(size_t adjustment) {
2149 __ subl(ESP, Immediate(adjustment));
2150 __ cfi().AdjustCFAOffset(adjustment);
2151}
2152
2153void CodeGeneratorX86::DecreaseFrame(size_t adjustment) {
2154 __ addl(ESP, Immediate(adjustment));
2155 __ cfi().AdjustCFAOffset(-adjustment);
2156}
2157
David Srbeckyc7098ff2016-02-09 14:30:11 +00002158void CodeGeneratorX86::GenerateNop() {
2159 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002160}
2161
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002162void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002163 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002164 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04002165 // Handle the long/FP comparisons made in instruction simplification.
2166 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002167 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04002168 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05002169 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00002170 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002171 locations->SetOut(Location::RequiresRegister());
2172 }
2173 break;
2174 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002175 case DataType::Type::kFloat32:
2176 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04002177 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002178 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
2179 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
2180 } else if (cond->InputAt(1)->IsConstant()) {
2181 locations->SetInAt(1, Location::RequiresFpuRegister());
2182 } else {
2183 locations->SetInAt(1, Location::Any());
2184 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002185 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002186 locations->SetOut(Location::RequiresRegister());
2187 }
2188 break;
2189 }
2190 default:
2191 locations->SetInAt(0, Location::RequiresRegister());
2192 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00002193 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002194 // We need a byte register.
2195 locations->SetOut(Location::RegisterLocation(ECX));
2196 }
2197 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002198 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002199}
2200
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002201void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002202 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002203 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002204 }
Mark Mendellc4701932015-04-10 13:18:51 -04002205
2206 LocationSummary* locations = cond->GetLocations();
2207 Location lhs = locations->InAt(0);
2208 Location rhs = locations->InAt(1);
2209 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05002210 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002211
2212 switch (cond->InputAt(0)->GetType()) {
2213 default: {
2214 // Integer case.
2215
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002216 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04002217 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01002218 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07002219 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04002220 return;
2221 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002222 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04002223 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2224 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002225 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002226 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04002227 GenerateFPJumps(cond, &true_label, &false_label);
2228 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002229 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002230 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04002231 GenerateFPJumps(cond, &true_label, &false_label);
2232 break;
2233 }
2234
2235 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002236 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002237
Roland Levillain4fa13f62015-07-06 18:11:54 +01002238 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04002239 __ Bind(&false_label);
2240 __ xorl(reg, reg);
2241 __ jmp(&done_label);
2242
Roland Levillain4fa13f62015-07-06 18:11:54 +01002243 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04002244 __ Bind(&true_label);
2245 __ movl(reg, Immediate(1));
2246 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002247}
2248
2249void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002250 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002251}
2252
2253void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002254 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002255}
2256
2257void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002258 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002259}
2260
2261void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002262 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002263}
2264
2265void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002266 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002267}
2268
2269void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002270 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002271}
2272
2273void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002274 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002275}
2276
2277void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002278 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002279}
2280
2281void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002282 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002283}
2284
2285void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002286 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002287}
2288
2289void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002290 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002291}
2292
2293void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002294 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002295}
2296
Aart Bike9f37602015-10-09 11:15:55 -07002297void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002298 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002299}
2300
2301void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002302 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002303}
2304
2305void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002306 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002307}
2308
2309void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002310 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002311}
2312
2313void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002314 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002315}
2316
2317void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002318 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002319}
2320
2321void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002322 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002323}
2324
2325void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002326 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002327}
2328
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002329void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002330 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002331 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002332 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002333}
2334
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002335void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002336 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002337}
2338
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002339void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2340 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002341 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002342 locations->SetOut(Location::ConstantLocation(constant));
2343}
2344
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002345void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002346 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002347}
2348
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002349void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002350 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002351 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002352 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002353}
2354
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002355void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002356 // Will be generated at use site.
2357}
2358
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002359void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2360 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002361 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002362 locations->SetOut(Location::ConstantLocation(constant));
2363}
2364
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002365void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002366 // Will be generated at use site.
2367}
2368
2369void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2370 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002371 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002372 locations->SetOut(Location::ConstantLocation(constant));
2373}
2374
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002375void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002376 // Will be generated at use site.
2377}
2378
Igor Murashkind01745e2017-04-05 16:40:31 -07002379void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2380 constructor_fence->SetLocations(nullptr);
2381}
2382
2383void InstructionCodeGeneratorX86::VisitConstructorFence(
2384 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2385 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2386}
2387
Calin Juravle27df7582015-04-17 19:12:31 +01002388void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2389 memory_barrier->SetLocations(nullptr);
2390}
2391
2392void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002393 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002394}
2395
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002396void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002397 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002398}
2399
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002400void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002401 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002402}
2403
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002404void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002405 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002406 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002407 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002408 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002409 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002410 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002411 case DataType::Type::kInt8:
2412 case DataType::Type::kUint16:
2413 case DataType::Type::kInt16:
2414 case DataType::Type::kInt32:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002415 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002416 break;
2417
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002418 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002419 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002420 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002421 break;
2422
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002423 case DataType::Type::kFloat32:
2424 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002425 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002426 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002427 break;
2428
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002429 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002430 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002431 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002432}
2433
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002434void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002435 switch (ret->InputAt(0)->GetType()) {
2436 case DataType::Type::kReference:
2437 case DataType::Type::kBool:
2438 case DataType::Type::kUint8:
2439 case DataType::Type::kInt8:
2440 case DataType::Type::kUint16:
2441 case DataType::Type::kInt16:
2442 case DataType::Type::kInt32:
2443 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
2444 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002445
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002446 case DataType::Type::kInt64:
2447 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2448 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
2449 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002450
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002451 case DataType::Type::kFloat32:
2452 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2453 if (GetGraph()->IsCompilingOsr()) {
2454 // To simplify callers of an OSR method, we put the return value in both
2455 // floating point and core registers.
2456 __ movd(EAX, XMM0);
2457 }
2458 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002459
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002460 case DataType::Type::kFloat64:
2461 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2462 if (GetGraph()->IsCompilingOsr()) {
2463 // To simplify callers of an OSR method, we put the return value in both
2464 // floating point and core registers.
2465 __ movd(EAX, XMM0);
2466 // Use XMM1 as temporary register to not clobber XMM0.
2467 __ movaps(XMM1, XMM0);
2468 __ psrlq(XMM1, Immediate(32));
2469 __ movd(EDX, XMM1);
2470 }
2471 break;
2472
2473 default:
2474 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002475 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002476 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002477}
2478
Calin Juravle175dc732015-08-25 15:42:32 +01002479void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2480 // The trampoline uses the same calling convention as dex calling conventions,
2481 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2482 // the method_idx.
2483 HandleInvoke(invoke);
2484}
2485
2486void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2487 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2488}
2489
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002490void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002491 // Explicit clinit checks triggered by static invokes must have been pruned by
2492 // art::PrepareForRegisterAllocation.
2493 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002494
Mark Mendellfb8d2792015-03-31 22:16:59 -04002495 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002496 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002497 if (invoke->GetLocations()->CanCall() &&
2498 invoke->HasPcRelativeMethodLoadKind() &&
2499 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).IsInvalid()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002500 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002501 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002502 return;
2503 }
2504
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01002505 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
Vladimir Marko86c87522020-05-11 16:55:55 +01002506 CriticalNativeCallingConventionVisitorX86 calling_convention_visitor(
2507 /*for_register_allocation=*/ true);
2508 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2509 } else {
2510 HandleInvoke(invoke);
2511 }
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002512
Vladimir Marko86c87522020-05-11 16:55:55 +01002513 // For PC-relative load kinds the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002514 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002515 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002516 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002517}
2518
Mark Mendell09ed1a32015-03-25 08:30:06 -04002519static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2520 if (invoke->GetLocations()->Intrinsified()) {
2521 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2522 intrinsic.Dispatch(invoke);
2523 return true;
2524 }
2525 return false;
2526}
2527
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002528void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002529 // Explicit clinit checks triggered by static invokes must have been pruned by
2530 // art::PrepareForRegisterAllocation.
2531 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002532
Mark Mendell09ed1a32015-03-25 08:30:06 -04002533 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2534 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002535 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002536
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002537 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002538 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002539 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002540}
2541
2542void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002543 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2544 if (intrinsic.TryDispatch(invoke)) {
2545 return;
2546 }
2547
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002548 HandleInvoke(invoke);
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002549
2550 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002551 // Add one temporary for inline cache update.
2552 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2553 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002554}
2555
2556void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002557 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002558 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002559}
2560
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002561void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002562 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2563 return;
2564 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002565
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002566 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002567 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002568}
2569
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002570void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002571 // This call to HandleInvoke allocates a temporary (core) register
2572 // which is also used to transfer the hidden argument from FP to
2573 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002574 HandleInvoke(invoke);
2575 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002576 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002577
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002578 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002579 // Add one temporary for inline cache update.
2580 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2581 }
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01002582
2583 // For PC-relative load kinds the invoke has an extra input, the PC-relative address base.
2584 if (IsPcRelativeMethodLoadKind(invoke->GetHiddenArgumentLoadKind())) {
2585 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2586 }
2587
2588 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
2589 invoke->GetLocations()->SetInAt(invoke->GetNumberOfArguments() - 1,
2590 Location::RequiresRegister());
2591 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002592}
2593
2594void CodeGeneratorX86::MaybeGenerateInlineCacheCheck(HInstruction* instruction, Register klass) {
2595 DCHECK_EQ(EAX, klass);
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002596 // We know the destination of an intrinsic, so no need to record inline
2597 // caches (also the intrinsic location builder doesn't request an additional
2598 // temporary).
2599 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002600 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002601 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002602 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
Nicolas Geoffray095dc462020-08-17 16:40:28 +01002603 ScopedProfilingInfoUse spiu(
2604 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
2605 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002606 if (info != nullptr) {
2607 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
2608 uint32_t address = reinterpret_cast32<uint32_t>(cache);
2609 if (kIsDebugBuild) {
2610 uint32_t temp_index = instruction->GetLocations()->GetTempCount() - 1u;
2611 CHECK_EQ(EBP, instruction->GetLocations()->GetTemp(temp_index).AsRegister<Register>());
2612 }
2613 Register temp = EBP;
2614 NearLabel done;
2615 __ movl(temp, Immediate(address));
2616 // Fast path for a monomorphic cache.
2617 __ cmpl(klass, Address(temp, InlineCache::ClassesOffset().Int32Value()));
2618 __ j(kEqual, &done);
2619 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(kQuickUpdateInlineCache).Int32Value());
2620 __ Bind(&done);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002621 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002622 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002623}
2624
2625void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2626 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002627 LocationSummary* locations = invoke->GetLocations();
2628 Register temp = locations->GetTemp(0).AsRegister<Register>();
2629 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002630 Location receiver = locations->InAt(0);
2631 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2632
Roland Levillain0d5a2812015-11-13 10:07:31 +00002633 // Set the hidden argument. This is safe to do this here, as XMM7
2634 // won't be modified thereafter, before the `call` instruction.
2635 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01002636 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
2637 __ movd(hidden_reg, locations->InAt(invoke->GetNumberOfArguments() - 1).AsRegister<Register>());
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00002638 } else if (invoke->GetHiddenArgumentLoadKind() != MethodLoadKind::kRuntimeCall) {
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01002639 codegen_->LoadMethod(invoke->GetHiddenArgumentLoadKind(), locations->GetTemp(0), invoke);
2640 __ movd(hidden_reg, temp);
2641 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002642
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002643 if (receiver.IsStackSlot()) {
2644 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002645 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002646 __ movl(temp, Address(temp, class_offset));
2647 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002648 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002649 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002650 }
Roland Levillain4d027112015-07-01 15:41:14 +01002651 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002652 // Instead of simply (possibly) unpoisoning `temp` here, we should
2653 // emit a read barrier for the previous class reference load.
2654 // However this is not required in practice, as this is an
2655 // intermediate/temporary reference and because the current
2656 // concurrent copying collector keeps the from-space memory
2657 // intact/accessible until the end of the marking phase (the
2658 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002659 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002660
2661 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
2662
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002663 // temp = temp->GetAddressOfIMT()
2664 __ movl(temp,
2665 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002666 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002667 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002668 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002669 __ movl(temp, Address(temp, method_offset));
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00002670 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRuntimeCall) {
2671 // We pass the method from the IMT in case of a conflict. This will ensure
2672 // we go into the runtime to resolve the actual method.
2673 __ movd(hidden_reg, temp);
2674 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002675 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002676 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002677 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002678
2679 DCHECK(!codegen_->IsLeafMethod());
2680 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2681}
2682
Orion Hodsonac141392017-01-13 11:53:47 +00002683void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00002684 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2685 if (intrinsic.TryDispatch(invoke)) {
2686 return;
2687 }
Orion Hodsonac141392017-01-13 11:53:47 +00002688 HandleInvoke(invoke);
2689}
2690
2691void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00002692 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2693 return;
2694 }
Orion Hodsonac141392017-01-13 11:53:47 +00002695 codegen_->GenerateInvokePolymorphicCall(invoke);
2696}
2697
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002698void LocationsBuilderX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2699 HandleInvoke(invoke);
2700}
2701
2702void InstructionCodeGeneratorX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2703 codegen_->GenerateInvokeCustomCall(invoke);
2704}
2705
Roland Levillain88cb1752014-10-20 16:36:47 +01002706void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2707 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002708 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002709 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002710 case DataType::Type::kInt32:
2711 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002712 locations->SetInAt(0, Location::RequiresRegister());
2713 locations->SetOut(Location::SameAsFirstInput());
2714 break;
2715
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002716 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002717 locations->SetInAt(0, Location::RequiresFpuRegister());
2718 locations->SetOut(Location::SameAsFirstInput());
2719 locations->AddTemp(Location::RequiresRegister());
2720 locations->AddTemp(Location::RequiresFpuRegister());
2721 break;
2722
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002723 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002724 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002725 locations->SetOut(Location::SameAsFirstInput());
2726 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002727 break;
2728
2729 default:
2730 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2731 }
2732}
2733
2734void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2735 LocationSummary* locations = neg->GetLocations();
2736 Location out = locations->Out();
2737 Location in = locations->InAt(0);
2738 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002739 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002740 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002741 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002742 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002743 break;
2744
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002745 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002746 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002747 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002748 __ negl(out.AsRegisterPairLow<Register>());
2749 // Negation is similar to subtraction from zero. The least
2750 // significant byte triggers a borrow when it is different from
2751 // zero; to take it into account, add 1 to the most significant
2752 // byte if the carry flag (CF) is set to 1 after the first NEGL
2753 // operation.
2754 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2755 __ negl(out.AsRegisterPairHigh<Register>());
2756 break;
2757
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002758 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002759 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002760 Register constant = locations->GetTemp(0).AsRegister<Register>();
2761 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002762 // Implement float negation with an exclusive or with value
2763 // 0x80000000 (mask for bit 31, representing the sign of a
2764 // single-precision floating-point number).
2765 __ movl(constant, Immediate(INT32_C(0x80000000)));
2766 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002767 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002768 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002769 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002770
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002771 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002772 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002773 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002774 // Implement double negation with an exclusive or with value
2775 // 0x8000000000000000 (mask for bit 63, representing the sign of
2776 // a double-precision floating-point number).
2777 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002778 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002779 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002780 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002781
2782 default:
2783 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2784 }
2785}
2786
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002787void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2788 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002789 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002790 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002791 locations->SetInAt(0, Location::RequiresFpuRegister());
2792 locations->SetInAt(1, Location::RequiresRegister());
2793 locations->SetOut(Location::SameAsFirstInput());
2794 locations->AddTemp(Location::RequiresFpuRegister());
2795}
2796
2797void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2798 LocationSummary* locations = neg->GetLocations();
2799 Location out = locations->Out();
2800 DCHECK(locations->InAt(0).Equals(out));
2801
2802 Register constant_area = locations->InAt(1).AsRegister<Register>();
2803 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002804 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002805 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2806 neg->GetBaseMethodAddress(),
2807 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002808 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2809 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002810 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2811 neg->GetBaseMethodAddress(),
2812 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002813 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2814 }
2815}
2816
Roland Levillaindff1f282014-11-05 14:15:05 +00002817void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002818 DataType::Type result_type = conversion->GetResultType();
2819 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002820 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2821 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002822
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002823 // The float-to-long and double-to-long type conversions rely on a
2824 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002825 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002826 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2827 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002828 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002829 : LocationSummary::kNoCall;
2830 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002831 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002832
Roland Levillaindff1f282014-11-05 14:15:05 +00002833 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002834 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002835 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002836 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002837 case DataType::Type::kUint8:
2838 case DataType::Type::kInt8:
2839 case DataType::Type::kUint16:
2840 case DataType::Type::kInt16:
2841 case DataType::Type::kInt32:
2842 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2843 // Make the output overlap to please the register allocator. This greatly simplifies
2844 // the validation of the linear scan implementation
2845 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2846 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002847 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002848 HInstruction* input = conversion->InputAt(0);
2849 Location input_location = input->IsConstant()
2850 ? Location::ConstantLocation(input->AsConstant())
2851 : Location::RegisterPairLocation(EAX, EDX);
2852 locations->SetInAt(0, input_location);
2853 // Make the output overlap to please the register allocator. This greatly simplifies
2854 // the validation of the linear scan implementation
2855 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2856 break;
2857 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002858
2859 default:
2860 LOG(FATAL) << "Unexpected type conversion from " << input_type
2861 << " to " << result_type;
2862 }
2863 break;
2864
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002865 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002866 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002867 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2868 locations->SetInAt(0, Location::Any());
2869 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002870 break;
2871
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002872 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002873 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002874 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002875 locations->SetInAt(0, Location::Any());
2876 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2877 break;
2878
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002879 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002880 locations->SetInAt(0, Location::RequiresFpuRegister());
2881 locations->SetOut(Location::RequiresRegister());
2882 locations->AddTemp(Location::RequiresFpuRegister());
2883 break;
2884
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002885 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002886 locations->SetInAt(0, Location::RequiresFpuRegister());
2887 locations->SetOut(Location::RequiresRegister());
2888 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002889 break;
2890
2891 default:
2892 LOG(FATAL) << "Unexpected type conversion from " << input_type
2893 << " to " << result_type;
2894 }
2895 break;
2896
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002897 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002898 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002899 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002900 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002901 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002902 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002903 case DataType::Type::kInt16:
2904 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002905 locations->SetInAt(0, Location::RegisterLocation(EAX));
2906 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2907 break;
2908
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002909 case DataType::Type::kFloat32:
2910 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002911 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002912 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2913 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2914
Vladimir Marko949c91f2015-01-27 10:48:44 +00002915 // The runtime helper puts the result in EAX, EDX.
2916 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002917 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002918 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002919
2920 default:
2921 LOG(FATAL) << "Unexpected type conversion from " << input_type
2922 << " to " << result_type;
2923 }
2924 break;
2925
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002926 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002927 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002928 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002929 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002930 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002931 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002932 case DataType::Type::kInt16:
2933 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002934 locations->SetInAt(0, Location::RequiresRegister());
2935 locations->SetOut(Location::RequiresFpuRegister());
2936 break;
2937
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002938 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002939 locations->SetInAt(0, Location::Any());
2940 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002941 break;
2942
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002943 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002944 locations->SetInAt(0, Location::RequiresFpuRegister());
2945 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002946 break;
2947
2948 default:
2949 LOG(FATAL) << "Unexpected type conversion from " << input_type
2950 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002951 }
Roland Levillaincff13742014-11-17 14:32:17 +00002952 break;
2953
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002954 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002955 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002956 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002957 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002958 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002959 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002960 case DataType::Type::kInt16:
2961 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002962 locations->SetInAt(0, Location::RequiresRegister());
2963 locations->SetOut(Location::RequiresFpuRegister());
2964 break;
2965
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002966 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002967 locations->SetInAt(0, Location::Any());
2968 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002969 break;
2970
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002971 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002972 locations->SetInAt(0, Location::RequiresFpuRegister());
2973 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002974 break;
2975
2976 default:
2977 LOG(FATAL) << "Unexpected type conversion from " << input_type
2978 << " to " << result_type;
2979 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002980 break;
2981
2982 default:
2983 LOG(FATAL) << "Unexpected type conversion from " << input_type
2984 << " to " << result_type;
2985 }
2986}
2987
2988void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2989 LocationSummary* locations = conversion->GetLocations();
2990 Location out = locations->Out();
2991 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002992 DataType::Type result_type = conversion->GetResultType();
2993 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002994 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2995 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002996 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002997 case DataType::Type::kUint8:
2998 switch (input_type) {
2999 case DataType::Type::kInt8:
3000 case DataType::Type::kUint16:
3001 case DataType::Type::kInt16:
3002 case DataType::Type::kInt32:
3003 if (in.IsRegister()) {
3004 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
3005 } else {
3006 DCHECK(in.GetConstant()->IsIntConstant());
3007 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
3008 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
3009 }
3010 break;
3011 case DataType::Type::kInt64:
3012 if (in.IsRegisterPair()) {
3013 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
3014 } else {
3015 DCHECK(in.GetConstant()->IsLongConstant());
3016 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3017 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
3018 }
3019 break;
3020
3021 default:
3022 LOG(FATAL) << "Unexpected type conversion from " << input_type
3023 << " to " << result_type;
3024 }
3025 break;
3026
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003027 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00003028 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003029 case DataType::Type::kUint8:
3030 case DataType::Type::kUint16:
3031 case DataType::Type::kInt16:
3032 case DataType::Type::kInt32:
3033 if (in.IsRegister()) {
3034 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
3035 } else {
3036 DCHECK(in.GetConstant()->IsIntConstant());
3037 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
3038 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
3039 }
3040 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003041 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003042 if (in.IsRegisterPair()) {
3043 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
3044 } else {
3045 DCHECK(in.GetConstant()->IsLongConstant());
3046 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3047 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
3048 }
3049 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003050
3051 default:
3052 LOG(FATAL) << "Unexpected type conversion from " << input_type
3053 << " to " << result_type;
3054 }
3055 break;
3056
3057 case DataType::Type::kUint16:
3058 switch (input_type) {
3059 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003060 case DataType::Type::kInt16:
3061 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003062 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003063 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
3064 } else if (in.IsStackSlot()) {
3065 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003066 } else {
3067 DCHECK(in.GetConstant()->IsIntConstant());
3068 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003069 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
3070 }
3071 break;
3072 case DataType::Type::kInt64:
3073 if (in.IsRegisterPair()) {
3074 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
3075 } else if (in.IsDoubleStackSlot()) {
3076 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
3077 } else {
3078 DCHECK(in.GetConstant()->IsLongConstant());
3079 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3080 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003081 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00003082 break;
3083
3084 default:
3085 LOG(FATAL) << "Unexpected type conversion from " << input_type
3086 << " to " << result_type;
3087 }
3088 break;
3089
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003090 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00003091 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003092 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003093 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00003094 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003095 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00003096 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003097 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00003098 } else {
3099 DCHECK(in.GetConstant()->IsIntConstant());
3100 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003101 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00003102 }
3103 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003104 case DataType::Type::kInt64:
3105 if (in.IsRegisterPair()) {
3106 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
3107 } else if (in.IsDoubleStackSlot()) {
3108 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
3109 } else {
3110 DCHECK(in.GetConstant()->IsLongConstant());
3111 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3112 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
3113 }
3114 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00003115
3116 default:
3117 LOG(FATAL) << "Unexpected type conversion from " << input_type
3118 << " to " << result_type;
3119 }
3120 break;
3121
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003122 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00003123 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003124 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00003125 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003126 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00003127 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003128 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00003129 } else {
3130 DCHECK(in.IsConstant());
3131 DCHECK(in.GetConstant()->IsLongConstant());
3132 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003133 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00003134 }
3135 break;
3136
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003137 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00003138 XmmRegister input = in.AsFpuRegister<XmmRegister>();
3139 Register output = out.AsRegister<Register>();
3140 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04003141 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00003142
3143 __ movl(output, Immediate(kPrimIntMax));
3144 // temp = int-to-float(output)
3145 __ cvtsi2ss(temp, output);
3146 // if input >= temp goto done
3147 __ comiss(input, temp);
3148 __ j(kAboveEqual, &done);
3149 // if input == NaN goto nan
3150 __ j(kUnordered, &nan);
3151 // output = float-to-int-truncate(input)
3152 __ cvttss2si(output, input);
3153 __ jmp(&done);
3154 __ Bind(&nan);
3155 // output = 0
3156 __ xorl(output, output);
3157 __ Bind(&done);
3158 break;
3159 }
3160
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003161 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003162 XmmRegister input = in.AsFpuRegister<XmmRegister>();
3163 Register output = out.AsRegister<Register>();
3164 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04003165 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003166
3167 __ movl(output, Immediate(kPrimIntMax));
3168 // temp = int-to-double(output)
3169 __ cvtsi2sd(temp, output);
3170 // if input >= temp goto done
3171 __ comisd(input, temp);
3172 __ j(kAboveEqual, &done);
3173 // if input == NaN goto nan
3174 __ j(kUnordered, &nan);
3175 // output = double-to-int-truncate(input)
3176 __ cvttsd2si(output, input);
3177 __ jmp(&done);
3178 __ Bind(&nan);
3179 // output = 0
3180 __ xorl(output, output);
3181 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00003182 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003183 }
Roland Levillain946e1432014-11-11 17:35:19 +00003184
3185 default:
3186 LOG(FATAL) << "Unexpected type conversion from " << input_type
3187 << " to " << result_type;
3188 }
3189 break;
3190
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003191 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00003192 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003193 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003194 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003195 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003196 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003197 case DataType::Type::kInt16:
3198 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00003199 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
3200 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003201 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00003202 __ cdq();
3203 break;
3204
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003205 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01003206 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003207 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00003208 break;
3209
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003210 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01003211 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003212 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00003213 break;
3214
3215 default:
3216 LOG(FATAL) << "Unexpected type conversion from " << input_type
3217 << " to " << result_type;
3218 }
3219 break;
3220
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003221 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00003222 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003223 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003224 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003225 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003226 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003227 case DataType::Type::kInt16:
3228 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003229 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003230 break;
3231
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003232 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003233 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00003234
Roland Levillain232ade02015-04-20 15:14:36 +01003235 // Create stack space for the call to
3236 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
3237 // TODO: enhance register allocator to ask for stack temporaries.
3238 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003239 adjustment = DataType::Size(DataType::Type::kInt64);
Vladimir Markodec78172020-06-19 15:31:23 +01003240 codegen_->IncreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003241 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003242
Roland Levillain232ade02015-04-20 15:14:36 +01003243 // Load the value to the FP stack, using temporaries if needed.
3244 PushOntoFPStack(in, 0, adjustment, false, true);
3245
3246 if (out.IsStackSlot()) {
3247 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
3248 } else {
3249 __ fstps(Address(ESP, 0));
3250 Location stack_temp = Location::StackSlot(0);
3251 codegen_->Move32(out, stack_temp);
3252 }
3253
3254 // Remove the temporary stack space we allocated.
3255 if (adjustment != 0) {
Vladimir Markodec78172020-06-19 15:31:23 +01003256 codegen_->DecreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003257 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003258 break;
3259 }
3260
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003261 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003262 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003263 break;
3264
3265 default:
3266 LOG(FATAL) << "Unexpected type conversion from " << input_type
3267 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003268 }
Roland Levillaincff13742014-11-17 14:32:17 +00003269 break;
3270
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003271 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00003272 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003273 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003274 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003275 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003276 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003277 case DataType::Type::kInt16:
3278 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003279 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003280 break;
3281
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003282 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003283 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00003284
Roland Levillain232ade02015-04-20 15:14:36 +01003285 // Create stack space for the call to
3286 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
3287 // TODO: enhance register allocator to ask for stack temporaries.
3288 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003289 adjustment = DataType::Size(DataType::Type::kInt64);
Vladimir Markodec78172020-06-19 15:31:23 +01003290 codegen_->IncreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003291 }
3292
3293 // Load the value to the FP stack, using temporaries if needed.
3294 PushOntoFPStack(in, 0, adjustment, false, true);
3295
3296 if (out.IsDoubleStackSlot()) {
3297 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
3298 } else {
3299 __ fstpl(Address(ESP, 0));
3300 Location stack_temp = Location::DoubleStackSlot(0);
3301 codegen_->Move64(out, stack_temp);
3302 }
3303
3304 // Remove the temporary stack space we allocated.
3305 if (adjustment != 0) {
Vladimir Markodec78172020-06-19 15:31:23 +01003306 codegen_->DecreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003307 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003308 break;
3309 }
3310
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003311 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003312 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003313 break;
3314
3315 default:
3316 LOG(FATAL) << "Unexpected type conversion from " << input_type
3317 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003318 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003319 break;
3320
3321 default:
3322 LOG(FATAL) << "Unexpected type conversion from " << input_type
3323 << " to " << result_type;
3324 }
3325}
3326
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003327void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003328 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003329 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003330 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003331 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05003332 locations->SetInAt(0, Location::RequiresRegister());
3333 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3334 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3335 break;
3336 }
3337
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003338 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003339 locations->SetInAt(0, Location::RequiresRegister());
3340 locations->SetInAt(1, Location::Any());
3341 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003342 break;
3343 }
3344
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003345 case DataType::Type::kFloat32:
3346 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003347 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003348 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3349 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003350 } else if (add->InputAt(1)->IsConstant()) {
3351 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003352 } else {
3353 locations->SetInAt(1, Location::Any());
3354 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003355 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003356 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003357 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003358
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003359 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003360 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Elliott Hughesc1896c92018-11-29 11:33:18 -08003361 UNREACHABLE();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003362 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003363}
3364
3365void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
3366 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003367 Location first = locations->InAt(0);
3368 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05003369 Location out = locations->Out();
3370
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003371 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003372 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003373 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003374 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3375 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003376 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3377 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05003378 } else {
3379 __ leal(out.AsRegister<Register>(), Address(
3380 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3381 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003382 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003383 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3384 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3385 __ addl(out.AsRegister<Register>(), Immediate(value));
3386 } else {
3387 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3388 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003389 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003390 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003391 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003392 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003393 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003394 }
3395
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003396 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003397 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003398 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3399 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003400 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003401 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3402 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003403 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003404 } else {
3405 DCHECK(second.IsConstant()) << second;
3406 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3407 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3408 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003409 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003410 break;
3411 }
3412
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003413 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003414 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003415 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003416 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3417 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003418 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003419 __ addss(first.AsFpuRegister<XmmRegister>(),
3420 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003421 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3422 const_area->GetBaseMethodAddress(),
3423 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003424 } else {
3425 DCHECK(second.IsStackSlot());
3426 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003427 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003428 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003429 }
3430
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003431 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003432 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003433 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003434 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3435 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003436 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003437 __ addsd(first.AsFpuRegister<XmmRegister>(),
3438 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003439 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3440 const_area->GetBaseMethodAddress(),
3441 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003442 } else {
3443 DCHECK(second.IsDoubleStackSlot());
3444 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003445 }
3446 break;
3447 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003448
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003449 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003450 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003451 }
3452}
3453
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003454void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003455 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003456 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003457 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003458 case DataType::Type::kInt32:
3459 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003460 locations->SetInAt(0, Location::RequiresRegister());
3461 locations->SetInAt(1, Location::Any());
3462 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003463 break;
3464 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003465 case DataType::Type::kFloat32:
3466 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003467 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003468 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3469 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003470 } else if (sub->InputAt(1)->IsConstant()) {
3471 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003472 } else {
3473 locations->SetInAt(1, Location::Any());
3474 }
Calin Juravle11351682014-10-23 15:38:15 +01003475 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003476 break;
Calin Juravle11351682014-10-23 15:38:15 +01003477 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003478
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003479 default:
Calin Juravle11351682014-10-23 15:38:15 +01003480 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003481 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003482}
3483
3484void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3485 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003486 Location first = locations->InAt(0);
3487 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003488 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003489 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003490 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003491 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003492 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003493 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003494 __ subl(first.AsRegister<Register>(),
3495 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003496 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003497 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003498 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003499 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003500 }
3501
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003502 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003503 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003504 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3505 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003506 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003507 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003508 __ sbbl(first.AsRegisterPairHigh<Register>(),
3509 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003510 } else {
3511 DCHECK(second.IsConstant()) << second;
3512 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3513 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3514 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003515 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003516 break;
3517 }
3518
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003519 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003520 if (second.IsFpuRegister()) {
3521 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3522 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3523 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003524 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003525 __ subss(first.AsFpuRegister<XmmRegister>(),
3526 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003527 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3528 const_area->GetBaseMethodAddress(),
3529 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003530 } else {
3531 DCHECK(second.IsStackSlot());
3532 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3533 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003534 break;
Calin Juravle11351682014-10-23 15:38:15 +01003535 }
3536
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003537 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003538 if (second.IsFpuRegister()) {
3539 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3540 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3541 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003542 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003543 __ subsd(first.AsFpuRegister<XmmRegister>(),
3544 codegen_->LiteralDoubleAddress(
3545 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003546 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003547 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3548 } else {
3549 DCHECK(second.IsDoubleStackSlot());
3550 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3551 }
Calin Juravle11351682014-10-23 15:38:15 +01003552 break;
3553 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003554
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003555 default:
Calin Juravle11351682014-10-23 15:38:15 +01003556 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003557 }
3558}
3559
Calin Juravle34bacdf2014-10-07 20:23:36 +01003560void LocationsBuilderX86::VisitMul(HMul* mul) {
3561 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003562 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003563 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003564 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003565 locations->SetInAt(0, Location::RequiresRegister());
3566 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003567 if (mul->InputAt(1)->IsIntConstant()) {
3568 // Can use 3 operand multiply.
3569 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3570 } else {
3571 locations->SetOut(Location::SameAsFirstInput());
3572 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003573 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003574 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003575 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003576 locations->SetInAt(1, Location::Any());
3577 locations->SetOut(Location::SameAsFirstInput());
3578 // Needed for imul on 32bits with 64bits output.
3579 locations->AddTemp(Location::RegisterLocation(EAX));
3580 locations->AddTemp(Location::RegisterLocation(EDX));
3581 break;
3582 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003583 case DataType::Type::kFloat32:
3584 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003585 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003586 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3587 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003588 } else if (mul->InputAt(1)->IsConstant()) {
3589 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003590 } else {
3591 locations->SetInAt(1, Location::Any());
3592 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003593 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003594 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003595 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003596
3597 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003598 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003599 }
3600}
3601
3602void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3603 LocationSummary* locations = mul->GetLocations();
3604 Location first = locations->InAt(0);
3605 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003606 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003607
3608 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003609 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003610 // The constant may have ended up in a register, so test explicitly to avoid
3611 // problems where the output may not be the same as the first operand.
3612 if (mul->InputAt(1)->IsIntConstant()) {
3613 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3614 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3615 } else if (second.IsRegister()) {
3616 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003617 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003618 } else {
3619 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003620 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003621 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003622 }
3623 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003624
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003625 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003626 Register in1_hi = first.AsRegisterPairHigh<Register>();
3627 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003628 Register eax = locations->GetTemp(0).AsRegister<Register>();
3629 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003630
3631 DCHECK_EQ(EAX, eax);
3632 DCHECK_EQ(EDX, edx);
3633
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003634 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003635 // output: in1
3636 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3637 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3638 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003639 if (second.IsConstant()) {
3640 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003641
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003642 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3643 int32_t low_value = Low32Bits(value);
3644 int32_t high_value = High32Bits(value);
3645 Immediate low(low_value);
3646 Immediate high(high_value);
3647
3648 __ movl(eax, high);
3649 // eax <- in1.lo * in2.hi
3650 __ imull(eax, in1_lo);
3651 // in1.hi <- in1.hi * in2.lo
3652 __ imull(in1_hi, low);
3653 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3654 __ addl(in1_hi, eax);
3655 // move in2_lo to eax to prepare for double precision
3656 __ movl(eax, low);
3657 // edx:eax <- in1.lo * in2.lo
3658 __ mull(in1_lo);
3659 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3660 __ addl(in1_hi, edx);
3661 // in1.lo <- (in1.lo * in2.lo)[31:0];
3662 __ movl(in1_lo, eax);
3663 } else if (second.IsRegisterPair()) {
3664 Register in2_hi = second.AsRegisterPairHigh<Register>();
3665 Register in2_lo = second.AsRegisterPairLow<Register>();
3666
3667 __ movl(eax, in2_hi);
3668 // eax <- in1.lo * in2.hi
3669 __ imull(eax, in1_lo);
3670 // in1.hi <- in1.hi * in2.lo
3671 __ imull(in1_hi, in2_lo);
3672 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3673 __ addl(in1_hi, eax);
3674 // move in1_lo to eax to prepare for double precision
3675 __ movl(eax, in1_lo);
3676 // edx:eax <- in1.lo * in2.lo
3677 __ mull(in2_lo);
3678 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3679 __ addl(in1_hi, edx);
3680 // in1.lo <- (in1.lo * in2.lo)[31:0];
3681 __ movl(in1_lo, eax);
3682 } else {
3683 DCHECK(second.IsDoubleStackSlot()) << second;
3684 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3685 Address in2_lo(ESP, second.GetStackIndex());
3686
3687 __ movl(eax, in2_hi);
3688 // eax <- in1.lo * in2.hi
3689 __ imull(eax, in1_lo);
3690 // in1.hi <- in1.hi * in2.lo
3691 __ imull(in1_hi, in2_lo);
3692 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3693 __ addl(in1_hi, eax);
3694 // move in1_lo to eax to prepare for double precision
3695 __ movl(eax, in1_lo);
3696 // edx:eax <- in1.lo * in2.lo
3697 __ mull(in2_lo);
3698 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3699 __ addl(in1_hi, edx);
3700 // in1.lo <- (in1.lo * in2.lo)[31:0];
3701 __ movl(in1_lo, eax);
3702 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003703
3704 break;
3705 }
3706
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003707 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003708 DCHECK(first.Equals(locations->Out()));
3709 if (second.IsFpuRegister()) {
3710 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3711 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3712 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003713 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003714 __ mulss(first.AsFpuRegister<XmmRegister>(),
3715 codegen_->LiteralFloatAddress(
3716 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003717 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003718 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3719 } else {
3720 DCHECK(second.IsStackSlot());
3721 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3722 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003723 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003724 }
3725
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003726 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003727 DCHECK(first.Equals(locations->Out()));
3728 if (second.IsFpuRegister()) {
3729 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3730 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3731 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003732 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003733 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3734 codegen_->LiteralDoubleAddress(
3735 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003736 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003737 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3738 } else {
3739 DCHECK(second.IsDoubleStackSlot());
3740 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3741 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003742 break;
3743 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003744
3745 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003746 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003747 }
3748}
3749
Roland Levillain232ade02015-04-20 15:14:36 +01003750void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3751 uint32_t temp_offset,
3752 uint32_t stack_adjustment,
3753 bool is_fp,
3754 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003755 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003756 DCHECK(!is_wide);
3757 if (is_fp) {
3758 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3759 } else {
3760 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3761 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003762 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003763 DCHECK(is_wide);
3764 if (is_fp) {
3765 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3766 } else {
3767 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3768 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003769 } else {
3770 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003771 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003772 Location stack_temp = Location::StackSlot(temp_offset);
3773 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003774 if (is_fp) {
3775 __ flds(Address(ESP, temp_offset));
3776 } else {
3777 __ filds(Address(ESP, temp_offset));
3778 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003779 } else {
3780 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3781 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003782 if (is_fp) {
3783 __ fldl(Address(ESP, temp_offset));
3784 } else {
3785 __ fildl(Address(ESP, temp_offset));
3786 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003787 }
3788 }
3789}
3790
3791void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003792 DataType::Type type = rem->GetResultType();
3793 bool is_float = type == DataType::Type::kFloat32;
3794 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003795 LocationSummary* locations = rem->GetLocations();
3796 Location first = locations->InAt(0);
3797 Location second = locations->InAt(1);
3798 Location out = locations->Out();
3799
3800 // Create stack space for 2 elements.
3801 // TODO: enhance register allocator to ask for stack temporaries.
Vladimir Markodec78172020-06-19 15:31:23 +01003802 codegen_->IncreaseFrame(2 * elem_size);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003803
3804 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003805 const bool is_wide = !is_float;
Andreas Gampe3db70682018-12-26 15:12:03 -08003806 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp= */ true, is_wide);
3807 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp= */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003808
3809 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003810 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003811 __ Bind(&retry);
3812 __ fprem();
3813
3814 // Move FP status to AX.
3815 __ fstsw();
3816
3817 // And see if the argument reduction is complete. This is signaled by the
3818 // C2 FPU flag bit set to 0.
3819 __ andl(EAX, Immediate(kC2ConditionMask));
3820 __ j(kNotEqual, &retry);
3821
3822 // We have settled on the final value. Retrieve it into an XMM register.
3823 // Store FP top of stack to real stack.
3824 if (is_float) {
3825 __ fsts(Address(ESP, 0));
3826 } else {
3827 __ fstl(Address(ESP, 0));
3828 }
3829
3830 // Pop the 2 items from the FP stack.
3831 __ fucompp();
3832
3833 // Load the value from the stack into an XMM register.
3834 DCHECK(out.IsFpuRegister()) << out;
3835 if (is_float) {
3836 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3837 } else {
3838 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3839 }
3840
3841 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01003842 codegen_->DecreaseFrame(2 * elem_size);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003843}
3844
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003845
3846void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3847 DCHECK(instruction->IsDiv() || instruction->IsRem());
3848
3849 LocationSummary* locations = instruction->GetLocations();
3850 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003851 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003852
3853 Register out_register = locations->Out().AsRegister<Register>();
3854 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003855 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003856
3857 DCHECK(imm == 1 || imm == -1);
3858
3859 if (instruction->IsRem()) {
3860 __ xorl(out_register, out_register);
3861 } else {
3862 __ movl(out_register, input_register);
3863 if (imm == -1) {
3864 __ negl(out_register);
3865 }
3866 }
3867}
3868
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303869void InstructionCodeGeneratorX86::RemByPowerOfTwo(HRem* instruction) {
3870 LocationSummary* locations = instruction->GetLocations();
3871 Location second = locations->InAt(1);
3872
3873 Register out = locations->Out().AsRegister<Register>();
3874 Register numerator = locations->InAt(0).AsRegister<Register>();
3875
3876 int32_t imm = Int64FromConstant(second.GetConstant());
3877 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3878 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3879
3880 Register tmp = locations->GetTemp(0).AsRegister<Register>();
3881 NearLabel done;
3882 __ movl(out, numerator);
3883 __ andl(out, Immediate(abs_imm-1));
3884 __ j(Condition::kZero, &done);
3885 __ leal(tmp, Address(out, static_cast<int32_t>(~(abs_imm-1))));
3886 __ testl(numerator, numerator);
3887 __ cmovl(Condition::kLess, out, tmp);
3888 __ Bind(&done);
3889}
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003890
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003891void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003892 LocationSummary* locations = instruction->GetLocations();
3893
3894 Register out_register = locations->Out().AsRegister<Register>();
3895 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003896 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003897 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3898 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003899
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003900 Register num = locations->GetTemp(0).AsRegister<Register>();
3901
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003902 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003903 __ testl(input_register, input_register);
3904 __ cmovl(kGreaterEqual, num, input_register);
3905 int shift = CTZ(imm);
3906 __ sarl(num, Immediate(shift));
3907
3908 if (imm < 0) {
3909 __ negl(num);
3910 }
3911
3912 __ movl(out_register, num);
3913}
3914
3915void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3916 DCHECK(instruction->IsDiv() || instruction->IsRem());
3917
3918 LocationSummary* locations = instruction->GetLocations();
3919 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3920
3921 Register eax = locations->InAt(0).AsRegister<Register>();
3922 Register out = locations->Out().AsRegister<Register>();
3923 Register num;
3924 Register edx;
3925
3926 if (instruction->IsDiv()) {
3927 edx = locations->GetTemp(0).AsRegister<Register>();
3928 num = locations->GetTemp(1).AsRegister<Register>();
3929 } else {
3930 edx = locations->Out().AsRegister<Register>();
3931 num = locations->GetTemp(0).AsRegister<Register>();
3932 }
3933
3934 DCHECK_EQ(EAX, eax);
3935 DCHECK_EQ(EDX, edx);
3936 if (instruction->IsDiv()) {
3937 DCHECK_EQ(EAX, out);
3938 } else {
3939 DCHECK_EQ(EDX, out);
3940 }
3941
3942 int64_t magic;
3943 int shift;
Andreas Gampe3db70682018-12-26 15:12:03 -08003944 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003945
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003946 // Save the numerator.
3947 __ movl(num, eax);
3948
3949 // EAX = magic
3950 __ movl(eax, Immediate(magic));
3951
3952 // EDX:EAX = magic * numerator
3953 __ imull(num);
3954
3955 if (imm > 0 && magic < 0) {
3956 // EDX += num
3957 __ addl(edx, num);
3958 } else if (imm < 0 && magic > 0) {
3959 __ subl(edx, num);
3960 }
3961
3962 // Shift if needed.
3963 if (shift != 0) {
3964 __ sarl(edx, Immediate(shift));
3965 }
3966
3967 // EDX += 1 if EDX < 0
3968 __ movl(eax, edx);
3969 __ shrl(edx, Immediate(31));
3970 __ addl(edx, eax);
3971
3972 if (instruction->IsRem()) {
3973 __ movl(eax, num);
3974 __ imull(edx, Immediate(imm));
3975 __ subl(eax, edx);
3976 __ movl(edx, eax);
3977 } else {
3978 __ movl(eax, edx);
3979 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003980}
3981
Calin Juravlebacfec32014-11-14 15:54:36 +00003982void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3983 DCHECK(instruction->IsDiv() || instruction->IsRem());
3984
3985 LocationSummary* locations = instruction->GetLocations();
3986 Location out = locations->Out();
3987 Location first = locations->InAt(0);
3988 Location second = locations->InAt(1);
3989 bool is_div = instruction->IsDiv();
3990
3991 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003992 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003993 DCHECK_EQ(EAX, first.AsRegister<Register>());
3994 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003995
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003996 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003997 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003998
3999 if (imm == 0) {
4000 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
4001 } else if (imm == 1 || imm == -1) {
4002 DivRemOneOrMinusOne(instruction);
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05304003 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
4004 if (is_div) {
4005 DivByPowerOfTwo(instruction->AsDiv());
4006 } else {
4007 RemByPowerOfTwo(instruction->AsRem());
4008 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004009 } else {
4010 DCHECK(imm <= -2 || imm >= 2);
4011 GenerateDivRemWithAnyConstant(instruction);
4012 }
4013 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004014 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00004015 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004016 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00004017
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004018 Register second_reg = second.AsRegister<Register>();
4019 // 0x80000000/-1 triggers an arithmetic exception!
4020 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
4021 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00004022
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004023 __ cmpl(second_reg, Immediate(-1));
4024 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00004025
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004026 // edx:eax <- sign-extended of eax
4027 __ cdq();
4028 // eax = quotient, edx = remainder
4029 __ idivl(second_reg);
4030 __ Bind(slow_path->GetExitLabel());
4031 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004032 break;
4033 }
4034
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004035 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004036 InvokeRuntimeCallingConvention calling_convention;
4037 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
4038 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
4039 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
4040 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
4041 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
4042 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
4043
4044 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004045 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004046 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004047 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004048 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004049 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004050 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004051 break;
4052 }
4053
4054 default:
4055 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
4056 }
4057}
4058
Calin Juravle7c4954d2014-10-28 16:57:40 +00004059void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004060 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004061 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004062 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01004063 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004064
Calin Juravle7c4954d2014-10-28 16:57:40 +00004065 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004066 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00004067 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004068 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00004069 locations->SetOut(Location::SameAsFirstInput());
4070 // Intel uses edx:eax as the dividend.
4071 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004072 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
4073 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
4074 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01004075 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004076 locations->AddTemp(Location::RequiresRegister());
4077 }
Calin Juravled0d48522014-11-04 16:40:20 +00004078 break;
4079 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004080 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004081 InvokeRuntimeCallingConvention calling_convention;
4082 locations->SetInAt(0, Location::RegisterPairLocation(
4083 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4084 locations->SetInAt(1, Location::RegisterPairLocation(
4085 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4086 // Runtime helper puts the result in EAX, EDX.
4087 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00004088 break;
4089 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004090 case DataType::Type::kFloat32:
4091 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004092 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00004093 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
4094 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00004095 } else if (div->InputAt(1)->IsConstant()) {
4096 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00004097 } else {
4098 locations->SetInAt(1, Location::Any());
4099 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004100 locations->SetOut(Location::SameAsFirstInput());
4101 break;
4102 }
4103
4104 default:
4105 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4106 }
4107}
4108
4109void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
4110 LocationSummary* locations = div->GetLocations();
4111 Location first = locations->InAt(0);
4112 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004113
4114 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004115 case DataType::Type::kInt32:
4116 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004117 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004118 break;
4119 }
4120
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004121 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04004122 if (second.IsFpuRegister()) {
4123 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
4124 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
4125 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00004126 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04004127 __ divss(first.AsFpuRegister<XmmRegister>(),
4128 codegen_->LiteralFloatAddress(
4129 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004130 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04004131 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
4132 } else {
4133 DCHECK(second.IsStackSlot());
4134 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
4135 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004136 break;
4137 }
4138
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004139 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04004140 if (second.IsFpuRegister()) {
4141 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
4142 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
4143 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00004144 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04004145 __ divsd(first.AsFpuRegister<XmmRegister>(),
4146 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004147 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
4148 const_area->GetBaseMethodAddress(),
4149 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04004150 } else {
4151 DCHECK(second.IsDoubleStackSlot());
4152 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
4153 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004154 break;
4155 }
4156
4157 default:
4158 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4159 }
4160}
4161
Calin Juravlebacfec32014-11-14 15:54:36 +00004162void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004163 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004164
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004165 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004166 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004167 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01004168 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00004169
Calin Juravled2ec87d2014-12-08 14:24:46 +00004170 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004171 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004172 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004173 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00004174 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004175 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
4176 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
4177 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01004178 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004179 locations->AddTemp(Location::RequiresRegister());
4180 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004181 break;
4182 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004183 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004184 InvokeRuntimeCallingConvention calling_convention;
4185 locations->SetInAt(0, Location::RegisterPairLocation(
4186 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4187 locations->SetInAt(1, Location::RegisterPairLocation(
4188 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4189 // Runtime helper puts the result in EAX, EDX.
4190 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
4191 break;
4192 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004193 case DataType::Type::kFloat64:
4194 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05004195 locations->SetInAt(0, Location::Any());
4196 locations->SetInAt(1, Location::Any());
4197 locations->SetOut(Location::RequiresFpuRegister());
4198 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00004199 break;
4200 }
4201
4202 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004203 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004204 }
4205}
4206
4207void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004208 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00004209 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004210 case DataType::Type::kInt32:
4211 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004212 GenerateDivRemIntegral(rem);
4213 break;
4214 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004215 case DataType::Type::kFloat32:
4216 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05004217 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00004218 break;
4219 }
4220 default:
4221 LOG(FATAL) << "Unexpected rem type " << type;
4222 }
4223}
4224
Aart Bik1f8d51b2018-02-15 10:42:37 -08004225static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
4226 LocationSummary* locations = new (allocator) LocationSummary(minmax);
4227 switch (minmax->GetResultType()) {
4228 case DataType::Type::kInt32:
4229 locations->SetInAt(0, Location::RequiresRegister());
4230 locations->SetInAt(1, Location::RequiresRegister());
4231 locations->SetOut(Location::SameAsFirstInput());
4232 break;
4233 case DataType::Type::kInt64:
4234 locations->SetInAt(0, Location::RequiresRegister());
4235 locations->SetInAt(1, Location::RequiresRegister());
4236 locations->SetOut(Location::SameAsFirstInput());
4237 // Register to use to perform a long subtract to set cc.
4238 locations->AddTemp(Location::RequiresRegister());
4239 break;
4240 case DataType::Type::kFloat32:
4241 locations->SetInAt(0, Location::RequiresFpuRegister());
4242 locations->SetInAt(1, Location::RequiresFpuRegister());
4243 locations->SetOut(Location::SameAsFirstInput());
4244 locations->AddTemp(Location::RequiresRegister());
4245 break;
4246 case DataType::Type::kFloat64:
4247 locations->SetInAt(0, Location::RequiresFpuRegister());
4248 locations->SetInAt(1, Location::RequiresFpuRegister());
4249 locations->SetOut(Location::SameAsFirstInput());
4250 break;
4251 default:
4252 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
4253 }
4254}
4255
Aart Bik351df3e2018-03-07 11:54:57 -08004256void InstructionCodeGeneratorX86::GenerateMinMaxInt(LocationSummary* locations,
4257 bool is_min,
4258 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08004259 Location op1_loc = locations->InAt(0);
4260 Location op2_loc = locations->InAt(1);
4261
4262 // Shortcut for same input locations.
4263 if (op1_loc.Equals(op2_loc)) {
4264 // Can return immediately, as op1_loc == out_loc.
4265 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
4266 // a copy here.
4267 DCHECK(locations->Out().Equals(op1_loc));
4268 return;
4269 }
4270
4271 if (type == DataType::Type::kInt64) {
4272 // Need to perform a subtract to get the sign right.
4273 // op1 is already in the same location as the output.
4274 Location output = locations->Out();
4275 Register output_lo = output.AsRegisterPairLow<Register>();
4276 Register output_hi = output.AsRegisterPairHigh<Register>();
4277
4278 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
4279 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
4280
4281 // The comparison is performed by subtracting the second operand from
4282 // the first operand and then setting the status flags in the same
4283 // manner as the SUB instruction."
4284 __ cmpl(output_lo, op2_lo);
4285
4286 // Now use a temp and the borrow to finish the subtraction of op2_hi.
4287 Register temp = locations->GetTemp(0).AsRegister<Register>();
4288 __ movl(temp, output_hi);
4289 __ sbbl(temp, op2_hi);
4290
4291 // Now the condition code is correct.
4292 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
4293 __ cmovl(cond, output_lo, op2_lo);
4294 __ cmovl(cond, output_hi, op2_hi);
4295 } else {
4296 DCHECK_EQ(type, DataType::Type::kInt32);
4297 Register out = locations->Out().AsRegister<Register>();
4298 Register op2 = op2_loc.AsRegister<Register>();
4299
4300 // (out := op1)
4301 // out <=? op2
4302 // if out is min jmp done
4303 // out := op2
4304 // done:
4305
4306 __ cmpl(out, op2);
4307 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
4308 __ cmovl(cond, out, op2);
4309 }
4310}
4311
4312void InstructionCodeGeneratorX86::GenerateMinMaxFP(LocationSummary* locations,
4313 bool is_min,
4314 DataType::Type type) {
4315 Location op1_loc = locations->InAt(0);
4316 Location op2_loc = locations->InAt(1);
4317 Location out_loc = locations->Out();
4318 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
4319
4320 // Shortcut for same input locations.
4321 if (op1_loc.Equals(op2_loc)) {
4322 DCHECK(out_loc.Equals(op1_loc));
4323 return;
4324 }
4325
4326 // (out := op1)
4327 // out <=? op2
4328 // if Nan jmp Nan_label
4329 // if out is min jmp done
4330 // if op2 is min jmp op2_label
4331 // handle -0/+0
4332 // jmp done
4333 // Nan_label:
4334 // out := NaN
4335 // op2_label:
4336 // out := op2
4337 // done:
4338 //
4339 // This removes one jmp, but needs to copy one input (op1) to out.
4340 //
4341 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
4342
4343 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
4344
4345 NearLabel nan, done, op2_label;
4346 if (type == DataType::Type::kFloat64) {
4347 __ ucomisd(out, op2);
4348 } else {
4349 DCHECK_EQ(type, DataType::Type::kFloat32);
4350 __ ucomiss(out, op2);
4351 }
4352
4353 __ j(Condition::kParityEven, &nan);
4354
4355 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
4356 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
4357
4358 // Handle 0.0/-0.0.
4359 if (is_min) {
4360 if (type == DataType::Type::kFloat64) {
4361 __ orpd(out, op2);
4362 } else {
4363 __ orps(out, op2);
4364 }
4365 } else {
4366 if (type == DataType::Type::kFloat64) {
4367 __ andpd(out, op2);
4368 } else {
4369 __ andps(out, op2);
4370 }
4371 }
4372 __ jmp(&done);
4373
4374 // NaN handling.
4375 __ Bind(&nan);
4376 if (type == DataType::Type::kFloat64) {
4377 // TODO: Use a constant from the constant table (requires extra input).
4378 __ LoadLongConstant(out, kDoubleNaN);
4379 } else {
4380 Register constant = locations->GetTemp(0).AsRegister<Register>();
4381 __ movl(constant, Immediate(kFloatNaN));
4382 __ movd(out, constant);
4383 }
4384 __ jmp(&done);
4385
4386 // out := op2;
4387 __ Bind(&op2_label);
4388 if (type == DataType::Type::kFloat64) {
4389 __ movsd(out, op2);
4390 } else {
4391 __ movss(out, op2);
4392 }
4393
4394 // Done.
4395 __ Bind(&done);
4396}
4397
Aart Bik351df3e2018-03-07 11:54:57 -08004398void InstructionCodeGeneratorX86::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4399 DataType::Type type = minmax->GetResultType();
4400 switch (type) {
4401 case DataType::Type::kInt32:
4402 case DataType::Type::kInt64:
4403 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
4404 break;
4405 case DataType::Type::kFloat32:
4406 case DataType::Type::kFloat64:
4407 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
4408 break;
4409 default:
4410 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4411 }
4412}
4413
Aart Bik1f8d51b2018-02-15 10:42:37 -08004414void LocationsBuilderX86::VisitMin(HMin* min) {
4415 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4416}
4417
4418void InstructionCodeGeneratorX86::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004419 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004420}
4421
4422void LocationsBuilderX86::VisitMax(HMax* max) {
4423 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4424}
4425
4426void InstructionCodeGeneratorX86::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004427 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004428}
4429
Aart Bik3dad3412018-02-28 12:01:46 -08004430void LocationsBuilderX86::VisitAbs(HAbs* abs) {
4431 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4432 switch (abs->GetResultType()) {
4433 case DataType::Type::kInt32:
4434 locations->SetInAt(0, Location::RegisterLocation(EAX));
4435 locations->SetOut(Location::SameAsFirstInput());
4436 locations->AddTemp(Location::RegisterLocation(EDX));
4437 break;
4438 case DataType::Type::kInt64:
4439 locations->SetInAt(0, Location::RequiresRegister());
4440 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4441 locations->AddTemp(Location::RequiresRegister());
4442 break;
4443 case DataType::Type::kFloat32:
4444 locations->SetInAt(0, Location::RequiresFpuRegister());
4445 locations->SetOut(Location::SameAsFirstInput());
4446 locations->AddTemp(Location::RequiresFpuRegister());
4447 locations->AddTemp(Location::RequiresRegister());
4448 break;
4449 case DataType::Type::kFloat64:
4450 locations->SetInAt(0, Location::RequiresFpuRegister());
4451 locations->SetOut(Location::SameAsFirstInput());
4452 locations->AddTemp(Location::RequiresFpuRegister());
4453 break;
4454 default:
4455 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4456 }
4457}
4458
4459void InstructionCodeGeneratorX86::VisitAbs(HAbs* abs) {
4460 LocationSummary* locations = abs->GetLocations();
4461 switch (abs->GetResultType()) {
4462 case DataType::Type::kInt32: {
4463 Register out = locations->Out().AsRegister<Register>();
4464 DCHECK_EQ(out, EAX);
4465 Register temp = locations->GetTemp(0).AsRegister<Register>();
4466 DCHECK_EQ(temp, EDX);
4467 // Sign extend EAX into EDX.
4468 __ cdq();
4469 // XOR EAX with sign.
4470 __ xorl(EAX, EDX);
4471 // Subtract out sign to correct.
4472 __ subl(EAX, EDX);
4473 // The result is in EAX.
4474 break;
4475 }
4476 case DataType::Type::kInt64: {
4477 Location input = locations->InAt(0);
4478 Register input_lo = input.AsRegisterPairLow<Register>();
4479 Register input_hi = input.AsRegisterPairHigh<Register>();
4480 Location output = locations->Out();
4481 Register output_lo = output.AsRegisterPairLow<Register>();
4482 Register output_hi = output.AsRegisterPairHigh<Register>();
4483 Register temp = locations->GetTemp(0).AsRegister<Register>();
4484 // Compute the sign into the temporary.
4485 __ movl(temp, input_hi);
4486 __ sarl(temp, Immediate(31));
4487 // Store the sign into the output.
4488 __ movl(output_lo, temp);
4489 __ movl(output_hi, temp);
4490 // XOR the input to the output.
4491 __ xorl(output_lo, input_lo);
4492 __ xorl(output_hi, input_hi);
4493 // Subtract the sign.
4494 __ subl(output_lo, temp);
4495 __ sbbl(output_hi, temp);
4496 break;
4497 }
4498 case DataType::Type::kFloat32: {
4499 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4500 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4501 Register constant = locations->GetTemp(1).AsRegister<Register>();
4502 __ movl(constant, Immediate(INT32_C(0x7FFFFFFF)));
4503 __ movd(temp, constant);
4504 __ andps(out, temp);
4505 break;
4506 }
4507 case DataType::Type::kFloat64: {
4508 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4509 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4510 // TODO: Use a constant from the constant table (requires extra input).
4511 __ LoadLongConstant(temp, INT64_C(0x7FFFFFFFFFFFFFFF));
4512 __ andpd(out, temp);
4513 break;
4514 }
4515 default:
4516 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4517 }
4518}
4519
Calin Juravled0d48522014-11-04 16:40:20 +00004520void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004521 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004522 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004523 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004524 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004525 case DataType::Type::kInt8:
4526 case DataType::Type::kUint16:
4527 case DataType::Type::kInt16:
4528 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004529 locations->SetInAt(0, Location::Any());
4530 break;
4531 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004532 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004533 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
4534 if (!instruction->IsConstant()) {
4535 locations->AddTemp(Location::RequiresRegister());
4536 }
4537 break;
4538 }
4539 default:
4540 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4541 }
Calin Juravled0d48522014-11-04 16:40:20 +00004542}
4543
4544void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004545 SlowPathCode* slow_path =
4546 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004547 codegen_->AddSlowPath(slow_path);
4548
4549 LocationSummary* locations = instruction->GetLocations();
4550 Location value = locations->InAt(0);
4551
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004552 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004553 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004554 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004555 case DataType::Type::kInt8:
4556 case DataType::Type::kUint16:
4557 case DataType::Type::kInt16:
4558 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004559 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004560 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004561 __ j(kEqual, slow_path->GetEntryLabel());
4562 } else if (value.IsStackSlot()) {
4563 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
4564 __ j(kEqual, slow_path->GetEntryLabel());
4565 } else {
4566 DCHECK(value.IsConstant()) << value;
4567 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004568 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004569 }
4570 }
4571 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004572 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004573 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004574 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004575 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004576 __ movl(temp, value.AsRegisterPairLow<Register>());
4577 __ orl(temp, value.AsRegisterPairHigh<Register>());
4578 __ j(kEqual, slow_path->GetEntryLabel());
4579 } else {
4580 DCHECK(value.IsConstant()) << value;
4581 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4582 __ jmp(slow_path->GetEntryLabel());
4583 }
4584 }
4585 break;
4586 }
4587 default:
4588 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004589 }
Calin Juravled0d48522014-11-04 16:40:20 +00004590}
4591
Calin Juravle9aec02f2014-11-18 23:06:35 +00004592void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
4593 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4594
4595 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004596 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004597
4598 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004599 case DataType::Type::kInt32:
4600 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004601 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00004602 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00004603 // The shift count needs to be in CL or a constant.
4604 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004605 locations->SetOut(Location::SameAsFirstInput());
4606 break;
4607 }
4608 default:
4609 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4610 }
4611}
4612
4613void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
4614 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4615
4616 LocationSummary* locations = op->GetLocations();
4617 Location first = locations->InAt(0);
4618 Location second = locations->InAt(1);
4619 DCHECK(first.Equals(locations->Out()));
4620
4621 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004622 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00004623 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004624 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004625 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004626 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004627 DCHECK_EQ(ECX, second_reg);
4628 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004629 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004630 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004631 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004632 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004633 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004634 }
4635 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004636 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004637 if (shift == 0) {
4638 return;
4639 }
4640 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004641 if (op->IsShl()) {
4642 __ shll(first_reg, imm);
4643 } else if (op->IsShr()) {
4644 __ sarl(first_reg, imm);
4645 } else {
4646 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004647 }
4648 }
4649 break;
4650 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004651 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004652 if (second.IsRegister()) {
4653 Register second_reg = second.AsRegister<Register>();
4654 DCHECK_EQ(ECX, second_reg);
4655 if (op->IsShl()) {
4656 GenerateShlLong(first, second_reg);
4657 } else if (op->IsShr()) {
4658 GenerateShrLong(first, second_reg);
4659 } else {
4660 GenerateUShrLong(first, second_reg);
4661 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004662 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00004663 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004664 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004665 // Nothing to do if the shift is 0, as the input is already the output.
4666 if (shift != 0) {
4667 if (op->IsShl()) {
4668 GenerateShlLong(first, shift);
4669 } else if (op->IsShr()) {
4670 GenerateShrLong(first, shift);
4671 } else {
4672 GenerateUShrLong(first, shift);
4673 }
4674 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004675 }
4676 break;
4677 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004678 default:
4679 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4680 }
4681}
4682
Mark P Mendell73945692015-04-29 14:56:17 +00004683void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
4684 Register low = loc.AsRegisterPairLow<Register>();
4685 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04004686 if (shift == 1) {
4687 // This is just an addition.
4688 __ addl(low, low);
4689 __ adcl(high, high);
4690 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00004691 // Shift by 32 is easy. High gets low, and low gets 0.
4692 codegen_->EmitParallelMoves(
4693 loc.ToLow(),
4694 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004695 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004696 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4697 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004698 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004699 } else if (shift > 32) {
4700 // Low part becomes 0. High part is low part << (shift-32).
4701 __ movl(high, low);
4702 __ shll(high, Immediate(shift - 32));
4703 __ xorl(low, low);
4704 } else {
4705 // Between 1 and 31.
4706 __ shld(high, low, Immediate(shift));
4707 __ shll(low, Immediate(shift));
4708 }
4709}
4710
Calin Juravle9aec02f2014-11-18 23:06:35 +00004711void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004712 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004713 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4714 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4715 __ testl(shifter, Immediate(32));
4716 __ j(kEqual, &done);
4717 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4718 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4719 __ Bind(&done);
4720}
4721
Mark P Mendell73945692015-04-29 14:56:17 +00004722void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4723 Register low = loc.AsRegisterPairLow<Register>();
4724 Register high = loc.AsRegisterPairHigh<Register>();
4725 if (shift == 32) {
4726 // Need to copy the sign.
4727 DCHECK_NE(low, high);
4728 __ movl(low, high);
4729 __ sarl(high, Immediate(31));
4730 } else if (shift > 32) {
4731 DCHECK_NE(low, high);
4732 // High part becomes sign. Low part is shifted by shift - 32.
4733 __ movl(low, high);
4734 __ sarl(high, Immediate(31));
4735 __ sarl(low, Immediate(shift - 32));
4736 } else {
4737 // Between 1 and 31.
4738 __ shrd(low, high, Immediate(shift));
4739 __ sarl(high, Immediate(shift));
4740 }
4741}
4742
Calin Juravle9aec02f2014-11-18 23:06:35 +00004743void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004744 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004745 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4746 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4747 __ testl(shifter, Immediate(32));
4748 __ j(kEqual, &done);
4749 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4750 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4751 __ Bind(&done);
4752}
4753
Mark P Mendell73945692015-04-29 14:56:17 +00004754void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4755 Register low = loc.AsRegisterPairLow<Register>();
4756 Register high = loc.AsRegisterPairHigh<Register>();
4757 if (shift == 32) {
4758 // Shift by 32 is easy. Low gets high, and high gets 0.
4759 codegen_->EmitParallelMoves(
4760 loc.ToHigh(),
4761 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004762 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004763 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4764 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004765 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004766 } else if (shift > 32) {
4767 // Low part is high >> (shift - 32). High part becomes 0.
4768 __ movl(low, high);
4769 __ shrl(low, Immediate(shift - 32));
4770 __ xorl(high, high);
4771 } else {
4772 // Between 1 and 31.
4773 __ shrd(low, high, Immediate(shift));
4774 __ shrl(high, Immediate(shift));
4775 }
4776}
4777
Calin Juravle9aec02f2014-11-18 23:06:35 +00004778void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004779 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004780 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4781 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4782 __ testl(shifter, Immediate(32));
4783 __ j(kEqual, &done);
4784 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4785 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4786 __ Bind(&done);
4787}
4788
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004789void LocationsBuilderX86::VisitRor(HRor* ror) {
4790 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004791 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004792
4793 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004794 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004795 // Add the temporary needed.
4796 locations->AddTemp(Location::RequiresRegister());
4797 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004798 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004799 locations->SetInAt(0, Location::RequiresRegister());
4800 // The shift count needs to be in CL (unless it is a constant).
4801 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4802 locations->SetOut(Location::SameAsFirstInput());
4803 break;
4804 default:
4805 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4806 UNREACHABLE();
4807 }
4808}
4809
4810void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4811 LocationSummary* locations = ror->GetLocations();
4812 Location first = locations->InAt(0);
4813 Location second = locations->InAt(1);
4814
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004815 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004816 Register first_reg = first.AsRegister<Register>();
4817 if (second.IsRegister()) {
4818 Register second_reg = second.AsRegister<Register>();
4819 __ rorl(first_reg, second_reg);
4820 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004821 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004822 __ rorl(first_reg, imm);
4823 }
4824 return;
4825 }
4826
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004827 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004828 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4829 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4830 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4831 if (second.IsRegister()) {
4832 Register second_reg = second.AsRegister<Register>();
4833 DCHECK_EQ(second_reg, ECX);
4834 __ movl(temp_reg, first_reg_hi);
4835 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4836 __ shrd(first_reg_lo, temp_reg, second_reg);
4837 __ movl(temp_reg, first_reg_hi);
4838 __ testl(second_reg, Immediate(32));
4839 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4840 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4841 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004842 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004843 if (shift_amt == 0) {
4844 // Already fine.
4845 return;
4846 }
4847 if (shift_amt == 32) {
4848 // Just swap.
4849 __ movl(temp_reg, first_reg_lo);
4850 __ movl(first_reg_lo, first_reg_hi);
4851 __ movl(first_reg_hi, temp_reg);
4852 return;
4853 }
4854
4855 Immediate imm(shift_amt);
4856 // Save the constents of the low value.
4857 __ movl(temp_reg, first_reg_lo);
4858
4859 // Shift right into low, feeding bits from high.
4860 __ shrd(first_reg_lo, first_reg_hi, imm);
4861
4862 // Shift right into high, feeding bits from the original low.
4863 __ shrd(first_reg_hi, temp_reg, imm);
4864
4865 // Swap if needed.
4866 if (shift_amt > 32) {
4867 __ movl(temp_reg, first_reg_lo);
4868 __ movl(first_reg_lo, first_reg_hi);
4869 __ movl(first_reg_hi, temp_reg);
4870 }
4871 }
4872}
4873
Calin Juravle9aec02f2014-11-18 23:06:35 +00004874void LocationsBuilderX86::VisitShl(HShl* shl) {
4875 HandleShift(shl);
4876}
4877
4878void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4879 HandleShift(shl);
4880}
4881
4882void LocationsBuilderX86::VisitShr(HShr* shr) {
4883 HandleShift(shr);
4884}
4885
4886void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4887 HandleShift(shr);
4888}
4889
4890void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4891 HandleShift(ushr);
4892}
4893
4894void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4895 HandleShift(ushr);
4896}
4897
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004898void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004899 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4900 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004901 locations->SetOut(Location::RegisterLocation(EAX));
Alex Lightd109e302018-06-27 10:25:41 -07004902 InvokeRuntimeCallingConvention calling_convention;
4903 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004904}
4905
4906void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07004907 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
4908 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4909 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004910}
4911
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004912void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004913 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4914 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004915 locations->SetOut(Location::RegisterLocation(EAX));
4916 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004917 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4918 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004919}
4920
4921void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01004922 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
4923 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004924 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004925 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004926 DCHECK(!codegen_->IsLeafMethod());
4927}
4928
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004929void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004930 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004931 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004932 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4933 if (location.IsStackSlot()) {
4934 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4935 } else if (location.IsDoubleStackSlot()) {
4936 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004937 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004938 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004939}
4940
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004941void InstructionCodeGeneratorX86::VisitParameterValue(
4942 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4943}
4944
4945void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4946 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004947 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004948 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4949}
4950
4951void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004952}
4953
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004954void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4955 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004956 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004957 locations->SetInAt(0, Location::RequiresRegister());
4958 locations->SetOut(Location::RequiresRegister());
4959}
4960
4961void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4962 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004963 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004964 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004965 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004966 __ movl(locations->Out().AsRegister<Register>(),
4967 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004968 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004969 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004970 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004971 __ movl(locations->Out().AsRegister<Register>(),
4972 Address(locations->InAt(0).AsRegister<Register>(),
4973 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4974 // temp = temp->GetImtEntryAt(method_offset);
4975 __ movl(locations->Out().AsRegister<Register>(),
4976 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004977 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004978}
4979
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004980void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004981 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004982 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004983 locations->SetInAt(0, Location::RequiresRegister());
4984 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004985}
4986
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004987void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4988 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004989 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004990 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004991 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004992 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004993 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004994 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004995 break;
4996
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004997 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004998 __ notl(out.AsRegisterPairLow<Register>());
4999 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005000 break;
5001
5002 default:
5003 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
5004 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005005}
5006
David Brazdil66d126e2015-04-03 16:02:44 +01005007void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
5008 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005009 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01005010 locations->SetInAt(0, Location::RequiresRegister());
5011 locations->SetOut(Location::SameAsFirstInput());
5012}
5013
5014void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01005015 LocationSummary* locations = bool_not->GetLocations();
5016 Location in = locations->InAt(0);
5017 Location out = locations->Out();
5018 DCHECK(in.Equals(out));
5019 __ xorl(out.AsRegister<Register>(), Immediate(1));
5020}
5021
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005022void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005023 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005024 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00005025 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005026 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005027 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005028 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005029 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005030 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005031 case DataType::Type::kInt32:
5032 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00005033 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00005034 locations->SetInAt(1, Location::Any());
5035 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5036 break;
5037 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005038 case DataType::Type::kFloat32:
5039 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00005040 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00005041 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
5042 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
5043 } else if (compare->InputAt(1)->IsConstant()) {
5044 locations->SetInAt(1, Location::RequiresFpuRegister());
5045 } else {
5046 locations->SetInAt(1, Location::Any());
5047 }
Calin Juravleddb7df22014-11-25 20:56:51 +00005048 locations->SetOut(Location::RequiresRegister());
5049 break;
5050 }
5051 default:
5052 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5053 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005054}
5055
5056void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005057 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005058 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00005059 Location left = locations->InAt(0);
5060 Location right = locations->InAt(1);
5061
Mark Mendell0c9497d2015-08-21 09:30:05 -04005062 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08005063 Condition less_cond = kLess;
5064
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005065 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005066 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005067 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005068 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005069 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005070 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005071 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01005072 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08005073 break;
5074 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005075 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005076 Register left_low = left.AsRegisterPairLow<Register>();
5077 Register left_high = left.AsRegisterPairHigh<Register>();
5078 int32_t val_low = 0;
5079 int32_t val_high = 0;
5080 bool right_is_const = false;
5081
5082 if (right.IsConstant()) {
5083 DCHECK(right.GetConstant()->IsLongConstant());
5084 right_is_const = true;
5085 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
5086 val_low = Low32Bits(val);
5087 val_high = High32Bits(val);
5088 }
5089
Calin Juravleddb7df22014-11-25 20:56:51 +00005090 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005091 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005092 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005093 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005094 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005095 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08005096 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005097 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005098 __ j(kLess, &less); // Signed compare.
5099 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005100 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005101 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005102 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005103 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005104 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005105 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08005106 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005107 }
Aart Bika19616e2016-02-01 18:57:58 -08005108 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00005109 break;
5110 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005111 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00005112 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00005113 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08005114 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00005115 break;
5116 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005117 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00005118 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00005119 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08005120 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005121 break;
5122 }
5123 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00005124 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005125 }
Aart Bika19616e2016-02-01 18:57:58 -08005126
Calin Juravleddb7df22014-11-25 20:56:51 +00005127 __ movl(out, Immediate(0));
5128 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08005129 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00005130
5131 __ Bind(&greater);
5132 __ movl(out, Immediate(1));
5133 __ jmp(&done);
5134
5135 __ Bind(&less);
5136 __ movl(out, Immediate(-1));
5137
5138 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005139}
5140
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005141void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005142 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005143 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005144 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01005145 locations->SetInAt(i, Location::Any());
5146 }
5147 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005148}
5149
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005150void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005151 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005152}
5153
Roland Levillain7c1559a2015-12-15 10:55:36 +00005154void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00005155 /*
5156 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
5157 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
5158 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
5159 */
5160 switch (kind) {
5161 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00005162 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00005163 break;
5164 }
5165 case MemBarrierKind::kAnyStore:
5166 case MemBarrierKind::kLoadAny:
5167 case MemBarrierKind::kStoreStore: {
5168 // nop
5169 break;
5170 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05005171 case MemBarrierKind::kNTStoreStore:
5172 // Non-Temporal Store/Store needs an explicit fence.
Andreas Gampe3db70682018-12-26 15:12:03 -08005173 MemoryFence(/* non-temporal= */ true);
Mark Mendell7aa04a12016-01-27 22:39:07 -05005174 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005175 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005176}
5177
Vladimir Markodc151b22015-10-15 18:02:30 +01005178HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
5179 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01005180 ArtMethod* method ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005181 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005182}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005183
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005184Register CodeGeneratorX86::GetInvokeExtraParameter(HInvoke* invoke, Register temp) {
5185 if (invoke->IsInvokeStaticOrDirect()) {
5186 return GetInvokeStaticOrDirectExtraParameter(invoke->AsInvokeStaticOrDirect(), temp);
5187 }
5188 DCHECK(invoke->IsInvokeInterface());
5189 Location location =
5190 invoke->GetLocations()->InAt(invoke->AsInvokeInterface()->GetSpecialInputIndex());
5191 return location.AsRegister<Register>();
5192}
5193
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005194Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
5195 Register temp) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00005196 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005197 if (!invoke->GetLocations()->Intrinsified()) {
5198 return location.AsRegister<Register>();
5199 }
5200 // For intrinsics we allow any location, so it may be on the stack.
5201 if (!location.IsRegister()) {
5202 __ movl(temp, Address(ESP, location.GetStackIndex()));
5203 return temp;
5204 }
5205 // For register locations, check if the register was saved. If so, get it from the stack.
5206 // Note: There is a chance that the register was saved but not overwritten, so we could
5207 // save one load. However, since this is just an intrinsic slow path we prefer this
5208 // simple and more robust approach rather that trying to determine if that's the case.
5209 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00005210 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
5211 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
5212 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
5213 __ movl(temp, Address(ESP, stack_offset));
5214 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005215 }
5216 return location.AsRegister<Register>();
5217}
5218
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005219void CodeGeneratorX86::LoadMethod(MethodLoadKind load_kind, Location temp, HInvoke* invoke) {
5220 switch (load_kind) {
5221 case MethodLoadKind::kBootImageLinkTimePcRelative: {
5222 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
5223 Register base_reg = GetInvokeExtraParameter(invoke, temp.AsRegister<Register>());
5224 __ leal(temp.AsRegister<Register>(),
5225 Address(base_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
5226 RecordBootImageMethodPatch(invoke);
5227 break;
5228 }
5229 case MethodLoadKind::kBootImageRelRo: {
5230 size_t index = invoke->IsInvokeInterface()
5231 ? invoke->AsInvokeInterface()->GetSpecialInputIndex()
5232 : invoke->AsInvokeStaticOrDirect()->GetSpecialInputIndex();
5233 Register base_reg = GetInvokeExtraParameter(invoke, temp.AsRegister<Register>());
5234 __ movl(temp.AsRegister<Register>(), Address(base_reg, kPlaceholder32BitOffset));
5235 RecordBootImageRelRoPatch(
5236 invoke->InputAt(index)->AsX86ComputeBaseMethodAddress(),
5237 GetBootImageOffset(invoke));
5238 break;
5239 }
5240 case MethodLoadKind::kBssEntry: {
5241 Register base_reg = GetInvokeExtraParameter(invoke, temp.AsRegister<Register>());
5242 __ movl(temp.AsRegister<Register>(), Address(base_reg, kPlaceholder32BitOffset));
5243 RecordMethodBssEntryPatch(invoke);
5244 // No need for memory fence, thanks to the x86 memory model.
5245 break;
5246 }
5247 case MethodLoadKind::kJitDirectAddress: {
5248 __ movl(temp.AsRegister<Register>(),
5249 Immediate(reinterpret_cast32<uint32_t>(invoke->GetResolvedMethod())));
5250 break;
5251 }
5252 case MethodLoadKind::kRuntimeCall: {
5253 // Test situation, don't do anything.
5254 break;
5255 }
5256 default: {
5257 LOG(FATAL) << "Load kind should have already been handled " << load_kind;
5258 UNREACHABLE();
5259 }
5260 }
5261}
5262
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005263void CodeGeneratorX86::GenerateStaticOrDirectCall(
5264 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00005265 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
5266 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005267 case MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005268 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005269 uint32_t offset =
5270 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
5271 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00005272 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005273 }
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005274 case MethodLoadKind::kRecursive: {
Vladimir Marko86c87522020-05-11 16:55:55 +01005275 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005276 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005277 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005278 case MethodLoadKind::kRuntimeCall: {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005279 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5280 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01005281 }
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005282 case MethodLoadKind::kBootImageLinkTimePcRelative:
5283 // For kCallCriticalNative we skip loading the method and do the call directly.
5284 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
5285 break;
5286 }
5287 FALLTHROUGH_INTENDED;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005288 default: {
5289 LoadMethod(invoke->GetMethodLoadKind(), callee_method, invoke);
5290 }
Vladimir Marko58155012015-08-19 12:49:41 +00005291 }
5292
5293 switch (invoke->GetCodePtrLocation()) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005294 case CodePtrLocation::kCallSelf:
Vladimir Marko58155012015-08-19 12:49:41 +00005295 __ call(GetFrameEntryLabel());
Vladimir Marko86c87522020-05-11 16:55:55 +01005296 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko58155012015-08-19 12:49:41 +00005297 break;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005298 case CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01005299 size_t out_frame_size =
5300 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorX86,
5301 kNativeStackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01005302 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005303 if (invoke->GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative) {
5304 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
5305 Register base_reg = GetInvokeExtraParameter(invoke, temp.AsRegister<Register>());
5306 __ call(Address(base_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
5307 RecordBootImageJniEntrypointPatch(invoke);
5308 } else {
5309 // (callee_method + offset_of_jni_entry_point)()
5310 __ call(Address(callee_method.AsRegister<Register>(),
5311 ArtMethod::EntryPointFromJniOffset(kX86PointerSize).Int32Value()));
5312 }
Vladimir Marko86c87522020-05-11 16:55:55 +01005313 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5314 if (out_frame_size == 0u && DataType::IsFloatingPointType(invoke->GetType())) {
5315 // Create space for conversion.
5316 out_frame_size = 8u;
Vladimir Markodec78172020-06-19 15:31:23 +01005317 IncreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01005318 }
5319 // Zero-/sign-extend or move the result when needed due to native and managed ABI mismatch.
5320 switch (invoke->GetType()) {
5321 case DataType::Type::kBool:
5322 __ movzxb(EAX, AL);
5323 break;
5324 case DataType::Type::kInt8:
5325 __ movsxb(EAX, AL);
5326 break;
5327 case DataType::Type::kUint16:
5328 __ movzxw(EAX, EAX);
5329 break;
5330 case DataType::Type::kInt16:
5331 __ movsxw(EAX, EAX);
5332 break;
5333 case DataType::Type::kFloat32:
5334 __ fstps(Address(ESP, 0));
5335 __ movss(XMM0, Address(ESP, 0));
5336 break;
5337 case DataType::Type::kFloat64:
5338 __ fstpl(Address(ESP, 0));
5339 __ movsd(XMM0, Address(ESP, 0));
5340 break;
5341 case DataType::Type::kInt32:
5342 case DataType::Type::kInt64:
5343 case DataType::Type::kVoid:
5344 break;
5345 default:
5346 DCHECK(false) << invoke->GetType();
5347 break;
5348 }
5349 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01005350 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01005351 }
5352 break;
5353 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005354 case CodePtrLocation::kCallArtMethod:
Vladimir Marko58155012015-08-19 12:49:41 +00005355 // (callee_method + offset_of_quick_compiled_code)()
5356 __ call(Address(callee_method.AsRegister<Register>(),
5357 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005358 kX86PointerSize).Int32Value()));
Vladimir Marko86c87522020-05-11 16:55:55 +01005359 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko58155012015-08-19 12:49:41 +00005360 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04005361 }
5362
5363 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04005364}
5365
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005366void CodeGeneratorX86::GenerateVirtualCall(
5367 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005368 Register temp = temp_in.AsRegister<Register>();
5369 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5370 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005371
5372 // Use the calling convention instead of the location of the receiver, as
5373 // intrinsics may have put the receiver in a different register. In the intrinsics
5374 // slow path, the arguments have been moved to the right place, so here we are
5375 // guaranteed that the receiver is the first register of the calling convention.
5376 InvokeDexCallingConvention calling_convention;
5377 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005378 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005379 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005380 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005381 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005382 // Instead of simply (possibly) unpoisoning `temp` here, we should
5383 // emit a read barrier for the previous class reference load.
5384 // However this is not required in practice, as this is an
5385 // intermediate/temporary reference and because the current
5386 // concurrent copying collector keeps the from-space memory
5387 // intact/accessible until the end of the marking phase (the
5388 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005389 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00005390
5391 MaybeGenerateInlineCacheCheck(invoke, temp);
5392
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005393 // temp = temp->GetMethodAt(method_offset);
5394 __ movl(temp, Address(temp, method_offset));
5395 // call temp->GetEntryPoint();
5396 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07005397 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005398 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005399}
5400
Vladimir Marko6fd16062018-06-26 11:02:04 +01005401void CodeGeneratorX86::RecordBootImageIntrinsicPatch(HX86ComputeBaseMethodAddress* method_address,
5402 uint32_t intrinsic_data) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005403 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005404 method_address, /* target_dex_file= */ nullptr, intrinsic_data);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005405 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005406}
5407
Vladimir Markob066d432018-01-03 13:14:37 +00005408void CodeGeneratorX86::RecordBootImageRelRoPatch(HX86ComputeBaseMethodAddress* method_address,
5409 uint32_t boot_image_offset) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005410 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005411 method_address, /* target_dex_file= */ nullptr, boot_image_offset);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005412 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Markob066d432018-01-03 13:14:37 +00005413}
5414
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005415void CodeGeneratorX86::RecordBootImageMethodPatch(HInvoke* invoke) {
5416 size_t index = invoke->IsInvokeInterface()
5417 ? invoke->AsInvokeInterface()->GetSpecialInputIndex()
5418 : invoke->AsInvokeStaticOrDirect()->GetSpecialInputIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005419 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005420 invoke->InputAt(index)->AsX86ComputeBaseMethodAddress();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005421 boot_image_method_patches_.emplace_back(
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005422 method_address,
5423 invoke->GetResolvedMethodReference().dex_file,
5424 invoke->GetResolvedMethodReference().index);
Vladimir Marko65979462017-05-19 17:25:12 +01005425 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005426}
5427
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005428void CodeGeneratorX86::RecordMethodBssEntryPatch(HInvoke* invoke) {
5429 size_t index = invoke->IsInvokeInterface()
5430 ? invoke->AsInvokeInterface()->GetSpecialInputIndex()
5431 : invoke->AsInvokeStaticOrDirect()->GetSpecialInputIndex();
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005432 DCHECK(IsSameDexFile(GetGraph()->GetDexFile(), *invoke->GetMethodReference().dex_file));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005433 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005434 invoke->InputAt(index)->AsX86ComputeBaseMethodAddress();
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005435 // Add the patch entry and bind its label at the end of the instruction.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005436 method_bss_entry_patches_.emplace_back(
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005437 method_address,
5438 invoke->GetMethodReference().dex_file,
5439 invoke->GetMethodReference().index);
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005440 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005441}
5442
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005443void CodeGeneratorX86::RecordBootImageTypePatch(HLoadClass* load_class) {
5444 HX86ComputeBaseMethodAddress* method_address =
5445 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5446 boot_image_type_patches_.emplace_back(
5447 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005448 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005449}
5450
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005451Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005452 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005453 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko8f63f102020-09-28 12:10:28 +01005454 ArenaDeque<X86PcRelativePatchInfo>* patches = nullptr;
5455 switch (load_class->GetLoadKind()) {
5456 case HLoadClass::LoadKind::kBssEntry:
5457 patches = &type_bss_entry_patches_;
5458 break;
5459 case HLoadClass::LoadKind::kBssEntryPublic:
5460 patches = &public_type_bss_entry_patches_;
5461 break;
5462 case HLoadClass::LoadKind::kBssEntryPackage:
5463 patches = &package_type_bss_entry_patches_;
5464 break;
5465 default:
5466 LOG(FATAL) << "Unexpected load kind: " << load_class->GetLoadKind();
5467 UNREACHABLE();
5468 }
5469 patches->emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005470 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005471 return &patches->back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005472}
5473
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005474void CodeGeneratorX86::RecordBootImageStringPatch(HLoadString* load_string) {
5475 HX86ComputeBaseMethodAddress* method_address =
5476 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
5477 boot_image_string_patches_.emplace_back(
5478 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
5479 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01005480}
5481
Vladimir Markoaad75c62016-10-03 08:46:48 +00005482Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005483 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005484 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005485 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005486 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005487 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005488}
5489
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005490void CodeGeneratorX86::RecordBootImageJniEntrypointPatch(HInvokeStaticOrDirect* invoke) {
5491 HX86ComputeBaseMethodAddress* method_address =
5492 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5493 boot_image_jni_entrypoint_patches_.emplace_back(
5494 method_address,
5495 invoke->GetResolvedMethodReference().dex_file,
5496 invoke->GetResolvedMethodReference().index);
5497 __ Bind(&boot_image_jni_entrypoint_patches_.back().label);
5498}
5499
Vladimir Markoeebb8212018-06-05 14:57:24 +01005500void CodeGeneratorX86::LoadBootImageAddress(Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01005501 uint32_t boot_image_reference,
Vladimir Markoeebb8212018-06-05 14:57:24 +01005502 HInvokeStaticOrDirect* invoke) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005503 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005504 HX86ComputeBaseMethodAddress* method_address =
5505 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5506 DCHECK(method_address != nullptr);
5507 Register method_address_reg =
5508 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005509 __ leal(reg, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005510 RecordBootImageIntrinsicPatch(method_address, boot_image_reference);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01005511 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01005512 HX86ComputeBaseMethodAddress* method_address =
5513 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5514 DCHECK(method_address != nullptr);
5515 Register method_address_reg =
5516 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005517 __ movl(reg, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005518 RecordBootImageRelRoPatch(method_address, boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01005519 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01005520 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01005521 gc::Heap* heap = Runtime::Current()->GetHeap();
5522 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01005523 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01005524 __ movl(reg, Immediate(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address))));
5525 }
5526}
5527
Vladimir Markode91ca92020-10-27 13:41:40 +00005528void CodeGeneratorX86::LoadIntrinsicDeclaringClass(Register reg, HInvokeStaticOrDirect* invoke) {
5529 DCHECK_NE(invoke->GetIntrinsic(), Intrinsics::kNone);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005530 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005531 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
Vladimir Marko6fd16062018-06-26 11:02:04 +01005532 HX86ComputeBaseMethodAddress* method_address =
5533 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5534 DCHECK(method_address != nullptr);
5535 Register method_address_reg =
5536 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Markode91ca92020-10-27 13:41:40 +00005537 __ leal(reg, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005538 MethodReference target_method = invoke->GetResolvedMethodReference();
Vladimir Marko6fd16062018-06-26 11:02:04 +01005539 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
5540 boot_image_type_patches_.emplace_back(method_address, target_method.dex_file, type_idx.index_);
5541 __ Bind(&boot_image_type_patches_.back().label);
5542 } else {
Vladimir Markode91ca92020-10-27 13:41:40 +00005543 uint32_t boot_image_offset = GetBootImageOffsetOfIntrinsicDeclaringClass(invoke);
5544 LoadBootImageAddress(reg, boot_image_offset, invoke);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005545 }
Vladimir Marko6fd16062018-06-26 11:02:04 +01005546}
5547
Vladimir Markoaad75c62016-10-03 08:46:48 +00005548// The label points to the end of the "movl" or another instruction but the literal offset
5549// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
5550constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
5551
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005552template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005553inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005554 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005555 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005556 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005557 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005558 linker_patches->push_back(Factory(literal_offset,
5559 info.target_dex_file,
5560 GetMethodAddressOffset(info.method_address),
5561 info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005562 }
5563}
5564
Vladimir Marko6fd16062018-06-26 11:02:04 +01005565template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
5566linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
5567 const DexFile* target_dex_file,
5568 uint32_t pc_insn_offset,
5569 uint32_t boot_image_offset) {
5570 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5571 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005572}
5573
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005574void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005575 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005576 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01005577 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005578 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00005579 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01005580 type_bss_entry_patches_.size() +
Vladimir Marko8f63f102020-09-28 12:10:28 +01005581 public_type_bss_entry_patches_.size() +
5582 package_type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005583 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01005584 string_bss_entry_patches_.size() +
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005585 boot_image_jni_entrypoint_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01005586 boot_image_other_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005587 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01005588 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005589 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
5590 boot_image_method_patches_, linker_patches);
5591 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
5592 boot_image_type_patches_, linker_patches);
5593 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005594 boot_image_string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005595 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005596 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005597 DCHECK(boot_image_type_patches_.empty());
5598 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01005599 }
5600 if (GetCompilerOptions().IsBootImage()) {
5601 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5602 boot_image_other_patches_, linker_patches);
5603 } else {
5604 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
5605 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005606 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005607 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5608 method_bss_entry_patches_, linker_patches);
5609 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5610 type_bss_entry_patches_, linker_patches);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005611 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PublicTypeBssEntryPatch>(
5612 public_type_bss_entry_patches_, linker_patches);
5613 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PackageTypeBssEntryPatch>(
5614 package_type_bss_entry_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005615 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5616 string_bss_entry_patches_, linker_patches);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005617 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeJniEntrypointPatch>(
5618 boot_image_jni_entrypoint_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005619 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005620}
5621
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005622void CodeGeneratorX86::MarkGCCard(Register temp,
5623 Register card,
5624 Register object,
5625 Register value,
5626 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005627 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005628 if (value_can_be_null) {
5629 __ testl(value, value);
5630 __ j(kEqual, &is_null);
5631 }
Roland Levillainc73f0522018-08-14 15:16:50 +01005632 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07005633 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01005634 // Calculate the offset (in the card table) of the card corresponding to
5635 // `object`.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005636 __ movl(temp, object);
5637 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01005638 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
5639 // `object`'s card.
5640 //
5641 // Register `card` contains the address of the card table. Note that the card
5642 // table's base is biased during its creation so that it always starts at an
5643 // address whose least-significant byte is equal to `kCardDirty` (see
5644 // art::gc::accounting::CardTable::Create). Therefore the MOVB instruction
5645 // below writes the `kCardDirty` (byte) value into the `object`'s card
5646 // (located at `card + object >> kCardShift`).
5647 //
5648 // This dual use of the value in register `card` (1. to calculate the location
5649 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
5650 // (no need to explicitly load `kCardDirty` as an immediate value).
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00005651 __ movb(Address(temp, card, TIMES_1, 0),
5652 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005653 if (value_can_be_null) {
5654 __ Bind(&is_null);
5655 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005656}
5657
Calin Juravle52c48962014-12-16 17:02:57 +00005658void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Alex Light3a73ffb2021-01-25 14:11:05 +00005659 DCHECK(instruction->IsInstanceFieldGet() ||
5660 instruction->IsStaticFieldGet() ||
5661 instruction->IsPredicatedInstanceFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005662
5663 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005664 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alex Light3a73ffb2021-01-25 14:11:05 +00005665 bool is_predicated = instruction->IsPredicatedInstanceFieldGet();
Nicolas Geoffray39468442014-09-02 15:17:15 +01005666 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005667 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5668 kEmitCompilerReadBarrier
5669 ? LocationSummary::kCallOnSlowPath
5670 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005671 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005672 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005673 }
Alex Light3a73ffb2021-01-25 14:11:05 +00005674 // receiver_input
5675 locations->SetInAt(is_predicated ? 1 : 0, Location::RequiresRegister());
5676 if (is_predicated) {
5677 if (DataType::IsFloatingPointType(instruction->GetType())) {
5678 locations->SetInAt(0, Location::RequiresFpuRegister());
5679 } else {
5680 locations->SetInAt(0, Location::RequiresRegister());
5681 }
5682 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005683 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alex Light3a73ffb2021-01-25 14:11:05 +00005684 locations->SetOut(is_predicated ? Location::SameAsFirstInput()
5685 : Location::RequiresFpuRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005686 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005687 // The output overlaps in case of long: we don't want the low move
5688 // to overwrite the object's location. Likewise, in the case of
5689 // an object field get with read barriers enabled, we do not want
5690 // the move to overwrite the object's location, as we need it to emit
5691 // the read barrier.
Alex Light3a73ffb2021-01-25 14:11:05 +00005692 locations->SetOut(is_predicated ? Location::SameAsFirstInput() : Location::RequiresRegister(),
5693 (object_field_get_with_read_barrier ||
5694 instruction->GetType() == DataType::Type::kInt64 ||
5695 is_predicated)
5696 ? Location::kOutputOverlap
5697 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005698 }
Calin Juravle52c48962014-12-16 17:02:57 +00005699
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005700 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00005701 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005702 // So we use an XMM register as a temp to achieve atomicity (first
5703 // load the temp into the XMM and then copy the XMM into the
5704 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00005705 locations->AddTemp(Location::RequiresFpuRegister());
5706 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005707}
5708
Calin Juravle52c48962014-12-16 17:02:57 +00005709void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
5710 const FieldInfo& field_info) {
Alex Light3a73ffb2021-01-25 14:11:05 +00005711 DCHECK(instruction->IsInstanceFieldGet() ||
5712 instruction->IsStaticFieldGet() ||
5713 instruction->IsPredicatedInstanceFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005714
Calin Juravle52c48962014-12-16 17:02:57 +00005715 LocationSummary* locations = instruction->GetLocations();
Alex Light3a73ffb2021-01-25 14:11:05 +00005716 Location base_loc = locations->InAt(instruction->IsPredicatedInstanceFieldGet() ? 1 : 0);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005717 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005718 Location out = locations->Out();
5719 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01005720 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5721 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00005722 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5723
Ulya Trafimovich322eced2021-06-02 15:39:36 +01005724 if (load_type == DataType::Type::kReference) {
5725 // /* HeapReference<Object> */ out = *(base + offset)
5726 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5727 // Note that a potential implicit null check is handled in this
5728 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
5729 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5730 instruction, out, base, offset, /* needs_null_check= */ true);
Calin Juravle52c48962014-12-16 17:02:57 +00005731 if (is_volatile) {
Ulya Trafimovich322eced2021-06-02 15:39:36 +01005732 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005733 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00005734 } else {
Ulya Trafimovich322eced2021-06-02 15:39:36 +01005735 __ movl(out.AsRegister<Register>(), Address(base, offset));
5736 codegen_->MaybeRecordImplicitNullCheck(instruction);
5737 if (is_volatile) {
5738 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5739 }
5740 // If read barriers are enabled, emit read barriers other than
5741 // Baker's using a slow path (and also unpoison the loaded
5742 // reference, if heap poisoning is enabled).
5743 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5744 }
5745 } else {
5746 Address src(base, offset);
5747 XmmRegister temp = (load_type == DataType::Type::kInt64 && is_volatile)
5748 ? locations->GetTemp(0).AsFpuRegister<XmmRegister>()
5749 : kNoXmmRegister;
5750 codegen_->LoadFromMemoryNoBarrier(load_type, out, src, instruction, temp, is_volatile);
5751 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005752 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5753 }
Roland Levillain4d027112015-07-01 15:41:14 +01005754 }
Calin Juravle52c48962014-12-16 17:02:57 +00005755}
5756
5757void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5758 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5759
5760 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005761 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00005762 locations->SetInAt(0, Location::RequiresRegister());
5763 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005764 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005765 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00005766
5767 // The register allocator does not support multiple
5768 // inputs that die at entry with one in a specific register.
5769 if (is_byte_type) {
5770 // Ensure the value is in a byte register.
5771 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005772 } else if (DataType::IsFloatingPointType(field_type)) {
5773 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05005774 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5775 locations->SetInAt(1, Location::RequiresFpuRegister());
5776 } else {
5777 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
5778 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005779 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05005780 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00005781 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005782
Calin Juravle52c48962014-12-16 17:02:57 +00005783 // 64bits value can be atomically written to an address with movsd and an XMM register.
5784 // We need two XMM registers because there's no easier way to (bit) copy a register pair
5785 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
5786 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
5787 // isolated cases when we need this it isn't worth adding the extra complexity.
5788 locations->AddTemp(Location::RequiresFpuRegister());
5789 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005790 } else {
5791 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5792
5793 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5794 // Temporary registers for the write barrier.
5795 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
5796 // Ensure the card is in a byte register.
5797 locations->AddTemp(Location::RegisterLocation(ECX));
5798 }
Calin Juravle52c48962014-12-16 17:02:57 +00005799 }
5800}
5801
5802void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Andra Danciucde98192020-09-13 12:32:09 +00005803 uint32_t value_index,
5804 DataType::Type field_type,
5805 Address field_addr,
5806 Register base,
5807 bool is_volatile,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005808 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005809 LocationSummary* locations = instruction->GetLocations();
Andra Danciucde98192020-09-13 12:32:09 +00005810 Location value = locations->InAt(value_index);
Roland Levillain4d027112015-07-01 15:41:14 +01005811 bool needs_write_barrier =
Andra Danciucde98192020-09-13 12:32:09 +00005812 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(value_index));
Calin Juravle52c48962014-12-16 17:02:57 +00005813
5814 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005815 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005816 }
5817
Mark Mendell81489372015-11-04 11:30:41 -05005818 bool maybe_record_implicit_null_check_done = false;
5819
Calin Juravle52c48962014-12-16 17:02:57 +00005820 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005821 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005822 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005823 case DataType::Type::kInt8: {
Andra Danciucde98192020-09-13 12:32:09 +00005824 if (value.IsConstant()) {
5825 __ movb(field_addr, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
5826 } else {
5827 __ movb(field_addr, value.AsRegister<ByteRegister>());
5828 }
Calin Juravle52c48962014-12-16 17:02:57 +00005829 break;
5830 }
5831
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005832 case DataType::Type::kUint16:
5833 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05005834 if (value.IsConstant()) {
Andra Danciucde98192020-09-13 12:32:09 +00005835 __ movw(field_addr, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05005836 } else {
Andra Danciucde98192020-09-13 12:32:09 +00005837 __ movw(field_addr, value.AsRegister<Register>());
Mark Mendell81489372015-11-04 11:30:41 -05005838 }
Calin Juravle52c48962014-12-16 17:02:57 +00005839 break;
5840 }
5841
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005842 case DataType::Type::kInt32:
5843 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01005844 if (kPoisonHeapReferences && needs_write_barrier) {
5845 // Note that in the case where `value` is a null reference,
5846 // we do not enter this block, as the reference does not
5847 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005848 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01005849 Register temp = locations->GetTemp(0).AsRegister<Register>();
5850 __ movl(temp, value.AsRegister<Register>());
5851 __ PoisonHeapReference(temp);
Andra Danciucde98192020-09-13 12:32:09 +00005852 __ movl(field_addr, temp);
Mark Mendell81489372015-11-04 11:30:41 -05005853 } else if (value.IsConstant()) {
5854 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Andra Danciucde98192020-09-13 12:32:09 +00005855 __ movl(field_addr, Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005856 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005857 DCHECK(value.IsRegister()) << value;
Andra Danciucde98192020-09-13 12:32:09 +00005858 __ movl(field_addr, value.AsRegister<Register>());
Roland Levillain4d027112015-07-01 15:41:14 +01005859 }
Calin Juravle52c48962014-12-16 17:02:57 +00005860 break;
5861 }
5862
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005863 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005864 if (is_volatile) {
5865 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5866 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5867 __ movd(temp1, value.AsRegisterPairLow<Register>());
5868 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5869 __ punpckldq(temp1, temp2);
Andra Danciucde98192020-09-13 12:32:09 +00005870 __ movsd(field_addr, temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005871 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005872 } else if (value.IsConstant()) {
5873 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
Andra Danciucde98192020-09-13 12:32:09 +00005874 __ movl(field_addr, Immediate(Low32Bits(v)));
Mark Mendell81489372015-11-04 11:30:41 -05005875 codegen_->MaybeRecordImplicitNullCheck(instruction);
Ulya Trafimovich893e2ed2021-06-10 16:18:12 +01005876 __ movl(Address::displace(field_addr, kX86WordSize), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005877 } else {
Andra Danciucde98192020-09-13 12:32:09 +00005878 __ movl(field_addr, value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005879 codegen_->MaybeRecordImplicitNullCheck(instruction);
Ulya Trafimovich893e2ed2021-06-10 16:18:12 +01005880 __ movl(Address::displace(field_addr, kX86WordSize), value.AsRegisterPairHigh<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00005881 }
Mark Mendell81489372015-11-04 11:30:41 -05005882 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005883 break;
5884 }
5885
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005886 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05005887 if (value.IsConstant()) {
5888 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Andra Danciucde98192020-09-13 12:32:09 +00005889 __ movl(field_addr, Immediate(v));
Mark Mendell81489372015-11-04 11:30:41 -05005890 } else {
Andra Danciucde98192020-09-13 12:32:09 +00005891 __ movss(field_addr, value.AsFpuRegister<XmmRegister>());
Mark Mendell81489372015-11-04 11:30:41 -05005892 }
Calin Juravle52c48962014-12-16 17:02:57 +00005893 break;
5894 }
5895
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005896 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005897 if (value.IsConstant()) {
Andra Danciuc992e422020-09-16 08:12:02 +00005898 DCHECK(!is_volatile);
Mark Mendell81489372015-11-04 11:30:41 -05005899 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
Andra Danciucde98192020-09-13 12:32:09 +00005900 __ movl(field_addr, Immediate(Low32Bits(v)));
Mark Mendell81489372015-11-04 11:30:41 -05005901 codegen_->MaybeRecordImplicitNullCheck(instruction);
Ulya Trafimovich893e2ed2021-06-10 16:18:12 +01005902 __ movl(Address::displace(field_addr, kX86WordSize), Immediate(High32Bits(v)));
Mark Mendell81489372015-11-04 11:30:41 -05005903 maybe_record_implicit_null_check_done = true;
5904 } else {
Andra Danciucde98192020-09-13 12:32:09 +00005905 __ movsd(field_addr, value.AsFpuRegister<XmmRegister>());
Mark Mendell81489372015-11-04 11:30:41 -05005906 }
Calin Juravle52c48962014-12-16 17:02:57 +00005907 break;
5908 }
5909
Aart Bik66c158e2018-01-31 12:55:04 -08005910 case DataType::Type::kUint32:
5911 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005912 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005913 LOG(FATAL) << "Unreachable type " << field_type;
5914 UNREACHABLE();
5915 }
5916
Mark Mendell81489372015-11-04 11:30:41 -05005917 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005918 codegen_->MaybeRecordImplicitNullCheck(instruction);
5919 }
5920
Roland Levillain4d027112015-07-01 15:41:14 +01005921 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005922 Register temp = locations->GetTemp(0).AsRegister<Register>();
5923 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005924 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005925 }
5926
Calin Juravle52c48962014-12-16 17:02:57 +00005927 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005928 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005929 }
5930}
5931
Andra Danciucde98192020-09-13 12:32:09 +00005932void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
5933 const FieldInfo& field_info,
5934 bool value_can_be_null) {
5935 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5936
5937 LocationSummary* locations = instruction->GetLocations();
5938 Register base = locations->InAt(0).AsRegister<Register>();
5939 bool is_volatile = field_info.IsVolatile();
5940 DataType::Type field_type = field_info.GetFieldType();
5941 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alex Light3a73ffb2021-01-25 14:11:05 +00005942 bool is_predicated =
5943 instruction->IsInstanceFieldSet() && instruction->AsInstanceFieldSet()->GetIsPredicatedSet();
Andra Danciucde98192020-09-13 12:32:09 +00005944
5945 Address field_addr(base, offset);
5946
Alex Light3a73ffb2021-01-25 14:11:05 +00005947 NearLabel pred_is_null;
5948 if (is_predicated) {
5949 __ testl(base, base);
5950 __ j(kEqual, &pred_is_null);
5951 }
5952
Andra Danciucde98192020-09-13 12:32:09 +00005953 HandleFieldSet(instruction,
5954 /* value_index= */ 1,
5955 field_type,
5956 field_addr,
5957 base,
5958 is_volatile,
5959 value_can_be_null);
Alex Light3a73ffb2021-01-25 14:11:05 +00005960
5961 if (is_predicated) {
5962 __ Bind(&pred_is_null);
5963 }
Andra Danciucde98192020-09-13 12:32:09 +00005964}
5965
Calin Juravle52c48962014-12-16 17:02:57 +00005966void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5967 HandleFieldGet(instruction, instruction->GetFieldInfo());
5968}
5969
5970void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5971 HandleFieldGet(instruction, instruction->GetFieldInfo());
5972}
5973
5974void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5975 HandleFieldSet(instruction, instruction->GetFieldInfo());
5976}
5977
5978void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005979 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005980}
5981
5982void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5983 HandleFieldSet(instruction, instruction->GetFieldInfo());
5984}
5985
5986void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005987 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005988}
5989
Alex Light3a73ffb2021-01-25 14:11:05 +00005990void LocationsBuilderX86::VisitPredicatedInstanceFieldGet(
5991 HPredicatedInstanceFieldGet* instruction) {
5992 HandleFieldGet(instruction, instruction->GetFieldInfo());
5993}
5994
Calin Juravle52c48962014-12-16 17:02:57 +00005995void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5996 HandleFieldGet(instruction, instruction->GetFieldInfo());
5997}
5998
Alex Light3a73ffb2021-01-25 14:11:05 +00005999void InstructionCodeGeneratorX86::VisitPredicatedInstanceFieldGet(
6000 HPredicatedInstanceFieldGet* instruction) {
6001 NearLabel finish;
6002 LocationSummary* locations = instruction->GetLocations();
6003 Register recv = locations->InAt(1).AsRegister<Register>();
6004 __ testl(recv, recv);
6005 __ j(kZero, &finish);
6006 HandleFieldGet(instruction, instruction->GetFieldInfo());
6007 __ Bind(&finish);
6008}
Calin Juravle52c48962014-12-16 17:02:57 +00006009void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6010 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006011}
6012
Vladimir Marko552a1342017-10-31 10:56:47 +00006013void LocationsBuilderX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6014 codegen_->CreateStringBuilderAppendLocations(instruction, Location::RegisterLocation(EAX));
6015}
6016
6017void InstructionCodeGeneratorX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6018 __ movl(EAX, Immediate(instruction->GetFormat()->GetValue()));
6019 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
6020}
6021
Calin Juravlee460d1d2015-09-29 04:52:17 +01006022void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
6023 HUnresolvedInstanceFieldGet* instruction) {
6024 FieldAccessCallingConventionX86 calling_convention;
6025 codegen_->CreateUnresolvedFieldLocationSummary(
6026 instruction, instruction->GetFieldType(), calling_convention);
6027}
6028
6029void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
6030 HUnresolvedInstanceFieldGet* instruction) {
6031 FieldAccessCallingConventionX86 calling_convention;
6032 codegen_->GenerateUnresolvedFieldAccess(instruction,
6033 instruction->GetFieldType(),
6034 instruction->GetFieldIndex(),
6035 instruction->GetDexPc(),
6036 calling_convention);
6037}
6038
6039void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
6040 HUnresolvedInstanceFieldSet* instruction) {
6041 FieldAccessCallingConventionX86 calling_convention;
6042 codegen_->CreateUnresolvedFieldLocationSummary(
6043 instruction, instruction->GetFieldType(), calling_convention);
6044}
6045
6046void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
6047 HUnresolvedInstanceFieldSet* instruction) {
6048 FieldAccessCallingConventionX86 calling_convention;
6049 codegen_->GenerateUnresolvedFieldAccess(instruction,
6050 instruction->GetFieldType(),
6051 instruction->GetFieldIndex(),
6052 instruction->GetDexPc(),
6053 calling_convention);
6054}
6055
6056void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
6057 HUnresolvedStaticFieldGet* instruction) {
6058 FieldAccessCallingConventionX86 calling_convention;
6059 codegen_->CreateUnresolvedFieldLocationSummary(
6060 instruction, instruction->GetFieldType(), calling_convention);
6061}
6062
6063void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
6064 HUnresolvedStaticFieldGet* instruction) {
6065 FieldAccessCallingConventionX86 calling_convention;
6066 codegen_->GenerateUnresolvedFieldAccess(instruction,
6067 instruction->GetFieldType(),
6068 instruction->GetFieldIndex(),
6069 instruction->GetDexPc(),
6070 calling_convention);
6071}
6072
6073void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
6074 HUnresolvedStaticFieldSet* instruction) {
6075 FieldAccessCallingConventionX86 calling_convention;
6076 codegen_->CreateUnresolvedFieldLocationSummary(
6077 instruction, instruction->GetFieldType(), calling_convention);
6078}
6079
6080void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
6081 HUnresolvedStaticFieldSet* instruction) {
6082 FieldAccessCallingConventionX86 calling_convention;
6083 codegen_->GenerateUnresolvedFieldAccess(instruction,
6084 instruction->GetFieldType(),
6085 instruction->GetFieldIndex(),
6086 instruction->GetDexPc(),
6087 calling_convention);
6088}
6089
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006090void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006091 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6092 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
6093 ? Location::RequiresRegister()
6094 : Location::Any();
6095 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006096}
6097
Calin Juravle2ae48182016-03-16 14:05:09 +00006098void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
6099 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00006100 return;
6101 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006102 LocationSummary* locations = instruction->GetLocations();
6103 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00006104
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006105 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00006106 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006107}
6108
Calin Juravle2ae48182016-03-16 14:05:09 +00006109void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006110 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006111 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006112
6113 LocationSummary* locations = instruction->GetLocations();
6114 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006115
6116 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04006117 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006118 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006119 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006120 } else {
6121 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00006122 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006123 __ jmp(slow_path->GetEntryLabel());
6124 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006125 }
6126 __ j(kEqual, slow_path->GetEntryLabel());
6127}
6128
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006129void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006130 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006131}
6132
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006133void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006134 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006135 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006136 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006137 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
6138 object_array_get_with_read_barrier
6139 ? LocationSummary::kCallOnSlowPath
6140 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01006141 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006142 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006143 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006144 locations->SetInAt(0, Location::RequiresRegister());
6145 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006146 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01006147 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6148 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006149 // The output overlaps in case of long: we don't want the low move
6150 // to overwrite the array's location. Likewise, in the case of an
6151 // object array get with read barriers enabled, we do not want the
6152 // move to overwrite the array's location, as we need it to emit
6153 // the read barrier.
6154 locations->SetOut(
6155 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006156 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
6157 ? Location::kOutputOverlap
6158 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01006159 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006160}
6161
6162void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
6163 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006164 Location obj_loc = locations->InAt(0);
6165 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006166 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006167 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006168 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006169
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006170 DataType::Type type = instruction->GetType();
Ulya Trafimovich322eced2021-06-02 15:39:36 +01006171 if (type == DataType::Type::kReference) {
6172 static_assert(
6173 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6174 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6175 // /* HeapReference<Object> */ out =
6176 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6177 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
6178 // Note that a potential implicit null check is handled in this
6179 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
6180 codegen_->GenerateArrayLoadWithBakerReadBarrier(
6181 instruction, out_loc, obj, data_offset, index, /* needs_null_check= */ true);
6182 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006183 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006184 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006185 codegen_->MaybeRecordImplicitNullCheck(instruction);
Ulya Trafimovich322eced2021-06-02 15:39:36 +01006186 // If read barriers are enabled, emit read barriers other than
6187 // Baker's using a slow path (and also unpoison the loaded
6188 // reference, if heap poisoning is enabled).
6189 if (index.IsConstant()) {
6190 uint32_t offset =
6191 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
6192 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6193 } else {
6194 codegen_->MaybeGenerateReadBarrierSlow(
6195 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6196 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006197 }
Ulya Trafimovich322eced2021-06-02 15:39:36 +01006198 } else if (type == DataType::Type::kUint16
6199 && mirror::kUseStringCompression
6200 && instruction->IsStringCharAt()) {
6201 // Branch cases into compressed and uncompressed for each index's type.
6202 Register out = out_loc.AsRegister<Register>();
6203 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
6204 NearLabel done, not_compressed;
6205 __ testb(Address(obj, count_offset), Immediate(1));
Calin Juravle77520bc2015-01-12 18:45:46 +00006206 codegen_->MaybeRecordImplicitNullCheck(instruction);
Ulya Trafimovich322eced2021-06-02 15:39:36 +01006207 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6208 "Expecting 0=compressed, 1=uncompressed");
6209 __ j(kNotZero, &not_compressed);
6210 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
6211 __ jmp(&done);
6212 __ Bind(&not_compressed);
6213 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
6214 __ Bind(&done);
6215 } else {
Ulya Trafimovichc8451cb2021-06-02 17:35:16 +01006216 ScaleFactor scale = CodeGenerator::ScaleFactorForType(type);
6217 Address src = CodeGeneratorX86::ArrayAddress(obj, index, scale, data_offset);
Ulya Trafimovich322eced2021-06-02 15:39:36 +01006218 codegen_->LoadFromMemoryNoBarrier(type, out_loc, src, instruction);
Calin Juravle77520bc2015-01-12 18:45:46 +00006219 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006220}
6221
6222void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006223 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006224
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006225 bool needs_write_barrier =
6226 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006227 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006228
Vladimir Markoca6fff82017-10-03 14:49:14 +01006229 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01006230 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006231 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006232
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006233 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006234 // We need the inputs to be different than the output in case of long operation.
6235 // In case of a byte operation, the register allocator does not support multiple
6236 // inputs that die at entry with one in a specific register.
6237 locations->SetInAt(0, Location::RequiresRegister());
6238 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6239 if (is_byte_type) {
6240 // Ensure the value is in a byte register.
6241 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006242 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05006243 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006244 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006245 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
6246 }
6247 if (needs_write_barrier) {
6248 // Temporary registers for the write barrier.
6249 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6250 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00006251 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006252 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006253}
6254
6255void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
6256 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006257 Location array_loc = locations->InAt(0);
6258 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006259 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006260 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006261 DataType::Type value_type = instruction->GetComponentType();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006262 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006263 bool needs_write_barrier =
6264 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006265
6266 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006267 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006268 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006269 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006270 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006271 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006272 if (value.IsRegister()) {
6273 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006274 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01006275 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006276 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006277 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006278 break;
6279 }
6280
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006281 case DataType::Type::kUint16:
6282 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006283 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006284 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006285 if (value.IsRegister()) {
6286 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006287 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01006288 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006289 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006290 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006291 break;
6292 }
6293
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006294 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006295 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006296 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006297
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006298 if (!value.IsRegister()) {
6299 // Just setting null.
6300 DCHECK(instruction->InputAt(2)->IsNullConstant());
6301 DCHECK(value.IsConstant()) << value;
6302 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00006303 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006304 DCHECK(!needs_write_barrier);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006305 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006306 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006307 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006308
6309 DCHECK(needs_write_barrier);
6310 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01006311 Location temp_loc = locations->GetTemp(0);
6312 Register temp = temp_loc.AsRegister<Register>();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006313
6314 bool can_value_be_null = instruction->GetValueCanBeNull();
6315 NearLabel do_store;
6316 if (can_value_be_null) {
6317 __ testl(register_value, register_value);
6318 __ j(kEqual, &do_store);
6319 }
6320
6321 SlowPathCode* slow_path = nullptr;
6322 if (needs_type_check) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006323 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006324 codegen_->AddSlowPath(slow_path);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006325
6326 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6327 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6328 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006329
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006330 // Note that when Baker read barriers are enabled, the type
6331 // checks are performed without read barriers. This is fine,
6332 // even in the case where a class object is in the from-space
6333 // after the flip, as a comparison involving such a type would
6334 // not produce a false positive; it may of course produce a
6335 // false negative, in which case we would take the ArraySet
6336 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01006337
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006338 // /* HeapReference<Class> */ temp = array->klass_
6339 __ movl(temp, Address(array, class_offset));
6340 codegen_->MaybeRecordImplicitNullCheck(instruction);
6341 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01006342
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006343 // /* HeapReference<Class> */ temp = temp->component_type_
6344 __ movl(temp, Address(temp, component_offset));
6345 // If heap poisoning is enabled, no need to unpoison `temp`
6346 // nor the object reference in `register_value->klass`, as
6347 // we are comparing two poisoned references.
6348 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01006349
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006350 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006351 NearLabel do_put;
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006352 __ j(kEqual, &do_put);
6353 // If heap poisoning is enabled, the `temp` reference has
6354 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006355 __ MaybeUnpoisonHeapReference(temp);
6356
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006357 // If heap poisoning is enabled, no need to unpoison the
6358 // heap reference loaded below, as it is only used for a
6359 // comparison with null.
6360 __ cmpl(Address(temp, super_offset), Immediate(0));
6361 __ j(kNotEqual, slow_path->GetEntryLabel());
6362 __ Bind(&do_put);
6363 } else {
6364 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006365 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006366 }
6367
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006368 Register card = locations->GetTemp(1).AsRegister<Register>();
6369 codegen_->MarkGCCard(
6370 temp, card, array, value.AsRegister<Register>(), /* value_can_be_null= */ false);
6371
6372 if (can_value_be_null) {
6373 DCHECK(do_store.IsLinked());
6374 __ Bind(&do_store);
6375 }
6376
6377 Register source = register_value;
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006378 if (kPoisonHeapReferences) {
6379 __ movl(temp, register_value);
6380 __ PoisonHeapReference(temp);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006381 source = temp;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006382 }
6383
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006384 __ movl(address, source);
6385
6386 if (can_value_be_null || !needs_type_check) {
6387 codegen_->MaybeRecordImplicitNullCheck(instruction);
6388 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006389
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006390 if (slow_path != nullptr) {
6391 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006392 }
6393
6394 break;
6395 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006396
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006397 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006398 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006399 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006400 if (value.IsRegister()) {
6401 __ movl(address, value.AsRegister<Register>());
6402 } else {
6403 DCHECK(value.IsConstant()) << value;
6404 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
6405 __ movl(address, Immediate(v));
6406 }
6407 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006408 break;
6409 }
6410
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006411 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006412 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006413 if (value.IsRegisterPair()) {
6414 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6415 value.AsRegisterPairLow<Register>());
6416 codegen_->MaybeRecordImplicitNullCheck(instruction);
6417 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6418 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006419 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006420 DCHECK(value.IsConstant());
6421 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
6422 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6423 Immediate(Low32Bits(val)));
6424 codegen_->MaybeRecordImplicitNullCheck(instruction);
6425 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6426 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006427 }
6428 break;
6429 }
6430
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006431 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006432 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006433 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006434 if (value.IsFpuRegister()) {
6435 __ movss(address, value.AsFpuRegister<XmmRegister>());
6436 } else {
6437 DCHECK(value.IsConstant());
6438 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
6439 __ movl(address, Immediate(v));
6440 }
6441 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006442 break;
6443 }
6444
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006445 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006446 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006447 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006448 if (value.IsFpuRegister()) {
6449 __ movsd(address, value.AsFpuRegister<XmmRegister>());
6450 } else {
6451 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006452 Address address_hi =
6453 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05006454 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
6455 __ movl(address, Immediate(Low32Bits(v)));
6456 codegen_->MaybeRecordImplicitNullCheck(instruction);
6457 __ movl(address_hi, Immediate(High32Bits(v)));
6458 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006459 break;
6460 }
6461
Aart Bik66c158e2018-01-31 12:55:04 -08006462 case DataType::Type::kUint32:
6463 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006464 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006465 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07006466 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006467 }
6468}
6469
6470void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006471 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006472 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04006473 if (!instruction->IsEmittedAtUseSite()) {
6474 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6475 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006476}
6477
6478void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04006479 if (instruction->IsEmittedAtUseSite()) {
6480 return;
6481 }
6482
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006483 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006484 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006485 Register obj = locations->InAt(0).AsRegister<Register>();
6486 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006487 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00006488 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07006489 // Mask out most significant bit in case the array is String's array of char.
6490 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006491 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006492 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006493}
6494
6495void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006496 RegisterSet caller_saves = RegisterSet::Empty();
6497 InvokeRuntimeCallingConvention calling_convention;
6498 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6499 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6500 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05006501 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04006502 HInstruction* length = instruction->InputAt(1);
6503 if (!length->IsEmittedAtUseSite()) {
6504 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6505 }
jessicahandojo4877b792016-09-08 19:49:13 -07006506 // Need register to see array's length.
6507 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6508 locations->AddTemp(Location::RequiresRegister());
6509 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006510}
6511
6512void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07006513 const bool is_string_compressed_char_at =
6514 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006515 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05006516 Location index_loc = locations->InAt(0);
6517 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006518 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006519 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006520
Mark Mendell99dbd682015-04-22 16:18:52 -04006521 if (length_loc.IsConstant()) {
6522 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
6523 if (index_loc.IsConstant()) {
6524 // BCE will remove the bounds check if we are guarenteed to pass.
6525 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6526 if (index < 0 || index >= length) {
6527 codegen_->AddSlowPath(slow_path);
6528 __ jmp(slow_path->GetEntryLabel());
6529 } else {
6530 // Some optimization after BCE may have generated this, and we should not
6531 // generate a bounds check if it is a valid range.
6532 }
6533 return;
6534 }
6535
6536 // We have to reverse the jump condition because the length is the constant.
6537 Register index_reg = index_loc.AsRegister<Register>();
6538 __ cmpl(index_reg, Immediate(length));
6539 codegen_->AddSlowPath(slow_path);
6540 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006541 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04006542 HInstruction* array_length = instruction->InputAt(1);
6543 if (array_length->IsEmittedAtUseSite()) {
6544 // Address the length field in the array.
6545 DCHECK(array_length->IsArrayLength());
6546 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
6547 Location array_loc = array_length->GetLocations()->InAt(0);
6548 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07006549 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006550 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
6551 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07006552 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
6553 __ movl(length_reg, array_len);
6554 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006555 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006556 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04006557 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006558 // Checking bounds for general case:
6559 // Array of char or string's array with feature compression off.
6560 if (index_loc.IsConstant()) {
6561 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6562 __ cmpl(array_len, Immediate(value));
6563 } else {
6564 __ cmpl(array_len, index_loc.AsRegister<Register>());
6565 }
6566 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04006567 }
Mark Mendell99dbd682015-04-22 16:18:52 -04006568 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006569 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04006570 }
6571 codegen_->AddSlowPath(slow_path);
6572 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006573 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006574}
6575
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006576void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006577 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006578}
6579
6580void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006581 if (instruction->GetNext()->IsSuspendCheck() &&
6582 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6583 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6584 // The back edge will generate the suspend check.
6585 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6586 }
6587
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006588 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6589}
6590
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006591void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006592 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6593 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07006594 // In suspend check slow path, usually there are no caller-save registers at all.
6595 // If SIMD instructions are present, however, we force spilling all live SIMD
6596 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07006597 locations->SetCustomSlowPathCallerSaves(
6598 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006599}
6600
6601void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006602 HBasicBlock* block = instruction->GetBlock();
6603 if (block->GetLoopInformation() != nullptr) {
6604 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6605 // The back edge will generate the suspend check.
6606 return;
6607 }
6608 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6609 // The goto will generate the suspend check.
6610 return;
6611 }
6612 GenerateSuspendCheck(instruction, nullptr);
6613}
6614
6615void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
6616 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006617 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006618 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
6619 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006620 slow_path =
6621 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006622 instruction->SetSlowPath(slow_path);
6623 codegen_->AddSlowPath(slow_path);
6624 if (successor != nullptr) {
6625 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006626 }
6627 } else {
6628 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6629 }
6630
Andreas Gampe542451c2016-07-26 09:02:02 -07006631 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006632 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006633 if (successor == nullptr) {
6634 __ j(kNotEqual, slow_path->GetEntryLabel());
6635 __ Bind(slow_path->GetReturnLabel());
6636 } else {
6637 __ j(kEqual, codegen_->GetLabelOf(successor));
6638 __ jmp(slow_path->GetEntryLabel());
6639 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006640}
6641
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006642X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
6643 return codegen_->GetAssembler();
6644}
6645
Aart Bikcfe50bb2017-12-12 14:54:12 -08006646void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006647 ScratchRegisterScope ensure_scratch(
6648 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6649 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6650 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05006651
Aart Bikcfe50bb2017-12-12 14:54:12 -08006652 // Now that temp register is available (possibly spilled), move blocks of memory.
6653 for (int i = 0; i < number_of_words; i++) {
6654 __ movl(temp_reg, Address(ESP, src + stack_offset));
6655 __ movl(Address(ESP, dst + stack_offset), temp_reg);
6656 stack_offset += kX86WordSize;
6657 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006658}
6659
6660void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006661 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006662 Location source = move->GetSource();
6663 Location destination = move->GetDestination();
6664
6665 if (source.IsRegister()) {
6666 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006667 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006668 } else if (destination.IsFpuRegister()) {
6669 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006670 } else {
6671 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006672 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006673 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006674 } else if (source.IsRegisterPair()) {
Vladimir Marko86c87522020-05-11 16:55:55 +01006675 if (destination.IsRegisterPair()) {
6676 __ movl(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
6677 DCHECK_NE(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
6678 __ movl(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
6679 } else if (destination.IsFpuRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006680 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Vladimir Markodec78172020-06-19 15:31:23 +01006681 // Push the 2 source registers to the stack.
Vladimir Marko86c87522020-05-11 16:55:55 +01006682 __ pushl(source.AsRegisterPairHigh<Register>());
6683 __ cfi().AdjustCFAOffset(elem_size);
6684 __ pushl(source.AsRegisterPairLow<Register>());
6685 __ cfi().AdjustCFAOffset(elem_size);
6686 // Load the destination register.
David Brazdil74eb1b22015-12-14 11:44:01 +00006687 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
6688 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01006689 codegen_->DecreaseFrame(2 * elem_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01006690 } else {
6691 DCHECK(destination.IsDoubleStackSlot());
6692 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
6693 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
6694 source.AsRegisterPairHigh<Register>());
6695 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006696 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006697 if (destination.IsRegister()) {
6698 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
6699 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006700 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006701 } else if (destination.IsRegisterPair()) {
Vladimir Marko86c87522020-05-11 16:55:55 +01006702 size_t elem_size = DataType::Size(DataType::Type::kInt32);
6703 // Create stack space for 2 elements.
Vladimir Markodec78172020-06-19 15:31:23 +01006704 codegen_->IncreaseFrame(2 * elem_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01006705 // Store the source register.
6706 __ movsd(Address(ESP, 0), source.AsFpuRegister<XmmRegister>());
6707 // And pop the values into destination registers.
6708 __ popl(destination.AsRegisterPairLow<Register>());
6709 __ cfi().AdjustCFAOffset(-elem_size);
6710 __ popl(destination.AsRegisterPairHigh<Register>());
6711 __ cfi().AdjustCFAOffset(-elem_size);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006712 } else if (destination.IsStackSlot()) {
6713 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006714 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006715 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006716 } else {
6717 DCHECK(destination.IsSIMDStackSlot());
6718 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05006719 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006720 } else if (source.IsStackSlot()) {
6721 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006722 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006723 } else if (destination.IsFpuRegister()) {
6724 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006725 } else {
6726 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08006727 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006728 }
6729 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006730 if (destination.IsRegisterPair()) {
6731 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
6732 __ movl(destination.AsRegisterPairHigh<Register>(),
6733 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
6734 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006735 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6736 } else {
6737 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006738 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006739 }
Aart Bik5576f372017-03-23 16:17:37 -07006740 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006741 if (destination.IsFpuRegister()) {
6742 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6743 } else {
6744 DCHECK(destination.IsSIMDStackSlot());
6745 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6746 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006747 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006748 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006749 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006750 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006751 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006752 if (value == 0) {
6753 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
6754 } else {
6755 __ movl(destination.AsRegister<Register>(), Immediate(value));
6756 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006757 } else {
6758 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05006759 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006760 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006761 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006762 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006763 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006764 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006765 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006766 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6767 if (value == 0) {
6768 // Easy handling of 0.0.
6769 __ xorps(dest, dest);
6770 } else {
6771 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006772 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6773 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
6774 __ movl(temp, Immediate(value));
6775 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006776 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006777 } else {
6778 DCHECK(destination.IsStackSlot()) << destination;
6779 __ movl(Address(ESP, destination.GetStackIndex()), imm);
6780 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006781 } else if (constant->IsLongConstant()) {
6782 int64_t value = constant->AsLongConstant()->GetValue();
6783 int32_t low_value = Low32Bits(value);
6784 int32_t high_value = High32Bits(value);
6785 Immediate low(low_value);
6786 Immediate high(high_value);
6787 if (destination.IsDoubleStackSlot()) {
6788 __ movl(Address(ESP, destination.GetStackIndex()), low);
6789 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6790 } else {
6791 __ movl(destination.AsRegisterPairLow<Register>(), low);
6792 __ movl(destination.AsRegisterPairHigh<Register>(), high);
6793 }
6794 } else {
6795 DCHECK(constant->IsDoubleConstant());
6796 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006797 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006798 int32_t low_value = Low32Bits(value);
6799 int32_t high_value = High32Bits(value);
6800 Immediate low(low_value);
6801 Immediate high(high_value);
6802 if (destination.IsFpuRegister()) {
6803 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6804 if (value == 0) {
6805 // Easy handling of 0.0.
6806 __ xorpd(dest, dest);
6807 } else {
6808 __ pushl(high);
Vladimir Marko86c87522020-05-11 16:55:55 +01006809 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006810 __ pushl(low);
Vladimir Marko86c87522020-05-11 16:55:55 +01006811 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006812 __ movsd(dest, Address(ESP, 0));
Vladimir Markodec78172020-06-19 15:31:23 +01006813 codegen_->DecreaseFrame(8);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006814 }
6815 } else {
6816 DCHECK(destination.IsDoubleStackSlot()) << destination;
6817 __ movl(Address(ESP, destination.GetStackIndex()), low);
6818 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6819 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006820 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006821 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006822 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006823 }
6824}
6825
Mark Mendella5c19ce2015-04-01 12:51:05 -04006826void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006827 Register suggested_scratch = reg == EAX ? EBX : EAX;
6828 ScratchRegisterScope ensure_scratch(
6829 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
6830
6831 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6832 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
6833 __ movl(Address(ESP, mem + stack_offset), reg);
6834 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006835}
6836
Mark Mendell7c8d0092015-01-26 11:21:33 -05006837void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006838 ScratchRegisterScope ensure_scratch(
6839 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6840
6841 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6842 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6843 __ movl(temp_reg, Address(ESP, mem + stack_offset));
6844 __ movss(Address(ESP, mem + stack_offset), reg);
6845 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006846}
6847
Aart Bikcfe50bb2017-12-12 14:54:12 -08006848void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
6849 size_t extra_slot = 4 * kX86WordSize;
Vladimir Markodec78172020-06-19 15:31:23 +01006850 codegen_->IncreaseFrame(extra_slot);
Aart Bikcfe50bb2017-12-12 14:54:12 -08006851 __ movups(Address(ESP, 0), XmmRegister(reg));
6852 ExchangeMemory(0, mem + extra_slot, 4);
6853 __ movups(XmmRegister(reg), Address(ESP, 0));
Vladimir Markodec78172020-06-19 15:31:23 +01006854 codegen_->DecreaseFrame(extra_slot);
Aart Bikcfe50bb2017-12-12 14:54:12 -08006855}
6856
6857void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006858 ScratchRegisterScope ensure_scratch1(
6859 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006860
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006861 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
6862 ScratchRegisterScope ensure_scratch2(
6863 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006864
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006865 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
6866 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006867
6868 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
6869 for (int i = 0; i < number_of_words; i++) {
6870 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
6871 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
6872 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
6873 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
6874 stack_offset += kX86WordSize;
6875 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006876}
6877
6878void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006879 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006880 Location source = move->GetSource();
6881 Location destination = move->GetDestination();
6882
6883 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04006884 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
6885 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
6886 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
6887 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
6888 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006889 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006890 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006891 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006892 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006893 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006894 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006895 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
6896 // Use XOR Swap algorithm to avoid a temporary.
6897 DCHECK_NE(source.reg(), destination.reg());
6898 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6899 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
6900 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6901 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
6902 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6903 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
6904 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006905 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
6906 // Take advantage of the 16 bytes in the XMM register.
6907 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
6908 Address stack(ESP, destination.GetStackIndex());
6909 // Load the double into the high doubleword.
6910 __ movhpd(reg, stack);
6911
6912 // Store the low double into the destination.
6913 __ movsd(stack, reg);
6914
6915 // Move the high double to the low double.
6916 __ psrldq(reg, Immediate(8));
6917 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6918 // Take advantage of the 16 bytes in the XMM register.
6919 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6920 Address stack(ESP, source.GetStackIndex());
6921 // Load the double into the high doubleword.
6922 __ movhpd(reg, stack);
6923
6924 // Store the low double into the destination.
6925 __ movsd(stack, reg);
6926
6927 // Move the high double to the low double.
6928 __ psrldq(reg, Immediate(8));
6929 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006930 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
6931 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
6932 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6933 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
6934 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6935 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
6936 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006937 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006938 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006939 }
6940}
6941
6942void ParallelMoveResolverX86::SpillScratch(int reg) {
6943 __ pushl(static_cast<Register>(reg));
6944}
6945
6946void ParallelMoveResolverX86::RestoreScratch(int reg) {
6947 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006948}
6949
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006950HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6951 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006952 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006953 case HLoadClass::LoadKind::kInvalid:
6954 LOG(FATAL) << "UNREACHABLE";
6955 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006956 case HLoadClass::LoadKind::kReferrersClass:
6957 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006958 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006959 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006960 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko8f63f102020-09-28 12:10:28 +01006961 case HLoadClass::LoadKind::kBssEntryPublic:
6962 case HLoadClass::LoadKind::kBssEntryPackage:
Vladimir Marko695348f2020-05-19 14:42:02 +01006963 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006964 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006965 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006966 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01006967 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006968 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006969 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006970 break;
6971 }
6972 return desired_class_load_kind;
6973}
6974
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006975void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006976 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006977 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006978 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006979 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006980 cls,
6981 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006982 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006983 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006984 return;
6985 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01006986 DCHECK_EQ(cls->NeedsAccessCheck(),
6987 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
6988 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006989
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006990 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6991 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006992 ? LocationSummary::kCallOnSlowPath
6993 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006994 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006995 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006996 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006997 }
6998
Vladimir Marko8f63f102020-09-28 12:10:28 +01006999 if (load_kind == HLoadClass::LoadKind::kReferrersClass || cls->HasPcRelativeLoadKind()) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007000 locations->SetInAt(0, Location::RequiresRegister());
7001 }
7002 locations->SetOut(Location::RequiresRegister());
Vladimir Marko8f63f102020-09-28 12:10:28 +01007003 if (call_kind == LocationSummary::kCallOnSlowPath && cls->HasPcRelativeLoadKind()) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007004 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7005 // Rely on the type resolution and/or initialization to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007006 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007007 } else {
7008 // For non-Baker read barrier we have a temp-clobbering call.
7009 }
7010 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007011}
7012
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007013Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01007014 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007015 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007016 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007017 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007018 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007019 PatchInfo<Label>* info = &jit_class_patches_.back();
7020 return &info->label;
7021}
7022
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007023// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7024// move.
7025void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007026 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007027 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007028 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01007029 return;
7030 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01007031 DCHECK_EQ(cls->NeedsAccessCheck(),
7032 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
7033 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Calin Juravle580b6092015-10-06 17:35:58 +01007034
Vladimir Marko41559982017-01-06 14:04:23 +00007035 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007036 Location out_loc = locations->Out();
7037 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007038
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007039 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007040 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7041 ? kWithoutReadBarrier
7042 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00007043 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007044 case HLoadClass::LoadKind::kReferrersClass: {
7045 DCHECK(!cls->CanCallRuntime());
7046 DCHECK(!cls->MustGenerateClinitCheck());
7047 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7048 Register current_method = locations->InAt(0).AsRegister<Register>();
7049 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007050 cls,
7051 out_loc,
7052 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Andreas Gampe3db70682018-12-26 15:12:03 -08007053 /* fixup_label= */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007054 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007055 break;
7056 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007057 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007058 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7059 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007060 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007061 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007062 __ leal(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007063 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007064 break;
7065 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007066 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007067 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7068 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007069 __ movl(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007070 codegen_->RecordBootImageRelRoPatch(cls->InputAt(0)->AsX86ComputeBaseMethodAddress(),
Vladimir Markode91ca92020-10-27 13:41:40 +00007071 CodeGenerator::GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007072 break;
7073 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01007074 case HLoadClass::LoadKind::kBssEntry:
7075 case HLoadClass::LoadKind::kBssEntryPublic:
7076 case HLoadClass::LoadKind::kBssEntryPackage: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007077 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007078 Address address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007079 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
7080 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007081 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007082 generate_null_check = true;
7083 break;
7084 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007085 case HLoadClass::LoadKind::kJitBootImageAddress: {
7086 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7087 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
7088 DCHECK_NE(address, 0u);
7089 __ movl(out, Immediate(address));
7090 break;
7091 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007092 case HLoadClass::LoadKind::kJitTableAddress: {
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007093 Address address = Address::Absolute(CodeGeneratorX86::kPlaceholder32BitOffset);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007094 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007095 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007096 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00007097 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007098 break;
7099 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007100 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007101 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007102 LOG(FATAL) << "UNREACHABLE";
7103 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007104 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007105
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007106 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7107 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007108 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007109 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007110
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007111 if (generate_null_check) {
7112 __ testl(out, out);
7113 __ j(kEqual, slow_path->GetEntryLabel());
7114 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007115
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007116 if (cls->MustGenerateClinitCheck()) {
7117 GenerateClassInitializationCheck(slow_path, out);
7118 } else {
7119 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007120 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007121 }
7122}
7123
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007124void LocationsBuilderX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7125 InvokeRuntimeCallingConvention calling_convention;
7126 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7127 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
7128}
7129
7130void InstructionCodeGeneratorX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7131 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
7132}
7133
Orion Hodson18259d72018-04-12 11:18:23 +01007134void LocationsBuilderX86::VisitLoadMethodType(HLoadMethodType* load) {
7135 InvokeRuntimeCallingConvention calling_convention;
7136 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7137 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
7138}
7139
7140void InstructionCodeGeneratorX86::VisitLoadMethodType(HLoadMethodType* load) {
7141 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
7142}
7143
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007144void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
7145 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007146 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007147 locations->SetInAt(0, Location::RequiresRegister());
7148 if (check->HasUses()) {
7149 locations->SetOut(Location::SameAsFirstInput());
7150 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007151 // Rely on the type initialization to save everything we need.
7152 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007153}
7154
7155void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007156 // We assume the class to not be null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007157 SlowPathCode* slow_path =
7158 new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(check->GetLoadClass(), check);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007159 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00007160 GenerateClassInitializationCheck(slow_path,
7161 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007162}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007163
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007164void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07007165 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00007166 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
7167 const size_t status_byte_offset =
7168 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
Vladimir Markobf121912019-06-04 13:49:05 +01007169 constexpr uint32_t shifted_visibly_initialized_value =
7170 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << (status_lsb_position % kBitsPerByte);
Vladimir Markodc682aa2018-01-04 18:42:57 +00007171
Vladimir Markobf121912019-06-04 13:49:05 +01007172 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_visibly_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00007173 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007174 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007175}
7176
Vladimir Marko175e7862018-03-27 09:03:13 +00007177void InstructionCodeGeneratorX86::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
7178 Register temp) {
7179 uint32_t path_to_root = check->GetBitstringPathToRoot();
7180 uint32_t mask = check->GetBitstringMask();
7181 DCHECK(IsPowerOfTwo(mask + 1));
7182 size_t mask_bits = WhichPowerOf2(mask + 1);
7183
7184 if (mask_bits == 16u) {
7185 // Compare the bitstring in memory.
7186 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
7187 } else {
7188 // /* uint32_t */ temp = temp->status_
7189 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
7190 // Compare the bitstring bits using SUB.
7191 __ subl(temp, Immediate(path_to_root));
7192 // Shift out bits that do not contribute to the comparison.
7193 __ shll(temp, Immediate(32u - mask_bits));
7194 }
7195}
7196
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007197HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
7198 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007199 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007200 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007201 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007202 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01007203 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007204 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007205 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007206 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01007207 DCHECK(GetCompilerOptions().IsJitCompiler());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007208 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007209 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007210 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007211 }
7212 return desired_string_load_kind;
7213}
7214
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007215void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007216 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007217 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007218 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007219 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007220 load_kind == HLoadString::LoadKind::kBootImageRelRo ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00007221 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007222 locations->SetInAt(0, Location::RequiresRegister());
7223 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007224 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007225 locations->SetOut(Location::RegisterLocation(EAX));
7226 } else {
7227 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007228 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7229 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007230 // Rely on the pResolveString to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007231 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007232 } else {
7233 // For non-Baker read barrier we have a temp-clobbering call.
7234 }
7235 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007236 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007237}
7238
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007239Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01007240 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007241 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007242 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007243 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007244 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007245 PatchInfo<Label>* info = &jit_string_patches_.back();
7246 return &info->label;
7247}
7248
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007249// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7250// move.
7251void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01007252 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007253 Location out_loc = locations->Out();
7254 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007255
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007256 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007257 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007258 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7259 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007260 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007261 __ leal(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007262 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007263 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007264 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007265 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007266 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7267 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007268 __ movl(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007269 codegen_->RecordBootImageRelRoPatch(load->InputAt(0)->AsX86ComputeBaseMethodAddress(),
Vladimir Markode91ca92020-10-27 13:41:40 +00007270 CodeGenerator::GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007271 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007272 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007273 case HLoadString::LoadKind::kBssEntry: {
7274 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007275 Address address = Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007276 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007277 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007278 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007279 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007280 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007281 codegen_->AddSlowPath(slow_path);
7282 __ testl(out, out);
7283 __ j(kEqual, slow_path->GetEntryLabel());
7284 __ Bind(slow_path->GetExitLabel());
7285 return;
7286 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007287 case HLoadString::LoadKind::kJitBootImageAddress: {
7288 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
7289 DCHECK_NE(address, 0u);
7290 __ movl(out, Immediate(address));
7291 return;
7292 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007293 case HLoadString::LoadKind::kJitTableAddress: {
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007294 Address address = Address::Absolute(CodeGeneratorX86::kPlaceholder32BitOffset);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007295 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007296 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007297 // /* GcRoot<mirror::String> */ out = *address
7298 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
7299 return;
7300 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007301 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007302 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007303 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007304
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007305 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007306 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007307 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007308 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007309 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7310 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007311}
7312
David Brazdilcb1c0552015-08-04 16:22:25 +01007313static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007314 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01007315}
7316
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007317void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
7318 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007319 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007320 locations->SetOut(Location::RequiresRegister());
7321}
7322
7323void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01007324 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
7325}
7326
7327void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007328 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01007329}
7330
7331void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7332 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007333}
7334
7335void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007336 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7337 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007338 InvokeRuntimeCallingConvention calling_convention;
7339 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7340}
7341
7342void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007343 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007344 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007345}
7346
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007347// Temp is used for read barrier.
7348static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7349 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00007350 !kUseBakerReadBarrier &&
7351 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00007352 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007353 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7354 return 1;
7355 }
7356 return 0;
7357}
7358
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007359// Interface case has 2 temps, one for holding the number of interfaces, one for the current
7360// interface pointer, the current interface is compared in memory.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007361// The other checks have one temp for loading the object's class.
7362static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007363 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007364 return 2;
7365 }
7366 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007367}
7368
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007369void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007370 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007371 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01007372 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007373 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007374 case TypeCheckKind::kExactCheck:
7375 case TypeCheckKind::kAbstractClassCheck:
7376 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007377 case TypeCheckKind::kArrayObjectCheck: {
7378 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7379 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7380 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007381 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007382 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007383 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007384 case TypeCheckKind::kUnresolvedCheck:
7385 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007386 call_kind = LocationSummary::kCallOnSlowPath;
7387 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007388 case TypeCheckKind::kBitstringCheck:
7389 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007390 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007391
Vladimir Markoca6fff82017-10-03 14:49:14 +01007392 LocationSummary* locations =
7393 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01007394 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007395 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007396 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007397 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007398 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7399 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7400 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7401 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7402 } else {
7403 locations->SetInAt(1, Location::Any());
7404 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007405 // Note that TypeCheckSlowPathX86 uses this "out" register too.
7406 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007407 // When read barriers are enabled, we need a temporary register for some cases.
7408 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007409}
7410
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007411void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007412 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007413 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007414 Location obj_loc = locations->InAt(0);
7415 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007416 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007417 Location out_loc = locations->Out();
7418 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007419 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7420 DCHECK_LE(num_temps, 1u);
7421 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007422 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007423 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7424 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7425 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07007426 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007427 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007428
7429 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007430 // Avoid null check if we know obj is not null.
7431 if (instruction->MustDoNullCheck()) {
7432 __ testl(obj, obj);
7433 __ j(kEqual, &zero);
7434 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007435
Roland Levillain7c1559a2015-12-15 10:55:36 +00007436 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007437 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007438 ReadBarrierOption read_barrier_option =
7439 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007440 // /* HeapReference<Class> */ out = obj->klass_
7441 GenerateReferenceLoadTwoRegisters(instruction,
7442 out_loc,
7443 obj_loc,
7444 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007445 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007446 if (cls.IsRegister()) {
7447 __ cmpl(out, cls.AsRegister<Register>());
7448 } else {
7449 DCHECK(cls.IsStackSlot()) << cls;
7450 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7451 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007452
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007453 // Classes must be equal for the instanceof to succeed.
7454 __ j(kNotEqual, &zero);
7455 __ movl(out, Immediate(1));
7456 __ jmp(&done);
7457 break;
7458 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007459
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007460 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007461 ReadBarrierOption read_barrier_option =
7462 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007463 // /* HeapReference<Class> */ out = obj->klass_
7464 GenerateReferenceLoadTwoRegisters(instruction,
7465 out_loc,
7466 obj_loc,
7467 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007468 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007469 // If the class is abstract, we eagerly fetch the super class of the
7470 // object to avoid doing a comparison we know will fail.
7471 NearLabel loop;
7472 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007473 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007474 GenerateReferenceLoadOneRegister(instruction,
7475 out_loc,
7476 super_offset,
7477 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007478 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007479 __ testl(out, out);
7480 // If `out` is null, we use it for the result, and jump to `done`.
7481 __ j(kEqual, &done);
7482 if (cls.IsRegister()) {
7483 __ cmpl(out, cls.AsRegister<Register>());
7484 } else {
7485 DCHECK(cls.IsStackSlot()) << cls;
7486 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7487 }
7488 __ j(kNotEqual, &loop);
7489 __ movl(out, Immediate(1));
7490 if (zero.IsLinked()) {
7491 __ jmp(&done);
7492 }
7493 break;
7494 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007495
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007496 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007497 ReadBarrierOption read_barrier_option =
7498 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007499 // /* HeapReference<Class> */ out = obj->klass_
7500 GenerateReferenceLoadTwoRegisters(instruction,
7501 out_loc,
7502 obj_loc,
7503 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007504 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007505 // Walk over the class hierarchy to find a match.
7506 NearLabel loop, success;
7507 __ Bind(&loop);
7508 if (cls.IsRegister()) {
7509 __ cmpl(out, cls.AsRegister<Register>());
7510 } else {
7511 DCHECK(cls.IsStackSlot()) << cls;
7512 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7513 }
7514 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007515 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007516 GenerateReferenceLoadOneRegister(instruction,
7517 out_loc,
7518 super_offset,
7519 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007520 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007521 __ testl(out, out);
7522 __ j(kNotEqual, &loop);
7523 // If `out` is null, we use it for the result, and jump to `done`.
7524 __ jmp(&done);
7525 __ Bind(&success);
7526 __ movl(out, Immediate(1));
7527 if (zero.IsLinked()) {
7528 __ jmp(&done);
7529 }
7530 break;
7531 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007532
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007533 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007534 ReadBarrierOption read_barrier_option =
7535 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007536 // /* HeapReference<Class> */ out = obj->klass_
7537 GenerateReferenceLoadTwoRegisters(instruction,
7538 out_loc,
7539 obj_loc,
7540 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007541 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007542 // Do an exact check.
7543 NearLabel exact_check;
7544 if (cls.IsRegister()) {
7545 __ cmpl(out, cls.AsRegister<Register>());
7546 } else {
7547 DCHECK(cls.IsStackSlot()) << cls;
7548 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7549 }
7550 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007551 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007552 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007553 GenerateReferenceLoadOneRegister(instruction,
7554 out_loc,
7555 component_offset,
7556 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007557 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007558 __ testl(out, out);
7559 // If `out` is null, we use it for the result, and jump to `done`.
7560 __ j(kEqual, &done);
7561 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
7562 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007563 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007564 __ movl(out, Immediate(1));
7565 __ jmp(&done);
7566 break;
7567 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007568
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007569 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007570 // No read barrier since the slow path will retry upon failure.
7571 // /* HeapReference<Class> */ out = obj->klass_
7572 GenerateReferenceLoadTwoRegisters(instruction,
7573 out_loc,
7574 obj_loc,
7575 class_offset,
7576 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007577 if (cls.IsRegister()) {
7578 __ cmpl(out, cls.AsRegister<Register>());
7579 } else {
7580 DCHECK(cls.IsStackSlot()) << cls;
7581 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7582 }
7583 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007584 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007585 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007586 codegen_->AddSlowPath(slow_path);
7587 __ j(kNotEqual, slow_path->GetEntryLabel());
7588 __ movl(out, Immediate(1));
7589 if (zero.IsLinked()) {
7590 __ jmp(&done);
7591 }
7592 break;
7593 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007594
Calin Juravle98893e12015-10-02 21:05:03 +01007595 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007596 case TypeCheckKind::kInterfaceCheck: {
7597 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007598 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00007599 // cases.
7600 //
7601 // We cannot directly call the InstanceofNonTrivial runtime
7602 // entry point without resorting to a type checking slow path
7603 // here (i.e. by calling InvokeRuntime directly), as it would
7604 // require to assign fixed registers for the inputs of this
7605 // HInstanceOf instruction (following the runtime calling
7606 // convention), which might be cluttered by the potential first
7607 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007608 //
7609 // TODO: Introduce a new runtime entry point taking the object
7610 // to test (instead of its class) as argument, and let it deal
7611 // with the read barrier issues. This will let us refactor this
7612 // case of the `switch` code as it was previously (with a direct
7613 // call to the runtime not using a type checking slow path).
7614 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007615 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007616 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007617 instruction, /* is_fatal= */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007618 codegen_->AddSlowPath(slow_path);
7619 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007620 if (zero.IsLinked()) {
7621 __ jmp(&done);
7622 }
7623 break;
7624 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007625
7626 case TypeCheckKind::kBitstringCheck: {
7627 // /* HeapReference<Class> */ temp = obj->klass_
7628 GenerateReferenceLoadTwoRegisters(instruction,
7629 out_loc,
7630 obj_loc,
7631 class_offset,
7632 kWithoutReadBarrier);
7633
7634 GenerateBitstringTypeCheckCompare(instruction, out);
7635 __ j(kNotEqual, &zero);
7636 __ movl(out, Immediate(1));
7637 __ jmp(&done);
7638 break;
7639 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007640 }
7641
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007642 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007643 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007644 __ xorl(out, out);
7645 }
7646
7647 if (done.IsLinked()) {
7648 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007649 }
7650
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007651 if (slow_path != nullptr) {
7652 __ Bind(slow_path->GetExitLabel());
7653 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007654}
7655
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007656void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007657 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007658 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007659 LocationSummary* locations =
7660 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007661 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007662 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7663 // Require a register for the interface check since there is a loop that compares the class to
7664 // a memory address.
7665 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007666 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7667 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7668 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7669 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007670 } else {
7671 locations->SetInAt(1, Location::Any());
7672 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007673 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007674 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
7675}
7676
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007677void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007678 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007679 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007680 Location obj_loc = locations->InAt(0);
7681 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007682 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007683 Location temp_loc = locations->GetTemp(0);
7684 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007685 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7686 DCHECK_GE(num_temps, 1u);
7687 DCHECK_LE(num_temps, 2u);
7688 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7689 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7690 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7691 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7692 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7693 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7694 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7695 const uint32_t object_array_data_offset =
7696 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007697
Vladimir Marko87584542017-12-12 17:47:52 +00007698 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007699 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007700 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7701 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007702 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007703
Roland Levillain0d5a2812015-11-13 10:07:31 +00007704 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007705 // Avoid null check if we know obj is not null.
7706 if (instruction->MustDoNullCheck()) {
7707 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007708 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007709 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007710
Roland Levillain0d5a2812015-11-13 10:07:31 +00007711 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007712 case TypeCheckKind::kExactCheck:
7713 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007714 // /* HeapReference<Class> */ temp = obj->klass_
7715 GenerateReferenceLoadTwoRegisters(instruction,
7716 temp_loc,
7717 obj_loc,
7718 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007719 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007720
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007721 if (cls.IsRegister()) {
7722 __ cmpl(temp, cls.AsRegister<Register>());
7723 } else {
7724 DCHECK(cls.IsStackSlot()) << cls;
7725 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7726 }
7727 // Jump to slow path for throwing the exception or doing a
7728 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007729 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007730 break;
7731 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007732
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007733 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007734 // /* HeapReference<Class> */ temp = obj->klass_
7735 GenerateReferenceLoadTwoRegisters(instruction,
7736 temp_loc,
7737 obj_loc,
7738 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007739 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007740
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007741 // If the class is abstract, we eagerly fetch the super class of the
7742 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007743 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007744 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007745 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007746 GenerateReferenceLoadOneRegister(instruction,
7747 temp_loc,
7748 super_offset,
7749 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007750 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007751
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007752 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7753 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007754 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007755 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007756
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007757 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007758 if (cls.IsRegister()) {
7759 __ cmpl(temp, cls.AsRegister<Register>());
7760 } else {
7761 DCHECK(cls.IsStackSlot()) << cls;
7762 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7763 }
7764 __ j(kNotEqual, &loop);
7765 break;
7766 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007767
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007768 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007769 // /* HeapReference<Class> */ temp = obj->klass_
7770 GenerateReferenceLoadTwoRegisters(instruction,
7771 temp_loc,
7772 obj_loc,
7773 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007774 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007775
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007776 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007777 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007778 __ Bind(&loop);
7779 if (cls.IsRegister()) {
7780 __ cmpl(temp, cls.AsRegister<Register>());
7781 } else {
7782 DCHECK(cls.IsStackSlot()) << cls;
7783 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7784 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007785 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007786
Roland Levillain0d5a2812015-11-13 10:07:31 +00007787 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007788 GenerateReferenceLoadOneRegister(instruction,
7789 temp_loc,
7790 super_offset,
7791 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007792 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007793
7794 // If the class reference currently in `temp` is not null, jump
7795 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007796 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007797 __ j(kNotZero, &loop);
7798 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007799 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007800 break;
7801 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007802
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007803 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007804 // /* HeapReference<Class> */ temp = obj->klass_
7805 GenerateReferenceLoadTwoRegisters(instruction,
7806 temp_loc,
7807 obj_loc,
7808 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007809 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007810
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007811 // Do an exact check.
7812 if (cls.IsRegister()) {
7813 __ cmpl(temp, cls.AsRegister<Register>());
7814 } else {
7815 DCHECK(cls.IsStackSlot()) << cls;
7816 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7817 }
7818 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007819
7820 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007821 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007822 GenerateReferenceLoadOneRegister(instruction,
7823 temp_loc,
7824 component_offset,
7825 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007826 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007827
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007828 // If the component type is null (i.e. the object not an array), jump to the slow path to
7829 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007830 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007831 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007832
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007833 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007834 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007835 break;
7836 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007837
Calin Juravle98893e12015-10-02 21:05:03 +01007838 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007839 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007840 // We cannot directly call the CheckCast runtime entry point
7841 // without resorting to a type checking slow path here (i.e. by
7842 // calling InvokeRuntime directly), as it would require to
7843 // assign fixed registers for the inputs of this HInstanceOf
7844 // instruction (following the runtime calling convention), which
7845 // might be cluttered by the potential first read barrier
7846 // emission at the beginning of this method.
7847 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007848 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007849
7850 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007851 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
7852 // We can not get false positives by doing this.
7853 // /* HeapReference<Class> */ temp = obj->klass_
7854 GenerateReferenceLoadTwoRegisters(instruction,
7855 temp_loc,
7856 obj_loc,
7857 class_offset,
7858 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007859
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007860 // /* HeapReference<Class> */ temp = temp->iftable_
7861 GenerateReferenceLoadTwoRegisters(instruction,
7862 temp_loc,
7863 temp_loc,
7864 iftable_offset,
7865 kWithoutReadBarrier);
7866 // Iftable is never null.
7867 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
7868 // Maybe poison the `cls` for direct comparison with memory.
7869 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
7870 // Loop through the iftable and check if any class matches.
7871 NearLabel start_loop;
7872 __ Bind(&start_loop);
7873 // Need to subtract first to handle the empty array case.
7874 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
7875 __ j(kNegative, type_check_slow_path->GetEntryLabel());
7876 // Go to next interface if the classes do not match.
7877 __ cmpl(cls.AsRegister<Register>(),
7878 CodeGeneratorX86::ArrayAddress(temp,
7879 maybe_temp2_loc,
7880 TIMES_4,
7881 object_array_data_offset));
7882 __ j(kNotEqual, &start_loop);
7883 // If `cls` was poisoned above, unpoison it.
7884 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007885 break;
7886 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007887
7888 case TypeCheckKind::kBitstringCheck: {
7889 // /* HeapReference<Class> */ temp = obj->klass_
7890 GenerateReferenceLoadTwoRegisters(instruction,
7891 temp_loc,
7892 obj_loc,
7893 class_offset,
7894 kWithoutReadBarrier);
7895
7896 GenerateBitstringTypeCheckCompare(instruction, temp);
7897 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
7898 break;
7899 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007900 }
7901 __ Bind(&done);
7902
Roland Levillain0d5a2812015-11-13 10:07:31 +00007903 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007904}
7905
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007906void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007907 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7908 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007909 InvokeRuntimeCallingConvention calling_convention;
7910 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7911}
7912
7913void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007914 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
7915 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01007916 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01007917 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007918 if (instruction->IsEnter()) {
7919 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7920 } else {
7921 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7922 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007923}
7924
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307925void LocationsBuilderX86::VisitX86AndNot(HX86AndNot* instruction) {
7926 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7927 DCHECK(DataType::IsIntOrLongType(instruction->GetType())) << instruction->GetType();
7928 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7929 locations->SetInAt(0, Location::RequiresRegister());
7930 locations->SetInAt(1, Location::RequiresRegister());
7931 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7932}
7933
7934void InstructionCodeGeneratorX86::VisitX86AndNot(HX86AndNot* instruction) {
7935 LocationSummary* locations = instruction->GetLocations();
7936 Location first = locations->InAt(0);
7937 Location second = locations->InAt(1);
7938 Location dest = locations->Out();
7939 if (instruction->GetResultType() == DataType::Type::kInt32) {
7940 __ andn(dest.AsRegister<Register>(),
7941 first.AsRegister<Register>(),
7942 second.AsRegister<Register>());
7943 } else {
7944 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
7945 __ andn(dest.AsRegisterPairLow<Register>(),
7946 first.AsRegisterPairLow<Register>(),
7947 second.AsRegisterPairLow<Register>());
7948 __ andn(dest.AsRegisterPairHigh<Register>(),
7949 first.AsRegisterPairHigh<Register>(),
7950 second.AsRegisterPairHigh<Register>());
7951 }
7952}
7953
7954void LocationsBuilderX86::VisitX86MaskOrResetLeastSetBit(HX86MaskOrResetLeastSetBit* instruction) {
7955 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7956 DCHECK(instruction->GetType() == DataType::Type::kInt32) << instruction->GetType();
7957 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7958 locations->SetInAt(0, Location::RequiresRegister());
7959 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7960}
7961
7962void InstructionCodeGeneratorX86::VisitX86MaskOrResetLeastSetBit(
7963 HX86MaskOrResetLeastSetBit* instruction) {
7964 LocationSummary* locations = instruction->GetLocations();
7965 Location src = locations->InAt(0);
7966 Location dest = locations->Out();
7967 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
7968 switch (instruction->GetOpKind()) {
7969 case HInstruction::kAnd:
7970 __ blsr(dest.AsRegister<Register>(), src.AsRegister<Register>());
7971 break;
7972 case HInstruction::kXor:
7973 __ blsmsk(dest.AsRegister<Register>(), src.AsRegister<Register>());
7974 break;
7975 default:
7976 LOG(FATAL) << "Unreachable";
7977 }
7978}
7979
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007980void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
7981void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
7982void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
7983
7984void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7985 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007986 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007987 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7988 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007989 locations->SetInAt(0, Location::RequiresRegister());
7990 locations->SetInAt(1, Location::Any());
7991 locations->SetOut(Location::SameAsFirstInput());
7992}
7993
7994void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
7995 HandleBitwiseOperation(instruction);
7996}
7997
7998void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
7999 HandleBitwiseOperation(instruction);
8000}
8001
8002void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
8003 HandleBitwiseOperation(instruction);
8004}
8005
8006void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
8007 LocationSummary* locations = instruction->GetLocations();
8008 Location first = locations->InAt(0);
8009 Location second = locations->InAt(1);
8010 DCHECK(first.Equals(locations->Out()));
8011
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008012 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008013 if (second.IsRegister()) {
8014 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00008015 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008016 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00008017 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008018 } else {
8019 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00008020 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008021 }
8022 } else if (second.IsConstant()) {
8023 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00008024 __ andl(first.AsRegister<Register>(),
8025 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008026 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00008027 __ orl(first.AsRegister<Register>(),
8028 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008029 } else {
8030 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00008031 __ xorl(first.AsRegister<Register>(),
8032 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008033 }
8034 } else {
8035 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00008036 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008037 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00008038 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008039 } else {
8040 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00008041 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008042 }
8043 }
8044 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008045 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008046 if (second.IsRegisterPair()) {
8047 if (instruction->IsAnd()) {
8048 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
8049 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
8050 } else if (instruction->IsOr()) {
8051 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
8052 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
8053 } else {
8054 DCHECK(instruction->IsXor());
8055 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
8056 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
8057 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008058 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008059 if (instruction->IsAnd()) {
8060 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
8061 __ andl(first.AsRegisterPairHigh<Register>(),
8062 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
8063 } else if (instruction->IsOr()) {
8064 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
8065 __ orl(first.AsRegisterPairHigh<Register>(),
8066 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
8067 } else {
8068 DCHECK(instruction->IsXor());
8069 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
8070 __ xorl(first.AsRegisterPairHigh<Register>(),
8071 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
8072 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008073 } else {
8074 DCHECK(second.IsConstant()) << second;
8075 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008076 int32_t low_value = Low32Bits(value);
8077 int32_t high_value = High32Bits(value);
8078 Immediate low(low_value);
8079 Immediate high(high_value);
8080 Register first_low = first.AsRegisterPairLow<Register>();
8081 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008082 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008083 if (low_value == 0) {
8084 __ xorl(first_low, first_low);
8085 } else if (low_value != -1) {
8086 __ andl(first_low, low);
8087 }
8088 if (high_value == 0) {
8089 __ xorl(first_high, first_high);
8090 } else if (high_value != -1) {
8091 __ andl(first_high, high);
8092 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008093 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008094 if (low_value != 0) {
8095 __ orl(first_low, low);
8096 }
8097 if (high_value != 0) {
8098 __ orl(first_high, high);
8099 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008100 } else {
8101 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008102 if (low_value != 0) {
8103 __ xorl(first_low, low);
8104 }
8105 if (high_value != 0) {
8106 __ xorl(first_high, high);
8107 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008108 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008109 }
8110 }
8111}
8112
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008113void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
8114 HInstruction* instruction,
8115 Location out,
8116 uint32_t offset,
8117 Location maybe_temp,
8118 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008119 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008120 if (read_barrier_option == kWithReadBarrier) {
8121 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008122 if (kUseBakerReadBarrier) {
8123 // Load with fast path based Baker's read barrier.
8124 // /* HeapReference<Object> */ out = *(out + offset)
8125 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008126 instruction, out, out_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008127 } else {
8128 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008129 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00008130 // in the following move operation, as we will need it for the
8131 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00008132 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008133 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008134 // /* HeapReference<Object> */ out = *(out + offset)
8135 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008136 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008137 }
8138 } else {
8139 // Plain load with no read barrier.
8140 // /* HeapReference<Object> */ out = *(out + offset)
8141 __ movl(out_reg, Address(out_reg, offset));
8142 __ MaybeUnpoisonHeapReference(out_reg);
8143 }
8144}
8145
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008146void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
8147 HInstruction* instruction,
8148 Location out,
8149 Location obj,
8150 uint32_t offset,
8151 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008152 Register out_reg = out.AsRegister<Register>();
8153 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008154 if (read_barrier_option == kWithReadBarrier) {
8155 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008156 if (kUseBakerReadBarrier) {
8157 // Load with fast path based Baker's read barrier.
8158 // /* HeapReference<Object> */ out = *(obj + offset)
8159 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008160 instruction, out, obj_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008161 } else {
8162 // Load with slow path based read barrier.
8163 // /* HeapReference<Object> */ out = *(obj + offset)
8164 __ movl(out_reg, Address(obj_reg, offset));
8165 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8166 }
8167 } else {
8168 // Plain load with no read barrier.
8169 // /* HeapReference<Object> */ out = *(obj + offset)
8170 __ movl(out_reg, Address(obj_reg, offset));
8171 __ MaybeUnpoisonHeapReference(out_reg);
8172 }
8173}
8174
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008175void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
8176 HInstruction* instruction,
8177 Location root,
8178 const Address& address,
8179 Label* fixup_label,
8180 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008181 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008182 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07008183 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008184 if (kUseBakerReadBarrier) {
8185 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
8186 // Baker's read barrier are used:
8187 //
Roland Levillaind966ce72017-02-09 16:20:14 +00008188 // root = obj.field;
8189 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8190 // if (temp != null) {
8191 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00008192 // }
8193
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008194 // /* GcRoot<mirror::Object> */ root = *address
8195 __ movl(root_reg, address);
8196 if (fixup_label != nullptr) {
8197 __ Bind(fixup_label);
8198 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008199 static_assert(
8200 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8201 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8202 "have different sizes.");
8203 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8204 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8205 "have different sizes.");
8206
Vladimir Marko953437b2016-08-24 08:30:46 +00008207 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008208 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008209 instruction, root, /* unpoison_ref_before_marking= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008210 codegen_->AddSlowPath(slow_path);
8211
Roland Levillaind966ce72017-02-09 16:20:14 +00008212 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
8213 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01008214 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00008215 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
8216 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008217 __ j(kNotEqual, slow_path->GetEntryLabel());
8218 __ Bind(slow_path->GetExitLabel());
8219 } else {
8220 // GC root loaded through a slow path for read barriers other
8221 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008222 // /* GcRoot<mirror::Object>* */ root = address
8223 __ leal(root_reg, address);
8224 if (fixup_label != nullptr) {
8225 __ Bind(fixup_label);
8226 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008227 // /* mirror::Object* */ root = root->Read()
8228 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8229 }
8230 } else {
8231 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008232 // /* GcRoot<mirror::Object> */ root = *address
8233 __ movl(root_reg, address);
8234 if (fixup_label != nullptr) {
8235 __ Bind(fixup_label);
8236 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00008237 // Note that GC roots are not affected by heap poisoning, thus we
8238 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008239 }
8240}
8241
8242void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8243 Location ref,
8244 Register obj,
8245 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00008246 bool needs_null_check) {
8247 DCHECK(kEmitCompilerReadBarrier);
8248 DCHECK(kUseBakerReadBarrier);
8249
8250 // /* HeapReference<Object> */ ref = *(obj + offset)
8251 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00008252 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008253}
8254
8255void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8256 Location ref,
8257 Register obj,
8258 uint32_t data_offset,
8259 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00008260 bool needs_null_check) {
8261 DCHECK(kEmitCompilerReadBarrier);
8262 DCHECK(kUseBakerReadBarrier);
8263
Roland Levillain3d312422016-06-23 13:53:42 +01008264 static_assert(
8265 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8266 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00008267 // /* HeapReference<Object> */ ref =
8268 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008269 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00008270 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008271}
8272
8273void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8274 Location ref,
8275 Register obj,
8276 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008277 bool needs_null_check,
8278 bool always_update_field,
8279 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008280 DCHECK(kEmitCompilerReadBarrier);
8281 DCHECK(kUseBakerReadBarrier);
8282
8283 // In slow path based read barriers, the read barrier call is
8284 // inserted after the original load. However, in fast path based
8285 // Baker's read barriers, we need to perform the load of
8286 // mirror::Object::monitor_ *before* the original reference load.
8287 // This load-load ordering is required by the read barrier.
8288 // The fast path/slow path (for Baker's algorithm) should look like:
8289 //
8290 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8291 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8292 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008293 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00008294 // if (is_gray) {
8295 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
8296 // }
8297 //
8298 // Note: the original implementation in ReadBarrier::Barrier is
8299 // slightly more complex as:
8300 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00008301 // the high-bits of rb_state, which are expected to be all zeroes
8302 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
8303 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008304 // - it performs additional checks that we do not do here for
8305 // performance reasons.
8306
8307 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00008308 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
8309
Vladimir Marko953437b2016-08-24 08:30:46 +00008310 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01008311 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008312 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00008313 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
8314 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
8315 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
8316
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008317 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00008318 // ref = ReadBarrier::Mark(ref);
8319 // At this point, just do the "if" and make sure that flags are preserved until the branch.
8320 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00008321 if (needs_null_check) {
8322 MaybeRecordImplicitNullCheck(instruction);
8323 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008324
8325 // Load fence to prevent load-load reordering.
8326 // Note that this is a no-op, thanks to the x86 memory model.
8327 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
8328
8329 // The actual reference load.
8330 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00008331 __ movl(ref_reg, src); // Flags are unaffected.
8332
8333 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
8334 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008335 SlowPathCode* slow_path;
8336 if (always_update_field) {
8337 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01008338 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008339 instruction, ref, obj, src, /* unpoison_ref_before_marking= */ true, *temp);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008340 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008341 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008342 instruction, ref, /* unpoison_ref_before_marking= */ true);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008343 }
Vladimir Marko953437b2016-08-24 08:30:46 +00008344 AddSlowPath(slow_path);
8345
8346 // We have done the "if" of the gray bit check above, now branch based on the flags.
8347 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00008348
8349 // Object* ref = ref_addr->AsMirrorPtr()
8350 __ MaybeUnpoisonHeapReference(ref_reg);
8351
Roland Levillain7c1559a2015-12-15 10:55:36 +00008352 __ Bind(slow_path->GetExitLabel());
8353}
8354
8355void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
8356 Location out,
8357 Location ref,
8358 Location obj,
8359 uint32_t offset,
8360 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008361 DCHECK(kEmitCompilerReadBarrier);
8362
Roland Levillain7c1559a2015-12-15 10:55:36 +00008363 // Insert a slow path based read barrier *after* the reference load.
8364 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008365 // If heap poisoning is enabled, the unpoisoning of the loaded
8366 // reference will be carried out by the runtime within the slow
8367 // path.
8368 //
8369 // Note that `ref` currently does not get unpoisoned (when heap
8370 // poisoning is enabled), which is alright as the `ref` argument is
8371 // not used by the artReadBarrierSlow entry point.
8372 //
8373 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008374 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00008375 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
8376 AddSlowPath(slow_path);
8377
Roland Levillain0d5a2812015-11-13 10:07:31 +00008378 __ jmp(slow_path->GetEntryLabel());
8379 __ Bind(slow_path->GetExitLabel());
8380}
8381
Roland Levillain7c1559a2015-12-15 10:55:36 +00008382void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
8383 Location out,
8384 Location ref,
8385 Location obj,
8386 uint32_t offset,
8387 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008388 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008389 // Baker's read barriers shall be handled by the fast path
8390 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
8391 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008392 // If heap poisoning is enabled, unpoisoning will be taken care of
8393 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008394 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008395 } else if (kPoisonHeapReferences) {
8396 __ UnpoisonHeapReference(out.AsRegister<Register>());
8397 }
8398}
8399
Roland Levillain7c1559a2015-12-15 10:55:36 +00008400void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8401 Location out,
8402 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008403 DCHECK(kEmitCompilerReadBarrier);
8404
Roland Levillain7c1559a2015-12-15 10:55:36 +00008405 // Insert a slow path based read barrier *after* the GC root load.
8406 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008407 // Note that GC roots are not affected by heap poisoning, so we do
8408 // not need to do anything special for this here.
8409 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008410 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008411 AddSlowPath(slow_path);
8412
Roland Levillain0d5a2812015-11-13 10:07:31 +00008413 __ jmp(slow_path->GetEntryLabel());
8414 __ Bind(slow_path->GetExitLabel());
8415}
8416
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008417void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008418 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008419 LOG(FATAL) << "Unreachable";
8420}
8421
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008422void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008423 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008424 LOG(FATAL) << "Unreachable";
8425}
8426
Mark Mendellfe57faa2015-09-18 09:26:15 -04008427// Simple implementation of packed switch - generate cascaded compare/jumps.
8428void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8429 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008430 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04008431 locations->SetInAt(0, Location::RequiresRegister());
8432}
8433
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008434void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
8435 int32_t lower_bound,
8436 uint32_t num_entries,
8437 HBasicBlock* switch_block,
8438 HBasicBlock* default_block) {
8439 // Figure out the correct compare values and jump conditions.
8440 // Handle the first compare/branch as a special case because it might
8441 // jump to the default case.
8442 DCHECK_GT(num_entries, 2u);
8443 Condition first_condition;
8444 uint32_t index;
8445 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
8446 if (lower_bound != 0) {
8447 first_condition = kLess;
8448 __ cmpl(value_reg, Immediate(lower_bound));
8449 __ j(first_condition, codegen_->GetLabelOf(default_block));
8450 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008451
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008452 index = 1;
8453 } else {
8454 // Handle all the compare/jumps below.
8455 first_condition = kBelow;
8456 index = 0;
8457 }
8458
8459 // Handle the rest of the compare/jumps.
8460 for (; index + 1 < num_entries; index += 2) {
8461 int32_t compare_to_value = lower_bound + index + 1;
8462 __ cmpl(value_reg, Immediate(compare_to_value));
8463 // Jump to successors[index] if value < case_value[index].
8464 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
8465 // Jump to successors[index + 1] if value == case_value[index + 1].
8466 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
8467 }
8468
8469 if (index != num_entries) {
8470 // There are an odd number of entries. Handle the last one.
8471 DCHECK_EQ(index + 1, num_entries);
8472 __ cmpl(value_reg, Immediate(lower_bound + index));
8473 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008474 }
8475
8476 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008477 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
8478 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008479 }
8480}
8481
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008482void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8483 int32_t lower_bound = switch_instr->GetStartValue();
8484 uint32_t num_entries = switch_instr->GetNumEntries();
8485 LocationSummary* locations = switch_instr->GetLocations();
8486 Register value_reg = locations->InAt(0).AsRegister<Register>();
8487
8488 GenPackedSwitchWithCompares(value_reg,
8489 lower_bound,
8490 num_entries,
8491 switch_instr->GetBlock(),
8492 switch_instr->GetDefaultBlock());
8493}
8494
Mark Mendell805b3b52015-09-18 14:10:29 -04008495void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8496 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008497 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04008498 locations->SetInAt(0, Location::RequiresRegister());
8499
8500 // Constant area pointer.
8501 locations->SetInAt(1, Location::RequiresRegister());
8502
8503 // And the temporary we need.
8504 locations->AddTemp(Location::RequiresRegister());
8505}
8506
8507void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8508 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008509 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04008510 LocationSummary* locations = switch_instr->GetLocations();
8511 Register value_reg = locations->InAt(0).AsRegister<Register>();
8512 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8513
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008514 if (num_entries <= kPackedSwitchJumpTableThreshold) {
8515 GenPackedSwitchWithCompares(value_reg,
8516 lower_bound,
8517 num_entries,
8518 switch_instr->GetBlock(),
8519 default_block);
8520 return;
8521 }
8522
Mark Mendell805b3b52015-09-18 14:10:29 -04008523 // Optimizing has a jump area.
8524 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
8525 Register constant_area = locations->InAt(1).AsRegister<Register>();
8526
8527 // Remove the bias, if needed.
8528 if (lower_bound != 0) {
8529 __ leal(temp_reg, Address(value_reg, -lower_bound));
8530 value_reg = temp_reg;
8531 }
8532
8533 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008534 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04008535 __ cmpl(value_reg, Immediate(num_entries - 1));
8536 __ j(kAbove, codegen_->GetLabelOf(default_block));
8537
8538 // We are in the range of the table.
8539 // Load (target-constant_area) from the jump table, indexing by the value.
8540 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
8541
8542 // Compute the actual target address by adding in constant_area.
8543 __ addl(temp_reg, constant_area);
8544
8545 // And jump.
8546 __ jmp(temp_reg);
8547}
8548
Mark Mendell0616ae02015-04-17 12:49:27 -04008549void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
8550 HX86ComputeBaseMethodAddress* insn) {
8551 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008552 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008553 locations->SetOut(Location::RequiresRegister());
8554}
8555
8556void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
8557 HX86ComputeBaseMethodAddress* insn) {
8558 LocationSummary* locations = insn->GetLocations();
8559 Register reg = locations->Out().AsRegister<Register>();
8560
8561 // Generate call to next instruction.
8562 Label next_instruction;
8563 __ call(&next_instruction);
8564 __ Bind(&next_instruction);
8565
8566 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008567 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04008568
8569 // Grab the return address off the stack.
8570 __ popl(reg);
8571}
8572
8573void LocationsBuilderX86::VisitX86LoadFromConstantTable(
8574 HX86LoadFromConstantTable* insn) {
8575 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008576 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008577
8578 locations->SetInAt(0, Location::RequiresRegister());
8579 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
8580
8581 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00008582 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008583 return;
8584 }
8585
8586 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008587 case DataType::Type::kFloat32:
8588 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008589 locations->SetOut(Location::RequiresFpuRegister());
8590 break;
8591
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008592 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008593 locations->SetOut(Location::RequiresRegister());
8594 break;
8595
8596 default:
8597 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8598 }
8599}
8600
8601void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00008602 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008603 return;
8604 }
8605
8606 LocationSummary* locations = insn->GetLocations();
8607 Location out = locations->Out();
8608 Register const_area = locations->InAt(0).AsRegister<Register>();
8609 HConstant *value = insn->GetConstant();
8610
8611 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008612 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008613 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008614 codegen_->LiteralFloatAddress(
8615 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008616 break;
8617
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008618 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008619 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008620 codegen_->LiteralDoubleAddress(
8621 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008622 break;
8623
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008624 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008625 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008626 codegen_->LiteralInt32Address(
8627 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008628 break;
8629
8630 default:
8631 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8632 }
8633}
8634
Mark Mendell0616ae02015-04-17 12:49:27 -04008635/**
8636 * Class to handle late fixup of offsets into constant area.
8637 */
Vladimir Marko5233f932015-09-29 19:01:15 +01008638class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04008639 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008640 RIPFixup(CodeGeneratorX86& codegen,
8641 HX86ComputeBaseMethodAddress* base_method_address,
8642 size_t offset)
8643 : codegen_(&codegen),
8644 base_method_address_(base_method_address),
8645 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008646
8647 protected:
8648 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
8649
8650 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008651 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008652
8653 private:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008654 void Process(const MemoryRegion& region, int pos) override {
Mark Mendell0616ae02015-04-17 12:49:27 -04008655 // Patch the correct offset for the instruction. The place to patch is the
8656 // last 4 bytes of the instruction.
8657 // The value to patch is the distance from the offset in the constant area
8658 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04008659 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008660 int32_t relative_position =
8661 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04008662
8663 // Patch in the right value.
8664 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
8665 }
8666
Mark Mendell0616ae02015-04-17 12:49:27 -04008667 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04008668 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008669};
8670
Mark Mendell805b3b52015-09-18 14:10:29 -04008671/**
8672 * Class to handle late fixup of offsets to a jump table that will be created in the
8673 * constant area.
8674 */
8675class JumpTableRIPFixup : public RIPFixup {
8676 public:
8677 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008678 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
8679 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008680
8681 void CreateJumpTable() {
8682 X86Assembler* assembler = codegen_->GetAssembler();
8683
8684 // Ensure that the reference to the jump table has the correct offset.
8685 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
8686 SetOffset(offset_in_constant_table);
8687
8688 // The label values in the jump table are computed relative to the
8689 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008690 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04008691
8692 // Populate the jump table with the correct values for the jump table.
8693 int32_t num_entries = switch_instr_->GetNumEntries();
8694 HBasicBlock* block = switch_instr_->GetBlock();
8695 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
8696 // The value that we want is the target offset - the position of the table.
8697 for (int32_t i = 0; i < num_entries; i++) {
8698 HBasicBlock* b = successors[i];
8699 Label* l = codegen_->GetLabelOf(b);
8700 DCHECK(l->IsBound());
8701 int32_t offset_to_block = l->Position() - relative_offset;
8702 assembler->AppendInt32(offset_to_block);
8703 }
8704 }
8705
8706 private:
8707 const HX86PackedSwitch* switch_instr_;
8708};
8709
8710void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
8711 // Generate the constant area if needed.
8712 X86Assembler* assembler = GetAssembler();
jaishank20d1c942019-03-08 15:08:17 +05308713
Mark Mendell805b3b52015-09-18 14:10:29 -04008714 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
8715 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
8716 // byte values.
8717 assembler->Align(4, 0);
8718 constant_area_start_ = assembler->CodeSize();
8719
8720 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008721 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04008722 jump_table->CreateJumpTable();
8723 }
8724
8725 // And now add the constant area to the generated code.
8726 assembler->AddConstantArea();
8727 }
8728
8729 // And finish up.
8730 CodeGenerator::Finalize(allocator);
8731}
8732
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008733Address CodeGeneratorX86::LiteralDoubleAddress(double v,
8734 HX86ComputeBaseMethodAddress* method_base,
8735 Register reg) {
8736 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008737 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008738 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008739}
8740
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008741Address CodeGeneratorX86::LiteralFloatAddress(float v,
8742 HX86ComputeBaseMethodAddress* method_base,
8743 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008744 AssemblerFixup* fixup =
8745 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008746 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008747}
8748
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008749Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
8750 HX86ComputeBaseMethodAddress* method_base,
8751 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008752 AssemblerFixup* fixup =
8753 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008754 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008755}
8756
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008757Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
8758 HX86ComputeBaseMethodAddress* method_base,
8759 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008760 AssemblerFixup* fixup =
8761 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008762 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008763}
8764
Aart Bika19616e2016-02-01 18:57:58 -08008765void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
8766 if (value == 0) {
8767 __ xorl(dest, dest);
8768 } else {
8769 __ movl(dest, Immediate(value));
8770 }
8771}
8772
8773void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
8774 if (value == 0) {
8775 __ testl(dest, dest);
8776 } else {
8777 __ cmpl(dest, Immediate(value));
8778 }
8779}
8780
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008781void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
8782 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07008783 GenerateIntCompare(lhs_reg, rhs);
8784}
8785
8786void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008787 if (rhs.IsConstant()) {
8788 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07008789 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008790 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07008791 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008792 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07008793 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008794 }
8795}
8796
8797Address CodeGeneratorX86::ArrayAddress(Register obj,
8798 Location index,
8799 ScaleFactor scale,
8800 uint32_t data_offset) {
8801 return index.IsConstant() ?
8802 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
8803 Address(obj, index.AsRegister<Register>(), scale, data_offset);
8804}
8805
Mark Mendell805b3b52015-09-18 14:10:29 -04008806Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
8807 Register reg,
8808 Register value) {
8809 // Create a fixup to be used to create and address the jump table.
8810 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008811 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04008812
8813 // We have to populate the jump tables.
8814 fixups_to_jump_tables_.push_back(table_fixup);
8815
8816 // We want a scaled address, as we are extracting the correct offset from the table.
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008817 return Address(reg, value, TIMES_4, kPlaceholder32BitOffset, table_fixup);
Mark Mendell805b3b52015-09-18 14:10:29 -04008818}
8819
Andreas Gampe85b62f22015-09-09 13:15:38 -07008820// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008821void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07008822 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008823 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008824 return;
8825 }
8826
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008827 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008828
8829 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
8830 if (target.Equals(return_loc)) {
8831 return;
8832 }
8833
8834 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8835 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008836 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008837 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008838 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
8839 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008840 GetMoveResolver()->EmitNativeCode(&parallel_move);
8841 } else {
8842 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01008843 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07008844 parallel_move.AddMove(return_loc, target, type, nullptr);
8845 GetMoveResolver()->EmitNativeCode(&parallel_move);
8846 }
8847}
8848
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008849void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
8850 const uint8_t* roots_data,
8851 const PatchInfo<Label>& info,
8852 uint64_t index_in_table) const {
8853 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
8854 uintptr_t address =
8855 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
Andreas Gampec55bb392018-09-21 00:02:02 +00008856 using unaligned_uint32_t __attribute__((__aligned__(1))) = uint32_t;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008857 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
8858 dchecked_integral_cast<uint32_t>(address);
8859}
8860
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008861void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8862 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008863 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008864 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008865 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008866 }
8867
8868 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008869 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008870 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008871 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008872 }
8873}
8874
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008875void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8876 ATTRIBUTE_UNUSED) {
8877 LOG(FATAL) << "Unreachable";
8878}
8879
8880void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8881 ATTRIBUTE_UNUSED) {
8882 LOG(FATAL) << "Unreachable";
8883}
8884
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05308885bool LocationsBuilderX86::CpuHasAvxFeatureFlag() {
8886 return codegen_->GetInstructionSetFeatures().HasAVX();
8887}
8888bool LocationsBuilderX86::CpuHasAvx2FeatureFlag() {
8889 return codegen_->GetInstructionSetFeatures().HasAVX2();
8890}
8891bool InstructionCodeGeneratorX86::CpuHasAvxFeatureFlag() {
8892 return codegen_->GetInstructionSetFeatures().HasAVX();
8893}
8894bool InstructionCodeGeneratorX86::CpuHasAvx2FeatureFlag() {
8895 return codegen_->GetInstructionSetFeatures().HasAVX2();
8896}
8897
Roland Levillain4d027112015-07-01 15:41:14 +01008898#undef __
8899
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008900} // namespace x86
8901} // namespace art