blob: 4fc29fcb0cfa94eb47d384b88411983e1ebd6cd9 [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,
1482 XmmRegister temp,
1483 bool is_atomic_load) {
Andra Danciu1ca6f322020-08-12 08:58:07 +00001484 switch (dst_type) {
1485 case DataType::Type::kBool:
1486 case DataType::Type::kUint8:
1487 __ movzxb(dst.AsRegister<Register>(), src);
1488 break;
1489 case DataType::Type::kInt8:
1490 __ movsxb(dst.AsRegister<Register>(), src);
1491 break;
1492 case DataType::Type::kInt16:
1493 __ movsxw(dst.AsRegister<Register>(), src);
1494 break;
1495 case DataType::Type::kUint16:
1496 __ movzxw(dst.AsRegister<Register>(), src);
1497 break;
1498 case DataType::Type::kInt32:
Andra Danciu1ca6f322020-08-12 08:58:07 +00001499 __ movl(dst.AsRegister<Register>(), src);
1500 break;
Andra Danciud0f71f22020-09-17 09:00:15 +00001501 case DataType::Type::kInt64: {
1502 if (is_atomic_load) {
1503 __ movsd(temp, src);
1504 __ movd(dst.AsRegisterPairLow<Register>(), temp);
1505 __ psrlq(temp, Immediate(32));
1506 __ movd(dst.AsRegisterPairHigh<Register>(), temp);
1507 } else {
1508 DCHECK_NE(src.GetBaseRegister(), dst.AsRegisterPairLow<Register>());
1509 Address src_high = src.displaceBy(kX86WordSize);
1510 __ movl(dst.AsRegisterPairLow<Register>(), src);
1511 __ movl(dst.AsRegisterPairHigh<Register>(), src_high);
1512 }
Andra Danciu1ca6f322020-08-12 08:58:07 +00001513 break;
1514 }
1515 case DataType::Type::kFloat32:
1516 __ movss(dst.AsFpuRegister<XmmRegister>(), src);
1517 break;
1518 case DataType::Type::kFloat64:
1519 __ movsd(dst.AsFpuRegister<XmmRegister>(), src);
1520 break;
Andra Danciu1ca6f322020-08-12 08:58:07 +00001521 case DataType::Type::kReference:
Andra Danciud0f71f22020-09-17 09:00:15 +00001522 __ movl(dst.AsRegister<Register>(), src);
1523 __ MaybeUnpoisonHeapReference(dst.AsRegister<Register>());
1524 break;
1525 default:
Andra Danciu1ca6f322020-08-12 08:58:07 +00001526 LOG(FATAL) << "Unreachable type " << dst_type;
1527 }
1528}
1529
Andra Danciu73c31802020-09-01 13:17:05 +00001530void CodeGeneratorX86::MoveToMemory(DataType::Type src_type,
1531 Location src,
1532 Register dst_base,
1533 Register dst_index,
1534 ScaleFactor dst_scale,
1535 int32_t dst_disp) {
1536 DCHECK(dst_base != Register::kNoRegister);
1537 Address dst = CreateAddress(dst_base, dst_index, dst_scale, dst_disp);
1538
1539 switch (src_type) {
1540 case DataType::Type::kBool:
1541 case DataType::Type::kUint8:
1542 case DataType::Type::kInt8: {
1543 if (src.IsConstant()) {
1544 __ movb(dst, Immediate(CodeGenerator::GetInt8ValueOf(src.GetConstant())));
1545 } else {
1546 __ movb(dst, src.AsRegister<ByteRegister>());
1547 }
1548 break;
1549 }
1550 case DataType::Type::kUint16:
1551 case DataType::Type::kInt16: {
1552 if (src.IsConstant()) {
1553 __ movw(dst, Immediate(CodeGenerator::GetInt16ValueOf(src.GetConstant())));
1554 } else {
1555 __ movw(dst, src.AsRegister<Register>());
1556 }
1557 break;
1558 }
1559 case DataType::Type::kUint32:
1560 case DataType::Type::kInt32: {
1561 if (src.IsConstant()) {
1562 int32_t v = CodeGenerator::GetInt32ValueOf(src.GetConstant());
1563 __ movl(dst, Immediate(v));
1564 } else {
1565 __ movl(dst, src.AsRegister<Register>());
1566 }
1567 break;
1568 }
1569 case DataType::Type::kUint64:
1570 case DataType::Type::kInt64: {
1571 Address dst_next_4_bytes = CreateAddress(dst_base, dst_index, dst_scale, dst_disp + 4);
1572 if (src.IsConstant()) {
1573 int64_t v = CodeGenerator::GetInt64ValueOf(src.GetConstant());
1574 __ movl(dst, Immediate(Low32Bits(v)));
1575 __ movl(dst_next_4_bytes, Immediate(High32Bits(v)));
1576 } else {
1577 __ movl(dst, src.AsRegisterPairLow<Register>());
1578 __ movl(dst_next_4_bytes, src.AsRegisterPairHigh<Register>());
1579 }
1580 break;
1581 }
1582 case DataType::Type::kFloat32: {
1583 if (src.IsConstant()) {
1584 int32_t v = CodeGenerator::GetInt32ValueOf(src.GetConstant());
1585 __ movl(dst, Immediate(v));
1586 } else {
1587 __ movss(dst, src.AsFpuRegister<XmmRegister>());
1588 }
1589 break;
1590 }
1591 case DataType::Type::kFloat64: {
1592 Address dst_next_4_bytes = CreateAddress(dst_base, dst_index, dst_scale, dst_disp + 4);
1593 if (src.IsConstant()) {
1594 int64_t v = CodeGenerator::GetInt64ValueOf(src.GetConstant());
1595 __ movl(dst, Immediate(Low32Bits(v)));
1596 __ movl(dst_next_4_bytes, Immediate(High32Bits(v)));
1597 } else {
1598 __ movsd(dst, src.AsFpuRegister<XmmRegister>());
1599 }
1600 break;
1601 }
1602 case DataType::Type::kVoid:
1603 case DataType::Type::kReference:
1604 LOG(FATAL) << "Unreachable type " << src_type;
1605 }
1606}
1607
Calin Juravle175dc732015-08-25 15:42:32 +01001608void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1609 DCHECK(location.IsRegister());
1610 __ movl(location.AsRegister<Register>(), Immediate(value));
1611}
1612
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001613void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001614 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001615 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1616 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1617 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001618 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001619 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001620 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001621 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001622}
1623
1624void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1625 if (location.IsRegister()) {
1626 locations->AddTemp(location);
1627 } else if (location.IsRegisterPair()) {
1628 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1629 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1630 } else {
1631 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1632 }
1633}
1634
David Brazdilfc6a86a2015-06-26 10:33:45 +00001635void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001636 if (successor->IsExitBlock()) {
1637 DCHECK(got->GetPrevious()->AlwaysThrows());
1638 return; // no code needed
1639 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001640
1641 HBasicBlock* block = got->GetBlock();
1642 HInstruction* previous = got->GetPrevious();
1643
1644 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001645 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001646 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001647 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1648 return;
1649 }
1650
1651 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1652 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1653 }
1654 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001655 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001656 }
1657}
1658
David Brazdilfc6a86a2015-06-26 10:33:45 +00001659void LocationsBuilderX86::VisitGoto(HGoto* got) {
1660 got->SetLocations(nullptr);
1661}
1662
1663void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1664 HandleGoto(got, got->GetSuccessor());
1665}
1666
1667void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1668 try_boundary->SetLocations(nullptr);
1669}
1670
1671void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1672 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1673 if (!successor->IsExitBlock()) {
1674 HandleGoto(try_boundary, successor);
1675 }
1676}
1677
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001678void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001679 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001680}
1681
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001682void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001683}
1684
Mark Mendell152408f2015-12-31 12:28:50 -05001685template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001686void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001687 LabelType* true_label,
1688 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001689 if (cond->IsFPConditionTrueIfNaN()) {
1690 __ j(kUnordered, true_label);
1691 } else if (cond->IsFPConditionFalseIfNaN()) {
1692 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001693 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001694 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001695}
1696
Mark Mendell152408f2015-12-31 12:28:50 -05001697template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001698void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001699 LabelType* true_label,
1700 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001701 LocationSummary* locations = cond->GetLocations();
1702 Location left = locations->InAt(0);
1703 Location right = locations->InAt(1);
1704 IfCondition if_cond = cond->GetCondition();
1705
Mark Mendellc4701932015-04-10 13:18:51 -04001706 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001707 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001708 IfCondition true_high_cond = if_cond;
1709 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001710 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001711
1712 // Set the conditions for the test, remembering that == needs to be
1713 // decided using the low words.
1714 switch (if_cond) {
1715 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001716 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001717 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001718 break;
1719 case kCondLT:
1720 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001721 break;
1722 case kCondLE:
1723 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001724 break;
1725 case kCondGT:
1726 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001727 break;
1728 case kCondGE:
1729 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001730 break;
Aart Bike9f37602015-10-09 11:15:55 -07001731 case kCondB:
1732 false_high_cond = kCondA;
1733 break;
1734 case kCondBE:
1735 true_high_cond = kCondB;
1736 break;
1737 case kCondA:
1738 false_high_cond = kCondB;
1739 break;
1740 case kCondAE:
1741 true_high_cond = kCondA;
1742 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001743 }
1744
1745 if (right.IsConstant()) {
1746 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001747 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001748 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001749
Aart Bika19616e2016-02-01 18:57:58 -08001750 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001751 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001752 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001753 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001754 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001755 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001756 __ j(X86Condition(true_high_cond), true_label);
1757 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001758 }
1759 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001760 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001761 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001762 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001763 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001764
1765 __ cmpl(left_high, right_high);
1766 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001767 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001768 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001769 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001770 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001771 __ j(X86Condition(true_high_cond), true_label);
1772 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001773 }
1774 // Must be equal high, so compare the lows.
1775 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001776 } else {
1777 DCHECK(right.IsDoubleStackSlot());
1778 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1779 if (if_cond == kCondNE) {
1780 __ j(X86Condition(true_high_cond), true_label);
1781 } else if (if_cond == kCondEQ) {
1782 __ j(X86Condition(false_high_cond), false_label);
1783 } else {
1784 __ j(X86Condition(true_high_cond), true_label);
1785 __ j(X86Condition(false_high_cond), false_label);
1786 }
1787 // Must be equal high, so compare the lows.
1788 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001789 }
1790 // The last comparison might be unsigned.
1791 __ j(final_condition, true_label);
1792}
1793
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001794void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1795 Location rhs,
1796 HInstruction* insn,
1797 bool is_double) {
1798 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1799 if (is_double) {
1800 if (rhs.IsFpuRegister()) {
1801 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1802 } else if (const_area != nullptr) {
1803 DCHECK(const_area->IsEmittedAtUseSite());
1804 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1805 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001806 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1807 const_area->GetBaseMethodAddress(),
1808 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001809 } else {
1810 DCHECK(rhs.IsDoubleStackSlot());
1811 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1812 }
1813 } else {
1814 if (rhs.IsFpuRegister()) {
1815 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1816 } else if (const_area != nullptr) {
1817 DCHECK(const_area->IsEmittedAtUseSite());
1818 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1819 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001820 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1821 const_area->GetBaseMethodAddress(),
1822 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001823 } else {
1824 DCHECK(rhs.IsStackSlot());
1825 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1826 }
1827 }
1828}
1829
Mark Mendell152408f2015-12-31 12:28:50 -05001830template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001831void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001832 LabelType* true_target_in,
1833 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001834 // Generated branching requires both targets to be explicit. If either of the
1835 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001836 LabelType fallthrough_target;
1837 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1838 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001839
Mark Mendellc4701932015-04-10 13:18:51 -04001840 LocationSummary* locations = condition->GetLocations();
1841 Location left = locations->InAt(0);
1842 Location right = locations->InAt(1);
1843
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001844 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001845 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001846 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001847 GenerateLongComparesAndJumps(condition, true_target, false_target);
1848 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001849 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001850 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001851 GenerateFPJumps(condition, true_target, false_target);
1852 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001853 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001854 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001855 GenerateFPJumps(condition, true_target, false_target);
1856 break;
1857 default:
1858 LOG(FATAL) << "Unexpected compare type " << type;
1859 }
1860
David Brazdil0debae72015-11-12 18:37:00 +00001861 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001862 __ jmp(false_target);
1863 }
David Brazdil0debae72015-11-12 18:37:00 +00001864
1865 if (fallthrough_target.IsLinked()) {
1866 __ Bind(&fallthrough_target);
1867 }
Mark Mendellc4701932015-04-10 13:18:51 -04001868}
1869
David Brazdil0debae72015-11-12 18:37:00 +00001870static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1871 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1872 // are set only strictly before `branch`. We can't use the eflags on long/FP
1873 // conditions if they are materialized due to the complex branching.
1874 return cond->IsCondition() &&
1875 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001876 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1877 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001878}
1879
Mark Mendell152408f2015-12-31 12:28:50 -05001880template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001881void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001882 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001883 LabelType* true_target,
1884 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001885 HInstruction* cond = instruction->InputAt(condition_input_index);
1886
1887 if (true_target == nullptr && false_target == nullptr) {
1888 // Nothing to do. The code always falls through.
1889 return;
1890 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001891 // Constant condition, statically compared against "true" (integer value 1).
1892 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001893 if (true_target != nullptr) {
1894 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001895 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001896 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001897 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001898 if (false_target != nullptr) {
1899 __ jmp(false_target);
1900 }
1901 }
1902 return;
1903 }
1904
1905 // The following code generates these patterns:
1906 // (1) true_target == nullptr && false_target != nullptr
1907 // - opposite condition true => branch to false_target
1908 // (2) true_target != nullptr && false_target == nullptr
1909 // - condition true => branch to true_target
1910 // (3) true_target != nullptr && false_target != nullptr
1911 // - condition true => branch to true_target
1912 // - branch to false_target
1913 if (IsBooleanValueOrMaterializedCondition(cond)) {
1914 if (AreEflagsSetFrom(cond, instruction)) {
1915 if (true_target == nullptr) {
1916 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1917 } else {
1918 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1919 }
1920 } else {
1921 // Materialized condition, compare against 0.
1922 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1923 if (lhs.IsRegister()) {
1924 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1925 } else {
1926 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1927 }
1928 if (true_target == nullptr) {
1929 __ j(kEqual, false_target);
1930 } else {
1931 __ j(kNotEqual, true_target);
1932 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001933 }
1934 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001935 // Condition has not been materialized, use its inputs as the comparison and
1936 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001937 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001938
1939 // If this is a long or FP comparison that has been folded into
1940 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001941 DataType::Type type = condition->InputAt(0)->GetType();
1942 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001943 GenerateCompareTestAndBranch(condition, true_target, false_target);
1944 return;
1945 }
1946
1947 Location lhs = condition->GetLocations()->InAt(0);
1948 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001949 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001950 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001951 if (true_target == nullptr) {
1952 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1953 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001954 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001955 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001956 }
David Brazdil0debae72015-11-12 18:37:00 +00001957
1958 // If neither branch falls through (case 3), the conditional branch to `true_target`
1959 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1960 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001961 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001962 }
1963}
1964
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001965void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001966 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001967 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001968 locations->SetInAt(0, Location::Any());
1969 }
1970}
1971
1972void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001973 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1974 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1975 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1976 nullptr : codegen_->GetLabelOf(true_successor);
1977 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1978 nullptr : codegen_->GetLabelOf(false_successor);
Andreas Gampe3db70682018-12-26 15:12:03 -08001979 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001980}
1981
1982void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001983 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001984 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001985 InvokeRuntimeCallingConvention calling_convention;
1986 RegisterSet caller_saves = RegisterSet::Empty();
1987 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1988 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001989 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001990 locations->SetInAt(0, Location::Any());
1991 }
1992}
1993
1994void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001995 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001996 GenerateTestAndBranch<Label>(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08001997 /* condition_input_index= */ 0,
David Brazdil74eb1b22015-12-14 11:44:01 +00001998 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08001999 /* false_target= */ nullptr);
David Brazdil74eb1b22015-12-14 11:44:01 +00002000}
2001
Mingyao Yang063fc772016-08-02 11:02:54 -07002002void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002003 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07002004 LocationSummary(flag, LocationSummary::kNoCall);
2005 locations->SetOut(Location::RequiresRegister());
2006}
2007
2008void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2009 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
2010 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
2011}
2012
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002013static bool SelectCanUseCMOV(HSelect* select) {
2014 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002015 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002016 return false;
2017 }
2018
2019 // A FP condition doesn't generate the single CC that we need.
2020 // In 32 bit mode, a long condition doesn't generate a single CC either.
2021 HInstruction* condition = select->GetCondition();
2022 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002023 DataType::Type compare_type = condition->InputAt(0)->GetType();
2024 if (compare_type == DataType::Type::kInt64 ||
2025 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002026 return false;
2027 }
2028 }
2029
2030 // We can generate a CMOV for this Select.
2031 return true;
2032}
2033
David Brazdil74eb1b22015-12-14 11:44:01 +00002034void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002035 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002036 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00002037 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002038 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00002039 } else {
2040 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002041 if (SelectCanUseCMOV(select)) {
2042 if (select->InputAt(1)->IsConstant()) {
2043 // Cmov can't handle a constant value.
2044 locations->SetInAt(1, Location::RequiresRegister());
2045 } else {
2046 locations->SetInAt(1, Location::Any());
2047 }
2048 } else {
2049 locations->SetInAt(1, Location::Any());
2050 }
David Brazdil74eb1b22015-12-14 11:44:01 +00002051 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002052 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2053 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00002054 }
2055 locations->SetOut(Location::SameAsFirstInput());
2056}
2057
2058void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
2059 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002060 DCHECK(locations->InAt(0).Equals(locations->Out()));
2061 if (SelectCanUseCMOV(select)) {
2062 // If both the condition and the source types are integer, we can generate
2063 // a CMOV to implement Select.
2064
2065 HInstruction* select_condition = select->GetCondition();
2066 Condition cond = kNotEqual;
2067
2068 // Figure out how to test the 'condition'.
2069 if (select_condition->IsCondition()) {
2070 HCondition* condition = select_condition->AsCondition();
2071 if (!condition->IsEmittedAtUseSite()) {
2072 // This was a previously materialized condition.
2073 // Can we use the existing condition code?
2074 if (AreEflagsSetFrom(condition, select)) {
2075 // Materialization was the previous instruction. Condition codes are right.
2076 cond = X86Condition(condition->GetCondition());
2077 } else {
2078 // No, we have to recreate the condition code.
2079 Register cond_reg = locations->InAt(2).AsRegister<Register>();
2080 __ testl(cond_reg, cond_reg);
2081 }
2082 } else {
2083 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002084 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
2085 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002086 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01002087 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002088 cond = X86Condition(condition->GetCondition());
2089 }
2090 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002091 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002092 Register cond_reg = locations->InAt(2).AsRegister<Register>();
2093 __ testl(cond_reg, cond_reg);
2094 }
2095
2096 // If the condition is true, overwrite the output, which already contains false.
2097 Location false_loc = locations->InAt(0);
2098 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002099 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002100 // 64 bit conditional move.
2101 Register false_high = false_loc.AsRegisterPairHigh<Register>();
2102 Register false_low = false_loc.AsRegisterPairLow<Register>();
2103 if (true_loc.IsRegisterPair()) {
2104 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
2105 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
2106 } else {
2107 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
2108 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
2109 }
2110 } else {
2111 // 32 bit conditional move.
2112 Register false_reg = false_loc.AsRegister<Register>();
2113 if (true_loc.IsRegister()) {
2114 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
2115 } else {
2116 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
2117 }
2118 }
2119 } else {
2120 NearLabel false_target;
2121 GenerateTestAndBranch<NearLabel>(
Andreas Gampe3db70682018-12-26 15:12:03 -08002122 select, /* condition_input_index= */ 2, /* true_target= */ nullptr, &false_target);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002123 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2124 __ Bind(&false_target);
2125 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002126}
2127
David Srbecky0cf44932015-12-09 14:09:59 +00002128void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002129 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00002130}
2131
David Srbeckyd28f4a02016-03-14 17:14:24 +00002132void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
2133 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002134}
2135
Vladimir Markodec78172020-06-19 15:31:23 +01002136void CodeGeneratorX86::IncreaseFrame(size_t adjustment) {
2137 __ subl(ESP, Immediate(adjustment));
2138 __ cfi().AdjustCFAOffset(adjustment);
2139}
2140
2141void CodeGeneratorX86::DecreaseFrame(size_t adjustment) {
2142 __ addl(ESP, Immediate(adjustment));
2143 __ cfi().AdjustCFAOffset(-adjustment);
2144}
2145
David Srbeckyc7098ff2016-02-09 14:30:11 +00002146void CodeGeneratorX86::GenerateNop() {
2147 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002148}
2149
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002150void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002151 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002152 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04002153 // Handle the long/FP comparisons made in instruction simplification.
2154 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002155 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04002156 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05002157 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00002158 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002159 locations->SetOut(Location::RequiresRegister());
2160 }
2161 break;
2162 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002163 case DataType::Type::kFloat32:
2164 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04002165 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002166 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
2167 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
2168 } else if (cond->InputAt(1)->IsConstant()) {
2169 locations->SetInAt(1, Location::RequiresFpuRegister());
2170 } else {
2171 locations->SetInAt(1, Location::Any());
2172 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002173 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002174 locations->SetOut(Location::RequiresRegister());
2175 }
2176 break;
2177 }
2178 default:
2179 locations->SetInAt(0, Location::RequiresRegister());
2180 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00002181 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002182 // We need a byte register.
2183 locations->SetOut(Location::RegisterLocation(ECX));
2184 }
2185 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002186 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002187}
2188
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002189void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002190 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002191 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002192 }
Mark Mendellc4701932015-04-10 13:18:51 -04002193
2194 LocationSummary* locations = cond->GetLocations();
2195 Location lhs = locations->InAt(0);
2196 Location rhs = locations->InAt(1);
2197 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05002198 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002199
2200 switch (cond->InputAt(0)->GetType()) {
2201 default: {
2202 // Integer case.
2203
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002204 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04002205 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01002206 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07002207 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04002208 return;
2209 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002210 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04002211 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2212 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002213 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002214 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04002215 GenerateFPJumps(cond, &true_label, &false_label);
2216 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002217 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002218 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04002219 GenerateFPJumps(cond, &true_label, &false_label);
2220 break;
2221 }
2222
2223 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002224 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002225
Roland Levillain4fa13f62015-07-06 18:11:54 +01002226 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04002227 __ Bind(&false_label);
2228 __ xorl(reg, reg);
2229 __ jmp(&done_label);
2230
Roland Levillain4fa13f62015-07-06 18:11:54 +01002231 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04002232 __ Bind(&true_label);
2233 __ movl(reg, Immediate(1));
2234 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002235}
2236
2237void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002238 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002239}
2240
2241void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002242 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002243}
2244
2245void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002246 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002247}
2248
2249void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002250 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002251}
2252
2253void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002254 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002255}
2256
2257void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002258 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002259}
2260
2261void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002262 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002263}
2264
2265void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002266 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002267}
2268
2269void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002270 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002271}
2272
2273void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002274 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002275}
2276
2277void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002278 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002279}
2280
2281void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002282 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002283}
2284
Aart Bike9f37602015-10-09 11:15:55 -07002285void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002286 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002287}
2288
2289void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002290 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002291}
2292
2293void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002294 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002295}
2296
2297void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002298 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002299}
2300
2301void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002302 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002303}
2304
2305void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002306 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002307}
2308
2309void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002310 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002311}
2312
2313void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002314 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002315}
2316
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002317void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002318 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002319 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002320 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002321}
2322
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002323void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002324 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002325}
2326
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002327void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2328 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002329 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002330 locations->SetOut(Location::ConstantLocation(constant));
2331}
2332
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002333void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002334 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002335}
2336
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002337void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002338 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002339 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002340 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002341}
2342
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002343void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002344 // Will be generated at use site.
2345}
2346
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002347void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2348 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002349 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002350 locations->SetOut(Location::ConstantLocation(constant));
2351}
2352
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002353void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002354 // Will be generated at use site.
2355}
2356
2357void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2358 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002359 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002360 locations->SetOut(Location::ConstantLocation(constant));
2361}
2362
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002363void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002364 // Will be generated at use site.
2365}
2366
Igor Murashkind01745e2017-04-05 16:40:31 -07002367void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2368 constructor_fence->SetLocations(nullptr);
2369}
2370
2371void InstructionCodeGeneratorX86::VisitConstructorFence(
2372 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2373 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2374}
2375
Calin Juravle27df7582015-04-17 19:12:31 +01002376void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2377 memory_barrier->SetLocations(nullptr);
2378}
2379
2380void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002381 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002382}
2383
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002384void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002385 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002386}
2387
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002388void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002389 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002390}
2391
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002392void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002393 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002394 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002395 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002396 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002397 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002398 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002399 case DataType::Type::kInt8:
2400 case DataType::Type::kUint16:
2401 case DataType::Type::kInt16:
2402 case DataType::Type::kInt32:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002403 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002404 break;
2405
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002406 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002407 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002408 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002409 break;
2410
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002411 case DataType::Type::kFloat32:
2412 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002413 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002414 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002415 break;
2416
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002417 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002418 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002419 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002420}
2421
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002422void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002423 switch (ret->InputAt(0)->GetType()) {
2424 case DataType::Type::kReference:
2425 case DataType::Type::kBool:
2426 case DataType::Type::kUint8:
2427 case DataType::Type::kInt8:
2428 case DataType::Type::kUint16:
2429 case DataType::Type::kInt16:
2430 case DataType::Type::kInt32:
2431 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
2432 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002433
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002434 case DataType::Type::kInt64:
2435 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2436 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
2437 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002438
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002439 case DataType::Type::kFloat32:
2440 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2441 if (GetGraph()->IsCompilingOsr()) {
2442 // To simplify callers of an OSR method, we put the return value in both
2443 // floating point and core registers.
2444 __ movd(EAX, XMM0);
2445 }
2446 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002447
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002448 case DataType::Type::kFloat64:
2449 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2450 if (GetGraph()->IsCompilingOsr()) {
2451 // To simplify callers of an OSR method, we put the return value in both
2452 // floating point and core registers.
2453 __ movd(EAX, XMM0);
2454 // Use XMM1 as temporary register to not clobber XMM0.
2455 __ movaps(XMM1, XMM0);
2456 __ psrlq(XMM1, Immediate(32));
2457 __ movd(EDX, XMM1);
2458 }
2459 break;
2460
2461 default:
2462 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002463 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002464 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002465}
2466
Calin Juravle175dc732015-08-25 15:42:32 +01002467void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2468 // The trampoline uses the same calling convention as dex calling conventions,
2469 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2470 // the method_idx.
2471 HandleInvoke(invoke);
2472}
2473
2474void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2475 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2476}
2477
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002478void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002479 // Explicit clinit checks triggered by static invokes must have been pruned by
2480 // art::PrepareForRegisterAllocation.
2481 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002482
Mark Mendellfb8d2792015-03-31 22:16:59 -04002483 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002484 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002485 if (invoke->GetLocations()->CanCall() &&
2486 invoke->HasPcRelativeMethodLoadKind() &&
2487 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).IsInvalid()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002488 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002489 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002490 return;
2491 }
2492
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01002493 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
Vladimir Marko86c87522020-05-11 16:55:55 +01002494 CriticalNativeCallingConventionVisitorX86 calling_convention_visitor(
2495 /*for_register_allocation=*/ true);
2496 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2497 } else {
2498 HandleInvoke(invoke);
2499 }
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002500
Vladimir Marko86c87522020-05-11 16:55:55 +01002501 // For PC-relative load kinds the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002502 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002503 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002504 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002505}
2506
Mark Mendell09ed1a32015-03-25 08:30:06 -04002507static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2508 if (invoke->GetLocations()->Intrinsified()) {
2509 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2510 intrinsic.Dispatch(invoke);
2511 return true;
2512 }
2513 return false;
2514}
2515
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002516void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002517 // Explicit clinit checks triggered by static invokes must have been pruned by
2518 // art::PrepareForRegisterAllocation.
2519 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002520
Mark Mendell09ed1a32015-03-25 08:30:06 -04002521 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2522 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002523 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002524
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002525 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002526 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002527 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002528}
2529
2530void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002531 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2532 if (intrinsic.TryDispatch(invoke)) {
2533 return;
2534 }
2535
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002536 HandleInvoke(invoke);
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002537
2538 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002539 // Add one temporary for inline cache update.
2540 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2541 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002542}
2543
2544void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002545 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002546 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002547}
2548
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002549void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002550 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2551 return;
2552 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002553
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002554 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002555 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002556}
2557
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002558void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002559 // This call to HandleInvoke allocates a temporary (core) register
2560 // which is also used to transfer the hidden argument from FP to
2561 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002562 HandleInvoke(invoke);
2563 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002564 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002565
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002566 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002567 // Add one temporary for inline cache update.
2568 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2569 }
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01002570
2571 // For PC-relative load kinds the invoke has an extra input, the PC-relative address base.
2572 if (IsPcRelativeMethodLoadKind(invoke->GetHiddenArgumentLoadKind())) {
2573 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2574 }
2575
2576 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
2577 invoke->GetLocations()->SetInAt(invoke->GetNumberOfArguments() - 1,
2578 Location::RequiresRegister());
2579 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002580}
2581
2582void CodeGeneratorX86::MaybeGenerateInlineCacheCheck(HInstruction* instruction, Register klass) {
2583 DCHECK_EQ(EAX, klass);
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002584 // We know the destination of an intrinsic, so no need to record inline
2585 // caches (also the intrinsic location builder doesn't request an additional
2586 // temporary).
2587 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002588 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002589 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002590 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
Nicolas Geoffray095dc462020-08-17 16:40:28 +01002591 ScopedProfilingInfoUse spiu(
2592 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
2593 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002594 if (info != nullptr) {
2595 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
2596 uint32_t address = reinterpret_cast32<uint32_t>(cache);
2597 if (kIsDebugBuild) {
2598 uint32_t temp_index = instruction->GetLocations()->GetTempCount() - 1u;
2599 CHECK_EQ(EBP, instruction->GetLocations()->GetTemp(temp_index).AsRegister<Register>());
2600 }
2601 Register temp = EBP;
2602 NearLabel done;
2603 __ movl(temp, Immediate(address));
2604 // Fast path for a monomorphic cache.
2605 __ cmpl(klass, Address(temp, InlineCache::ClassesOffset().Int32Value()));
2606 __ j(kEqual, &done);
2607 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(kQuickUpdateInlineCache).Int32Value());
2608 __ Bind(&done);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002609 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002610 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002611}
2612
2613void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2614 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002615 LocationSummary* locations = invoke->GetLocations();
2616 Register temp = locations->GetTemp(0).AsRegister<Register>();
2617 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002618 Location receiver = locations->InAt(0);
2619 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2620
Roland Levillain0d5a2812015-11-13 10:07:31 +00002621 // Set the hidden argument. This is safe to do this here, as XMM7
2622 // won't be modified thereafter, before the `call` instruction.
2623 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01002624 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
2625 __ movd(hidden_reg, locations->InAt(invoke->GetNumberOfArguments() - 1).AsRegister<Register>());
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00002626 } else if (invoke->GetHiddenArgumentLoadKind() != MethodLoadKind::kRuntimeCall) {
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01002627 codegen_->LoadMethod(invoke->GetHiddenArgumentLoadKind(), locations->GetTemp(0), invoke);
2628 __ movd(hidden_reg, temp);
2629 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002630
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002631 if (receiver.IsStackSlot()) {
2632 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002633 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002634 __ movl(temp, Address(temp, class_offset));
2635 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002636 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002637 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002638 }
Roland Levillain4d027112015-07-01 15:41:14 +01002639 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002640 // Instead of simply (possibly) unpoisoning `temp` here, we should
2641 // emit a read barrier for the previous class reference load.
2642 // However this is not required in practice, as this is an
2643 // intermediate/temporary reference and because the current
2644 // concurrent copying collector keeps the from-space memory
2645 // intact/accessible until the end of the marking phase (the
2646 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002647 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002648
2649 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
2650
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002651 // temp = temp->GetAddressOfIMT()
2652 __ movl(temp,
2653 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002654 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002655 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002656 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002657 __ movl(temp, Address(temp, method_offset));
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00002658 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRuntimeCall) {
2659 // We pass the method from the IMT in case of a conflict. This will ensure
2660 // we go into the runtime to resolve the actual method.
2661 __ movd(hidden_reg, temp);
2662 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002663 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002664 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002665 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002666
2667 DCHECK(!codegen_->IsLeafMethod());
2668 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2669}
2670
Orion Hodsonac141392017-01-13 11:53:47 +00002671void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00002672 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2673 if (intrinsic.TryDispatch(invoke)) {
2674 return;
2675 }
Orion Hodsonac141392017-01-13 11:53:47 +00002676 HandleInvoke(invoke);
2677}
2678
2679void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00002680 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2681 return;
2682 }
Orion Hodsonac141392017-01-13 11:53:47 +00002683 codegen_->GenerateInvokePolymorphicCall(invoke);
2684}
2685
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002686void LocationsBuilderX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2687 HandleInvoke(invoke);
2688}
2689
2690void InstructionCodeGeneratorX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2691 codegen_->GenerateInvokeCustomCall(invoke);
2692}
2693
Roland Levillain88cb1752014-10-20 16:36:47 +01002694void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2695 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002696 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002697 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002698 case DataType::Type::kInt32:
2699 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002700 locations->SetInAt(0, Location::RequiresRegister());
2701 locations->SetOut(Location::SameAsFirstInput());
2702 break;
2703
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002704 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002705 locations->SetInAt(0, Location::RequiresFpuRegister());
2706 locations->SetOut(Location::SameAsFirstInput());
2707 locations->AddTemp(Location::RequiresRegister());
2708 locations->AddTemp(Location::RequiresFpuRegister());
2709 break;
2710
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002711 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002712 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002713 locations->SetOut(Location::SameAsFirstInput());
2714 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002715 break;
2716
2717 default:
2718 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2719 }
2720}
2721
2722void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2723 LocationSummary* locations = neg->GetLocations();
2724 Location out = locations->Out();
2725 Location in = locations->InAt(0);
2726 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002727 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002728 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002729 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002730 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002731 break;
2732
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002733 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002734 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002735 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002736 __ negl(out.AsRegisterPairLow<Register>());
2737 // Negation is similar to subtraction from zero. The least
2738 // significant byte triggers a borrow when it is different from
2739 // zero; to take it into account, add 1 to the most significant
2740 // byte if the carry flag (CF) is set to 1 after the first NEGL
2741 // operation.
2742 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2743 __ negl(out.AsRegisterPairHigh<Register>());
2744 break;
2745
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002746 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002747 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002748 Register constant = locations->GetTemp(0).AsRegister<Register>();
2749 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002750 // Implement float negation with an exclusive or with value
2751 // 0x80000000 (mask for bit 31, representing the sign of a
2752 // single-precision floating-point number).
2753 __ movl(constant, Immediate(INT32_C(0x80000000)));
2754 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002755 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002756 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002757 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002758
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002759 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002760 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002761 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002762 // Implement double negation with an exclusive or with value
2763 // 0x8000000000000000 (mask for bit 63, representing the sign of
2764 // a double-precision floating-point number).
2765 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002766 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002767 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002768 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002769
2770 default:
2771 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2772 }
2773}
2774
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002775void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2776 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002777 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002778 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002779 locations->SetInAt(0, Location::RequiresFpuRegister());
2780 locations->SetInAt(1, Location::RequiresRegister());
2781 locations->SetOut(Location::SameAsFirstInput());
2782 locations->AddTemp(Location::RequiresFpuRegister());
2783}
2784
2785void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2786 LocationSummary* locations = neg->GetLocations();
2787 Location out = locations->Out();
2788 DCHECK(locations->InAt(0).Equals(out));
2789
2790 Register constant_area = locations->InAt(1).AsRegister<Register>();
2791 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002792 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002793 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2794 neg->GetBaseMethodAddress(),
2795 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002796 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2797 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002798 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2799 neg->GetBaseMethodAddress(),
2800 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002801 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2802 }
2803}
2804
Roland Levillaindff1f282014-11-05 14:15:05 +00002805void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002806 DataType::Type result_type = conversion->GetResultType();
2807 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002808 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2809 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002810
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002811 // The float-to-long and double-to-long type conversions rely on a
2812 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002813 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002814 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2815 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002816 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002817 : LocationSummary::kNoCall;
2818 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002819 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002820
Roland Levillaindff1f282014-11-05 14:15:05 +00002821 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002822 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002823 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002824 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002825 case DataType::Type::kUint8:
2826 case DataType::Type::kInt8:
2827 case DataType::Type::kUint16:
2828 case DataType::Type::kInt16:
2829 case DataType::Type::kInt32:
2830 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2831 // Make the output overlap to please the register allocator. This greatly simplifies
2832 // the validation of the linear scan implementation
2833 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2834 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002835 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002836 HInstruction* input = conversion->InputAt(0);
2837 Location input_location = input->IsConstant()
2838 ? Location::ConstantLocation(input->AsConstant())
2839 : Location::RegisterPairLocation(EAX, EDX);
2840 locations->SetInAt(0, input_location);
2841 // Make the output overlap to please the register allocator. This greatly simplifies
2842 // the validation of the linear scan implementation
2843 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2844 break;
2845 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002846
2847 default:
2848 LOG(FATAL) << "Unexpected type conversion from " << input_type
2849 << " to " << result_type;
2850 }
2851 break;
2852
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002853 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002854 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002855 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2856 locations->SetInAt(0, Location::Any());
2857 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002858 break;
2859
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002860 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002861 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002862 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002863 locations->SetInAt(0, Location::Any());
2864 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2865 break;
2866
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002867 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002868 locations->SetInAt(0, Location::RequiresFpuRegister());
2869 locations->SetOut(Location::RequiresRegister());
2870 locations->AddTemp(Location::RequiresFpuRegister());
2871 break;
2872
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002873 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002874 locations->SetInAt(0, Location::RequiresFpuRegister());
2875 locations->SetOut(Location::RequiresRegister());
2876 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002877 break;
2878
2879 default:
2880 LOG(FATAL) << "Unexpected type conversion from " << input_type
2881 << " to " << result_type;
2882 }
2883 break;
2884
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002885 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002886 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002887 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002888 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002889 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002890 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002891 case DataType::Type::kInt16:
2892 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002893 locations->SetInAt(0, Location::RegisterLocation(EAX));
2894 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2895 break;
2896
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002897 case DataType::Type::kFloat32:
2898 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002899 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002900 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2901 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2902
Vladimir Marko949c91f2015-01-27 10:48:44 +00002903 // The runtime helper puts the result in EAX, EDX.
2904 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002905 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002906 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002907
2908 default:
2909 LOG(FATAL) << "Unexpected type conversion from " << input_type
2910 << " to " << result_type;
2911 }
2912 break;
2913
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002914 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002915 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002916 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002917 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002918 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002919 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002920 case DataType::Type::kInt16:
2921 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002922 locations->SetInAt(0, Location::RequiresRegister());
2923 locations->SetOut(Location::RequiresFpuRegister());
2924 break;
2925
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002926 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002927 locations->SetInAt(0, Location::Any());
2928 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002929 break;
2930
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002931 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002932 locations->SetInAt(0, Location::RequiresFpuRegister());
2933 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002934 break;
2935
2936 default:
2937 LOG(FATAL) << "Unexpected type conversion from " << input_type
2938 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002939 }
Roland Levillaincff13742014-11-17 14:32:17 +00002940 break;
2941
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002942 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002943 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002944 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002945 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002946 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002947 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002948 case DataType::Type::kInt16:
2949 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002950 locations->SetInAt(0, Location::RequiresRegister());
2951 locations->SetOut(Location::RequiresFpuRegister());
2952 break;
2953
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002954 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002955 locations->SetInAt(0, Location::Any());
2956 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002957 break;
2958
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002959 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002960 locations->SetInAt(0, Location::RequiresFpuRegister());
2961 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002962 break;
2963
2964 default:
2965 LOG(FATAL) << "Unexpected type conversion from " << input_type
2966 << " to " << result_type;
2967 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002968 break;
2969
2970 default:
2971 LOG(FATAL) << "Unexpected type conversion from " << input_type
2972 << " to " << result_type;
2973 }
2974}
2975
2976void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2977 LocationSummary* locations = conversion->GetLocations();
2978 Location out = locations->Out();
2979 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002980 DataType::Type result_type = conversion->GetResultType();
2981 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002982 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2983 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002984 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002985 case DataType::Type::kUint8:
2986 switch (input_type) {
2987 case DataType::Type::kInt8:
2988 case DataType::Type::kUint16:
2989 case DataType::Type::kInt16:
2990 case DataType::Type::kInt32:
2991 if (in.IsRegister()) {
2992 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2993 } else {
2994 DCHECK(in.GetConstant()->IsIntConstant());
2995 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2996 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2997 }
2998 break;
2999 case DataType::Type::kInt64:
3000 if (in.IsRegisterPair()) {
3001 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
3002 } else {
3003 DCHECK(in.GetConstant()->IsLongConstant());
3004 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3005 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
3006 }
3007 break;
3008
3009 default:
3010 LOG(FATAL) << "Unexpected type conversion from " << input_type
3011 << " to " << result_type;
3012 }
3013 break;
3014
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003015 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00003016 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003017 case DataType::Type::kUint8:
3018 case DataType::Type::kUint16:
3019 case DataType::Type::kInt16:
3020 case DataType::Type::kInt32:
3021 if (in.IsRegister()) {
3022 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
3023 } else {
3024 DCHECK(in.GetConstant()->IsIntConstant());
3025 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
3026 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
3027 }
3028 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003029 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003030 if (in.IsRegisterPair()) {
3031 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
3032 } else {
3033 DCHECK(in.GetConstant()->IsLongConstant());
3034 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3035 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
3036 }
3037 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003038
3039 default:
3040 LOG(FATAL) << "Unexpected type conversion from " << input_type
3041 << " to " << result_type;
3042 }
3043 break;
3044
3045 case DataType::Type::kUint16:
3046 switch (input_type) {
3047 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003048 case DataType::Type::kInt16:
3049 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003050 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003051 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
3052 } else if (in.IsStackSlot()) {
3053 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003054 } else {
3055 DCHECK(in.GetConstant()->IsIntConstant());
3056 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003057 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
3058 }
3059 break;
3060 case DataType::Type::kInt64:
3061 if (in.IsRegisterPair()) {
3062 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
3063 } else if (in.IsDoubleStackSlot()) {
3064 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
3065 } else {
3066 DCHECK(in.GetConstant()->IsLongConstant());
3067 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3068 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003069 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00003070 break;
3071
3072 default:
3073 LOG(FATAL) << "Unexpected type conversion from " << input_type
3074 << " to " << result_type;
3075 }
3076 break;
3077
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003078 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00003079 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003080 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003081 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00003082 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003083 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00003084 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003085 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00003086 } else {
3087 DCHECK(in.GetConstant()->IsIntConstant());
3088 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003089 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00003090 }
3091 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003092 case DataType::Type::kInt64:
3093 if (in.IsRegisterPair()) {
3094 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
3095 } else if (in.IsDoubleStackSlot()) {
3096 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
3097 } else {
3098 DCHECK(in.GetConstant()->IsLongConstant());
3099 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3100 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
3101 }
3102 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00003103
3104 default:
3105 LOG(FATAL) << "Unexpected type conversion from " << input_type
3106 << " to " << result_type;
3107 }
3108 break;
3109
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003110 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00003111 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003112 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00003113 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003114 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00003115 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003116 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00003117 } else {
3118 DCHECK(in.IsConstant());
3119 DCHECK(in.GetConstant()->IsLongConstant());
3120 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003121 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00003122 }
3123 break;
3124
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003125 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00003126 XmmRegister input = in.AsFpuRegister<XmmRegister>();
3127 Register output = out.AsRegister<Register>();
3128 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04003129 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00003130
3131 __ movl(output, Immediate(kPrimIntMax));
3132 // temp = int-to-float(output)
3133 __ cvtsi2ss(temp, output);
3134 // if input >= temp goto done
3135 __ comiss(input, temp);
3136 __ j(kAboveEqual, &done);
3137 // if input == NaN goto nan
3138 __ j(kUnordered, &nan);
3139 // output = float-to-int-truncate(input)
3140 __ cvttss2si(output, input);
3141 __ jmp(&done);
3142 __ Bind(&nan);
3143 // output = 0
3144 __ xorl(output, output);
3145 __ Bind(&done);
3146 break;
3147 }
3148
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003149 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003150 XmmRegister input = in.AsFpuRegister<XmmRegister>();
3151 Register output = out.AsRegister<Register>();
3152 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04003153 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003154
3155 __ movl(output, Immediate(kPrimIntMax));
3156 // temp = int-to-double(output)
3157 __ cvtsi2sd(temp, output);
3158 // if input >= temp goto done
3159 __ comisd(input, temp);
3160 __ j(kAboveEqual, &done);
3161 // if input == NaN goto nan
3162 __ j(kUnordered, &nan);
3163 // output = double-to-int-truncate(input)
3164 __ cvttsd2si(output, input);
3165 __ jmp(&done);
3166 __ Bind(&nan);
3167 // output = 0
3168 __ xorl(output, output);
3169 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00003170 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003171 }
Roland Levillain946e1432014-11-11 17:35:19 +00003172
3173 default:
3174 LOG(FATAL) << "Unexpected type conversion from " << input_type
3175 << " to " << result_type;
3176 }
3177 break;
3178
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003179 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00003180 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003181 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003182 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003183 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003184 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003185 case DataType::Type::kInt16:
3186 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00003187 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
3188 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003189 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00003190 __ cdq();
3191 break;
3192
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003193 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01003194 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003195 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00003196 break;
3197
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003198 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01003199 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003200 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00003201 break;
3202
3203 default:
3204 LOG(FATAL) << "Unexpected type conversion from " << input_type
3205 << " to " << result_type;
3206 }
3207 break;
3208
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003209 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00003210 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003211 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003212 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003213 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003214 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003215 case DataType::Type::kInt16:
3216 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003217 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003218 break;
3219
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003220 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003221 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00003222
Roland Levillain232ade02015-04-20 15:14:36 +01003223 // Create stack space for the call to
3224 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
3225 // TODO: enhance register allocator to ask for stack temporaries.
3226 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003227 adjustment = DataType::Size(DataType::Type::kInt64);
Vladimir Markodec78172020-06-19 15:31:23 +01003228 codegen_->IncreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003229 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003230
Roland Levillain232ade02015-04-20 15:14:36 +01003231 // Load the value to the FP stack, using temporaries if needed.
3232 PushOntoFPStack(in, 0, adjustment, false, true);
3233
3234 if (out.IsStackSlot()) {
3235 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
3236 } else {
3237 __ fstps(Address(ESP, 0));
3238 Location stack_temp = Location::StackSlot(0);
3239 codegen_->Move32(out, stack_temp);
3240 }
3241
3242 // Remove the temporary stack space we allocated.
3243 if (adjustment != 0) {
Vladimir Markodec78172020-06-19 15:31:23 +01003244 codegen_->DecreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003245 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003246 break;
3247 }
3248
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003249 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003250 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003251 break;
3252
3253 default:
3254 LOG(FATAL) << "Unexpected type conversion from " << input_type
3255 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003256 }
Roland Levillaincff13742014-11-17 14:32:17 +00003257 break;
3258
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003259 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00003260 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003261 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003262 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003263 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003264 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003265 case DataType::Type::kInt16:
3266 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003267 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003268 break;
3269
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003270 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003271 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00003272
Roland Levillain232ade02015-04-20 15:14:36 +01003273 // Create stack space for the call to
3274 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
3275 // TODO: enhance register allocator to ask for stack temporaries.
3276 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003277 adjustment = DataType::Size(DataType::Type::kInt64);
Vladimir Markodec78172020-06-19 15:31:23 +01003278 codegen_->IncreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003279 }
3280
3281 // Load the value to the FP stack, using temporaries if needed.
3282 PushOntoFPStack(in, 0, adjustment, false, true);
3283
3284 if (out.IsDoubleStackSlot()) {
3285 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
3286 } else {
3287 __ fstpl(Address(ESP, 0));
3288 Location stack_temp = Location::DoubleStackSlot(0);
3289 codegen_->Move64(out, stack_temp);
3290 }
3291
3292 // Remove the temporary stack space we allocated.
3293 if (adjustment != 0) {
Vladimir Markodec78172020-06-19 15:31:23 +01003294 codegen_->DecreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003295 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003296 break;
3297 }
3298
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003299 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003300 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003301 break;
3302
3303 default:
3304 LOG(FATAL) << "Unexpected type conversion from " << input_type
3305 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003306 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003307 break;
3308
3309 default:
3310 LOG(FATAL) << "Unexpected type conversion from " << input_type
3311 << " to " << result_type;
3312 }
3313}
3314
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003315void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003316 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003317 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003318 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003319 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05003320 locations->SetInAt(0, Location::RequiresRegister());
3321 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3322 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3323 break;
3324 }
3325
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003326 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003327 locations->SetInAt(0, Location::RequiresRegister());
3328 locations->SetInAt(1, Location::Any());
3329 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003330 break;
3331 }
3332
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003333 case DataType::Type::kFloat32:
3334 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003335 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003336 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3337 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003338 } else if (add->InputAt(1)->IsConstant()) {
3339 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003340 } else {
3341 locations->SetInAt(1, Location::Any());
3342 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003343 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003344 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003345 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003346
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003347 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003348 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Elliott Hughesc1896c92018-11-29 11:33:18 -08003349 UNREACHABLE();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003350 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003351}
3352
3353void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
3354 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003355 Location first = locations->InAt(0);
3356 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05003357 Location out = locations->Out();
3358
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003359 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003360 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003361 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003362 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3363 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003364 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3365 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05003366 } else {
3367 __ leal(out.AsRegister<Register>(), Address(
3368 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3369 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003370 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003371 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3372 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3373 __ addl(out.AsRegister<Register>(), Immediate(value));
3374 } else {
3375 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3376 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003377 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003378 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003379 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003380 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003381 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003382 }
3383
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003384 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003385 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003386 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3387 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003388 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003389 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3390 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003391 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003392 } else {
3393 DCHECK(second.IsConstant()) << second;
3394 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3395 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3396 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003397 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003398 break;
3399 }
3400
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003401 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003402 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003403 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003404 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3405 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003406 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003407 __ addss(first.AsFpuRegister<XmmRegister>(),
3408 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003409 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3410 const_area->GetBaseMethodAddress(),
3411 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003412 } else {
3413 DCHECK(second.IsStackSlot());
3414 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003415 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003416 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003417 }
3418
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003419 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003420 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003421 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003422 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3423 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003424 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003425 __ addsd(first.AsFpuRegister<XmmRegister>(),
3426 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003427 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3428 const_area->GetBaseMethodAddress(),
3429 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003430 } else {
3431 DCHECK(second.IsDoubleStackSlot());
3432 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003433 }
3434 break;
3435 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003436
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003437 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003438 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003439 }
3440}
3441
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003442void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003443 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003444 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003445 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003446 case DataType::Type::kInt32:
3447 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003448 locations->SetInAt(0, Location::RequiresRegister());
3449 locations->SetInAt(1, Location::Any());
3450 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003451 break;
3452 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003453 case DataType::Type::kFloat32:
3454 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003455 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003456 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3457 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003458 } else if (sub->InputAt(1)->IsConstant()) {
3459 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003460 } else {
3461 locations->SetInAt(1, Location::Any());
3462 }
Calin Juravle11351682014-10-23 15:38:15 +01003463 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003464 break;
Calin Juravle11351682014-10-23 15:38:15 +01003465 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003466
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003467 default:
Calin Juravle11351682014-10-23 15:38:15 +01003468 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003469 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003470}
3471
3472void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3473 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003474 Location first = locations->InAt(0);
3475 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003476 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003477 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003478 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003479 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003480 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003481 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003482 __ subl(first.AsRegister<Register>(),
3483 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003484 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003485 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003486 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003487 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003488 }
3489
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003490 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003491 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003492 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3493 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003494 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003495 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003496 __ sbbl(first.AsRegisterPairHigh<Register>(),
3497 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003498 } else {
3499 DCHECK(second.IsConstant()) << second;
3500 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3501 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3502 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003503 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003504 break;
3505 }
3506
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003507 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003508 if (second.IsFpuRegister()) {
3509 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3510 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3511 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003512 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003513 __ subss(first.AsFpuRegister<XmmRegister>(),
3514 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003515 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3516 const_area->GetBaseMethodAddress(),
3517 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003518 } else {
3519 DCHECK(second.IsStackSlot());
3520 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3521 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003522 break;
Calin Juravle11351682014-10-23 15:38:15 +01003523 }
3524
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003525 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003526 if (second.IsFpuRegister()) {
3527 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3528 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3529 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003530 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003531 __ subsd(first.AsFpuRegister<XmmRegister>(),
3532 codegen_->LiteralDoubleAddress(
3533 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003534 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003535 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3536 } else {
3537 DCHECK(second.IsDoubleStackSlot());
3538 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3539 }
Calin Juravle11351682014-10-23 15:38:15 +01003540 break;
3541 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003542
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003543 default:
Calin Juravle11351682014-10-23 15:38:15 +01003544 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003545 }
3546}
3547
Calin Juravle34bacdf2014-10-07 20:23:36 +01003548void LocationsBuilderX86::VisitMul(HMul* mul) {
3549 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003550 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003551 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003552 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003553 locations->SetInAt(0, Location::RequiresRegister());
3554 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003555 if (mul->InputAt(1)->IsIntConstant()) {
3556 // Can use 3 operand multiply.
3557 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3558 } else {
3559 locations->SetOut(Location::SameAsFirstInput());
3560 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003561 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003562 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003563 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003564 locations->SetInAt(1, Location::Any());
3565 locations->SetOut(Location::SameAsFirstInput());
3566 // Needed for imul on 32bits with 64bits output.
3567 locations->AddTemp(Location::RegisterLocation(EAX));
3568 locations->AddTemp(Location::RegisterLocation(EDX));
3569 break;
3570 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003571 case DataType::Type::kFloat32:
3572 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003573 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003574 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3575 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003576 } else if (mul->InputAt(1)->IsConstant()) {
3577 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003578 } else {
3579 locations->SetInAt(1, Location::Any());
3580 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003581 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003582 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003583 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003584
3585 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003586 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003587 }
3588}
3589
3590void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3591 LocationSummary* locations = mul->GetLocations();
3592 Location first = locations->InAt(0);
3593 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003594 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003595
3596 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003597 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003598 // The constant may have ended up in a register, so test explicitly to avoid
3599 // problems where the output may not be the same as the first operand.
3600 if (mul->InputAt(1)->IsIntConstant()) {
3601 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3602 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3603 } else if (second.IsRegister()) {
3604 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003605 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003606 } else {
3607 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003608 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003609 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003610 }
3611 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003612
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003613 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003614 Register in1_hi = first.AsRegisterPairHigh<Register>();
3615 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003616 Register eax = locations->GetTemp(0).AsRegister<Register>();
3617 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003618
3619 DCHECK_EQ(EAX, eax);
3620 DCHECK_EQ(EDX, edx);
3621
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003622 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003623 // output: in1
3624 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3625 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3626 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003627 if (second.IsConstant()) {
3628 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003629
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003630 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3631 int32_t low_value = Low32Bits(value);
3632 int32_t high_value = High32Bits(value);
3633 Immediate low(low_value);
3634 Immediate high(high_value);
3635
3636 __ movl(eax, high);
3637 // eax <- in1.lo * in2.hi
3638 __ imull(eax, in1_lo);
3639 // in1.hi <- in1.hi * in2.lo
3640 __ imull(in1_hi, low);
3641 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3642 __ addl(in1_hi, eax);
3643 // move in2_lo to eax to prepare for double precision
3644 __ movl(eax, low);
3645 // edx:eax <- in1.lo * in2.lo
3646 __ mull(in1_lo);
3647 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3648 __ addl(in1_hi, edx);
3649 // in1.lo <- (in1.lo * in2.lo)[31:0];
3650 __ movl(in1_lo, eax);
3651 } else if (second.IsRegisterPair()) {
3652 Register in2_hi = second.AsRegisterPairHigh<Register>();
3653 Register in2_lo = second.AsRegisterPairLow<Register>();
3654
3655 __ movl(eax, in2_hi);
3656 // eax <- in1.lo * in2.hi
3657 __ imull(eax, in1_lo);
3658 // in1.hi <- in1.hi * in2.lo
3659 __ imull(in1_hi, in2_lo);
3660 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3661 __ addl(in1_hi, eax);
3662 // move in1_lo to eax to prepare for double precision
3663 __ movl(eax, in1_lo);
3664 // edx:eax <- in1.lo * in2.lo
3665 __ mull(in2_lo);
3666 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3667 __ addl(in1_hi, edx);
3668 // in1.lo <- (in1.lo * in2.lo)[31:0];
3669 __ movl(in1_lo, eax);
3670 } else {
3671 DCHECK(second.IsDoubleStackSlot()) << second;
3672 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3673 Address in2_lo(ESP, second.GetStackIndex());
3674
3675 __ movl(eax, in2_hi);
3676 // eax <- in1.lo * in2.hi
3677 __ imull(eax, in1_lo);
3678 // in1.hi <- in1.hi * in2.lo
3679 __ imull(in1_hi, in2_lo);
3680 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3681 __ addl(in1_hi, eax);
3682 // move in1_lo to eax to prepare for double precision
3683 __ movl(eax, in1_lo);
3684 // edx:eax <- in1.lo * in2.lo
3685 __ mull(in2_lo);
3686 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3687 __ addl(in1_hi, edx);
3688 // in1.lo <- (in1.lo * in2.lo)[31:0];
3689 __ movl(in1_lo, eax);
3690 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003691
3692 break;
3693 }
3694
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003695 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003696 DCHECK(first.Equals(locations->Out()));
3697 if (second.IsFpuRegister()) {
3698 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3699 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3700 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003701 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003702 __ mulss(first.AsFpuRegister<XmmRegister>(),
3703 codegen_->LiteralFloatAddress(
3704 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003705 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003706 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3707 } else {
3708 DCHECK(second.IsStackSlot());
3709 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3710 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003711 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003712 }
3713
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003714 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003715 DCHECK(first.Equals(locations->Out()));
3716 if (second.IsFpuRegister()) {
3717 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3718 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3719 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003720 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003721 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3722 codegen_->LiteralDoubleAddress(
3723 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003724 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003725 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3726 } else {
3727 DCHECK(second.IsDoubleStackSlot());
3728 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3729 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003730 break;
3731 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003732
3733 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003734 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003735 }
3736}
3737
Roland Levillain232ade02015-04-20 15:14:36 +01003738void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3739 uint32_t temp_offset,
3740 uint32_t stack_adjustment,
3741 bool is_fp,
3742 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003743 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003744 DCHECK(!is_wide);
3745 if (is_fp) {
3746 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3747 } else {
3748 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3749 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003750 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003751 DCHECK(is_wide);
3752 if (is_fp) {
3753 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3754 } else {
3755 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3756 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003757 } else {
3758 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003759 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003760 Location stack_temp = Location::StackSlot(temp_offset);
3761 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003762 if (is_fp) {
3763 __ flds(Address(ESP, temp_offset));
3764 } else {
3765 __ filds(Address(ESP, temp_offset));
3766 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003767 } else {
3768 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3769 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003770 if (is_fp) {
3771 __ fldl(Address(ESP, temp_offset));
3772 } else {
3773 __ fildl(Address(ESP, temp_offset));
3774 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003775 }
3776 }
3777}
3778
3779void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003780 DataType::Type type = rem->GetResultType();
3781 bool is_float = type == DataType::Type::kFloat32;
3782 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003783 LocationSummary* locations = rem->GetLocations();
3784 Location first = locations->InAt(0);
3785 Location second = locations->InAt(1);
3786 Location out = locations->Out();
3787
3788 // Create stack space for 2 elements.
3789 // TODO: enhance register allocator to ask for stack temporaries.
Vladimir Markodec78172020-06-19 15:31:23 +01003790 codegen_->IncreaseFrame(2 * elem_size);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003791
3792 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003793 const bool is_wide = !is_float;
Andreas Gampe3db70682018-12-26 15:12:03 -08003794 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp= */ true, is_wide);
3795 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp= */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003796
3797 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003798 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003799 __ Bind(&retry);
3800 __ fprem();
3801
3802 // Move FP status to AX.
3803 __ fstsw();
3804
3805 // And see if the argument reduction is complete. This is signaled by the
3806 // C2 FPU flag bit set to 0.
3807 __ andl(EAX, Immediate(kC2ConditionMask));
3808 __ j(kNotEqual, &retry);
3809
3810 // We have settled on the final value. Retrieve it into an XMM register.
3811 // Store FP top of stack to real stack.
3812 if (is_float) {
3813 __ fsts(Address(ESP, 0));
3814 } else {
3815 __ fstl(Address(ESP, 0));
3816 }
3817
3818 // Pop the 2 items from the FP stack.
3819 __ fucompp();
3820
3821 // Load the value from the stack into an XMM register.
3822 DCHECK(out.IsFpuRegister()) << out;
3823 if (is_float) {
3824 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3825 } else {
3826 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3827 }
3828
3829 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01003830 codegen_->DecreaseFrame(2 * elem_size);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003831}
3832
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003833
3834void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3835 DCHECK(instruction->IsDiv() || instruction->IsRem());
3836
3837 LocationSummary* locations = instruction->GetLocations();
3838 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003839 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003840
3841 Register out_register = locations->Out().AsRegister<Register>();
3842 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003843 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003844
3845 DCHECK(imm == 1 || imm == -1);
3846
3847 if (instruction->IsRem()) {
3848 __ xorl(out_register, out_register);
3849 } else {
3850 __ movl(out_register, input_register);
3851 if (imm == -1) {
3852 __ negl(out_register);
3853 }
3854 }
3855}
3856
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303857void InstructionCodeGeneratorX86::RemByPowerOfTwo(HRem* instruction) {
3858 LocationSummary* locations = instruction->GetLocations();
3859 Location second = locations->InAt(1);
3860
3861 Register out = locations->Out().AsRegister<Register>();
3862 Register numerator = locations->InAt(0).AsRegister<Register>();
3863
3864 int32_t imm = Int64FromConstant(second.GetConstant());
3865 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3866 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3867
3868 Register tmp = locations->GetTemp(0).AsRegister<Register>();
3869 NearLabel done;
3870 __ movl(out, numerator);
3871 __ andl(out, Immediate(abs_imm-1));
3872 __ j(Condition::kZero, &done);
3873 __ leal(tmp, Address(out, static_cast<int32_t>(~(abs_imm-1))));
3874 __ testl(numerator, numerator);
3875 __ cmovl(Condition::kLess, out, tmp);
3876 __ Bind(&done);
3877}
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003878
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003879void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003880 LocationSummary* locations = instruction->GetLocations();
3881
3882 Register out_register = locations->Out().AsRegister<Register>();
3883 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003884 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003885 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3886 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003887
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003888 Register num = locations->GetTemp(0).AsRegister<Register>();
3889
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003890 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003891 __ testl(input_register, input_register);
3892 __ cmovl(kGreaterEqual, num, input_register);
3893 int shift = CTZ(imm);
3894 __ sarl(num, Immediate(shift));
3895
3896 if (imm < 0) {
3897 __ negl(num);
3898 }
3899
3900 __ movl(out_register, num);
3901}
3902
3903void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3904 DCHECK(instruction->IsDiv() || instruction->IsRem());
3905
3906 LocationSummary* locations = instruction->GetLocations();
3907 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3908
3909 Register eax = locations->InAt(0).AsRegister<Register>();
3910 Register out = locations->Out().AsRegister<Register>();
3911 Register num;
3912 Register edx;
3913
3914 if (instruction->IsDiv()) {
3915 edx = locations->GetTemp(0).AsRegister<Register>();
3916 num = locations->GetTemp(1).AsRegister<Register>();
3917 } else {
3918 edx = locations->Out().AsRegister<Register>();
3919 num = locations->GetTemp(0).AsRegister<Register>();
3920 }
3921
3922 DCHECK_EQ(EAX, eax);
3923 DCHECK_EQ(EDX, edx);
3924 if (instruction->IsDiv()) {
3925 DCHECK_EQ(EAX, out);
3926 } else {
3927 DCHECK_EQ(EDX, out);
3928 }
3929
3930 int64_t magic;
3931 int shift;
Andreas Gampe3db70682018-12-26 15:12:03 -08003932 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003933
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003934 // Save the numerator.
3935 __ movl(num, eax);
3936
3937 // EAX = magic
3938 __ movl(eax, Immediate(magic));
3939
3940 // EDX:EAX = magic * numerator
3941 __ imull(num);
3942
3943 if (imm > 0 && magic < 0) {
3944 // EDX += num
3945 __ addl(edx, num);
3946 } else if (imm < 0 && magic > 0) {
3947 __ subl(edx, num);
3948 }
3949
3950 // Shift if needed.
3951 if (shift != 0) {
3952 __ sarl(edx, Immediate(shift));
3953 }
3954
3955 // EDX += 1 if EDX < 0
3956 __ movl(eax, edx);
3957 __ shrl(edx, Immediate(31));
3958 __ addl(edx, eax);
3959
3960 if (instruction->IsRem()) {
3961 __ movl(eax, num);
3962 __ imull(edx, Immediate(imm));
3963 __ subl(eax, edx);
3964 __ movl(edx, eax);
3965 } else {
3966 __ movl(eax, edx);
3967 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003968}
3969
Calin Juravlebacfec32014-11-14 15:54:36 +00003970void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3971 DCHECK(instruction->IsDiv() || instruction->IsRem());
3972
3973 LocationSummary* locations = instruction->GetLocations();
3974 Location out = locations->Out();
3975 Location first = locations->InAt(0);
3976 Location second = locations->InAt(1);
3977 bool is_div = instruction->IsDiv();
3978
3979 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003980 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003981 DCHECK_EQ(EAX, first.AsRegister<Register>());
3982 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003983
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003984 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003985 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003986
3987 if (imm == 0) {
3988 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3989 } else if (imm == 1 || imm == -1) {
3990 DivRemOneOrMinusOne(instruction);
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303991 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
3992 if (is_div) {
3993 DivByPowerOfTwo(instruction->AsDiv());
3994 } else {
3995 RemByPowerOfTwo(instruction->AsRem());
3996 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003997 } else {
3998 DCHECK(imm <= -2 || imm >= 2);
3999 GenerateDivRemWithAnyConstant(instruction);
4000 }
4001 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004002 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00004003 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004004 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00004005
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004006 Register second_reg = second.AsRegister<Register>();
4007 // 0x80000000/-1 triggers an arithmetic exception!
4008 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
4009 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00004010
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004011 __ cmpl(second_reg, Immediate(-1));
4012 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00004013
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004014 // edx:eax <- sign-extended of eax
4015 __ cdq();
4016 // eax = quotient, edx = remainder
4017 __ idivl(second_reg);
4018 __ Bind(slow_path->GetExitLabel());
4019 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004020 break;
4021 }
4022
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004023 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004024 InvokeRuntimeCallingConvention calling_convention;
4025 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
4026 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
4027 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
4028 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
4029 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
4030 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
4031
4032 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004033 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004034 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004035 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004036 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004037 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004038 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004039 break;
4040 }
4041
4042 default:
4043 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
4044 }
4045}
4046
Calin Juravle7c4954d2014-10-28 16:57:40 +00004047void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004048 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004049 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004050 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01004051 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004052
Calin Juravle7c4954d2014-10-28 16:57:40 +00004053 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004054 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00004055 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004056 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00004057 locations->SetOut(Location::SameAsFirstInput());
4058 // Intel uses edx:eax as the dividend.
4059 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004060 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
4061 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
4062 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01004063 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004064 locations->AddTemp(Location::RequiresRegister());
4065 }
Calin Juravled0d48522014-11-04 16:40:20 +00004066 break;
4067 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004068 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004069 InvokeRuntimeCallingConvention calling_convention;
4070 locations->SetInAt(0, Location::RegisterPairLocation(
4071 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4072 locations->SetInAt(1, Location::RegisterPairLocation(
4073 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4074 // Runtime helper puts the result in EAX, EDX.
4075 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00004076 break;
4077 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004078 case DataType::Type::kFloat32:
4079 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004080 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00004081 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
4082 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00004083 } else if (div->InputAt(1)->IsConstant()) {
4084 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00004085 } else {
4086 locations->SetInAt(1, Location::Any());
4087 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004088 locations->SetOut(Location::SameAsFirstInput());
4089 break;
4090 }
4091
4092 default:
4093 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4094 }
4095}
4096
4097void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
4098 LocationSummary* locations = div->GetLocations();
4099 Location first = locations->InAt(0);
4100 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004101
4102 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004103 case DataType::Type::kInt32:
4104 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004105 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004106 break;
4107 }
4108
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004109 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04004110 if (second.IsFpuRegister()) {
4111 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
4112 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
4113 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00004114 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04004115 __ divss(first.AsFpuRegister<XmmRegister>(),
4116 codegen_->LiteralFloatAddress(
4117 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004118 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04004119 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
4120 } else {
4121 DCHECK(second.IsStackSlot());
4122 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
4123 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004124 break;
4125 }
4126
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004127 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04004128 if (second.IsFpuRegister()) {
4129 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
4130 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
4131 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00004132 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04004133 __ divsd(first.AsFpuRegister<XmmRegister>(),
4134 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004135 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
4136 const_area->GetBaseMethodAddress(),
4137 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04004138 } else {
4139 DCHECK(second.IsDoubleStackSlot());
4140 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
4141 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004142 break;
4143 }
4144
4145 default:
4146 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4147 }
4148}
4149
Calin Juravlebacfec32014-11-14 15:54:36 +00004150void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004151 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004152
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004153 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004154 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004155 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01004156 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00004157
Calin Juravled2ec87d2014-12-08 14:24:46 +00004158 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004159 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004160 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004161 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00004162 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004163 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
4164 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
4165 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01004166 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004167 locations->AddTemp(Location::RequiresRegister());
4168 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004169 break;
4170 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004171 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004172 InvokeRuntimeCallingConvention calling_convention;
4173 locations->SetInAt(0, Location::RegisterPairLocation(
4174 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4175 locations->SetInAt(1, Location::RegisterPairLocation(
4176 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4177 // Runtime helper puts the result in EAX, EDX.
4178 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
4179 break;
4180 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004181 case DataType::Type::kFloat64:
4182 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05004183 locations->SetInAt(0, Location::Any());
4184 locations->SetInAt(1, Location::Any());
4185 locations->SetOut(Location::RequiresFpuRegister());
4186 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00004187 break;
4188 }
4189
4190 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004191 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004192 }
4193}
4194
4195void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004196 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00004197 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004198 case DataType::Type::kInt32:
4199 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004200 GenerateDivRemIntegral(rem);
4201 break;
4202 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004203 case DataType::Type::kFloat32:
4204 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05004205 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00004206 break;
4207 }
4208 default:
4209 LOG(FATAL) << "Unexpected rem type " << type;
4210 }
4211}
4212
Aart Bik1f8d51b2018-02-15 10:42:37 -08004213static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
4214 LocationSummary* locations = new (allocator) LocationSummary(minmax);
4215 switch (minmax->GetResultType()) {
4216 case DataType::Type::kInt32:
4217 locations->SetInAt(0, Location::RequiresRegister());
4218 locations->SetInAt(1, Location::RequiresRegister());
4219 locations->SetOut(Location::SameAsFirstInput());
4220 break;
4221 case DataType::Type::kInt64:
4222 locations->SetInAt(0, Location::RequiresRegister());
4223 locations->SetInAt(1, Location::RequiresRegister());
4224 locations->SetOut(Location::SameAsFirstInput());
4225 // Register to use to perform a long subtract to set cc.
4226 locations->AddTemp(Location::RequiresRegister());
4227 break;
4228 case DataType::Type::kFloat32:
4229 locations->SetInAt(0, Location::RequiresFpuRegister());
4230 locations->SetInAt(1, Location::RequiresFpuRegister());
4231 locations->SetOut(Location::SameAsFirstInput());
4232 locations->AddTemp(Location::RequiresRegister());
4233 break;
4234 case DataType::Type::kFloat64:
4235 locations->SetInAt(0, Location::RequiresFpuRegister());
4236 locations->SetInAt(1, Location::RequiresFpuRegister());
4237 locations->SetOut(Location::SameAsFirstInput());
4238 break;
4239 default:
4240 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
4241 }
4242}
4243
Aart Bik351df3e2018-03-07 11:54:57 -08004244void InstructionCodeGeneratorX86::GenerateMinMaxInt(LocationSummary* locations,
4245 bool is_min,
4246 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08004247 Location op1_loc = locations->InAt(0);
4248 Location op2_loc = locations->InAt(1);
4249
4250 // Shortcut for same input locations.
4251 if (op1_loc.Equals(op2_loc)) {
4252 // Can return immediately, as op1_loc == out_loc.
4253 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
4254 // a copy here.
4255 DCHECK(locations->Out().Equals(op1_loc));
4256 return;
4257 }
4258
4259 if (type == DataType::Type::kInt64) {
4260 // Need to perform a subtract to get the sign right.
4261 // op1 is already in the same location as the output.
4262 Location output = locations->Out();
4263 Register output_lo = output.AsRegisterPairLow<Register>();
4264 Register output_hi = output.AsRegisterPairHigh<Register>();
4265
4266 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
4267 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
4268
4269 // The comparison is performed by subtracting the second operand from
4270 // the first operand and then setting the status flags in the same
4271 // manner as the SUB instruction."
4272 __ cmpl(output_lo, op2_lo);
4273
4274 // Now use a temp and the borrow to finish the subtraction of op2_hi.
4275 Register temp = locations->GetTemp(0).AsRegister<Register>();
4276 __ movl(temp, output_hi);
4277 __ sbbl(temp, op2_hi);
4278
4279 // Now the condition code is correct.
4280 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
4281 __ cmovl(cond, output_lo, op2_lo);
4282 __ cmovl(cond, output_hi, op2_hi);
4283 } else {
4284 DCHECK_EQ(type, DataType::Type::kInt32);
4285 Register out = locations->Out().AsRegister<Register>();
4286 Register op2 = op2_loc.AsRegister<Register>();
4287
4288 // (out := op1)
4289 // out <=? op2
4290 // if out is min jmp done
4291 // out := op2
4292 // done:
4293
4294 __ cmpl(out, op2);
4295 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
4296 __ cmovl(cond, out, op2);
4297 }
4298}
4299
4300void InstructionCodeGeneratorX86::GenerateMinMaxFP(LocationSummary* locations,
4301 bool is_min,
4302 DataType::Type type) {
4303 Location op1_loc = locations->InAt(0);
4304 Location op2_loc = locations->InAt(1);
4305 Location out_loc = locations->Out();
4306 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
4307
4308 // Shortcut for same input locations.
4309 if (op1_loc.Equals(op2_loc)) {
4310 DCHECK(out_loc.Equals(op1_loc));
4311 return;
4312 }
4313
4314 // (out := op1)
4315 // out <=? op2
4316 // if Nan jmp Nan_label
4317 // if out is min jmp done
4318 // if op2 is min jmp op2_label
4319 // handle -0/+0
4320 // jmp done
4321 // Nan_label:
4322 // out := NaN
4323 // op2_label:
4324 // out := op2
4325 // done:
4326 //
4327 // This removes one jmp, but needs to copy one input (op1) to out.
4328 //
4329 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
4330
4331 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
4332
4333 NearLabel nan, done, op2_label;
4334 if (type == DataType::Type::kFloat64) {
4335 __ ucomisd(out, op2);
4336 } else {
4337 DCHECK_EQ(type, DataType::Type::kFloat32);
4338 __ ucomiss(out, op2);
4339 }
4340
4341 __ j(Condition::kParityEven, &nan);
4342
4343 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
4344 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
4345
4346 // Handle 0.0/-0.0.
4347 if (is_min) {
4348 if (type == DataType::Type::kFloat64) {
4349 __ orpd(out, op2);
4350 } else {
4351 __ orps(out, op2);
4352 }
4353 } else {
4354 if (type == DataType::Type::kFloat64) {
4355 __ andpd(out, op2);
4356 } else {
4357 __ andps(out, op2);
4358 }
4359 }
4360 __ jmp(&done);
4361
4362 // NaN handling.
4363 __ Bind(&nan);
4364 if (type == DataType::Type::kFloat64) {
4365 // TODO: Use a constant from the constant table (requires extra input).
4366 __ LoadLongConstant(out, kDoubleNaN);
4367 } else {
4368 Register constant = locations->GetTemp(0).AsRegister<Register>();
4369 __ movl(constant, Immediate(kFloatNaN));
4370 __ movd(out, constant);
4371 }
4372 __ jmp(&done);
4373
4374 // out := op2;
4375 __ Bind(&op2_label);
4376 if (type == DataType::Type::kFloat64) {
4377 __ movsd(out, op2);
4378 } else {
4379 __ movss(out, op2);
4380 }
4381
4382 // Done.
4383 __ Bind(&done);
4384}
4385
Aart Bik351df3e2018-03-07 11:54:57 -08004386void InstructionCodeGeneratorX86::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4387 DataType::Type type = minmax->GetResultType();
4388 switch (type) {
4389 case DataType::Type::kInt32:
4390 case DataType::Type::kInt64:
4391 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
4392 break;
4393 case DataType::Type::kFloat32:
4394 case DataType::Type::kFloat64:
4395 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
4396 break;
4397 default:
4398 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4399 }
4400}
4401
Aart Bik1f8d51b2018-02-15 10:42:37 -08004402void LocationsBuilderX86::VisitMin(HMin* min) {
4403 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4404}
4405
4406void InstructionCodeGeneratorX86::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004407 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004408}
4409
4410void LocationsBuilderX86::VisitMax(HMax* max) {
4411 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4412}
4413
4414void InstructionCodeGeneratorX86::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004415 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004416}
4417
Aart Bik3dad3412018-02-28 12:01:46 -08004418void LocationsBuilderX86::VisitAbs(HAbs* abs) {
4419 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4420 switch (abs->GetResultType()) {
4421 case DataType::Type::kInt32:
4422 locations->SetInAt(0, Location::RegisterLocation(EAX));
4423 locations->SetOut(Location::SameAsFirstInput());
4424 locations->AddTemp(Location::RegisterLocation(EDX));
4425 break;
4426 case DataType::Type::kInt64:
4427 locations->SetInAt(0, Location::RequiresRegister());
4428 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4429 locations->AddTemp(Location::RequiresRegister());
4430 break;
4431 case DataType::Type::kFloat32:
4432 locations->SetInAt(0, Location::RequiresFpuRegister());
4433 locations->SetOut(Location::SameAsFirstInput());
4434 locations->AddTemp(Location::RequiresFpuRegister());
4435 locations->AddTemp(Location::RequiresRegister());
4436 break;
4437 case DataType::Type::kFloat64:
4438 locations->SetInAt(0, Location::RequiresFpuRegister());
4439 locations->SetOut(Location::SameAsFirstInput());
4440 locations->AddTemp(Location::RequiresFpuRegister());
4441 break;
4442 default:
4443 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4444 }
4445}
4446
4447void InstructionCodeGeneratorX86::VisitAbs(HAbs* abs) {
4448 LocationSummary* locations = abs->GetLocations();
4449 switch (abs->GetResultType()) {
4450 case DataType::Type::kInt32: {
4451 Register out = locations->Out().AsRegister<Register>();
4452 DCHECK_EQ(out, EAX);
4453 Register temp = locations->GetTemp(0).AsRegister<Register>();
4454 DCHECK_EQ(temp, EDX);
4455 // Sign extend EAX into EDX.
4456 __ cdq();
4457 // XOR EAX with sign.
4458 __ xorl(EAX, EDX);
4459 // Subtract out sign to correct.
4460 __ subl(EAX, EDX);
4461 // The result is in EAX.
4462 break;
4463 }
4464 case DataType::Type::kInt64: {
4465 Location input = locations->InAt(0);
4466 Register input_lo = input.AsRegisterPairLow<Register>();
4467 Register input_hi = input.AsRegisterPairHigh<Register>();
4468 Location output = locations->Out();
4469 Register output_lo = output.AsRegisterPairLow<Register>();
4470 Register output_hi = output.AsRegisterPairHigh<Register>();
4471 Register temp = locations->GetTemp(0).AsRegister<Register>();
4472 // Compute the sign into the temporary.
4473 __ movl(temp, input_hi);
4474 __ sarl(temp, Immediate(31));
4475 // Store the sign into the output.
4476 __ movl(output_lo, temp);
4477 __ movl(output_hi, temp);
4478 // XOR the input to the output.
4479 __ xorl(output_lo, input_lo);
4480 __ xorl(output_hi, input_hi);
4481 // Subtract the sign.
4482 __ subl(output_lo, temp);
4483 __ sbbl(output_hi, temp);
4484 break;
4485 }
4486 case DataType::Type::kFloat32: {
4487 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4488 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4489 Register constant = locations->GetTemp(1).AsRegister<Register>();
4490 __ movl(constant, Immediate(INT32_C(0x7FFFFFFF)));
4491 __ movd(temp, constant);
4492 __ andps(out, temp);
4493 break;
4494 }
4495 case DataType::Type::kFloat64: {
4496 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4497 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4498 // TODO: Use a constant from the constant table (requires extra input).
4499 __ LoadLongConstant(temp, INT64_C(0x7FFFFFFFFFFFFFFF));
4500 __ andpd(out, temp);
4501 break;
4502 }
4503 default:
4504 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4505 }
4506}
4507
Calin Juravled0d48522014-11-04 16:40:20 +00004508void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004509 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004510 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004511 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004512 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004513 case DataType::Type::kInt8:
4514 case DataType::Type::kUint16:
4515 case DataType::Type::kInt16:
4516 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004517 locations->SetInAt(0, Location::Any());
4518 break;
4519 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004520 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004521 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
4522 if (!instruction->IsConstant()) {
4523 locations->AddTemp(Location::RequiresRegister());
4524 }
4525 break;
4526 }
4527 default:
4528 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4529 }
Calin Juravled0d48522014-11-04 16:40:20 +00004530}
4531
4532void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004533 SlowPathCode* slow_path =
4534 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004535 codegen_->AddSlowPath(slow_path);
4536
4537 LocationSummary* locations = instruction->GetLocations();
4538 Location value = locations->InAt(0);
4539
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004540 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004541 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004542 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004543 case DataType::Type::kInt8:
4544 case DataType::Type::kUint16:
4545 case DataType::Type::kInt16:
4546 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004547 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004548 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004549 __ j(kEqual, slow_path->GetEntryLabel());
4550 } else if (value.IsStackSlot()) {
4551 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
4552 __ j(kEqual, slow_path->GetEntryLabel());
4553 } else {
4554 DCHECK(value.IsConstant()) << value;
4555 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004556 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004557 }
4558 }
4559 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004560 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004561 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004562 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004563 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004564 __ movl(temp, value.AsRegisterPairLow<Register>());
4565 __ orl(temp, value.AsRegisterPairHigh<Register>());
4566 __ j(kEqual, slow_path->GetEntryLabel());
4567 } else {
4568 DCHECK(value.IsConstant()) << value;
4569 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4570 __ jmp(slow_path->GetEntryLabel());
4571 }
4572 }
4573 break;
4574 }
4575 default:
4576 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004577 }
Calin Juravled0d48522014-11-04 16:40:20 +00004578}
4579
Calin Juravle9aec02f2014-11-18 23:06:35 +00004580void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
4581 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4582
4583 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004584 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004585
4586 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004587 case DataType::Type::kInt32:
4588 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004589 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00004590 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00004591 // The shift count needs to be in CL or a constant.
4592 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004593 locations->SetOut(Location::SameAsFirstInput());
4594 break;
4595 }
4596 default:
4597 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4598 }
4599}
4600
4601void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
4602 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4603
4604 LocationSummary* locations = op->GetLocations();
4605 Location first = locations->InAt(0);
4606 Location second = locations->InAt(1);
4607 DCHECK(first.Equals(locations->Out()));
4608
4609 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004610 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00004611 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004612 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004613 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004614 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004615 DCHECK_EQ(ECX, second_reg);
4616 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004617 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004618 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004619 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004620 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004621 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004622 }
4623 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004624 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004625 if (shift == 0) {
4626 return;
4627 }
4628 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004629 if (op->IsShl()) {
4630 __ shll(first_reg, imm);
4631 } else if (op->IsShr()) {
4632 __ sarl(first_reg, imm);
4633 } else {
4634 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004635 }
4636 }
4637 break;
4638 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004639 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004640 if (second.IsRegister()) {
4641 Register second_reg = second.AsRegister<Register>();
4642 DCHECK_EQ(ECX, second_reg);
4643 if (op->IsShl()) {
4644 GenerateShlLong(first, second_reg);
4645 } else if (op->IsShr()) {
4646 GenerateShrLong(first, second_reg);
4647 } else {
4648 GenerateUShrLong(first, second_reg);
4649 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004650 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00004651 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004652 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004653 // Nothing to do if the shift is 0, as the input is already the output.
4654 if (shift != 0) {
4655 if (op->IsShl()) {
4656 GenerateShlLong(first, shift);
4657 } else if (op->IsShr()) {
4658 GenerateShrLong(first, shift);
4659 } else {
4660 GenerateUShrLong(first, shift);
4661 }
4662 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004663 }
4664 break;
4665 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004666 default:
4667 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4668 }
4669}
4670
Mark P Mendell73945692015-04-29 14:56:17 +00004671void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
4672 Register low = loc.AsRegisterPairLow<Register>();
4673 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04004674 if (shift == 1) {
4675 // This is just an addition.
4676 __ addl(low, low);
4677 __ adcl(high, high);
4678 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00004679 // Shift by 32 is easy. High gets low, and low gets 0.
4680 codegen_->EmitParallelMoves(
4681 loc.ToLow(),
4682 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004683 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004684 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4685 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004686 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004687 } else if (shift > 32) {
4688 // Low part becomes 0. High part is low part << (shift-32).
4689 __ movl(high, low);
4690 __ shll(high, Immediate(shift - 32));
4691 __ xorl(low, low);
4692 } else {
4693 // Between 1 and 31.
4694 __ shld(high, low, Immediate(shift));
4695 __ shll(low, Immediate(shift));
4696 }
4697}
4698
Calin Juravle9aec02f2014-11-18 23:06:35 +00004699void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004700 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004701 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4702 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4703 __ testl(shifter, Immediate(32));
4704 __ j(kEqual, &done);
4705 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4706 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4707 __ Bind(&done);
4708}
4709
Mark P Mendell73945692015-04-29 14:56:17 +00004710void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4711 Register low = loc.AsRegisterPairLow<Register>();
4712 Register high = loc.AsRegisterPairHigh<Register>();
4713 if (shift == 32) {
4714 // Need to copy the sign.
4715 DCHECK_NE(low, high);
4716 __ movl(low, high);
4717 __ sarl(high, Immediate(31));
4718 } else if (shift > 32) {
4719 DCHECK_NE(low, high);
4720 // High part becomes sign. Low part is shifted by shift - 32.
4721 __ movl(low, high);
4722 __ sarl(high, Immediate(31));
4723 __ sarl(low, Immediate(shift - 32));
4724 } else {
4725 // Between 1 and 31.
4726 __ shrd(low, high, Immediate(shift));
4727 __ sarl(high, Immediate(shift));
4728 }
4729}
4730
Calin Juravle9aec02f2014-11-18 23:06:35 +00004731void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004732 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004733 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4734 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4735 __ testl(shifter, Immediate(32));
4736 __ j(kEqual, &done);
4737 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4738 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4739 __ Bind(&done);
4740}
4741
Mark P Mendell73945692015-04-29 14:56:17 +00004742void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4743 Register low = loc.AsRegisterPairLow<Register>();
4744 Register high = loc.AsRegisterPairHigh<Register>();
4745 if (shift == 32) {
4746 // Shift by 32 is easy. Low gets high, and high gets 0.
4747 codegen_->EmitParallelMoves(
4748 loc.ToHigh(),
4749 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004750 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004751 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4752 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004753 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004754 } else if (shift > 32) {
4755 // Low part is high >> (shift - 32). High part becomes 0.
4756 __ movl(low, high);
4757 __ shrl(low, Immediate(shift - 32));
4758 __ xorl(high, high);
4759 } else {
4760 // Between 1 and 31.
4761 __ shrd(low, high, Immediate(shift));
4762 __ shrl(high, Immediate(shift));
4763 }
4764}
4765
Calin Juravle9aec02f2014-11-18 23:06:35 +00004766void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004767 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004768 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4769 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4770 __ testl(shifter, Immediate(32));
4771 __ j(kEqual, &done);
4772 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4773 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4774 __ Bind(&done);
4775}
4776
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004777void LocationsBuilderX86::VisitRor(HRor* ror) {
4778 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004779 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004780
4781 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004782 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004783 // Add the temporary needed.
4784 locations->AddTemp(Location::RequiresRegister());
4785 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004786 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004787 locations->SetInAt(0, Location::RequiresRegister());
4788 // The shift count needs to be in CL (unless it is a constant).
4789 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4790 locations->SetOut(Location::SameAsFirstInput());
4791 break;
4792 default:
4793 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4794 UNREACHABLE();
4795 }
4796}
4797
4798void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4799 LocationSummary* locations = ror->GetLocations();
4800 Location first = locations->InAt(0);
4801 Location second = locations->InAt(1);
4802
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004803 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004804 Register first_reg = first.AsRegister<Register>();
4805 if (second.IsRegister()) {
4806 Register second_reg = second.AsRegister<Register>();
4807 __ rorl(first_reg, second_reg);
4808 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004809 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004810 __ rorl(first_reg, imm);
4811 }
4812 return;
4813 }
4814
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004815 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004816 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4817 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4818 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4819 if (second.IsRegister()) {
4820 Register second_reg = second.AsRegister<Register>();
4821 DCHECK_EQ(second_reg, ECX);
4822 __ movl(temp_reg, first_reg_hi);
4823 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4824 __ shrd(first_reg_lo, temp_reg, second_reg);
4825 __ movl(temp_reg, first_reg_hi);
4826 __ testl(second_reg, Immediate(32));
4827 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4828 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4829 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004830 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004831 if (shift_amt == 0) {
4832 // Already fine.
4833 return;
4834 }
4835 if (shift_amt == 32) {
4836 // Just swap.
4837 __ movl(temp_reg, first_reg_lo);
4838 __ movl(first_reg_lo, first_reg_hi);
4839 __ movl(first_reg_hi, temp_reg);
4840 return;
4841 }
4842
4843 Immediate imm(shift_amt);
4844 // Save the constents of the low value.
4845 __ movl(temp_reg, first_reg_lo);
4846
4847 // Shift right into low, feeding bits from high.
4848 __ shrd(first_reg_lo, first_reg_hi, imm);
4849
4850 // Shift right into high, feeding bits from the original low.
4851 __ shrd(first_reg_hi, temp_reg, imm);
4852
4853 // Swap if needed.
4854 if (shift_amt > 32) {
4855 __ movl(temp_reg, first_reg_lo);
4856 __ movl(first_reg_lo, first_reg_hi);
4857 __ movl(first_reg_hi, temp_reg);
4858 }
4859 }
4860}
4861
Calin Juravle9aec02f2014-11-18 23:06:35 +00004862void LocationsBuilderX86::VisitShl(HShl* shl) {
4863 HandleShift(shl);
4864}
4865
4866void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4867 HandleShift(shl);
4868}
4869
4870void LocationsBuilderX86::VisitShr(HShr* shr) {
4871 HandleShift(shr);
4872}
4873
4874void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4875 HandleShift(shr);
4876}
4877
4878void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4879 HandleShift(ushr);
4880}
4881
4882void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4883 HandleShift(ushr);
4884}
4885
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004886void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004887 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4888 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004889 locations->SetOut(Location::RegisterLocation(EAX));
Alex Lightd109e302018-06-27 10:25:41 -07004890 InvokeRuntimeCallingConvention calling_convention;
4891 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004892}
4893
4894void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07004895 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
4896 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4897 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004898}
4899
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004900void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004901 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4902 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004903 locations->SetOut(Location::RegisterLocation(EAX));
4904 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004905 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4906 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004907}
4908
4909void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01004910 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
4911 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004912 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004913 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004914 DCHECK(!codegen_->IsLeafMethod());
4915}
4916
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004917void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004918 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004919 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004920 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4921 if (location.IsStackSlot()) {
4922 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4923 } else if (location.IsDoubleStackSlot()) {
4924 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004925 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004926 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004927}
4928
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004929void InstructionCodeGeneratorX86::VisitParameterValue(
4930 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4931}
4932
4933void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4934 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004935 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004936 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4937}
4938
4939void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004940}
4941
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004942void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4943 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004944 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004945 locations->SetInAt(0, Location::RequiresRegister());
4946 locations->SetOut(Location::RequiresRegister());
4947}
4948
4949void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4950 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004951 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004952 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004953 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004954 __ movl(locations->Out().AsRegister<Register>(),
4955 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004956 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004957 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004958 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004959 __ movl(locations->Out().AsRegister<Register>(),
4960 Address(locations->InAt(0).AsRegister<Register>(),
4961 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4962 // temp = temp->GetImtEntryAt(method_offset);
4963 __ movl(locations->Out().AsRegister<Register>(),
4964 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004965 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004966}
4967
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004968void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004969 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004970 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004971 locations->SetInAt(0, Location::RequiresRegister());
4972 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004973}
4974
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004975void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4976 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004977 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004978 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004979 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004980 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004981 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004982 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004983 break;
4984
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004985 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004986 __ notl(out.AsRegisterPairLow<Register>());
4987 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004988 break;
4989
4990 default:
4991 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4992 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004993}
4994
David Brazdil66d126e2015-04-03 16:02:44 +01004995void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4996 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004997 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004998 locations->SetInAt(0, Location::RequiresRegister());
4999 locations->SetOut(Location::SameAsFirstInput());
5000}
5001
5002void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01005003 LocationSummary* locations = bool_not->GetLocations();
5004 Location in = locations->InAt(0);
5005 Location out = locations->Out();
5006 DCHECK(in.Equals(out));
5007 __ xorl(out.AsRegister<Register>(), Immediate(1));
5008}
5009
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005010void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005011 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005012 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00005013 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005014 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005015 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005016 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005017 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005018 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005019 case DataType::Type::kInt32:
5020 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00005021 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00005022 locations->SetInAt(1, Location::Any());
5023 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5024 break;
5025 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005026 case DataType::Type::kFloat32:
5027 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00005028 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00005029 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
5030 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
5031 } else if (compare->InputAt(1)->IsConstant()) {
5032 locations->SetInAt(1, Location::RequiresFpuRegister());
5033 } else {
5034 locations->SetInAt(1, Location::Any());
5035 }
Calin Juravleddb7df22014-11-25 20:56:51 +00005036 locations->SetOut(Location::RequiresRegister());
5037 break;
5038 }
5039 default:
5040 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5041 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005042}
5043
5044void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005045 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005046 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00005047 Location left = locations->InAt(0);
5048 Location right = locations->InAt(1);
5049
Mark Mendell0c9497d2015-08-21 09:30:05 -04005050 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08005051 Condition less_cond = kLess;
5052
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005053 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005054 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005055 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005056 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005057 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005058 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005059 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01005060 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08005061 break;
5062 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005063 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005064 Register left_low = left.AsRegisterPairLow<Register>();
5065 Register left_high = left.AsRegisterPairHigh<Register>();
5066 int32_t val_low = 0;
5067 int32_t val_high = 0;
5068 bool right_is_const = false;
5069
5070 if (right.IsConstant()) {
5071 DCHECK(right.GetConstant()->IsLongConstant());
5072 right_is_const = true;
5073 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
5074 val_low = Low32Bits(val);
5075 val_high = High32Bits(val);
5076 }
5077
Calin Juravleddb7df22014-11-25 20:56:51 +00005078 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005079 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005080 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005081 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005082 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005083 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08005084 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005085 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005086 __ j(kLess, &less); // Signed compare.
5087 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005088 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005089 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005090 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005091 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005092 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005093 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08005094 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005095 }
Aart Bika19616e2016-02-01 18:57:58 -08005096 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00005097 break;
5098 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005099 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00005100 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00005101 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08005102 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00005103 break;
5104 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005105 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00005106 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00005107 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08005108 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005109 break;
5110 }
5111 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00005112 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005113 }
Aart Bika19616e2016-02-01 18:57:58 -08005114
Calin Juravleddb7df22014-11-25 20:56:51 +00005115 __ movl(out, Immediate(0));
5116 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08005117 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00005118
5119 __ Bind(&greater);
5120 __ movl(out, Immediate(1));
5121 __ jmp(&done);
5122
5123 __ Bind(&less);
5124 __ movl(out, Immediate(-1));
5125
5126 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005127}
5128
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005129void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005130 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005131 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005132 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01005133 locations->SetInAt(i, Location::Any());
5134 }
5135 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005136}
5137
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005138void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005139 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005140}
5141
Roland Levillain7c1559a2015-12-15 10:55:36 +00005142void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00005143 /*
5144 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
5145 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
5146 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
5147 */
5148 switch (kind) {
5149 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00005150 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00005151 break;
5152 }
5153 case MemBarrierKind::kAnyStore:
5154 case MemBarrierKind::kLoadAny:
5155 case MemBarrierKind::kStoreStore: {
5156 // nop
5157 break;
5158 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05005159 case MemBarrierKind::kNTStoreStore:
5160 // Non-Temporal Store/Store needs an explicit fence.
Andreas Gampe3db70682018-12-26 15:12:03 -08005161 MemoryFence(/* non-temporal= */ true);
Mark Mendell7aa04a12016-01-27 22:39:07 -05005162 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005163 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005164}
5165
Vladimir Markodc151b22015-10-15 18:02:30 +01005166HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
5167 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01005168 ArtMethod* method ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005169 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005170}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005171
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005172Register CodeGeneratorX86::GetInvokeExtraParameter(HInvoke* invoke, Register temp) {
5173 if (invoke->IsInvokeStaticOrDirect()) {
5174 return GetInvokeStaticOrDirectExtraParameter(invoke->AsInvokeStaticOrDirect(), temp);
5175 }
5176 DCHECK(invoke->IsInvokeInterface());
5177 Location location =
5178 invoke->GetLocations()->InAt(invoke->AsInvokeInterface()->GetSpecialInputIndex());
5179 return location.AsRegister<Register>();
5180}
5181
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005182Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
5183 Register temp) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00005184 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005185 if (!invoke->GetLocations()->Intrinsified()) {
5186 return location.AsRegister<Register>();
5187 }
5188 // For intrinsics we allow any location, so it may be on the stack.
5189 if (!location.IsRegister()) {
5190 __ movl(temp, Address(ESP, location.GetStackIndex()));
5191 return temp;
5192 }
5193 // For register locations, check if the register was saved. If so, get it from the stack.
5194 // Note: There is a chance that the register was saved but not overwritten, so we could
5195 // save one load. However, since this is just an intrinsic slow path we prefer this
5196 // simple and more robust approach rather that trying to determine if that's the case.
5197 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00005198 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
5199 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
5200 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
5201 __ movl(temp, Address(ESP, stack_offset));
5202 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005203 }
5204 return location.AsRegister<Register>();
5205}
5206
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005207void CodeGeneratorX86::LoadMethod(MethodLoadKind load_kind, Location temp, HInvoke* invoke) {
5208 switch (load_kind) {
5209 case MethodLoadKind::kBootImageLinkTimePcRelative: {
5210 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
5211 Register base_reg = GetInvokeExtraParameter(invoke, temp.AsRegister<Register>());
5212 __ leal(temp.AsRegister<Register>(),
5213 Address(base_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
5214 RecordBootImageMethodPatch(invoke);
5215 break;
5216 }
5217 case MethodLoadKind::kBootImageRelRo: {
5218 size_t index = invoke->IsInvokeInterface()
5219 ? invoke->AsInvokeInterface()->GetSpecialInputIndex()
5220 : invoke->AsInvokeStaticOrDirect()->GetSpecialInputIndex();
5221 Register base_reg = GetInvokeExtraParameter(invoke, temp.AsRegister<Register>());
5222 __ movl(temp.AsRegister<Register>(), Address(base_reg, kPlaceholder32BitOffset));
5223 RecordBootImageRelRoPatch(
5224 invoke->InputAt(index)->AsX86ComputeBaseMethodAddress(),
5225 GetBootImageOffset(invoke));
5226 break;
5227 }
5228 case MethodLoadKind::kBssEntry: {
5229 Register base_reg = GetInvokeExtraParameter(invoke, temp.AsRegister<Register>());
5230 __ movl(temp.AsRegister<Register>(), Address(base_reg, kPlaceholder32BitOffset));
5231 RecordMethodBssEntryPatch(invoke);
5232 // No need for memory fence, thanks to the x86 memory model.
5233 break;
5234 }
5235 case MethodLoadKind::kJitDirectAddress: {
5236 __ movl(temp.AsRegister<Register>(),
5237 Immediate(reinterpret_cast32<uint32_t>(invoke->GetResolvedMethod())));
5238 break;
5239 }
5240 case MethodLoadKind::kRuntimeCall: {
5241 // Test situation, don't do anything.
5242 break;
5243 }
5244 default: {
5245 LOG(FATAL) << "Load kind should have already been handled " << load_kind;
5246 UNREACHABLE();
5247 }
5248 }
5249}
5250
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005251void CodeGeneratorX86::GenerateStaticOrDirectCall(
5252 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00005253 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
5254 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005255 case MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005256 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005257 uint32_t offset =
5258 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
5259 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00005260 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005261 }
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005262 case MethodLoadKind::kRecursive: {
Vladimir Marko86c87522020-05-11 16:55:55 +01005263 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005264 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005265 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005266 case MethodLoadKind::kRuntimeCall: {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005267 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5268 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01005269 }
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005270 case MethodLoadKind::kBootImageLinkTimePcRelative:
5271 // For kCallCriticalNative we skip loading the method and do the call directly.
5272 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
5273 break;
5274 }
5275 FALLTHROUGH_INTENDED;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005276 default: {
5277 LoadMethod(invoke->GetMethodLoadKind(), callee_method, invoke);
5278 }
Vladimir Marko58155012015-08-19 12:49:41 +00005279 }
5280
5281 switch (invoke->GetCodePtrLocation()) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005282 case CodePtrLocation::kCallSelf:
Vladimir Marko58155012015-08-19 12:49:41 +00005283 __ call(GetFrameEntryLabel());
Vladimir Marko86c87522020-05-11 16:55:55 +01005284 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko58155012015-08-19 12:49:41 +00005285 break;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005286 case CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01005287 size_t out_frame_size =
5288 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorX86,
5289 kNativeStackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01005290 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005291 if (invoke->GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative) {
5292 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
5293 Register base_reg = GetInvokeExtraParameter(invoke, temp.AsRegister<Register>());
5294 __ call(Address(base_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
5295 RecordBootImageJniEntrypointPatch(invoke);
5296 } else {
5297 // (callee_method + offset_of_jni_entry_point)()
5298 __ call(Address(callee_method.AsRegister<Register>(),
5299 ArtMethod::EntryPointFromJniOffset(kX86PointerSize).Int32Value()));
5300 }
Vladimir Marko86c87522020-05-11 16:55:55 +01005301 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5302 if (out_frame_size == 0u && DataType::IsFloatingPointType(invoke->GetType())) {
5303 // Create space for conversion.
5304 out_frame_size = 8u;
Vladimir Markodec78172020-06-19 15:31:23 +01005305 IncreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01005306 }
5307 // Zero-/sign-extend or move the result when needed due to native and managed ABI mismatch.
5308 switch (invoke->GetType()) {
5309 case DataType::Type::kBool:
5310 __ movzxb(EAX, AL);
5311 break;
5312 case DataType::Type::kInt8:
5313 __ movsxb(EAX, AL);
5314 break;
5315 case DataType::Type::kUint16:
5316 __ movzxw(EAX, EAX);
5317 break;
5318 case DataType::Type::kInt16:
5319 __ movsxw(EAX, EAX);
5320 break;
5321 case DataType::Type::kFloat32:
5322 __ fstps(Address(ESP, 0));
5323 __ movss(XMM0, Address(ESP, 0));
5324 break;
5325 case DataType::Type::kFloat64:
5326 __ fstpl(Address(ESP, 0));
5327 __ movsd(XMM0, Address(ESP, 0));
5328 break;
5329 case DataType::Type::kInt32:
5330 case DataType::Type::kInt64:
5331 case DataType::Type::kVoid:
5332 break;
5333 default:
5334 DCHECK(false) << invoke->GetType();
5335 break;
5336 }
5337 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01005338 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01005339 }
5340 break;
5341 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005342 case CodePtrLocation::kCallArtMethod:
Vladimir Marko58155012015-08-19 12:49:41 +00005343 // (callee_method + offset_of_quick_compiled_code)()
5344 __ call(Address(callee_method.AsRegister<Register>(),
5345 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005346 kX86PointerSize).Int32Value()));
Vladimir Marko86c87522020-05-11 16:55:55 +01005347 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko58155012015-08-19 12:49:41 +00005348 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04005349 }
5350
5351 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04005352}
5353
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005354void CodeGeneratorX86::GenerateVirtualCall(
5355 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005356 Register temp = temp_in.AsRegister<Register>();
5357 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5358 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005359
5360 // Use the calling convention instead of the location of the receiver, as
5361 // intrinsics may have put the receiver in a different register. In the intrinsics
5362 // slow path, the arguments have been moved to the right place, so here we are
5363 // guaranteed that the receiver is the first register of the calling convention.
5364 InvokeDexCallingConvention calling_convention;
5365 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005366 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005367 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005368 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005369 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005370 // Instead of simply (possibly) unpoisoning `temp` here, we should
5371 // emit a read barrier for the previous class reference load.
5372 // However this is not required in practice, as this is an
5373 // intermediate/temporary reference and because the current
5374 // concurrent copying collector keeps the from-space memory
5375 // intact/accessible until the end of the marking phase (the
5376 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005377 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00005378
5379 MaybeGenerateInlineCacheCheck(invoke, temp);
5380
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005381 // temp = temp->GetMethodAt(method_offset);
5382 __ movl(temp, Address(temp, method_offset));
5383 // call temp->GetEntryPoint();
5384 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07005385 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005386 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005387}
5388
Vladimir Marko6fd16062018-06-26 11:02:04 +01005389void CodeGeneratorX86::RecordBootImageIntrinsicPatch(HX86ComputeBaseMethodAddress* method_address,
5390 uint32_t intrinsic_data) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005391 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005392 method_address, /* target_dex_file= */ nullptr, intrinsic_data);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005393 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005394}
5395
Vladimir Markob066d432018-01-03 13:14:37 +00005396void CodeGeneratorX86::RecordBootImageRelRoPatch(HX86ComputeBaseMethodAddress* method_address,
5397 uint32_t boot_image_offset) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005398 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005399 method_address, /* target_dex_file= */ nullptr, boot_image_offset);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005400 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Markob066d432018-01-03 13:14:37 +00005401}
5402
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005403void CodeGeneratorX86::RecordBootImageMethodPatch(HInvoke* invoke) {
5404 size_t index = invoke->IsInvokeInterface()
5405 ? invoke->AsInvokeInterface()->GetSpecialInputIndex()
5406 : invoke->AsInvokeStaticOrDirect()->GetSpecialInputIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005407 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005408 invoke->InputAt(index)->AsX86ComputeBaseMethodAddress();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005409 boot_image_method_patches_.emplace_back(
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005410 method_address,
5411 invoke->GetResolvedMethodReference().dex_file,
5412 invoke->GetResolvedMethodReference().index);
Vladimir Marko65979462017-05-19 17:25:12 +01005413 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005414}
5415
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005416void CodeGeneratorX86::RecordMethodBssEntryPatch(HInvoke* invoke) {
5417 size_t index = invoke->IsInvokeInterface()
5418 ? invoke->AsInvokeInterface()->GetSpecialInputIndex()
5419 : invoke->AsInvokeStaticOrDirect()->GetSpecialInputIndex();
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005420 DCHECK(IsSameDexFile(GetGraph()->GetDexFile(), *invoke->GetMethodReference().dex_file));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005421 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005422 invoke->InputAt(index)->AsX86ComputeBaseMethodAddress();
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005423 // Add the patch entry and bind its label at the end of the instruction.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005424 method_bss_entry_patches_.emplace_back(
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005425 method_address,
5426 invoke->GetMethodReference().dex_file,
5427 invoke->GetMethodReference().index);
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005428 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005429}
5430
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005431void CodeGeneratorX86::RecordBootImageTypePatch(HLoadClass* load_class) {
5432 HX86ComputeBaseMethodAddress* method_address =
5433 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5434 boot_image_type_patches_.emplace_back(
5435 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005436 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005437}
5438
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005439Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005440 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005441 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko8f63f102020-09-28 12:10:28 +01005442 ArenaDeque<X86PcRelativePatchInfo>* patches = nullptr;
5443 switch (load_class->GetLoadKind()) {
5444 case HLoadClass::LoadKind::kBssEntry:
5445 patches = &type_bss_entry_patches_;
5446 break;
5447 case HLoadClass::LoadKind::kBssEntryPublic:
5448 patches = &public_type_bss_entry_patches_;
5449 break;
5450 case HLoadClass::LoadKind::kBssEntryPackage:
5451 patches = &package_type_bss_entry_patches_;
5452 break;
5453 default:
5454 LOG(FATAL) << "Unexpected load kind: " << load_class->GetLoadKind();
5455 UNREACHABLE();
5456 }
5457 patches->emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005458 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005459 return &patches->back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005460}
5461
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005462void CodeGeneratorX86::RecordBootImageStringPatch(HLoadString* load_string) {
5463 HX86ComputeBaseMethodAddress* method_address =
5464 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
5465 boot_image_string_patches_.emplace_back(
5466 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
5467 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01005468}
5469
Vladimir Markoaad75c62016-10-03 08:46:48 +00005470Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005471 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005472 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005473 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005474 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005475 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005476}
5477
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005478void CodeGeneratorX86::RecordBootImageJniEntrypointPatch(HInvokeStaticOrDirect* invoke) {
5479 HX86ComputeBaseMethodAddress* method_address =
5480 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5481 boot_image_jni_entrypoint_patches_.emplace_back(
5482 method_address,
5483 invoke->GetResolvedMethodReference().dex_file,
5484 invoke->GetResolvedMethodReference().index);
5485 __ Bind(&boot_image_jni_entrypoint_patches_.back().label);
5486}
5487
Vladimir Markoeebb8212018-06-05 14:57:24 +01005488void CodeGeneratorX86::LoadBootImageAddress(Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01005489 uint32_t boot_image_reference,
Vladimir Markoeebb8212018-06-05 14:57:24 +01005490 HInvokeStaticOrDirect* invoke) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005491 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005492 HX86ComputeBaseMethodAddress* method_address =
5493 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5494 DCHECK(method_address != nullptr);
5495 Register method_address_reg =
5496 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005497 __ leal(reg, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005498 RecordBootImageIntrinsicPatch(method_address, boot_image_reference);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01005499 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01005500 HX86ComputeBaseMethodAddress* method_address =
5501 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5502 DCHECK(method_address != nullptr);
5503 Register method_address_reg =
5504 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005505 __ movl(reg, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005506 RecordBootImageRelRoPatch(method_address, boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01005507 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01005508 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01005509 gc::Heap* heap = Runtime::Current()->GetHeap();
5510 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01005511 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01005512 __ movl(reg, Immediate(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address))));
5513 }
5514}
5515
Vladimir Markode91ca92020-10-27 13:41:40 +00005516void CodeGeneratorX86::LoadIntrinsicDeclaringClass(Register reg, HInvokeStaticOrDirect* invoke) {
5517 DCHECK_NE(invoke->GetIntrinsic(), Intrinsics::kNone);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005518 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005519 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
Vladimir Marko6fd16062018-06-26 11:02:04 +01005520 HX86ComputeBaseMethodAddress* method_address =
5521 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5522 DCHECK(method_address != nullptr);
5523 Register method_address_reg =
5524 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Markode91ca92020-10-27 13:41:40 +00005525 __ leal(reg, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005526 MethodReference target_method = invoke->GetResolvedMethodReference();
Vladimir Marko6fd16062018-06-26 11:02:04 +01005527 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
5528 boot_image_type_patches_.emplace_back(method_address, target_method.dex_file, type_idx.index_);
5529 __ Bind(&boot_image_type_patches_.back().label);
5530 } else {
Vladimir Markode91ca92020-10-27 13:41:40 +00005531 uint32_t boot_image_offset = GetBootImageOffsetOfIntrinsicDeclaringClass(invoke);
5532 LoadBootImageAddress(reg, boot_image_offset, invoke);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005533 }
Vladimir Marko6fd16062018-06-26 11:02:04 +01005534}
5535
Vladimir Markoaad75c62016-10-03 08:46:48 +00005536// The label points to the end of the "movl" or another instruction but the literal offset
5537// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
5538constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
5539
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005540template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005541inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005542 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005543 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005544 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005545 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005546 linker_patches->push_back(Factory(literal_offset,
5547 info.target_dex_file,
5548 GetMethodAddressOffset(info.method_address),
5549 info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005550 }
5551}
5552
Vladimir Marko6fd16062018-06-26 11:02:04 +01005553template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
5554linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
5555 const DexFile* target_dex_file,
5556 uint32_t pc_insn_offset,
5557 uint32_t boot_image_offset) {
5558 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5559 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005560}
5561
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005562void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005563 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005564 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01005565 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005566 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00005567 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01005568 type_bss_entry_patches_.size() +
Vladimir Marko8f63f102020-09-28 12:10:28 +01005569 public_type_bss_entry_patches_.size() +
5570 package_type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005571 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01005572 string_bss_entry_patches_.size() +
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005573 boot_image_jni_entrypoint_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01005574 boot_image_other_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005575 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01005576 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005577 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
5578 boot_image_method_patches_, linker_patches);
5579 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
5580 boot_image_type_patches_, linker_patches);
5581 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005582 boot_image_string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005583 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005584 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005585 DCHECK(boot_image_type_patches_.empty());
5586 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01005587 }
5588 if (GetCompilerOptions().IsBootImage()) {
5589 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5590 boot_image_other_patches_, linker_patches);
5591 } else {
5592 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
5593 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005594 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005595 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5596 method_bss_entry_patches_, linker_patches);
5597 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5598 type_bss_entry_patches_, linker_patches);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005599 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PublicTypeBssEntryPatch>(
5600 public_type_bss_entry_patches_, linker_patches);
5601 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PackageTypeBssEntryPatch>(
5602 package_type_bss_entry_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005603 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5604 string_bss_entry_patches_, linker_patches);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005605 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeJniEntrypointPatch>(
5606 boot_image_jni_entrypoint_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005607 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005608}
5609
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005610void CodeGeneratorX86::MarkGCCard(Register temp,
5611 Register card,
5612 Register object,
5613 Register value,
5614 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005615 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005616 if (value_can_be_null) {
5617 __ testl(value, value);
5618 __ j(kEqual, &is_null);
5619 }
Roland Levillainc73f0522018-08-14 15:16:50 +01005620 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07005621 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01005622 // Calculate the offset (in the card table) of the card corresponding to
5623 // `object`.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005624 __ movl(temp, object);
5625 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01005626 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
5627 // `object`'s card.
5628 //
5629 // Register `card` contains the address of the card table. Note that the card
5630 // table's base is biased during its creation so that it always starts at an
5631 // address whose least-significant byte is equal to `kCardDirty` (see
5632 // art::gc::accounting::CardTable::Create). Therefore the MOVB instruction
5633 // below writes the `kCardDirty` (byte) value into the `object`'s card
5634 // (located at `card + object >> kCardShift`).
5635 //
5636 // This dual use of the value in register `card` (1. to calculate the location
5637 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
5638 // (no need to explicitly load `kCardDirty` as an immediate value).
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00005639 __ movb(Address(temp, card, TIMES_1, 0),
5640 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005641 if (value_can_be_null) {
5642 __ Bind(&is_null);
5643 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005644}
5645
Calin Juravle52c48962014-12-16 17:02:57 +00005646void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Alex Light3a73ffb2021-01-25 14:11:05 +00005647 DCHECK(instruction->IsInstanceFieldGet() ||
5648 instruction->IsStaticFieldGet() ||
5649 instruction->IsPredicatedInstanceFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005650
5651 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005652 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alex Light3a73ffb2021-01-25 14:11:05 +00005653 bool is_predicated = instruction->IsPredicatedInstanceFieldGet();
Nicolas Geoffray39468442014-09-02 15:17:15 +01005654 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005655 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5656 kEmitCompilerReadBarrier
5657 ? LocationSummary::kCallOnSlowPath
5658 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005659 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005660 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005661 }
Alex Light3a73ffb2021-01-25 14:11:05 +00005662 // receiver_input
5663 locations->SetInAt(is_predicated ? 1 : 0, Location::RequiresRegister());
5664 if (is_predicated) {
5665 if (DataType::IsFloatingPointType(instruction->GetType())) {
5666 locations->SetInAt(0, Location::RequiresFpuRegister());
5667 } else {
5668 locations->SetInAt(0, Location::RequiresRegister());
5669 }
5670 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005671 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alex Light3a73ffb2021-01-25 14:11:05 +00005672 locations->SetOut(is_predicated ? Location::SameAsFirstInput()
5673 : Location::RequiresFpuRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005674 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005675 // The output overlaps in case of long: we don't want the low move
5676 // to overwrite the object's location. Likewise, in the case of
5677 // an object field get with read barriers enabled, we do not want
5678 // the move to overwrite the object's location, as we need it to emit
5679 // the read barrier.
Alex Light3a73ffb2021-01-25 14:11:05 +00005680 locations->SetOut(is_predicated ? Location::SameAsFirstInput() : Location::RequiresRegister(),
5681 (object_field_get_with_read_barrier ||
5682 instruction->GetType() == DataType::Type::kInt64 ||
5683 is_predicated)
5684 ? Location::kOutputOverlap
5685 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005686 }
Calin Juravle52c48962014-12-16 17:02:57 +00005687
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005688 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00005689 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005690 // So we use an XMM register as a temp to achieve atomicity (first
5691 // load the temp into the XMM and then copy the XMM into the
5692 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00005693 locations->AddTemp(Location::RequiresFpuRegister());
5694 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005695}
5696
Calin Juravle52c48962014-12-16 17:02:57 +00005697void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
5698 const FieldInfo& field_info) {
Alex Light3a73ffb2021-01-25 14:11:05 +00005699 DCHECK(instruction->IsInstanceFieldGet() ||
5700 instruction->IsStaticFieldGet() ||
5701 instruction->IsPredicatedInstanceFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005702
Calin Juravle52c48962014-12-16 17:02:57 +00005703 LocationSummary* locations = instruction->GetLocations();
Alex Light3a73ffb2021-01-25 14:11:05 +00005704 Location base_loc = locations->InAt(instruction->IsPredicatedInstanceFieldGet() ? 1 : 0);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005705 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005706 Location out = locations->Out();
5707 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01005708 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5709 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00005710 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5711
Vladimir Marko61b92282017-10-11 13:23:17 +01005712 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005713 case DataType::Type::kBool:
5714 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005715 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005716 break;
5717 }
5718
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005719 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005720 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005721 break;
5722 }
5723
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005724 case DataType::Type::kUint16: {
5725 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005726 break;
5727 }
5728
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005729 case DataType::Type::kInt16: {
5730 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005731 break;
5732 }
5733
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005734 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00005735 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005736 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005737
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005738 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005739 // /* HeapReference<Object> */ out = *(base + offset)
5740 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005741 // Note that a potential implicit null check is handled in this
5742 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
5743 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005744 instruction, out, base, offset, /* needs_null_check= */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005745 if (is_volatile) {
5746 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5747 }
5748 } else {
5749 __ movl(out.AsRegister<Register>(), Address(base, offset));
5750 codegen_->MaybeRecordImplicitNullCheck(instruction);
5751 if (is_volatile) {
5752 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5753 }
5754 // If read barriers are enabled, emit read barriers other than
5755 // Baker's using a slow path (and also unpoison the loaded
5756 // reference, if heap poisoning is enabled).
5757 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5758 }
5759 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005760 }
5761
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005762 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005763 if (is_volatile) {
5764 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5765 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005766 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005767 __ movd(out.AsRegisterPairLow<Register>(), temp);
5768 __ psrlq(temp, Immediate(32));
5769 __ movd(out.AsRegisterPairHigh<Register>(), temp);
5770 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005771 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00005772 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005773 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005774 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
5775 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005776 break;
5777 }
5778
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005779 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00005780 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005781 break;
5782 }
5783
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005784 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005785 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005786 break;
5787 }
5788
Aart Bik66c158e2018-01-31 12:55:04 -08005789 case DataType::Type::kUint32:
5790 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005791 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005792 LOG(FATAL) << "Unreachable type " << load_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005793 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005794 }
Calin Juravle52c48962014-12-16 17:02:57 +00005795
Vladimir Marko61b92282017-10-11 13:23:17 +01005796 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005797 // Potential implicit null checks, in the case of reference or
5798 // long fields, are handled in the previous switch statement.
5799 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005800 codegen_->MaybeRecordImplicitNullCheck(instruction);
5801 }
5802
Calin Juravle52c48962014-12-16 17:02:57 +00005803 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005804 if (load_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005805 // Memory barriers, in the case of references, are also handled
5806 // in the previous switch statement.
5807 } else {
5808 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5809 }
Roland Levillain4d027112015-07-01 15:41:14 +01005810 }
Calin Juravle52c48962014-12-16 17:02:57 +00005811}
5812
5813void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5814 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5815
5816 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005817 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00005818 locations->SetInAt(0, Location::RequiresRegister());
5819 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005820 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005821 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00005822
5823 // The register allocator does not support multiple
5824 // inputs that die at entry with one in a specific register.
5825 if (is_byte_type) {
5826 // Ensure the value is in a byte register.
5827 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005828 } else if (DataType::IsFloatingPointType(field_type)) {
5829 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05005830 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5831 locations->SetInAt(1, Location::RequiresFpuRegister());
5832 } else {
5833 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
5834 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005835 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05005836 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00005837 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005838
Calin Juravle52c48962014-12-16 17:02:57 +00005839 // 64bits value can be atomically written to an address with movsd and an XMM register.
5840 // We need two XMM registers because there's no easier way to (bit) copy a register pair
5841 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
5842 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
5843 // isolated cases when we need this it isn't worth adding the extra complexity.
5844 locations->AddTemp(Location::RequiresFpuRegister());
5845 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005846 } else {
5847 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5848
5849 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5850 // Temporary registers for the write barrier.
5851 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
5852 // Ensure the card is in a byte register.
5853 locations->AddTemp(Location::RegisterLocation(ECX));
5854 }
Calin Juravle52c48962014-12-16 17:02:57 +00005855 }
5856}
5857
5858void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Andra Danciucde98192020-09-13 12:32:09 +00005859 uint32_t value_index,
5860 DataType::Type field_type,
5861 Address field_addr,
5862 Register base,
5863 bool is_volatile,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005864 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005865 LocationSummary* locations = instruction->GetLocations();
Andra Danciucde98192020-09-13 12:32:09 +00005866 Location value = locations->InAt(value_index);
Roland Levillain4d027112015-07-01 15:41:14 +01005867 bool needs_write_barrier =
Andra Danciucde98192020-09-13 12:32:09 +00005868 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(value_index));
Calin Juravle52c48962014-12-16 17:02:57 +00005869
5870 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005871 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005872 }
5873
Mark Mendell81489372015-11-04 11:30:41 -05005874 bool maybe_record_implicit_null_check_done = false;
5875
Calin Juravle52c48962014-12-16 17:02:57 +00005876 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005877 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005878 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005879 case DataType::Type::kInt8: {
Andra Danciucde98192020-09-13 12:32:09 +00005880 if (value.IsConstant()) {
5881 __ movb(field_addr, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
5882 } else {
5883 __ movb(field_addr, value.AsRegister<ByteRegister>());
5884 }
Calin Juravle52c48962014-12-16 17:02:57 +00005885 break;
5886 }
5887
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005888 case DataType::Type::kUint16:
5889 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05005890 if (value.IsConstant()) {
Andra Danciucde98192020-09-13 12:32:09 +00005891 __ movw(field_addr, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05005892 } else {
Andra Danciucde98192020-09-13 12:32:09 +00005893 __ movw(field_addr, value.AsRegister<Register>());
Mark Mendell81489372015-11-04 11:30:41 -05005894 }
Calin Juravle52c48962014-12-16 17:02:57 +00005895 break;
5896 }
5897
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005898 case DataType::Type::kInt32:
5899 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01005900 if (kPoisonHeapReferences && needs_write_barrier) {
5901 // Note that in the case where `value` is a null reference,
5902 // we do not enter this block, as the reference does not
5903 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005904 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01005905 Register temp = locations->GetTemp(0).AsRegister<Register>();
5906 __ movl(temp, value.AsRegister<Register>());
5907 __ PoisonHeapReference(temp);
Andra Danciucde98192020-09-13 12:32:09 +00005908 __ movl(field_addr, temp);
Mark Mendell81489372015-11-04 11:30:41 -05005909 } else if (value.IsConstant()) {
5910 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Andra Danciucde98192020-09-13 12:32:09 +00005911 __ movl(field_addr, Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005912 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005913 DCHECK(value.IsRegister()) << value;
Andra Danciucde98192020-09-13 12:32:09 +00005914 __ movl(field_addr, value.AsRegister<Register>());
Roland Levillain4d027112015-07-01 15:41:14 +01005915 }
Calin Juravle52c48962014-12-16 17:02:57 +00005916 break;
5917 }
5918
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005919 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005920 if (is_volatile) {
5921 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5922 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5923 __ movd(temp1, value.AsRegisterPairLow<Register>());
5924 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5925 __ punpckldq(temp1, temp2);
Andra Danciucde98192020-09-13 12:32:09 +00005926 __ movsd(field_addr, temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005927 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005928 } else if (value.IsConstant()) {
5929 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
Andra Danciucde98192020-09-13 12:32:09 +00005930 __ movl(field_addr, Immediate(Low32Bits(v)));
Mark Mendell81489372015-11-04 11:30:41 -05005931 codegen_->MaybeRecordImplicitNullCheck(instruction);
Andra Danciucde98192020-09-13 12:32:09 +00005932 __ movl(field_addr.displaceBy(kX86WordSize), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005933 } else {
Andra Danciucde98192020-09-13 12:32:09 +00005934 __ movl(field_addr, value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005935 codegen_->MaybeRecordImplicitNullCheck(instruction);
Andra Danciucde98192020-09-13 12:32:09 +00005936 __ movl(field_addr.displaceBy(kX86WordSize), value.AsRegisterPairHigh<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00005937 }
Mark Mendell81489372015-11-04 11:30:41 -05005938 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005939 break;
5940 }
5941
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005942 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05005943 if (value.IsConstant()) {
5944 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Andra Danciucde98192020-09-13 12:32:09 +00005945 __ movl(field_addr, Immediate(v));
Mark Mendell81489372015-11-04 11:30:41 -05005946 } else {
Andra Danciucde98192020-09-13 12:32:09 +00005947 __ movss(field_addr, value.AsFpuRegister<XmmRegister>());
Mark Mendell81489372015-11-04 11:30:41 -05005948 }
Calin Juravle52c48962014-12-16 17:02:57 +00005949 break;
5950 }
5951
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005952 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005953 if (value.IsConstant()) {
Andra Danciuc992e422020-09-16 08:12:02 +00005954 DCHECK(!is_volatile);
Mark Mendell81489372015-11-04 11:30:41 -05005955 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
Andra Danciucde98192020-09-13 12:32:09 +00005956 __ movl(field_addr, Immediate(Low32Bits(v)));
Mark Mendell81489372015-11-04 11:30:41 -05005957 codegen_->MaybeRecordImplicitNullCheck(instruction);
Andra Danciucde98192020-09-13 12:32:09 +00005958 __ movl(field_addr.displaceBy(kX86WordSize), Immediate(High32Bits(v)));
Mark Mendell81489372015-11-04 11:30:41 -05005959 maybe_record_implicit_null_check_done = true;
5960 } else {
Andra Danciucde98192020-09-13 12:32:09 +00005961 __ movsd(field_addr, value.AsFpuRegister<XmmRegister>());
Mark Mendell81489372015-11-04 11:30:41 -05005962 }
Calin Juravle52c48962014-12-16 17:02:57 +00005963 break;
5964 }
5965
Aart Bik66c158e2018-01-31 12:55:04 -08005966 case DataType::Type::kUint32:
5967 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005968 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005969 LOG(FATAL) << "Unreachable type " << field_type;
5970 UNREACHABLE();
5971 }
5972
Mark Mendell81489372015-11-04 11:30:41 -05005973 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005974 codegen_->MaybeRecordImplicitNullCheck(instruction);
5975 }
5976
Roland Levillain4d027112015-07-01 15:41:14 +01005977 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005978 Register temp = locations->GetTemp(0).AsRegister<Register>();
5979 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005980 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005981 }
5982
Calin Juravle52c48962014-12-16 17:02:57 +00005983 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005984 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005985 }
5986}
5987
Andra Danciucde98192020-09-13 12:32:09 +00005988void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
5989 const FieldInfo& field_info,
5990 bool value_can_be_null) {
5991 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5992
5993 LocationSummary* locations = instruction->GetLocations();
5994 Register base = locations->InAt(0).AsRegister<Register>();
5995 bool is_volatile = field_info.IsVolatile();
5996 DataType::Type field_type = field_info.GetFieldType();
5997 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alex Light3a73ffb2021-01-25 14:11:05 +00005998 bool is_predicated =
5999 instruction->IsInstanceFieldSet() && instruction->AsInstanceFieldSet()->GetIsPredicatedSet();
Andra Danciucde98192020-09-13 12:32:09 +00006000
6001 Address field_addr(base, offset);
6002
Alex Light3a73ffb2021-01-25 14:11:05 +00006003 NearLabel pred_is_null;
6004 if (is_predicated) {
6005 __ testl(base, base);
6006 __ j(kEqual, &pred_is_null);
6007 }
6008
Andra Danciucde98192020-09-13 12:32:09 +00006009 HandleFieldSet(instruction,
6010 /* value_index= */ 1,
6011 field_type,
6012 field_addr,
6013 base,
6014 is_volatile,
6015 value_can_be_null);
Alex Light3a73ffb2021-01-25 14:11:05 +00006016
6017 if (is_predicated) {
6018 __ Bind(&pred_is_null);
6019 }
Andra Danciucde98192020-09-13 12:32:09 +00006020}
6021
Calin Juravle52c48962014-12-16 17:02:57 +00006022void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6023 HandleFieldGet(instruction, instruction->GetFieldInfo());
6024}
6025
6026void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
6027 HandleFieldGet(instruction, instruction->GetFieldInfo());
6028}
6029
6030void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6031 HandleFieldSet(instruction, instruction->GetFieldInfo());
6032}
6033
6034void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006035 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00006036}
6037
6038void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6039 HandleFieldSet(instruction, instruction->GetFieldInfo());
6040}
6041
6042void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006043 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00006044}
6045
Alex Light3a73ffb2021-01-25 14:11:05 +00006046void LocationsBuilderX86::VisitPredicatedInstanceFieldGet(
6047 HPredicatedInstanceFieldGet* instruction) {
6048 HandleFieldGet(instruction, instruction->GetFieldInfo());
6049}
6050
Calin Juravle52c48962014-12-16 17:02:57 +00006051void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6052 HandleFieldGet(instruction, instruction->GetFieldInfo());
6053}
6054
Alex Light3a73ffb2021-01-25 14:11:05 +00006055void InstructionCodeGeneratorX86::VisitPredicatedInstanceFieldGet(
6056 HPredicatedInstanceFieldGet* instruction) {
6057 NearLabel finish;
6058 LocationSummary* locations = instruction->GetLocations();
6059 Register recv = locations->InAt(1).AsRegister<Register>();
6060 __ testl(recv, recv);
6061 __ j(kZero, &finish);
6062 HandleFieldGet(instruction, instruction->GetFieldInfo());
6063 __ Bind(&finish);
6064}
Calin Juravle52c48962014-12-16 17:02:57 +00006065void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6066 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006067}
6068
Vladimir Marko552a1342017-10-31 10:56:47 +00006069void LocationsBuilderX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6070 codegen_->CreateStringBuilderAppendLocations(instruction, Location::RegisterLocation(EAX));
6071}
6072
6073void InstructionCodeGeneratorX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6074 __ movl(EAX, Immediate(instruction->GetFormat()->GetValue()));
6075 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
6076}
6077
Calin Juravlee460d1d2015-09-29 04:52:17 +01006078void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
6079 HUnresolvedInstanceFieldGet* instruction) {
6080 FieldAccessCallingConventionX86 calling_convention;
6081 codegen_->CreateUnresolvedFieldLocationSummary(
6082 instruction, instruction->GetFieldType(), calling_convention);
6083}
6084
6085void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
6086 HUnresolvedInstanceFieldGet* instruction) {
6087 FieldAccessCallingConventionX86 calling_convention;
6088 codegen_->GenerateUnresolvedFieldAccess(instruction,
6089 instruction->GetFieldType(),
6090 instruction->GetFieldIndex(),
6091 instruction->GetDexPc(),
6092 calling_convention);
6093}
6094
6095void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
6096 HUnresolvedInstanceFieldSet* instruction) {
6097 FieldAccessCallingConventionX86 calling_convention;
6098 codegen_->CreateUnresolvedFieldLocationSummary(
6099 instruction, instruction->GetFieldType(), calling_convention);
6100}
6101
6102void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
6103 HUnresolvedInstanceFieldSet* instruction) {
6104 FieldAccessCallingConventionX86 calling_convention;
6105 codegen_->GenerateUnresolvedFieldAccess(instruction,
6106 instruction->GetFieldType(),
6107 instruction->GetFieldIndex(),
6108 instruction->GetDexPc(),
6109 calling_convention);
6110}
6111
6112void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
6113 HUnresolvedStaticFieldGet* instruction) {
6114 FieldAccessCallingConventionX86 calling_convention;
6115 codegen_->CreateUnresolvedFieldLocationSummary(
6116 instruction, instruction->GetFieldType(), calling_convention);
6117}
6118
6119void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
6120 HUnresolvedStaticFieldGet* instruction) {
6121 FieldAccessCallingConventionX86 calling_convention;
6122 codegen_->GenerateUnresolvedFieldAccess(instruction,
6123 instruction->GetFieldType(),
6124 instruction->GetFieldIndex(),
6125 instruction->GetDexPc(),
6126 calling_convention);
6127}
6128
6129void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
6130 HUnresolvedStaticFieldSet* instruction) {
6131 FieldAccessCallingConventionX86 calling_convention;
6132 codegen_->CreateUnresolvedFieldLocationSummary(
6133 instruction, instruction->GetFieldType(), calling_convention);
6134}
6135
6136void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
6137 HUnresolvedStaticFieldSet* instruction) {
6138 FieldAccessCallingConventionX86 calling_convention;
6139 codegen_->GenerateUnresolvedFieldAccess(instruction,
6140 instruction->GetFieldType(),
6141 instruction->GetFieldIndex(),
6142 instruction->GetDexPc(),
6143 calling_convention);
6144}
6145
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006146void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006147 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6148 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
6149 ? Location::RequiresRegister()
6150 : Location::Any();
6151 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006152}
6153
Calin Juravle2ae48182016-03-16 14:05:09 +00006154void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
6155 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00006156 return;
6157 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006158 LocationSummary* locations = instruction->GetLocations();
6159 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00006160
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006161 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00006162 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006163}
6164
Calin Juravle2ae48182016-03-16 14:05:09 +00006165void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006166 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006167 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006168
6169 LocationSummary* locations = instruction->GetLocations();
6170 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006171
6172 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04006173 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006174 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006175 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006176 } else {
6177 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00006178 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006179 __ jmp(slow_path->GetEntryLabel());
6180 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006181 }
6182 __ j(kEqual, slow_path->GetEntryLabel());
6183}
6184
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006185void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006186 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006187}
6188
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006189void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006190 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006191 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006192 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006193 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
6194 object_array_get_with_read_barrier
6195 ? LocationSummary::kCallOnSlowPath
6196 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01006197 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006198 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006199 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006200 locations->SetInAt(0, Location::RequiresRegister());
6201 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006202 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01006203 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6204 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006205 // The output overlaps in case of long: we don't want the low move
6206 // to overwrite the array's location. Likewise, in the case of an
6207 // object array get with read barriers enabled, we do not want the
6208 // move to overwrite the array's location, as we need it to emit
6209 // the read barrier.
6210 locations->SetOut(
6211 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006212 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
6213 ? Location::kOutputOverlap
6214 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01006215 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006216}
6217
6218void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
6219 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006220 Location obj_loc = locations->InAt(0);
6221 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006222 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006223 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006224 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006225
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006226 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00006227 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006228 case DataType::Type::kBool:
6229 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006230 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006231 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006232 break;
6233 }
6234
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006235 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006236 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006237 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006238 break;
6239 }
6240
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006241 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006242 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07006243 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6244 // Branch cases into compressed and uncompressed for each index's type.
6245 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
6246 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00006247 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006248 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006249 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6250 "Expecting 0=compressed, 1=uncompressed");
6251 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07006252 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
6253 __ jmp(&done);
6254 __ Bind(&not_compressed);
6255 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
6256 __ Bind(&done);
6257 } else {
6258 // Common case for charAt of array of char or when string compression's
6259 // feature is turned off.
6260 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
6261 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006262 break;
6263 }
6264
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006265 case DataType::Type::kInt16: {
6266 Register out = out_loc.AsRegister<Register>();
6267 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
6268 break;
6269 }
6270
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006271 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006272 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006273 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006274 break;
6275 }
6276
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006277 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006278 static_assert(
6279 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6280 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00006281 // /* HeapReference<Object> */ out =
6282 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6283 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006284 // Note that a potential implicit null check is handled in this
6285 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
6286 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08006287 instruction, out_loc, obj, data_offset, index, /* needs_null_check= */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006288 } else {
6289 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006290 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
6291 codegen_->MaybeRecordImplicitNullCheck(instruction);
6292 // If read barriers are enabled, emit read barriers other than
6293 // Baker's using a slow path (and also unpoison the loaded
6294 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00006295 if (index.IsConstant()) {
6296 uint32_t offset =
6297 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006298 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6299 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006300 codegen_->MaybeGenerateReadBarrierSlow(
6301 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6302 }
6303 }
6304 break;
6305 }
6306
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006307 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006308 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006309 __ movl(out_loc.AsRegisterPairLow<Register>(),
6310 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
6311 codegen_->MaybeRecordImplicitNullCheck(instruction);
6312 __ movl(out_loc.AsRegisterPairHigh<Register>(),
6313 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006314 break;
6315 }
6316
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006317 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006318 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006319 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006320 break;
6321 }
6322
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006323 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006324 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006325 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006326 break;
6327 }
6328
Aart Bik66c158e2018-01-31 12:55:04 -08006329 case DataType::Type::kUint32:
6330 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006331 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00006332 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07006333 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006334 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006335
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006336 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006337 // Potential implicit null checks, in the case of reference or
6338 // long arrays, are handled in the previous switch statement.
6339 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00006340 codegen_->MaybeRecordImplicitNullCheck(instruction);
6341 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006342}
6343
6344void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006345 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006346
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006347 bool needs_write_barrier =
6348 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006349 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006350
Vladimir Markoca6fff82017-10-03 14:49:14 +01006351 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01006352 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006353 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006354
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006355 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006356 // We need the inputs to be different than the output in case of long operation.
6357 // In case of a byte operation, the register allocator does not support multiple
6358 // inputs that die at entry with one in a specific register.
6359 locations->SetInAt(0, Location::RequiresRegister());
6360 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6361 if (is_byte_type) {
6362 // Ensure the value is in a byte register.
6363 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006364 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05006365 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006366 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006367 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
6368 }
6369 if (needs_write_barrier) {
6370 // Temporary registers for the write barrier.
6371 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6372 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00006373 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006374 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006375}
6376
6377void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
6378 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006379 Location array_loc = locations->InAt(0);
6380 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006381 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006382 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006383 DataType::Type value_type = instruction->GetComponentType();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006384 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006385 bool needs_write_barrier =
6386 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006387
6388 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006389 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006390 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006391 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006392 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006393 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006394 if (value.IsRegister()) {
6395 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006396 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01006397 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006398 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006399 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006400 break;
6401 }
6402
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006403 case DataType::Type::kUint16:
6404 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006405 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006406 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006407 if (value.IsRegister()) {
6408 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006409 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01006410 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006411 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006412 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006413 break;
6414 }
6415
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006416 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006417 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006418 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006419
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006420 if (!value.IsRegister()) {
6421 // Just setting null.
6422 DCHECK(instruction->InputAt(2)->IsNullConstant());
6423 DCHECK(value.IsConstant()) << value;
6424 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00006425 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006426 DCHECK(!needs_write_barrier);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006427 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006428 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006429 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006430
6431 DCHECK(needs_write_barrier);
6432 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01006433 Location temp_loc = locations->GetTemp(0);
6434 Register temp = temp_loc.AsRegister<Register>();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006435
6436 bool can_value_be_null = instruction->GetValueCanBeNull();
6437 NearLabel do_store;
6438 if (can_value_be_null) {
6439 __ testl(register_value, register_value);
6440 __ j(kEqual, &do_store);
6441 }
6442
6443 SlowPathCode* slow_path = nullptr;
6444 if (needs_type_check) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006445 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006446 codegen_->AddSlowPath(slow_path);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006447
6448 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6449 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6450 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006451
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006452 // Note that when Baker read barriers are enabled, the type
6453 // checks are performed without read barriers. This is fine,
6454 // even in the case where a class object is in the from-space
6455 // after the flip, as a comparison involving such a type would
6456 // not produce a false positive; it may of course produce a
6457 // false negative, in which case we would take the ArraySet
6458 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01006459
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006460 // /* HeapReference<Class> */ temp = array->klass_
6461 __ movl(temp, Address(array, class_offset));
6462 codegen_->MaybeRecordImplicitNullCheck(instruction);
6463 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01006464
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006465 // /* HeapReference<Class> */ temp = temp->component_type_
6466 __ movl(temp, Address(temp, component_offset));
6467 // If heap poisoning is enabled, no need to unpoison `temp`
6468 // nor the object reference in `register_value->klass`, as
6469 // we are comparing two poisoned references.
6470 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01006471
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006472 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006473 NearLabel do_put;
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006474 __ j(kEqual, &do_put);
6475 // If heap poisoning is enabled, the `temp` reference has
6476 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006477 __ MaybeUnpoisonHeapReference(temp);
6478
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006479 // If heap poisoning is enabled, no need to unpoison the
6480 // heap reference loaded below, as it is only used for a
6481 // comparison with null.
6482 __ cmpl(Address(temp, super_offset), Immediate(0));
6483 __ j(kNotEqual, slow_path->GetEntryLabel());
6484 __ Bind(&do_put);
6485 } else {
6486 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006487 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006488 }
6489
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006490 Register card = locations->GetTemp(1).AsRegister<Register>();
6491 codegen_->MarkGCCard(
6492 temp, card, array, value.AsRegister<Register>(), /* value_can_be_null= */ false);
6493
6494 if (can_value_be_null) {
6495 DCHECK(do_store.IsLinked());
6496 __ Bind(&do_store);
6497 }
6498
6499 Register source = register_value;
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006500 if (kPoisonHeapReferences) {
6501 __ movl(temp, register_value);
6502 __ PoisonHeapReference(temp);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006503 source = temp;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006504 }
6505
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006506 __ movl(address, source);
6507
6508 if (can_value_be_null || !needs_type_check) {
6509 codegen_->MaybeRecordImplicitNullCheck(instruction);
6510 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006511
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006512 if (slow_path != nullptr) {
6513 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006514 }
6515
6516 break;
6517 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006518
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006519 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006520 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006521 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006522 if (value.IsRegister()) {
6523 __ movl(address, value.AsRegister<Register>());
6524 } else {
6525 DCHECK(value.IsConstant()) << value;
6526 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
6527 __ movl(address, Immediate(v));
6528 }
6529 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006530 break;
6531 }
6532
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006533 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006534 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006535 if (value.IsRegisterPair()) {
6536 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6537 value.AsRegisterPairLow<Register>());
6538 codegen_->MaybeRecordImplicitNullCheck(instruction);
6539 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6540 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006541 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006542 DCHECK(value.IsConstant());
6543 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
6544 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6545 Immediate(Low32Bits(val)));
6546 codegen_->MaybeRecordImplicitNullCheck(instruction);
6547 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6548 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006549 }
6550 break;
6551 }
6552
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006553 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006554 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006555 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006556 if (value.IsFpuRegister()) {
6557 __ movss(address, value.AsFpuRegister<XmmRegister>());
6558 } else {
6559 DCHECK(value.IsConstant());
6560 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
6561 __ movl(address, Immediate(v));
6562 }
6563 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006564 break;
6565 }
6566
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006567 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006568 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006569 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006570 if (value.IsFpuRegister()) {
6571 __ movsd(address, value.AsFpuRegister<XmmRegister>());
6572 } else {
6573 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006574 Address address_hi =
6575 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05006576 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
6577 __ movl(address, Immediate(Low32Bits(v)));
6578 codegen_->MaybeRecordImplicitNullCheck(instruction);
6579 __ movl(address_hi, Immediate(High32Bits(v)));
6580 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006581 break;
6582 }
6583
Aart Bik66c158e2018-01-31 12:55:04 -08006584 case DataType::Type::kUint32:
6585 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006586 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006587 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07006588 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006589 }
6590}
6591
6592void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006593 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006594 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04006595 if (!instruction->IsEmittedAtUseSite()) {
6596 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6597 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006598}
6599
6600void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04006601 if (instruction->IsEmittedAtUseSite()) {
6602 return;
6603 }
6604
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006605 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006606 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006607 Register obj = locations->InAt(0).AsRegister<Register>();
6608 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006609 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00006610 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07006611 // Mask out most significant bit in case the array is String's array of char.
6612 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006613 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006614 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006615}
6616
6617void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006618 RegisterSet caller_saves = RegisterSet::Empty();
6619 InvokeRuntimeCallingConvention calling_convention;
6620 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6621 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6622 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05006623 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04006624 HInstruction* length = instruction->InputAt(1);
6625 if (!length->IsEmittedAtUseSite()) {
6626 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6627 }
jessicahandojo4877b792016-09-08 19:49:13 -07006628 // Need register to see array's length.
6629 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6630 locations->AddTemp(Location::RequiresRegister());
6631 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006632}
6633
6634void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07006635 const bool is_string_compressed_char_at =
6636 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006637 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05006638 Location index_loc = locations->InAt(0);
6639 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006640 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006641 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006642
Mark Mendell99dbd682015-04-22 16:18:52 -04006643 if (length_loc.IsConstant()) {
6644 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
6645 if (index_loc.IsConstant()) {
6646 // BCE will remove the bounds check if we are guarenteed to pass.
6647 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6648 if (index < 0 || index >= length) {
6649 codegen_->AddSlowPath(slow_path);
6650 __ jmp(slow_path->GetEntryLabel());
6651 } else {
6652 // Some optimization after BCE may have generated this, and we should not
6653 // generate a bounds check if it is a valid range.
6654 }
6655 return;
6656 }
6657
6658 // We have to reverse the jump condition because the length is the constant.
6659 Register index_reg = index_loc.AsRegister<Register>();
6660 __ cmpl(index_reg, Immediate(length));
6661 codegen_->AddSlowPath(slow_path);
6662 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006663 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04006664 HInstruction* array_length = instruction->InputAt(1);
6665 if (array_length->IsEmittedAtUseSite()) {
6666 // Address the length field in the array.
6667 DCHECK(array_length->IsArrayLength());
6668 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
6669 Location array_loc = array_length->GetLocations()->InAt(0);
6670 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07006671 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006672 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
6673 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07006674 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
6675 __ movl(length_reg, array_len);
6676 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006677 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006678 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04006679 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006680 // Checking bounds for general case:
6681 // Array of char or string's array with feature compression off.
6682 if (index_loc.IsConstant()) {
6683 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6684 __ cmpl(array_len, Immediate(value));
6685 } else {
6686 __ cmpl(array_len, index_loc.AsRegister<Register>());
6687 }
6688 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04006689 }
Mark Mendell99dbd682015-04-22 16:18:52 -04006690 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006691 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04006692 }
6693 codegen_->AddSlowPath(slow_path);
6694 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006695 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006696}
6697
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006698void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006699 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006700}
6701
6702void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006703 if (instruction->GetNext()->IsSuspendCheck() &&
6704 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6705 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6706 // The back edge will generate the suspend check.
6707 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6708 }
6709
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006710 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6711}
6712
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006713void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006714 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6715 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07006716 // In suspend check slow path, usually there are no caller-save registers at all.
6717 // If SIMD instructions are present, however, we force spilling all live SIMD
6718 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07006719 locations->SetCustomSlowPathCallerSaves(
6720 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006721}
6722
6723void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006724 HBasicBlock* block = instruction->GetBlock();
6725 if (block->GetLoopInformation() != nullptr) {
6726 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6727 // The back edge will generate the suspend check.
6728 return;
6729 }
6730 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6731 // The goto will generate the suspend check.
6732 return;
6733 }
6734 GenerateSuspendCheck(instruction, nullptr);
6735}
6736
6737void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
6738 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006739 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006740 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
6741 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006742 slow_path =
6743 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006744 instruction->SetSlowPath(slow_path);
6745 codegen_->AddSlowPath(slow_path);
6746 if (successor != nullptr) {
6747 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006748 }
6749 } else {
6750 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6751 }
6752
Andreas Gampe542451c2016-07-26 09:02:02 -07006753 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006754 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006755 if (successor == nullptr) {
6756 __ j(kNotEqual, slow_path->GetEntryLabel());
6757 __ Bind(slow_path->GetReturnLabel());
6758 } else {
6759 __ j(kEqual, codegen_->GetLabelOf(successor));
6760 __ jmp(slow_path->GetEntryLabel());
6761 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006762}
6763
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006764X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
6765 return codegen_->GetAssembler();
6766}
6767
Aart Bikcfe50bb2017-12-12 14:54:12 -08006768void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006769 ScratchRegisterScope ensure_scratch(
6770 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6771 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6772 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05006773
Aart Bikcfe50bb2017-12-12 14:54:12 -08006774 // Now that temp register is available (possibly spilled), move blocks of memory.
6775 for (int i = 0; i < number_of_words; i++) {
6776 __ movl(temp_reg, Address(ESP, src + stack_offset));
6777 __ movl(Address(ESP, dst + stack_offset), temp_reg);
6778 stack_offset += kX86WordSize;
6779 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006780}
6781
6782void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006783 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006784 Location source = move->GetSource();
6785 Location destination = move->GetDestination();
6786
6787 if (source.IsRegister()) {
6788 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006789 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006790 } else if (destination.IsFpuRegister()) {
6791 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006792 } else {
6793 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006794 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006795 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006796 } else if (source.IsRegisterPair()) {
Vladimir Marko86c87522020-05-11 16:55:55 +01006797 if (destination.IsRegisterPair()) {
6798 __ movl(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
6799 DCHECK_NE(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
6800 __ movl(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
6801 } else if (destination.IsFpuRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006802 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Vladimir Markodec78172020-06-19 15:31:23 +01006803 // Push the 2 source registers to the stack.
Vladimir Marko86c87522020-05-11 16:55:55 +01006804 __ pushl(source.AsRegisterPairHigh<Register>());
6805 __ cfi().AdjustCFAOffset(elem_size);
6806 __ pushl(source.AsRegisterPairLow<Register>());
6807 __ cfi().AdjustCFAOffset(elem_size);
6808 // Load the destination register.
David Brazdil74eb1b22015-12-14 11:44:01 +00006809 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
6810 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01006811 codegen_->DecreaseFrame(2 * elem_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01006812 } else {
6813 DCHECK(destination.IsDoubleStackSlot());
6814 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
6815 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
6816 source.AsRegisterPairHigh<Register>());
6817 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006818 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006819 if (destination.IsRegister()) {
6820 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
6821 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006822 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006823 } else if (destination.IsRegisterPair()) {
Vladimir Marko86c87522020-05-11 16:55:55 +01006824 size_t elem_size = DataType::Size(DataType::Type::kInt32);
6825 // Create stack space for 2 elements.
Vladimir Markodec78172020-06-19 15:31:23 +01006826 codegen_->IncreaseFrame(2 * elem_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01006827 // Store the source register.
6828 __ movsd(Address(ESP, 0), source.AsFpuRegister<XmmRegister>());
6829 // And pop the values into destination registers.
6830 __ popl(destination.AsRegisterPairLow<Register>());
6831 __ cfi().AdjustCFAOffset(-elem_size);
6832 __ popl(destination.AsRegisterPairHigh<Register>());
6833 __ cfi().AdjustCFAOffset(-elem_size);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006834 } else if (destination.IsStackSlot()) {
6835 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006836 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006837 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006838 } else {
6839 DCHECK(destination.IsSIMDStackSlot());
6840 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05006841 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006842 } else if (source.IsStackSlot()) {
6843 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006844 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006845 } else if (destination.IsFpuRegister()) {
6846 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006847 } else {
6848 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08006849 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006850 }
6851 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006852 if (destination.IsRegisterPair()) {
6853 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
6854 __ movl(destination.AsRegisterPairHigh<Register>(),
6855 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
6856 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006857 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6858 } else {
6859 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006860 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006861 }
Aart Bik5576f372017-03-23 16:17:37 -07006862 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006863 if (destination.IsFpuRegister()) {
6864 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6865 } else {
6866 DCHECK(destination.IsSIMDStackSlot());
6867 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6868 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006869 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006870 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006871 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006872 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006873 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006874 if (value == 0) {
6875 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
6876 } else {
6877 __ movl(destination.AsRegister<Register>(), Immediate(value));
6878 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006879 } else {
6880 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05006881 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006882 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006883 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006884 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006885 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006886 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006887 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006888 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6889 if (value == 0) {
6890 // Easy handling of 0.0.
6891 __ xorps(dest, dest);
6892 } else {
6893 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006894 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6895 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
6896 __ movl(temp, Immediate(value));
6897 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006898 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006899 } else {
6900 DCHECK(destination.IsStackSlot()) << destination;
6901 __ movl(Address(ESP, destination.GetStackIndex()), imm);
6902 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006903 } else if (constant->IsLongConstant()) {
6904 int64_t value = constant->AsLongConstant()->GetValue();
6905 int32_t low_value = Low32Bits(value);
6906 int32_t high_value = High32Bits(value);
6907 Immediate low(low_value);
6908 Immediate high(high_value);
6909 if (destination.IsDoubleStackSlot()) {
6910 __ movl(Address(ESP, destination.GetStackIndex()), low);
6911 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6912 } else {
6913 __ movl(destination.AsRegisterPairLow<Register>(), low);
6914 __ movl(destination.AsRegisterPairHigh<Register>(), high);
6915 }
6916 } else {
6917 DCHECK(constant->IsDoubleConstant());
6918 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006919 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006920 int32_t low_value = Low32Bits(value);
6921 int32_t high_value = High32Bits(value);
6922 Immediate low(low_value);
6923 Immediate high(high_value);
6924 if (destination.IsFpuRegister()) {
6925 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6926 if (value == 0) {
6927 // Easy handling of 0.0.
6928 __ xorpd(dest, dest);
6929 } else {
6930 __ pushl(high);
Vladimir Marko86c87522020-05-11 16:55:55 +01006931 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006932 __ pushl(low);
Vladimir Marko86c87522020-05-11 16:55:55 +01006933 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006934 __ movsd(dest, Address(ESP, 0));
Vladimir Markodec78172020-06-19 15:31:23 +01006935 codegen_->DecreaseFrame(8);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006936 }
6937 } else {
6938 DCHECK(destination.IsDoubleStackSlot()) << destination;
6939 __ movl(Address(ESP, destination.GetStackIndex()), low);
6940 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6941 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006942 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006943 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006944 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006945 }
6946}
6947
Mark Mendella5c19ce2015-04-01 12:51:05 -04006948void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006949 Register suggested_scratch = reg == EAX ? EBX : EAX;
6950 ScratchRegisterScope ensure_scratch(
6951 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
6952
6953 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6954 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
6955 __ movl(Address(ESP, mem + stack_offset), reg);
6956 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006957}
6958
Mark Mendell7c8d0092015-01-26 11:21:33 -05006959void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006960 ScratchRegisterScope ensure_scratch(
6961 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6962
6963 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6964 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6965 __ movl(temp_reg, Address(ESP, mem + stack_offset));
6966 __ movss(Address(ESP, mem + stack_offset), reg);
6967 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006968}
6969
Aart Bikcfe50bb2017-12-12 14:54:12 -08006970void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
6971 size_t extra_slot = 4 * kX86WordSize;
Vladimir Markodec78172020-06-19 15:31:23 +01006972 codegen_->IncreaseFrame(extra_slot);
Aart Bikcfe50bb2017-12-12 14:54:12 -08006973 __ movups(Address(ESP, 0), XmmRegister(reg));
6974 ExchangeMemory(0, mem + extra_slot, 4);
6975 __ movups(XmmRegister(reg), Address(ESP, 0));
Vladimir Markodec78172020-06-19 15:31:23 +01006976 codegen_->DecreaseFrame(extra_slot);
Aart Bikcfe50bb2017-12-12 14:54:12 -08006977}
6978
6979void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006980 ScratchRegisterScope ensure_scratch1(
6981 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006982
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006983 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
6984 ScratchRegisterScope ensure_scratch2(
6985 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006986
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006987 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
6988 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006989
6990 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
6991 for (int i = 0; i < number_of_words; i++) {
6992 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
6993 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
6994 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
6995 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
6996 stack_offset += kX86WordSize;
6997 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006998}
6999
7000void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007001 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01007002 Location source = move->GetSource();
7003 Location destination = move->GetDestination();
7004
7005 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04007006 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
7007 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
7008 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
7009 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
7010 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01007011 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007012 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01007013 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007014 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01007015 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08007016 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05007017 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
7018 // Use XOR Swap algorithm to avoid a temporary.
7019 DCHECK_NE(source.reg(), destination.reg());
7020 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
7021 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
7022 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
7023 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
7024 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
7025 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
7026 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007027 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
7028 // Take advantage of the 16 bytes in the XMM register.
7029 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
7030 Address stack(ESP, destination.GetStackIndex());
7031 // Load the double into the high doubleword.
7032 __ movhpd(reg, stack);
7033
7034 // Store the low double into the destination.
7035 __ movsd(stack, reg);
7036
7037 // Move the high double to the low double.
7038 __ psrldq(reg, Immediate(8));
7039 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
7040 // Take advantage of the 16 bytes in the XMM register.
7041 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
7042 Address stack(ESP, source.GetStackIndex());
7043 // Load the double into the high doubleword.
7044 __ movhpd(reg, stack);
7045
7046 // Store the low double into the destination.
7047 __ movsd(stack, reg);
7048
7049 // Move the high double to the low double.
7050 __ psrldq(reg, Immediate(8));
7051 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08007052 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
7053 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
7054 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
7055 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
7056 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
7057 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
7058 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01007059 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05007060 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01007061 }
7062}
7063
7064void ParallelMoveResolverX86::SpillScratch(int reg) {
7065 __ pushl(static_cast<Register>(reg));
7066}
7067
7068void ParallelMoveResolverX86::RestoreScratch(int reg) {
7069 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007070}
7071
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007072HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
7073 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007074 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007075 case HLoadClass::LoadKind::kInvalid:
7076 LOG(FATAL) << "UNREACHABLE";
7077 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007078 case HLoadClass::LoadKind::kReferrersClass:
7079 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007080 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007081 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007082 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko8f63f102020-09-28 12:10:28 +01007083 case HLoadClass::LoadKind::kBssEntryPublic:
7084 case HLoadClass::LoadKind::kBssEntryPackage:
Vladimir Marko695348f2020-05-19 14:42:02 +01007085 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007086 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007087 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007088 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01007089 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007090 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007091 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007092 break;
7093 }
7094 return desired_class_load_kind;
7095}
7096
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007097void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007098 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007099 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007100 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00007101 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007102 cls,
7103 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00007104 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00007105 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007106 return;
7107 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01007108 DCHECK_EQ(cls->NeedsAccessCheck(),
7109 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
7110 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007111
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007112 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7113 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007114 ? LocationSummary::kCallOnSlowPath
7115 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007116 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007117 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007118 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007119 }
7120
Vladimir Marko8f63f102020-09-28 12:10:28 +01007121 if (load_kind == HLoadClass::LoadKind::kReferrersClass || cls->HasPcRelativeLoadKind()) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007122 locations->SetInAt(0, Location::RequiresRegister());
7123 }
7124 locations->SetOut(Location::RequiresRegister());
Vladimir Marko8f63f102020-09-28 12:10:28 +01007125 if (call_kind == LocationSummary::kCallOnSlowPath && cls->HasPcRelativeLoadKind()) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007126 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7127 // Rely on the type resolution and/or initialization to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007128 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007129 } else {
7130 // For non-Baker read barrier we have a temp-clobbering call.
7131 }
7132 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007133}
7134
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007135Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01007136 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007137 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007138 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007139 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007140 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007141 PatchInfo<Label>* info = &jit_class_patches_.back();
7142 return &info->label;
7143}
7144
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007145// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7146// move.
7147void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007148 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007149 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007150 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01007151 return;
7152 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01007153 DCHECK_EQ(cls->NeedsAccessCheck(),
7154 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
7155 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Calin Juravle580b6092015-10-06 17:35:58 +01007156
Vladimir Marko41559982017-01-06 14:04:23 +00007157 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007158 Location out_loc = locations->Out();
7159 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007160
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007161 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007162 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7163 ? kWithoutReadBarrier
7164 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00007165 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007166 case HLoadClass::LoadKind::kReferrersClass: {
7167 DCHECK(!cls->CanCallRuntime());
7168 DCHECK(!cls->MustGenerateClinitCheck());
7169 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7170 Register current_method = locations->InAt(0).AsRegister<Register>();
7171 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007172 cls,
7173 out_loc,
7174 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Andreas Gampe3db70682018-12-26 15:12:03 -08007175 /* fixup_label= */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007176 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007177 break;
7178 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007179 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007180 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7181 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007182 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007183 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007184 __ leal(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007185 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007186 break;
7187 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007188 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007189 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7190 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007191 __ movl(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007192 codegen_->RecordBootImageRelRoPatch(cls->InputAt(0)->AsX86ComputeBaseMethodAddress(),
Vladimir Markode91ca92020-10-27 13:41:40 +00007193 CodeGenerator::GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007194 break;
7195 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01007196 case HLoadClass::LoadKind::kBssEntry:
7197 case HLoadClass::LoadKind::kBssEntryPublic:
7198 case HLoadClass::LoadKind::kBssEntryPackage: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007199 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007200 Address address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007201 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
7202 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007203 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007204 generate_null_check = true;
7205 break;
7206 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007207 case HLoadClass::LoadKind::kJitBootImageAddress: {
7208 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7209 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
7210 DCHECK_NE(address, 0u);
7211 __ movl(out, Immediate(address));
7212 break;
7213 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007214 case HLoadClass::LoadKind::kJitTableAddress: {
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007215 Address address = Address::Absolute(CodeGeneratorX86::kPlaceholder32BitOffset);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007216 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007217 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007218 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00007219 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007220 break;
7221 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007222 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007223 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007224 LOG(FATAL) << "UNREACHABLE";
7225 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007226 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007227
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007228 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7229 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007230 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007231 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007232
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007233 if (generate_null_check) {
7234 __ testl(out, out);
7235 __ j(kEqual, slow_path->GetEntryLabel());
7236 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007237
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007238 if (cls->MustGenerateClinitCheck()) {
7239 GenerateClassInitializationCheck(slow_path, out);
7240 } else {
7241 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007242 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007243 }
7244}
7245
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007246void LocationsBuilderX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7247 InvokeRuntimeCallingConvention calling_convention;
7248 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7249 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
7250}
7251
7252void InstructionCodeGeneratorX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7253 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
7254}
7255
Orion Hodson18259d72018-04-12 11:18:23 +01007256void LocationsBuilderX86::VisitLoadMethodType(HLoadMethodType* load) {
7257 InvokeRuntimeCallingConvention calling_convention;
7258 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7259 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
7260}
7261
7262void InstructionCodeGeneratorX86::VisitLoadMethodType(HLoadMethodType* load) {
7263 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
7264}
7265
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007266void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
7267 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007268 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007269 locations->SetInAt(0, Location::RequiresRegister());
7270 if (check->HasUses()) {
7271 locations->SetOut(Location::SameAsFirstInput());
7272 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007273 // Rely on the type initialization to save everything we need.
7274 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007275}
7276
7277void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007278 // We assume the class to not be null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007279 SlowPathCode* slow_path =
7280 new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(check->GetLoadClass(), check);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007281 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00007282 GenerateClassInitializationCheck(slow_path,
7283 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007284}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007285
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007286void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07007287 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00007288 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
7289 const size_t status_byte_offset =
7290 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
Vladimir Markobf121912019-06-04 13:49:05 +01007291 constexpr uint32_t shifted_visibly_initialized_value =
7292 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << (status_lsb_position % kBitsPerByte);
Vladimir Markodc682aa2018-01-04 18:42:57 +00007293
Vladimir Markobf121912019-06-04 13:49:05 +01007294 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_visibly_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00007295 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007296 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007297}
7298
Vladimir Marko175e7862018-03-27 09:03:13 +00007299void InstructionCodeGeneratorX86::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
7300 Register temp) {
7301 uint32_t path_to_root = check->GetBitstringPathToRoot();
7302 uint32_t mask = check->GetBitstringMask();
7303 DCHECK(IsPowerOfTwo(mask + 1));
7304 size_t mask_bits = WhichPowerOf2(mask + 1);
7305
7306 if (mask_bits == 16u) {
7307 // Compare the bitstring in memory.
7308 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
7309 } else {
7310 // /* uint32_t */ temp = temp->status_
7311 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
7312 // Compare the bitstring bits using SUB.
7313 __ subl(temp, Immediate(path_to_root));
7314 // Shift out bits that do not contribute to the comparison.
7315 __ shll(temp, Immediate(32u - mask_bits));
7316 }
7317}
7318
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007319HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
7320 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007321 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007322 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007323 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007324 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01007325 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007326 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007327 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007328 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01007329 DCHECK(GetCompilerOptions().IsJitCompiler());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007330 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007331 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007332 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007333 }
7334 return desired_string_load_kind;
7335}
7336
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007337void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007338 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007339 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007340 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007341 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007342 load_kind == HLoadString::LoadKind::kBootImageRelRo ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00007343 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007344 locations->SetInAt(0, Location::RequiresRegister());
7345 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007346 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007347 locations->SetOut(Location::RegisterLocation(EAX));
7348 } else {
7349 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007350 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7351 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007352 // Rely on the pResolveString to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007353 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007354 } else {
7355 // For non-Baker read barrier we have a temp-clobbering call.
7356 }
7357 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007358 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007359}
7360
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007361Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01007362 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007363 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007364 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007365 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007366 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007367 PatchInfo<Label>* info = &jit_string_patches_.back();
7368 return &info->label;
7369}
7370
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007371// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7372// move.
7373void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01007374 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007375 Location out_loc = locations->Out();
7376 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007377
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007378 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007379 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007380 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7381 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007382 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007383 __ leal(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007384 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007385 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007386 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007387 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007388 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7389 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007390 __ movl(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007391 codegen_->RecordBootImageRelRoPatch(load->InputAt(0)->AsX86ComputeBaseMethodAddress(),
Vladimir Markode91ca92020-10-27 13:41:40 +00007392 CodeGenerator::GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007393 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007394 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007395 case HLoadString::LoadKind::kBssEntry: {
7396 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007397 Address address = Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007398 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007399 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007400 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007401 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007402 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007403 codegen_->AddSlowPath(slow_path);
7404 __ testl(out, out);
7405 __ j(kEqual, slow_path->GetEntryLabel());
7406 __ Bind(slow_path->GetExitLabel());
7407 return;
7408 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007409 case HLoadString::LoadKind::kJitBootImageAddress: {
7410 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
7411 DCHECK_NE(address, 0u);
7412 __ movl(out, Immediate(address));
7413 return;
7414 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007415 case HLoadString::LoadKind::kJitTableAddress: {
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007416 Address address = Address::Absolute(CodeGeneratorX86::kPlaceholder32BitOffset);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007417 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007418 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007419 // /* GcRoot<mirror::String> */ out = *address
7420 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
7421 return;
7422 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007423 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007424 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007425 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007426
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007427 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007428 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007429 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007430 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007431 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7432 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007433}
7434
David Brazdilcb1c0552015-08-04 16:22:25 +01007435static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007436 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01007437}
7438
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007439void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
7440 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007441 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007442 locations->SetOut(Location::RequiresRegister());
7443}
7444
7445void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01007446 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
7447}
7448
7449void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007450 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01007451}
7452
7453void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7454 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007455}
7456
7457void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007458 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7459 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007460 InvokeRuntimeCallingConvention calling_convention;
7461 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7462}
7463
7464void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007465 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007466 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007467}
7468
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007469// Temp is used for read barrier.
7470static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7471 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00007472 !kUseBakerReadBarrier &&
7473 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00007474 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007475 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7476 return 1;
7477 }
7478 return 0;
7479}
7480
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007481// Interface case has 2 temps, one for holding the number of interfaces, one for the current
7482// interface pointer, the current interface is compared in memory.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007483// The other checks have one temp for loading the object's class.
7484static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007485 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007486 return 2;
7487 }
7488 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007489}
7490
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007491void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007492 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007493 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01007494 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007495 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007496 case TypeCheckKind::kExactCheck:
7497 case TypeCheckKind::kAbstractClassCheck:
7498 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007499 case TypeCheckKind::kArrayObjectCheck: {
7500 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7501 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7502 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007503 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007504 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007505 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007506 case TypeCheckKind::kUnresolvedCheck:
7507 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007508 call_kind = LocationSummary::kCallOnSlowPath;
7509 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007510 case TypeCheckKind::kBitstringCheck:
7511 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007512 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007513
Vladimir Markoca6fff82017-10-03 14:49:14 +01007514 LocationSummary* locations =
7515 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01007516 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007517 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007518 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007519 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007520 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7521 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7522 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7523 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7524 } else {
7525 locations->SetInAt(1, Location::Any());
7526 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007527 // Note that TypeCheckSlowPathX86 uses this "out" register too.
7528 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007529 // When read barriers are enabled, we need a temporary register for some cases.
7530 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007531}
7532
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007533void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007534 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007535 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007536 Location obj_loc = locations->InAt(0);
7537 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007538 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007539 Location out_loc = locations->Out();
7540 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007541 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7542 DCHECK_LE(num_temps, 1u);
7543 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007544 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007545 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7546 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7547 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07007548 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007549 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007550
7551 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007552 // Avoid null check if we know obj is not null.
7553 if (instruction->MustDoNullCheck()) {
7554 __ testl(obj, obj);
7555 __ j(kEqual, &zero);
7556 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007557
Roland Levillain7c1559a2015-12-15 10:55:36 +00007558 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007559 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007560 ReadBarrierOption read_barrier_option =
7561 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007562 // /* HeapReference<Class> */ out = obj->klass_
7563 GenerateReferenceLoadTwoRegisters(instruction,
7564 out_loc,
7565 obj_loc,
7566 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007567 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007568 if (cls.IsRegister()) {
7569 __ cmpl(out, cls.AsRegister<Register>());
7570 } else {
7571 DCHECK(cls.IsStackSlot()) << cls;
7572 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7573 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007574
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007575 // Classes must be equal for the instanceof to succeed.
7576 __ j(kNotEqual, &zero);
7577 __ movl(out, Immediate(1));
7578 __ jmp(&done);
7579 break;
7580 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007581
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007582 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007583 ReadBarrierOption read_barrier_option =
7584 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007585 // /* HeapReference<Class> */ out = obj->klass_
7586 GenerateReferenceLoadTwoRegisters(instruction,
7587 out_loc,
7588 obj_loc,
7589 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007590 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007591 // If the class is abstract, we eagerly fetch the super class of the
7592 // object to avoid doing a comparison we know will fail.
7593 NearLabel loop;
7594 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007595 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007596 GenerateReferenceLoadOneRegister(instruction,
7597 out_loc,
7598 super_offset,
7599 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007600 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007601 __ testl(out, out);
7602 // If `out` is null, we use it for the result, and jump to `done`.
7603 __ j(kEqual, &done);
7604 if (cls.IsRegister()) {
7605 __ cmpl(out, cls.AsRegister<Register>());
7606 } else {
7607 DCHECK(cls.IsStackSlot()) << cls;
7608 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7609 }
7610 __ j(kNotEqual, &loop);
7611 __ movl(out, Immediate(1));
7612 if (zero.IsLinked()) {
7613 __ jmp(&done);
7614 }
7615 break;
7616 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007617
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007618 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007619 ReadBarrierOption read_barrier_option =
7620 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007621 // /* HeapReference<Class> */ out = obj->klass_
7622 GenerateReferenceLoadTwoRegisters(instruction,
7623 out_loc,
7624 obj_loc,
7625 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007626 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007627 // Walk over the class hierarchy to find a match.
7628 NearLabel loop, success;
7629 __ Bind(&loop);
7630 if (cls.IsRegister()) {
7631 __ cmpl(out, cls.AsRegister<Register>());
7632 } else {
7633 DCHECK(cls.IsStackSlot()) << cls;
7634 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7635 }
7636 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007637 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007638 GenerateReferenceLoadOneRegister(instruction,
7639 out_loc,
7640 super_offset,
7641 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007642 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007643 __ testl(out, out);
7644 __ j(kNotEqual, &loop);
7645 // If `out` is null, we use it for the result, and jump to `done`.
7646 __ jmp(&done);
7647 __ Bind(&success);
7648 __ movl(out, Immediate(1));
7649 if (zero.IsLinked()) {
7650 __ jmp(&done);
7651 }
7652 break;
7653 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007654
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007655 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007656 ReadBarrierOption read_barrier_option =
7657 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007658 // /* HeapReference<Class> */ out = obj->klass_
7659 GenerateReferenceLoadTwoRegisters(instruction,
7660 out_loc,
7661 obj_loc,
7662 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007663 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007664 // Do an exact check.
7665 NearLabel exact_check;
7666 if (cls.IsRegister()) {
7667 __ cmpl(out, cls.AsRegister<Register>());
7668 } else {
7669 DCHECK(cls.IsStackSlot()) << cls;
7670 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7671 }
7672 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007673 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007674 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007675 GenerateReferenceLoadOneRegister(instruction,
7676 out_loc,
7677 component_offset,
7678 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007679 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007680 __ testl(out, out);
7681 // If `out` is null, we use it for the result, and jump to `done`.
7682 __ j(kEqual, &done);
7683 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
7684 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007685 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007686 __ movl(out, Immediate(1));
7687 __ jmp(&done);
7688 break;
7689 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007690
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007691 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007692 // No read barrier since the slow path will retry upon failure.
7693 // /* HeapReference<Class> */ out = obj->klass_
7694 GenerateReferenceLoadTwoRegisters(instruction,
7695 out_loc,
7696 obj_loc,
7697 class_offset,
7698 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007699 if (cls.IsRegister()) {
7700 __ cmpl(out, cls.AsRegister<Register>());
7701 } else {
7702 DCHECK(cls.IsStackSlot()) << cls;
7703 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7704 }
7705 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007706 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007707 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007708 codegen_->AddSlowPath(slow_path);
7709 __ j(kNotEqual, slow_path->GetEntryLabel());
7710 __ movl(out, Immediate(1));
7711 if (zero.IsLinked()) {
7712 __ jmp(&done);
7713 }
7714 break;
7715 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007716
Calin Juravle98893e12015-10-02 21:05:03 +01007717 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007718 case TypeCheckKind::kInterfaceCheck: {
7719 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007720 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00007721 // cases.
7722 //
7723 // We cannot directly call the InstanceofNonTrivial runtime
7724 // entry point without resorting to a type checking slow path
7725 // here (i.e. by calling InvokeRuntime directly), as it would
7726 // require to assign fixed registers for the inputs of this
7727 // HInstanceOf instruction (following the runtime calling
7728 // convention), which might be cluttered by the potential first
7729 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007730 //
7731 // TODO: Introduce a new runtime entry point taking the object
7732 // to test (instead of its class) as argument, and let it deal
7733 // with the read barrier issues. This will let us refactor this
7734 // case of the `switch` code as it was previously (with a direct
7735 // call to the runtime not using a type checking slow path).
7736 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007737 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007738 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007739 instruction, /* is_fatal= */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007740 codegen_->AddSlowPath(slow_path);
7741 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007742 if (zero.IsLinked()) {
7743 __ jmp(&done);
7744 }
7745 break;
7746 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007747
7748 case TypeCheckKind::kBitstringCheck: {
7749 // /* HeapReference<Class> */ temp = obj->klass_
7750 GenerateReferenceLoadTwoRegisters(instruction,
7751 out_loc,
7752 obj_loc,
7753 class_offset,
7754 kWithoutReadBarrier);
7755
7756 GenerateBitstringTypeCheckCompare(instruction, out);
7757 __ j(kNotEqual, &zero);
7758 __ movl(out, Immediate(1));
7759 __ jmp(&done);
7760 break;
7761 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007762 }
7763
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007764 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007765 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007766 __ xorl(out, out);
7767 }
7768
7769 if (done.IsLinked()) {
7770 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007771 }
7772
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007773 if (slow_path != nullptr) {
7774 __ Bind(slow_path->GetExitLabel());
7775 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007776}
7777
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007778void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007779 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007780 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007781 LocationSummary* locations =
7782 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007783 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007784 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7785 // Require a register for the interface check since there is a loop that compares the class to
7786 // a memory address.
7787 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007788 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7789 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7790 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7791 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007792 } else {
7793 locations->SetInAt(1, Location::Any());
7794 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007795 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007796 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
7797}
7798
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007799void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007800 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007801 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007802 Location obj_loc = locations->InAt(0);
7803 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007804 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007805 Location temp_loc = locations->GetTemp(0);
7806 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007807 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7808 DCHECK_GE(num_temps, 1u);
7809 DCHECK_LE(num_temps, 2u);
7810 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7811 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7812 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7813 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7814 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7815 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7816 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7817 const uint32_t object_array_data_offset =
7818 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007819
Vladimir Marko87584542017-12-12 17:47:52 +00007820 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007821 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007822 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7823 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007824 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007825
Roland Levillain0d5a2812015-11-13 10:07:31 +00007826 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007827 // Avoid null check if we know obj is not null.
7828 if (instruction->MustDoNullCheck()) {
7829 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007830 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007831 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007832
Roland Levillain0d5a2812015-11-13 10:07:31 +00007833 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007834 case TypeCheckKind::kExactCheck:
7835 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007836 // /* HeapReference<Class> */ temp = obj->klass_
7837 GenerateReferenceLoadTwoRegisters(instruction,
7838 temp_loc,
7839 obj_loc,
7840 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007841 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007842
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007843 if (cls.IsRegister()) {
7844 __ cmpl(temp, cls.AsRegister<Register>());
7845 } else {
7846 DCHECK(cls.IsStackSlot()) << cls;
7847 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7848 }
7849 // Jump to slow path for throwing the exception or doing a
7850 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007851 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007852 break;
7853 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007854
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007855 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007856 // /* HeapReference<Class> */ temp = obj->klass_
7857 GenerateReferenceLoadTwoRegisters(instruction,
7858 temp_loc,
7859 obj_loc,
7860 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007861 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007862
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007863 // If the class is abstract, we eagerly fetch the super class of the
7864 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007865 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007866 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007867 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007868 GenerateReferenceLoadOneRegister(instruction,
7869 temp_loc,
7870 super_offset,
7871 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007872 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007873
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007874 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7875 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007876 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007877 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007878
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007879 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007880 if (cls.IsRegister()) {
7881 __ cmpl(temp, cls.AsRegister<Register>());
7882 } else {
7883 DCHECK(cls.IsStackSlot()) << cls;
7884 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7885 }
7886 __ j(kNotEqual, &loop);
7887 break;
7888 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007889
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007890 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007891 // /* HeapReference<Class> */ temp = obj->klass_
7892 GenerateReferenceLoadTwoRegisters(instruction,
7893 temp_loc,
7894 obj_loc,
7895 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007896 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007897
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007898 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007899 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007900 __ Bind(&loop);
7901 if (cls.IsRegister()) {
7902 __ cmpl(temp, cls.AsRegister<Register>());
7903 } else {
7904 DCHECK(cls.IsStackSlot()) << cls;
7905 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7906 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007907 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007908
Roland Levillain0d5a2812015-11-13 10:07:31 +00007909 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007910 GenerateReferenceLoadOneRegister(instruction,
7911 temp_loc,
7912 super_offset,
7913 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007914 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007915
7916 // If the class reference currently in `temp` is not null, jump
7917 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007918 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007919 __ j(kNotZero, &loop);
7920 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007921 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007922 break;
7923 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007924
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007925 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007926 // /* HeapReference<Class> */ temp = obj->klass_
7927 GenerateReferenceLoadTwoRegisters(instruction,
7928 temp_loc,
7929 obj_loc,
7930 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007931 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007932
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007933 // Do an exact check.
7934 if (cls.IsRegister()) {
7935 __ cmpl(temp, cls.AsRegister<Register>());
7936 } else {
7937 DCHECK(cls.IsStackSlot()) << cls;
7938 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7939 }
7940 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007941
7942 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007943 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007944 GenerateReferenceLoadOneRegister(instruction,
7945 temp_loc,
7946 component_offset,
7947 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007948 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007949
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007950 // If the component type is null (i.e. the object not an array), jump to the slow path to
7951 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007952 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007953 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007954
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007955 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007956 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007957 break;
7958 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007959
Calin Juravle98893e12015-10-02 21:05:03 +01007960 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007961 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007962 // We cannot directly call the CheckCast runtime entry point
7963 // without resorting to a type checking slow path here (i.e. by
7964 // calling InvokeRuntime directly), as it would require to
7965 // assign fixed registers for the inputs of this HInstanceOf
7966 // instruction (following the runtime calling convention), which
7967 // might be cluttered by the potential first read barrier
7968 // emission at the beginning of this method.
7969 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007970 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007971
7972 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007973 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
7974 // We can not get false positives by doing this.
7975 // /* HeapReference<Class> */ temp = obj->klass_
7976 GenerateReferenceLoadTwoRegisters(instruction,
7977 temp_loc,
7978 obj_loc,
7979 class_offset,
7980 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007981
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007982 // /* HeapReference<Class> */ temp = temp->iftable_
7983 GenerateReferenceLoadTwoRegisters(instruction,
7984 temp_loc,
7985 temp_loc,
7986 iftable_offset,
7987 kWithoutReadBarrier);
7988 // Iftable is never null.
7989 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
7990 // Maybe poison the `cls` for direct comparison with memory.
7991 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
7992 // Loop through the iftable and check if any class matches.
7993 NearLabel start_loop;
7994 __ Bind(&start_loop);
7995 // Need to subtract first to handle the empty array case.
7996 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
7997 __ j(kNegative, type_check_slow_path->GetEntryLabel());
7998 // Go to next interface if the classes do not match.
7999 __ cmpl(cls.AsRegister<Register>(),
8000 CodeGeneratorX86::ArrayAddress(temp,
8001 maybe_temp2_loc,
8002 TIMES_4,
8003 object_array_data_offset));
8004 __ j(kNotEqual, &start_loop);
8005 // If `cls` was poisoned above, unpoison it.
8006 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07008007 break;
8008 }
Vladimir Marko175e7862018-03-27 09:03:13 +00008009
8010 case TypeCheckKind::kBitstringCheck: {
8011 // /* HeapReference<Class> */ temp = obj->klass_
8012 GenerateReferenceLoadTwoRegisters(instruction,
8013 temp_loc,
8014 obj_loc,
8015 class_offset,
8016 kWithoutReadBarrier);
8017
8018 GenerateBitstringTypeCheckCompare(instruction, temp);
8019 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
8020 break;
8021 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00008022 }
8023 __ Bind(&done);
8024
Roland Levillain0d5a2812015-11-13 10:07:31 +00008025 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00008026}
8027
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008028void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008029 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8030 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008031 InvokeRuntimeCallingConvention calling_convention;
8032 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8033}
8034
8035void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01008036 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
8037 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01008038 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01008039 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008040 if (instruction->IsEnter()) {
8041 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8042 } else {
8043 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8044 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008045}
8046
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05308047void LocationsBuilderX86::VisitX86AndNot(HX86AndNot* instruction) {
8048 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
8049 DCHECK(DataType::IsIntOrLongType(instruction->GetType())) << instruction->GetType();
8050 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
8051 locations->SetInAt(0, Location::RequiresRegister());
8052 locations->SetInAt(1, Location::RequiresRegister());
8053 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8054}
8055
8056void InstructionCodeGeneratorX86::VisitX86AndNot(HX86AndNot* instruction) {
8057 LocationSummary* locations = instruction->GetLocations();
8058 Location first = locations->InAt(0);
8059 Location second = locations->InAt(1);
8060 Location dest = locations->Out();
8061 if (instruction->GetResultType() == DataType::Type::kInt32) {
8062 __ andn(dest.AsRegister<Register>(),
8063 first.AsRegister<Register>(),
8064 second.AsRegister<Register>());
8065 } else {
8066 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
8067 __ andn(dest.AsRegisterPairLow<Register>(),
8068 first.AsRegisterPairLow<Register>(),
8069 second.AsRegisterPairLow<Register>());
8070 __ andn(dest.AsRegisterPairHigh<Register>(),
8071 first.AsRegisterPairHigh<Register>(),
8072 second.AsRegisterPairHigh<Register>());
8073 }
8074}
8075
8076void LocationsBuilderX86::VisitX86MaskOrResetLeastSetBit(HX86MaskOrResetLeastSetBit* instruction) {
8077 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
8078 DCHECK(instruction->GetType() == DataType::Type::kInt32) << instruction->GetType();
8079 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
8080 locations->SetInAt(0, Location::RequiresRegister());
8081 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8082}
8083
8084void InstructionCodeGeneratorX86::VisitX86MaskOrResetLeastSetBit(
8085 HX86MaskOrResetLeastSetBit* instruction) {
8086 LocationSummary* locations = instruction->GetLocations();
8087 Location src = locations->InAt(0);
8088 Location dest = locations->Out();
8089 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
8090 switch (instruction->GetOpKind()) {
8091 case HInstruction::kAnd:
8092 __ blsr(dest.AsRegister<Register>(), src.AsRegister<Register>());
8093 break;
8094 case HInstruction::kXor:
8095 __ blsmsk(dest.AsRegister<Register>(), src.AsRegister<Register>());
8096 break;
8097 default:
8098 LOG(FATAL) << "Unreachable";
8099 }
8100}
8101
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008102void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
8103void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
8104void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
8105
8106void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
8107 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008108 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008109 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
8110 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008111 locations->SetInAt(0, Location::RequiresRegister());
8112 locations->SetInAt(1, Location::Any());
8113 locations->SetOut(Location::SameAsFirstInput());
8114}
8115
8116void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
8117 HandleBitwiseOperation(instruction);
8118}
8119
8120void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
8121 HandleBitwiseOperation(instruction);
8122}
8123
8124void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
8125 HandleBitwiseOperation(instruction);
8126}
8127
8128void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
8129 LocationSummary* locations = instruction->GetLocations();
8130 Location first = locations->InAt(0);
8131 Location second = locations->InAt(1);
8132 DCHECK(first.Equals(locations->Out()));
8133
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008134 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008135 if (second.IsRegister()) {
8136 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00008137 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008138 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00008139 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008140 } else {
8141 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00008142 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008143 }
8144 } else if (second.IsConstant()) {
8145 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00008146 __ andl(first.AsRegister<Register>(),
8147 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008148 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00008149 __ orl(first.AsRegister<Register>(),
8150 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008151 } else {
8152 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00008153 __ xorl(first.AsRegister<Register>(),
8154 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008155 }
8156 } else {
8157 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00008158 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008159 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00008160 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008161 } else {
8162 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00008163 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008164 }
8165 }
8166 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008167 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008168 if (second.IsRegisterPair()) {
8169 if (instruction->IsAnd()) {
8170 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
8171 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
8172 } else if (instruction->IsOr()) {
8173 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
8174 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
8175 } else {
8176 DCHECK(instruction->IsXor());
8177 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
8178 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
8179 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008180 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008181 if (instruction->IsAnd()) {
8182 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
8183 __ andl(first.AsRegisterPairHigh<Register>(),
8184 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
8185 } else if (instruction->IsOr()) {
8186 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
8187 __ orl(first.AsRegisterPairHigh<Register>(),
8188 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
8189 } else {
8190 DCHECK(instruction->IsXor());
8191 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
8192 __ xorl(first.AsRegisterPairHigh<Register>(),
8193 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
8194 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008195 } else {
8196 DCHECK(second.IsConstant()) << second;
8197 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008198 int32_t low_value = Low32Bits(value);
8199 int32_t high_value = High32Bits(value);
8200 Immediate low(low_value);
8201 Immediate high(high_value);
8202 Register first_low = first.AsRegisterPairLow<Register>();
8203 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008204 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008205 if (low_value == 0) {
8206 __ xorl(first_low, first_low);
8207 } else if (low_value != -1) {
8208 __ andl(first_low, low);
8209 }
8210 if (high_value == 0) {
8211 __ xorl(first_high, first_high);
8212 } else if (high_value != -1) {
8213 __ andl(first_high, high);
8214 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008215 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008216 if (low_value != 0) {
8217 __ orl(first_low, low);
8218 }
8219 if (high_value != 0) {
8220 __ orl(first_high, high);
8221 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008222 } else {
8223 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008224 if (low_value != 0) {
8225 __ xorl(first_low, low);
8226 }
8227 if (high_value != 0) {
8228 __ xorl(first_high, high);
8229 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008230 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008231 }
8232 }
8233}
8234
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008235void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
8236 HInstruction* instruction,
8237 Location out,
8238 uint32_t offset,
8239 Location maybe_temp,
8240 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008241 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008242 if (read_barrier_option == kWithReadBarrier) {
8243 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008244 if (kUseBakerReadBarrier) {
8245 // Load with fast path based Baker's read barrier.
8246 // /* HeapReference<Object> */ out = *(out + offset)
8247 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008248 instruction, out, out_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008249 } else {
8250 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008251 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00008252 // in the following move operation, as we will need it for the
8253 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00008254 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008255 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008256 // /* HeapReference<Object> */ out = *(out + offset)
8257 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008258 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008259 }
8260 } else {
8261 // Plain load with no read barrier.
8262 // /* HeapReference<Object> */ out = *(out + offset)
8263 __ movl(out_reg, Address(out_reg, offset));
8264 __ MaybeUnpoisonHeapReference(out_reg);
8265 }
8266}
8267
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008268void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
8269 HInstruction* instruction,
8270 Location out,
8271 Location obj,
8272 uint32_t offset,
8273 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008274 Register out_reg = out.AsRegister<Register>();
8275 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008276 if (read_barrier_option == kWithReadBarrier) {
8277 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008278 if (kUseBakerReadBarrier) {
8279 // Load with fast path based Baker's read barrier.
8280 // /* HeapReference<Object> */ out = *(obj + offset)
8281 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008282 instruction, out, obj_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008283 } else {
8284 // Load with slow path based read barrier.
8285 // /* HeapReference<Object> */ out = *(obj + offset)
8286 __ movl(out_reg, Address(obj_reg, offset));
8287 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8288 }
8289 } else {
8290 // Plain load with no read barrier.
8291 // /* HeapReference<Object> */ out = *(obj + offset)
8292 __ movl(out_reg, Address(obj_reg, offset));
8293 __ MaybeUnpoisonHeapReference(out_reg);
8294 }
8295}
8296
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008297void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
8298 HInstruction* instruction,
8299 Location root,
8300 const Address& address,
8301 Label* fixup_label,
8302 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008303 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008304 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07008305 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008306 if (kUseBakerReadBarrier) {
8307 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
8308 // Baker's read barrier are used:
8309 //
Roland Levillaind966ce72017-02-09 16:20:14 +00008310 // root = obj.field;
8311 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8312 // if (temp != null) {
8313 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00008314 // }
8315
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008316 // /* GcRoot<mirror::Object> */ root = *address
8317 __ movl(root_reg, address);
8318 if (fixup_label != nullptr) {
8319 __ Bind(fixup_label);
8320 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008321 static_assert(
8322 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8323 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8324 "have different sizes.");
8325 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8326 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8327 "have different sizes.");
8328
Vladimir Marko953437b2016-08-24 08:30:46 +00008329 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008330 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008331 instruction, root, /* unpoison_ref_before_marking= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008332 codegen_->AddSlowPath(slow_path);
8333
Roland Levillaind966ce72017-02-09 16:20:14 +00008334 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
8335 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01008336 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00008337 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
8338 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008339 __ j(kNotEqual, slow_path->GetEntryLabel());
8340 __ Bind(slow_path->GetExitLabel());
8341 } else {
8342 // GC root loaded through a slow path for read barriers other
8343 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008344 // /* GcRoot<mirror::Object>* */ root = address
8345 __ leal(root_reg, address);
8346 if (fixup_label != nullptr) {
8347 __ Bind(fixup_label);
8348 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008349 // /* mirror::Object* */ root = root->Read()
8350 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8351 }
8352 } else {
8353 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008354 // /* GcRoot<mirror::Object> */ root = *address
8355 __ movl(root_reg, address);
8356 if (fixup_label != nullptr) {
8357 __ Bind(fixup_label);
8358 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00008359 // Note that GC roots are not affected by heap poisoning, thus we
8360 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008361 }
8362}
8363
8364void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8365 Location ref,
8366 Register obj,
8367 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00008368 bool needs_null_check) {
8369 DCHECK(kEmitCompilerReadBarrier);
8370 DCHECK(kUseBakerReadBarrier);
8371
8372 // /* HeapReference<Object> */ ref = *(obj + offset)
8373 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00008374 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008375}
8376
8377void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8378 Location ref,
8379 Register obj,
8380 uint32_t data_offset,
8381 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00008382 bool needs_null_check) {
8383 DCHECK(kEmitCompilerReadBarrier);
8384 DCHECK(kUseBakerReadBarrier);
8385
Roland Levillain3d312422016-06-23 13:53:42 +01008386 static_assert(
8387 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8388 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00008389 // /* HeapReference<Object> */ ref =
8390 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008391 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00008392 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008393}
8394
8395void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8396 Location ref,
8397 Register obj,
8398 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008399 bool needs_null_check,
8400 bool always_update_field,
8401 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008402 DCHECK(kEmitCompilerReadBarrier);
8403 DCHECK(kUseBakerReadBarrier);
8404
8405 // In slow path based read barriers, the read barrier call is
8406 // inserted after the original load. However, in fast path based
8407 // Baker's read barriers, we need to perform the load of
8408 // mirror::Object::monitor_ *before* the original reference load.
8409 // This load-load ordering is required by the read barrier.
8410 // The fast path/slow path (for Baker's algorithm) should look like:
8411 //
8412 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8413 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8414 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008415 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00008416 // if (is_gray) {
8417 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
8418 // }
8419 //
8420 // Note: the original implementation in ReadBarrier::Barrier is
8421 // slightly more complex as:
8422 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00008423 // the high-bits of rb_state, which are expected to be all zeroes
8424 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
8425 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008426 // - it performs additional checks that we do not do here for
8427 // performance reasons.
8428
8429 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00008430 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
8431
Vladimir Marko953437b2016-08-24 08:30:46 +00008432 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01008433 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008434 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00008435 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
8436 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
8437 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
8438
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008439 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00008440 // ref = ReadBarrier::Mark(ref);
8441 // At this point, just do the "if" and make sure that flags are preserved until the branch.
8442 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00008443 if (needs_null_check) {
8444 MaybeRecordImplicitNullCheck(instruction);
8445 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008446
8447 // Load fence to prevent load-load reordering.
8448 // Note that this is a no-op, thanks to the x86 memory model.
8449 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
8450
8451 // The actual reference load.
8452 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00008453 __ movl(ref_reg, src); // Flags are unaffected.
8454
8455 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
8456 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008457 SlowPathCode* slow_path;
8458 if (always_update_field) {
8459 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01008460 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008461 instruction, ref, obj, src, /* unpoison_ref_before_marking= */ true, *temp);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008462 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008463 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008464 instruction, ref, /* unpoison_ref_before_marking= */ true);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008465 }
Vladimir Marko953437b2016-08-24 08:30:46 +00008466 AddSlowPath(slow_path);
8467
8468 // We have done the "if" of the gray bit check above, now branch based on the flags.
8469 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00008470
8471 // Object* ref = ref_addr->AsMirrorPtr()
8472 __ MaybeUnpoisonHeapReference(ref_reg);
8473
Roland Levillain7c1559a2015-12-15 10:55:36 +00008474 __ Bind(slow_path->GetExitLabel());
8475}
8476
8477void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
8478 Location out,
8479 Location ref,
8480 Location obj,
8481 uint32_t offset,
8482 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008483 DCHECK(kEmitCompilerReadBarrier);
8484
Roland Levillain7c1559a2015-12-15 10:55:36 +00008485 // Insert a slow path based read barrier *after* the reference load.
8486 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008487 // If heap poisoning is enabled, the unpoisoning of the loaded
8488 // reference will be carried out by the runtime within the slow
8489 // path.
8490 //
8491 // Note that `ref` currently does not get unpoisoned (when heap
8492 // poisoning is enabled), which is alright as the `ref` argument is
8493 // not used by the artReadBarrierSlow entry point.
8494 //
8495 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008496 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00008497 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
8498 AddSlowPath(slow_path);
8499
Roland Levillain0d5a2812015-11-13 10:07:31 +00008500 __ jmp(slow_path->GetEntryLabel());
8501 __ Bind(slow_path->GetExitLabel());
8502}
8503
Roland Levillain7c1559a2015-12-15 10:55:36 +00008504void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
8505 Location out,
8506 Location ref,
8507 Location obj,
8508 uint32_t offset,
8509 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008510 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008511 // Baker's read barriers shall be handled by the fast path
8512 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
8513 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008514 // If heap poisoning is enabled, unpoisoning will be taken care of
8515 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008516 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008517 } else if (kPoisonHeapReferences) {
8518 __ UnpoisonHeapReference(out.AsRegister<Register>());
8519 }
8520}
8521
Roland Levillain7c1559a2015-12-15 10:55:36 +00008522void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8523 Location out,
8524 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008525 DCHECK(kEmitCompilerReadBarrier);
8526
Roland Levillain7c1559a2015-12-15 10:55:36 +00008527 // Insert a slow path based read barrier *after* the GC root load.
8528 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008529 // Note that GC roots are not affected by heap poisoning, so we do
8530 // not need to do anything special for this here.
8531 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008532 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008533 AddSlowPath(slow_path);
8534
Roland Levillain0d5a2812015-11-13 10:07:31 +00008535 __ jmp(slow_path->GetEntryLabel());
8536 __ Bind(slow_path->GetExitLabel());
8537}
8538
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008539void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008540 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008541 LOG(FATAL) << "Unreachable";
8542}
8543
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008544void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008545 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008546 LOG(FATAL) << "Unreachable";
8547}
8548
Mark Mendellfe57faa2015-09-18 09:26:15 -04008549// Simple implementation of packed switch - generate cascaded compare/jumps.
8550void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8551 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008552 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04008553 locations->SetInAt(0, Location::RequiresRegister());
8554}
8555
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008556void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
8557 int32_t lower_bound,
8558 uint32_t num_entries,
8559 HBasicBlock* switch_block,
8560 HBasicBlock* default_block) {
8561 // Figure out the correct compare values and jump conditions.
8562 // Handle the first compare/branch as a special case because it might
8563 // jump to the default case.
8564 DCHECK_GT(num_entries, 2u);
8565 Condition first_condition;
8566 uint32_t index;
8567 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
8568 if (lower_bound != 0) {
8569 first_condition = kLess;
8570 __ cmpl(value_reg, Immediate(lower_bound));
8571 __ j(first_condition, codegen_->GetLabelOf(default_block));
8572 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008573
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008574 index = 1;
8575 } else {
8576 // Handle all the compare/jumps below.
8577 first_condition = kBelow;
8578 index = 0;
8579 }
8580
8581 // Handle the rest of the compare/jumps.
8582 for (; index + 1 < num_entries; index += 2) {
8583 int32_t compare_to_value = lower_bound + index + 1;
8584 __ cmpl(value_reg, Immediate(compare_to_value));
8585 // Jump to successors[index] if value < case_value[index].
8586 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
8587 // Jump to successors[index + 1] if value == case_value[index + 1].
8588 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
8589 }
8590
8591 if (index != num_entries) {
8592 // There are an odd number of entries. Handle the last one.
8593 DCHECK_EQ(index + 1, num_entries);
8594 __ cmpl(value_reg, Immediate(lower_bound + index));
8595 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008596 }
8597
8598 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008599 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
8600 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008601 }
8602}
8603
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008604void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8605 int32_t lower_bound = switch_instr->GetStartValue();
8606 uint32_t num_entries = switch_instr->GetNumEntries();
8607 LocationSummary* locations = switch_instr->GetLocations();
8608 Register value_reg = locations->InAt(0).AsRegister<Register>();
8609
8610 GenPackedSwitchWithCompares(value_reg,
8611 lower_bound,
8612 num_entries,
8613 switch_instr->GetBlock(),
8614 switch_instr->GetDefaultBlock());
8615}
8616
Mark Mendell805b3b52015-09-18 14:10:29 -04008617void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8618 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008619 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04008620 locations->SetInAt(0, Location::RequiresRegister());
8621
8622 // Constant area pointer.
8623 locations->SetInAt(1, Location::RequiresRegister());
8624
8625 // And the temporary we need.
8626 locations->AddTemp(Location::RequiresRegister());
8627}
8628
8629void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8630 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008631 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04008632 LocationSummary* locations = switch_instr->GetLocations();
8633 Register value_reg = locations->InAt(0).AsRegister<Register>();
8634 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8635
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008636 if (num_entries <= kPackedSwitchJumpTableThreshold) {
8637 GenPackedSwitchWithCompares(value_reg,
8638 lower_bound,
8639 num_entries,
8640 switch_instr->GetBlock(),
8641 default_block);
8642 return;
8643 }
8644
Mark Mendell805b3b52015-09-18 14:10:29 -04008645 // Optimizing has a jump area.
8646 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
8647 Register constant_area = locations->InAt(1).AsRegister<Register>();
8648
8649 // Remove the bias, if needed.
8650 if (lower_bound != 0) {
8651 __ leal(temp_reg, Address(value_reg, -lower_bound));
8652 value_reg = temp_reg;
8653 }
8654
8655 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008656 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04008657 __ cmpl(value_reg, Immediate(num_entries - 1));
8658 __ j(kAbove, codegen_->GetLabelOf(default_block));
8659
8660 // We are in the range of the table.
8661 // Load (target-constant_area) from the jump table, indexing by the value.
8662 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
8663
8664 // Compute the actual target address by adding in constant_area.
8665 __ addl(temp_reg, constant_area);
8666
8667 // And jump.
8668 __ jmp(temp_reg);
8669}
8670
Mark Mendell0616ae02015-04-17 12:49:27 -04008671void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
8672 HX86ComputeBaseMethodAddress* insn) {
8673 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008674 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008675 locations->SetOut(Location::RequiresRegister());
8676}
8677
8678void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
8679 HX86ComputeBaseMethodAddress* insn) {
8680 LocationSummary* locations = insn->GetLocations();
8681 Register reg = locations->Out().AsRegister<Register>();
8682
8683 // Generate call to next instruction.
8684 Label next_instruction;
8685 __ call(&next_instruction);
8686 __ Bind(&next_instruction);
8687
8688 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008689 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04008690
8691 // Grab the return address off the stack.
8692 __ popl(reg);
8693}
8694
8695void LocationsBuilderX86::VisitX86LoadFromConstantTable(
8696 HX86LoadFromConstantTable* insn) {
8697 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008698 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008699
8700 locations->SetInAt(0, Location::RequiresRegister());
8701 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
8702
8703 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00008704 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008705 return;
8706 }
8707
8708 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008709 case DataType::Type::kFloat32:
8710 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008711 locations->SetOut(Location::RequiresFpuRegister());
8712 break;
8713
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008714 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008715 locations->SetOut(Location::RequiresRegister());
8716 break;
8717
8718 default:
8719 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8720 }
8721}
8722
8723void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00008724 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008725 return;
8726 }
8727
8728 LocationSummary* locations = insn->GetLocations();
8729 Location out = locations->Out();
8730 Register const_area = locations->InAt(0).AsRegister<Register>();
8731 HConstant *value = insn->GetConstant();
8732
8733 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008734 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008735 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008736 codegen_->LiteralFloatAddress(
8737 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008738 break;
8739
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008740 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008741 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008742 codegen_->LiteralDoubleAddress(
8743 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008744 break;
8745
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008746 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008747 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008748 codegen_->LiteralInt32Address(
8749 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008750 break;
8751
8752 default:
8753 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8754 }
8755}
8756
Mark Mendell0616ae02015-04-17 12:49:27 -04008757/**
8758 * Class to handle late fixup of offsets into constant area.
8759 */
Vladimir Marko5233f932015-09-29 19:01:15 +01008760class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04008761 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008762 RIPFixup(CodeGeneratorX86& codegen,
8763 HX86ComputeBaseMethodAddress* base_method_address,
8764 size_t offset)
8765 : codegen_(&codegen),
8766 base_method_address_(base_method_address),
8767 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008768
8769 protected:
8770 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
8771
8772 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008773 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008774
8775 private:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008776 void Process(const MemoryRegion& region, int pos) override {
Mark Mendell0616ae02015-04-17 12:49:27 -04008777 // Patch the correct offset for the instruction. The place to patch is the
8778 // last 4 bytes of the instruction.
8779 // The value to patch is the distance from the offset in the constant area
8780 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04008781 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008782 int32_t relative_position =
8783 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04008784
8785 // Patch in the right value.
8786 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
8787 }
8788
Mark Mendell0616ae02015-04-17 12:49:27 -04008789 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04008790 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008791};
8792
Mark Mendell805b3b52015-09-18 14:10:29 -04008793/**
8794 * Class to handle late fixup of offsets to a jump table that will be created in the
8795 * constant area.
8796 */
8797class JumpTableRIPFixup : public RIPFixup {
8798 public:
8799 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008800 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
8801 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008802
8803 void CreateJumpTable() {
8804 X86Assembler* assembler = codegen_->GetAssembler();
8805
8806 // Ensure that the reference to the jump table has the correct offset.
8807 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
8808 SetOffset(offset_in_constant_table);
8809
8810 // The label values in the jump table are computed relative to the
8811 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008812 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04008813
8814 // Populate the jump table with the correct values for the jump table.
8815 int32_t num_entries = switch_instr_->GetNumEntries();
8816 HBasicBlock* block = switch_instr_->GetBlock();
8817 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
8818 // The value that we want is the target offset - the position of the table.
8819 for (int32_t i = 0; i < num_entries; i++) {
8820 HBasicBlock* b = successors[i];
8821 Label* l = codegen_->GetLabelOf(b);
8822 DCHECK(l->IsBound());
8823 int32_t offset_to_block = l->Position() - relative_offset;
8824 assembler->AppendInt32(offset_to_block);
8825 }
8826 }
8827
8828 private:
8829 const HX86PackedSwitch* switch_instr_;
8830};
8831
8832void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
8833 // Generate the constant area if needed.
8834 X86Assembler* assembler = GetAssembler();
jaishank20d1c942019-03-08 15:08:17 +05308835
Mark Mendell805b3b52015-09-18 14:10:29 -04008836 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
8837 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
8838 // byte values.
8839 assembler->Align(4, 0);
8840 constant_area_start_ = assembler->CodeSize();
8841
8842 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008843 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04008844 jump_table->CreateJumpTable();
8845 }
8846
8847 // And now add the constant area to the generated code.
8848 assembler->AddConstantArea();
8849 }
8850
8851 // And finish up.
8852 CodeGenerator::Finalize(allocator);
8853}
8854
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008855Address CodeGeneratorX86::LiteralDoubleAddress(double v,
8856 HX86ComputeBaseMethodAddress* method_base,
8857 Register reg) {
8858 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008859 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008860 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008861}
8862
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008863Address CodeGeneratorX86::LiteralFloatAddress(float v,
8864 HX86ComputeBaseMethodAddress* method_base,
8865 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008866 AssemblerFixup* fixup =
8867 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008868 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008869}
8870
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008871Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
8872 HX86ComputeBaseMethodAddress* method_base,
8873 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008874 AssemblerFixup* fixup =
8875 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008876 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008877}
8878
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008879Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
8880 HX86ComputeBaseMethodAddress* method_base,
8881 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008882 AssemblerFixup* fixup =
8883 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008884 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008885}
8886
Aart Bika19616e2016-02-01 18:57:58 -08008887void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
8888 if (value == 0) {
8889 __ xorl(dest, dest);
8890 } else {
8891 __ movl(dest, Immediate(value));
8892 }
8893}
8894
8895void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
8896 if (value == 0) {
8897 __ testl(dest, dest);
8898 } else {
8899 __ cmpl(dest, Immediate(value));
8900 }
8901}
8902
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008903void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
8904 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07008905 GenerateIntCompare(lhs_reg, rhs);
8906}
8907
8908void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008909 if (rhs.IsConstant()) {
8910 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07008911 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008912 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07008913 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008914 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07008915 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008916 }
8917}
8918
8919Address CodeGeneratorX86::ArrayAddress(Register obj,
8920 Location index,
8921 ScaleFactor scale,
8922 uint32_t data_offset) {
8923 return index.IsConstant() ?
8924 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
8925 Address(obj, index.AsRegister<Register>(), scale, data_offset);
8926}
8927
Mark Mendell805b3b52015-09-18 14:10:29 -04008928Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
8929 Register reg,
8930 Register value) {
8931 // Create a fixup to be used to create and address the jump table.
8932 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008933 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04008934
8935 // We have to populate the jump tables.
8936 fixups_to_jump_tables_.push_back(table_fixup);
8937
8938 // We want a scaled address, as we are extracting the correct offset from the table.
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008939 return Address(reg, value, TIMES_4, kPlaceholder32BitOffset, table_fixup);
Mark Mendell805b3b52015-09-18 14:10:29 -04008940}
8941
Andreas Gampe85b62f22015-09-09 13:15:38 -07008942// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008943void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07008944 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008945 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008946 return;
8947 }
8948
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008949 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008950
8951 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
8952 if (target.Equals(return_loc)) {
8953 return;
8954 }
8955
8956 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8957 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008958 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008959 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008960 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
8961 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008962 GetMoveResolver()->EmitNativeCode(&parallel_move);
8963 } else {
8964 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01008965 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07008966 parallel_move.AddMove(return_loc, target, type, nullptr);
8967 GetMoveResolver()->EmitNativeCode(&parallel_move);
8968 }
8969}
8970
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008971void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
8972 const uint8_t* roots_data,
8973 const PatchInfo<Label>& info,
8974 uint64_t index_in_table) const {
8975 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
8976 uintptr_t address =
8977 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
Andreas Gampec55bb392018-09-21 00:02:02 +00008978 using unaligned_uint32_t __attribute__((__aligned__(1))) = uint32_t;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008979 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
8980 dchecked_integral_cast<uint32_t>(address);
8981}
8982
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008983void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8984 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008985 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008986 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008987 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008988 }
8989
8990 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008991 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008992 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008993 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008994 }
8995}
8996
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008997void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8998 ATTRIBUTE_UNUSED) {
8999 LOG(FATAL) << "Unreachable";
9000}
9001
9002void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
9003 ATTRIBUTE_UNUSED) {
9004 LOG(FATAL) << "Unreachable";
9005}
9006
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05309007bool LocationsBuilderX86::CpuHasAvxFeatureFlag() {
9008 return codegen_->GetInstructionSetFeatures().HasAVX();
9009}
9010bool LocationsBuilderX86::CpuHasAvx2FeatureFlag() {
9011 return codegen_->GetInstructionSetFeatures().HasAVX2();
9012}
9013bool InstructionCodeGeneratorX86::CpuHasAvxFeatureFlag() {
9014 return codegen_->GetInstructionSetFeatures().HasAVX();
9015}
9016bool InstructionCodeGeneratorX86::CpuHasAvx2FeatureFlag() {
9017 return codegen_->GetInstructionSetFeatures().HasAVX2();
9018}
9019
Roland Levillain4d027112015-07-01 15:41:14 +01009020#undef __
9021
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00009022} // namespace x86
9023} // namespace art