blob: c49b08ba690d15614f6b036f05d3d9c3146653af [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 ||
Sorin Basca2f01e8e2021-06-18 06:44:07 +0000590 intrinsic == Intrinsics::kJdkUnsafeCASObject ||
Andra Danciu9dfb1a92020-09-22 13:27:18 +0000591 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) == kVarHandleCAS ||
Andra Danciu370948e2020-09-23 08:07:25 +0000592 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) == kVarHandleGetAndSet ||
593 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) == kVarHandleCAX);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100594
595 __ Bind(GetEntryLabel());
596 if (unpoison_ref_before_marking_) {
597 // Object* ref = ref_addr->AsMirrorPtr()
598 __ MaybeUnpoisonHeapReference(ref_reg);
599 }
600
601 // Save the old (unpoisoned) reference.
602 __ movl(temp_, ref_reg);
603
604 // No need to save live registers; it's taken care of by the
605 // entrypoint. Also, there is no need to update the stack mask,
606 // as this runtime call will not trigger a garbage collection.
607 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
608 DCHECK_NE(ref_reg, ESP);
609 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
610 // "Compact" slow path, saving two moves.
611 //
612 // Instead of using the standard runtime calling convention (input
613 // and output in EAX):
614 //
615 // EAX <- ref
616 // EAX <- ReadBarrierMark(EAX)
617 // ref <- EAX
618 //
619 // we just use rX (the register containing `ref`) as input and output
620 // of a dedicated entrypoint:
621 //
622 // rX <- ReadBarrierMarkRegX(rX)
623 //
Roland Levillain97c46462017-05-11 14:04:03 +0100624 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100625 // This runtime call does not require a stack map.
626 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
627
628 // If the new reference is different from the old reference,
629 // update the field in the holder (`*field_addr`).
630 //
631 // Note that this field could also hold a different object, if
632 // another thread had concurrently changed it. In that case, the
633 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
634 // operation below would abort the CAS, leaving the field as-is.
635 NearLabel done;
636 __ cmpl(temp_, ref_reg);
637 __ j(kEqual, &done);
638
639 // Update the the holder's field atomically. This may fail if
640 // mutator updates before us, but it's OK. This is achieved
641 // using a strong compare-and-set (CAS) operation with relaxed
642 // memory synchronization ordering, where the expected value is
643 // the old reference and the desired value is the new reference.
644 // This operation is implemented with a 32-bit LOCK CMPXLCHG
645 // instruction, which requires the expected value (the old
646 // reference) to be in EAX. Save EAX beforehand, and move the
647 // expected value (stored in `temp_`) into EAX.
648 __ pushl(EAX);
649 __ movl(EAX, temp_);
650
651 // Convenience aliases.
652 Register base = obj_;
653 Register expected = EAX;
654 Register value = ref_reg;
655
656 bool base_equals_value = (base == value);
657 if (kPoisonHeapReferences) {
658 if (base_equals_value) {
659 // If `base` and `value` are the same register location, move
660 // `value` to a temporary register. This way, poisoning
661 // `value` won't invalidate `base`.
662 value = temp_;
663 __ movl(value, base);
664 }
665
666 // Check that the register allocator did not assign the location
667 // of `expected` (EAX) to `value` nor to `base`, so that heap
668 // poisoning (when enabled) works as intended below.
669 // - If `value` were equal to `expected`, both references would
670 // be poisoned twice, meaning they would not be poisoned at
671 // all, as heap poisoning uses address negation.
672 // - If `base` were equal to `expected`, poisoning `expected`
673 // would invalidate `base`.
674 DCHECK_NE(value, expected);
675 DCHECK_NE(base, expected);
676
677 __ PoisonHeapReference(expected);
678 __ PoisonHeapReference(value);
679 }
680
681 __ LockCmpxchgl(field_addr_, value);
682
683 // If heap poisoning is enabled, we need to unpoison the values
684 // that were poisoned earlier.
685 if (kPoisonHeapReferences) {
686 if (base_equals_value) {
687 // `value` has been moved to a temporary register, no need
688 // to unpoison it.
689 } else {
690 __ UnpoisonHeapReference(value);
691 }
692 // No need to unpoison `expected` (EAX), as it is be overwritten below.
693 }
694
695 // Restore EAX.
696 __ popl(EAX);
697
698 __ Bind(&done);
699 __ jmp(GetExitLabel());
700 }
701
702 private:
703 // The location (register) of the marked object reference.
704 const Location ref_;
705 // The register containing the object holding the marked object reference field.
706 const Register obj_;
707 // The address of the marked reference field. The base of this address must be `obj_`.
708 const Address field_addr_;
709
710 // Should the reference in `ref_` be unpoisoned prior to marking it?
711 const bool unpoison_ref_before_marking_;
712
713 const Register temp_;
714
715 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
716};
717
Roland Levillain0d5a2812015-11-13 10:07:31 +0000718// Slow path generating a read barrier for a heap reference.
719class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
720 public:
721 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
722 Location out,
723 Location ref,
724 Location obj,
725 uint32_t offset,
726 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000727 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000728 out_(out),
729 ref_(ref),
730 obj_(obj),
731 offset_(offset),
732 index_(index) {
733 DCHECK(kEmitCompilerReadBarrier);
734 // If `obj` is equal to `out` or `ref`, it means the initial object
735 // has been overwritten by (or after) the heap object reference load
736 // to be instrumented, e.g.:
737 //
738 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000739 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000740 //
741 // In that case, we have lost the information about the original
742 // object, and the emitted read barrier cannot work properly.
743 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
744 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
745 }
746
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100747 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000748 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
749 LocationSummary* locations = instruction_->GetLocations();
750 Register reg_out = out_.AsRegister<Register>();
751 DCHECK(locations->CanCall());
752 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100753 DCHECK(instruction_->IsInstanceFieldGet() ||
Alex Light3a73ffb2021-01-25 14:11:05 +0000754 instruction_->IsPredicatedInstanceFieldGet() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100755 instruction_->IsStaticFieldGet() ||
756 instruction_->IsArrayGet() ||
757 instruction_->IsInstanceOf() ||
758 instruction_->IsCheckCast() ||
Vladimir Marko94d2c812020-11-05 10:04:45 +0000759 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000760 << "Unexpected instruction in read barrier for heap reference slow path: "
761 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000762
763 __ Bind(GetEntryLabel());
764 SaveLiveRegisters(codegen, locations);
765
766 // We may have to change the index's value, but as `index_` is a
767 // constant member (like other "inputs" of this slow path),
768 // introduce a copy of it, `index`.
769 Location index = index_;
770 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100771 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000772 if (instruction_->IsArrayGet()) {
773 // Compute the actual memory offset and store it in `index`.
774 Register index_reg = index_.AsRegister<Register>();
775 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
776 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
777 // We are about to change the value of `index_reg` (see the
778 // calls to art::x86::X86Assembler::shll and
779 // art::x86::X86Assembler::AddImmediate below), but it has
780 // not been saved by the previous call to
781 // art::SlowPathCode::SaveLiveRegisters, as it is a
782 // callee-save register --
783 // art::SlowPathCode::SaveLiveRegisters does not consider
784 // callee-save registers, as it has been designed with the
785 // assumption that callee-save registers are supposed to be
786 // handled by the called function. So, as a callee-save
787 // register, `index_reg` _would_ eventually be saved onto
788 // the stack, but it would be too late: we would have
789 // changed its value earlier. Therefore, we manually save
790 // it here into another freely available register,
791 // `free_reg`, chosen of course among the caller-save
792 // registers (as a callee-save `free_reg` register would
793 // exhibit the same problem).
794 //
795 // Note we could have requested a temporary register from
796 // the register allocator instead; but we prefer not to, as
797 // this is a slow path, and we know we can find a
798 // caller-save register that is available.
799 Register free_reg = FindAvailableCallerSaveRegister(codegen);
800 __ movl(free_reg, index_reg);
801 index_reg = free_reg;
802 index = Location::RegisterLocation(index_reg);
803 } else {
804 // The initial register stored in `index_` has already been
805 // saved in the call to art::SlowPathCode::SaveLiveRegisters
806 // (as it is not a callee-save register), so we can freely
807 // use it.
808 }
809 // Shifting the index value contained in `index_reg` by the scale
810 // factor (2) cannot overflow in practice, as the runtime is
811 // unable to allocate object arrays with a size larger than
812 // 2^26 - 1 (that is, 2^28 - 4 bytes).
813 __ shll(index_reg, Immediate(TIMES_4));
814 static_assert(
815 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
816 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
817 __ AddImmediate(index_reg, Immediate(offset_));
818 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100819 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
820 // intrinsics, `index_` is not shifted by a scale factor of 2
821 // (as in the case of ArrayGet), as it is actually an offset
822 // to an object field within an object.
823 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000824 DCHECK(instruction_->GetLocations()->Intrinsified());
825 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
Sorin Basca2f01e8e2021-06-18 06:44:07 +0000826 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile) ||
827 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kJdkUnsafeGetObject) ||
Sorin Basca4a4696a2021-10-09 07:14:40 +0000828 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kJdkUnsafeGetObjectVolatile) ||
829 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kJdkUnsafeGetObjectAcquire))
Roland Levillain0d5a2812015-11-13 10:07:31 +0000830 << instruction_->AsInvoke()->GetIntrinsic();
831 DCHECK_EQ(offset_, 0U);
832 DCHECK(index_.IsRegisterPair());
833 // UnsafeGet's offset location is a register pair, the low
834 // part contains the correct offset.
835 index = index_.ToLow();
836 }
837 }
838
839 // We're moving two or three locations to locations that could
840 // overlap, so we need a parallel move resolver.
841 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100842 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000843 parallel_move.AddMove(ref_,
844 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100845 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000846 nullptr);
847 parallel_move.AddMove(obj_,
848 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100849 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000850 nullptr);
851 if (index.IsValid()) {
852 parallel_move.AddMove(index,
853 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100854 DataType::Type::kInt32,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000855 nullptr);
856 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
857 } else {
858 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
859 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
860 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100861 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000862 CheckEntrypointTypes<
863 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
864 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
865
866 RestoreLiveRegisters(codegen, locations);
867 __ jmp(GetExitLabel());
868 }
869
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100870 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathX86"; }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000871
872 private:
873 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
874 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
875 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
876 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
877 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
878 return static_cast<Register>(i);
879 }
880 }
881 // We shall never fail to find a free caller-save register, as
882 // there are more than two core caller-save registers on x86
883 // (meaning it is possible to find one which is different from
884 // `ref` and `obj`).
885 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
886 LOG(FATAL) << "Could not find a free caller-save register";
887 UNREACHABLE();
888 }
889
Roland Levillain0d5a2812015-11-13 10:07:31 +0000890 const Location out_;
891 const Location ref_;
892 const Location obj_;
893 const uint32_t offset_;
894 // An additional location containing an index to an array.
895 // Only used for HArrayGet and the UnsafeGetObject &
896 // UnsafeGetObjectVolatile intrinsics.
897 const Location index_;
898
899 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
900};
901
902// Slow path generating a read barrier for a GC root.
903class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
904 public:
905 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000906 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000907 DCHECK(kEmitCompilerReadBarrier);
908 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000909
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100910 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000911 LocationSummary* locations = instruction_->GetLocations();
912 Register reg_out = out_.AsRegister<Register>();
913 DCHECK(locations->CanCall());
914 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000915 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
916 << "Unexpected instruction in read barrier for GC root slow path: "
917 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000918
919 __ Bind(GetEntryLabel());
920 SaveLiveRegisters(codegen, locations);
921
922 InvokeRuntimeCallingConvention calling_convention;
923 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
924 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100925 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000926 instruction_,
927 instruction_->GetDexPc(),
928 this);
929 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
930 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
931
932 RestoreLiveRegisters(codegen, locations);
933 __ jmp(GetExitLabel());
934 }
935
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100936 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathX86"; }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000937
938 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000939 const Location out_;
940 const Location root_;
941
942 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
943};
944
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100945#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100946// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
947#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100948
Aart Bike9f37602015-10-09 11:15:55 -0700949inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700950 switch (cond) {
951 case kCondEQ: return kEqual;
952 case kCondNE: return kNotEqual;
953 case kCondLT: return kLess;
954 case kCondLE: return kLessEqual;
955 case kCondGT: return kGreater;
956 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700957 case kCondB: return kBelow;
958 case kCondBE: return kBelowEqual;
959 case kCondA: return kAbove;
960 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700961 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100962 LOG(FATAL) << "Unreachable";
963 UNREACHABLE();
964}
965
Aart Bike9f37602015-10-09 11:15:55 -0700966// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100967inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
968 switch (cond) {
969 case kCondEQ: return kEqual;
970 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700971 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100972 case kCondLT: return kBelow;
973 case kCondLE: return kBelowEqual;
974 case kCondGT: return kAbove;
975 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700976 // Unsigned remain unchanged.
977 case kCondB: return kBelow;
978 case kCondBE: return kBelowEqual;
979 case kCondA: return kAbove;
980 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100981 }
982 LOG(FATAL) << "Unreachable";
983 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700984}
985
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100986void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100987 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100988}
989
990void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100991 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100992}
993
Vladimir Markoa0431112018-06-25 09:32:54 +0100994const X86InstructionSetFeatures& CodeGeneratorX86::GetInstructionSetFeatures() const {
995 return *GetCompilerOptions().GetInstructionSetFeatures()->AsX86InstructionSetFeatures();
996}
997
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100998size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
999 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
1000 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001001}
1002
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001003size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1004 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
1005 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001006}
1007
Mark Mendell7c8d0092015-01-26 11:21:33 -05001008size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001009 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001010 __ movups(Address(ESP, stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -07001011 } else {
1012 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
1013 }
Artem Serov6a0b6572019-07-26 20:38:37 +01001014 return GetSlowPathFPWidth();
Mark Mendell7c8d0092015-01-26 11:21:33 -05001015}
1016
1017size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001018 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001019 __ movups(XmmRegister(reg_id), Address(ESP, stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -07001020 } else {
1021 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
1022 }
Artem Serov6a0b6572019-07-26 20:38:37 +01001023 return GetSlowPathFPWidth();
Mark Mendell7c8d0092015-01-26 11:21:33 -05001024}
1025
Calin Juravle175dc732015-08-25 15:42:32 +01001026void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
1027 HInstruction* instruction,
1028 uint32_t dex_pc,
1029 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001030 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001031 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
1032 if (EntrypointRequiresStackMap(entrypoint)) {
1033 RecordPcInfo(instruction, dex_pc, slow_path);
1034 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001035}
1036
Roland Levillaindec8f632016-07-22 17:10:06 +01001037void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1038 HInstruction* instruction,
1039 SlowPathCode* slow_path) {
1040 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001041 GenerateInvokeRuntime(entry_point_offset);
1042}
1043
1044void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001045 __ fs()->call(Address::Absolute(entry_point_offset));
1046}
1047
Mark Mendellfb8d2792015-03-31 22:16:59 -04001048CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001049 const CompilerOptions& compiler_options,
1050 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001051 : CodeGenerator(graph,
1052 kNumberOfCpuRegisters,
1053 kNumberOfXmmRegisters,
1054 kNumberOfRegisterPairs,
1055 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1056 arraysize(kCoreCalleeSaves))
1057 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001058 0,
1059 compiler_options,
1060 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001061 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001062 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001063 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001064 move_resolver_(graph->GetAllocator(), this),
1065 assembler_(graph->GetAllocator()),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001066 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1067 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1068 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1069 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko8f63f102020-09-28 12:10:28 +01001070 public_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1071 package_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001072 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001073 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoeb9eb002020-10-02 13:54:19 +01001074 boot_image_jni_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +01001075 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001076 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1077 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001078 constant_area_start_(-1),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001079 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001080 method_address_offset_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001081 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001082 // Use a fake return address register to mimic Quick.
1083 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001084}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001085
David Brazdil58282f42016-01-14 12:45:10 +00001086void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001087 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001088 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001089}
1090
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001091InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001092 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001093 assembler_(codegen->GetAssembler()),
1094 codegen_(codegen) {}
1095
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001096static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001097 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001098}
1099
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001100void CodeGeneratorX86::MaybeIncrementHotness(bool is_frame_entry) {
1101 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1102 Register reg = EAX;
1103 if (is_frame_entry) {
1104 reg = kMethodRegisterArgument;
1105 } else {
1106 __ pushl(EAX);
Vladimir Markodec78172020-06-19 15:31:23 +01001107 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001108 __ movl(EAX, Address(ESP, kX86WordSize));
1109 }
1110 NearLabel overflow;
1111 __ cmpw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()),
1112 Immediate(ArtMethod::MaxCounter()));
1113 __ j(kEqual, &overflow);
1114 __ addw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()),
1115 Immediate(1));
1116 __ Bind(&overflow);
1117 if (!is_frame_entry) {
1118 __ popl(EAX);
Vladimir Markodec78172020-06-19 15:31:23 +01001119 __ cfi().AdjustCFAOffset(-4);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001120 }
1121 }
1122
1123 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffray095dc462020-08-17 16:40:28 +01001124 ScopedProfilingInfoUse spiu(
1125 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
1126 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001127 if (info != nullptr) {
1128 uint32_t address = reinterpret_cast32<uint32_t>(info);
1129 NearLabel done;
1130 if (HasEmptyFrame()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001131 CHECK(is_frame_entry);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001132 // Alignment
Vladimir Markodec78172020-06-19 15:31:23 +01001133 IncreaseFrame(8);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001134 // We need a temporary. The stub also expects the method at bottom of stack.
1135 __ pushl(EAX);
1136 __ cfi().AdjustCFAOffset(4);
1137 __ movl(EAX, Immediate(address));
1138 __ addw(Address(EAX, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()),
1139 Immediate(1));
Nicolas Geoffray8b8d93d2020-09-17 14:30:01 +01001140 __ andw(Address(EAX, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()),
1141 Immediate(interpreter::kTieredHotnessMask));
1142 __ j(kNotZero, &done);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001143 GenerateInvokeRuntime(
1144 GetThreadOffset<kX86PointerSize>(kQuickCompileOptimized).Int32Value());
1145 __ Bind(&done);
1146 // We don't strictly require to restore EAX, but this makes the generated
1147 // code easier to reason about.
1148 __ popl(EAX);
1149 __ cfi().AdjustCFAOffset(-4);
Vladimir Markodec78172020-06-19 15:31:23 +01001150 DecreaseFrame(8);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001151 } else {
1152 if (!RequiresCurrentMethod()) {
1153 CHECK(is_frame_entry);
1154 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1155 }
1156 // We need a temporary.
1157 __ pushl(EAX);
1158 __ cfi().AdjustCFAOffset(4);
1159 __ movl(EAX, Immediate(address));
1160 __ addw(Address(EAX, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()),
1161 Immediate(1));
1162 __ popl(EAX); // Put stack as expected before exiting or calling stub.
1163 __ cfi().AdjustCFAOffset(-4);
1164 __ j(kCarryClear, &done);
1165 GenerateInvokeRuntime(
1166 GetThreadOffset<kX86PointerSize>(kQuickCompileOptimized).Int32Value());
1167 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001168 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001169 }
1170 }
1171}
1172
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001173void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001174 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001175 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001176 bool skip_overflow_check =
1177 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001178 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001179
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001180 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001181 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86);
1182 __ testl(EAX, Address(ESP, -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001183 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001184 }
1185
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001186 if (!HasEmptyFrame()) {
1187 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1188 Register reg = kCoreCalleeSaves[i];
1189 if (allocated_registers_.ContainsCoreRegister(reg)) {
1190 __ pushl(reg);
1191 __ cfi().AdjustCFAOffset(kX86WordSize);
1192 __ cfi().RelOffset(DWARFReg(reg), 0);
1193 }
1194 }
Mark Mendell5f874182015-03-04 15:42:45 -05001195
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001196 int adjust = GetFrameSize() - FrameEntrySpillSize();
Vladimir Markodec78172020-06-19 15:31:23 +01001197 IncreaseFrame(adjust);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001198 // Save the current method if we need it. Note that we do not
1199 // do this in HCurrentMethod, as the instruction might have been removed
1200 // in the SSA graph.
1201 if (RequiresCurrentMethod()) {
1202 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1203 }
1204
1205 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1206 // Initialize should_deoptimize flag to 0.
1207 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
Mark Mendell5f874182015-03-04 15:42:45 -05001208 }
1209 }
1210
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001211 MaybeIncrementHotness(/* is_frame_entry= */ true);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001212}
1213
1214void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001215 __ cfi().RememberState();
1216 if (!HasEmptyFrame()) {
1217 int adjust = GetFrameSize() - FrameEntrySpillSize();
Vladimir Markodec78172020-06-19 15:31:23 +01001218 DecreaseFrame(adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001219
David Srbeckyc34dc932015-04-12 09:27:43 +01001220 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1221 Register reg = kCoreCalleeSaves[i];
1222 if (allocated_registers_.ContainsCoreRegister(reg)) {
1223 __ popl(reg);
1224 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1225 __ cfi().Restore(DWARFReg(reg));
1226 }
Mark Mendell5f874182015-03-04 15:42:45 -05001227 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001228 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001229 __ ret();
1230 __ cfi().RestoreState();
1231 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001232}
1233
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001234void CodeGeneratorX86::Bind(HBasicBlock* block) {
1235 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001236}
1237
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001238Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001239 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001240 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001241 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001242 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001243 case DataType::Type::kInt8:
1244 case DataType::Type::kUint16:
1245 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08001246 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001247 case DataType::Type::kInt32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001248 return Location::RegisterLocation(EAX);
1249
Aart Bik66c158e2018-01-31 12:55:04 -08001250 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001251 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001252 return Location::RegisterPairLocation(EAX, EDX);
1253
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001254 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001255 return Location::NoLocation();
1256
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001257 case DataType::Type::kFloat64:
1258 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001259 return Location::FpuRegisterLocation(XMM0);
1260 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001261
1262 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001263}
1264
1265Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1266 return Location::RegisterLocation(kMethodRegisterArgument);
1267}
1268
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001269Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001270 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001271 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001272 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001273 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001274 case DataType::Type::kInt8:
1275 case DataType::Type::kUint16:
1276 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001277 case DataType::Type::kInt32: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001278 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001279 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001280 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001281 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001282 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001283 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001284 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001285 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001286
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001287 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001288 uint32_t index = gp_index_;
1289 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001290 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001291 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001292 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1293 calling_convention.GetRegisterPairAt(index));
1294 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001295 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001296 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1297 }
1298 }
1299
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001300 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001301 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001302 stack_index_++;
1303 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1304 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1305 } else {
1306 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1307 }
1308 }
1309
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001310 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001311 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001312 stack_index_ += 2;
1313 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1314 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1315 } else {
1316 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001317 }
1318 }
1319
Aart Bik66c158e2018-01-31 12:55:04 -08001320 case DataType::Type::kUint32:
1321 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001322 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001323 LOG(FATAL) << "Unexpected parameter type " << type;
Elliott Hughesc1896c92018-11-29 11:33:18 -08001324 UNREACHABLE();
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001325 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001326 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001327}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001328
Vladimir Marko86c87522020-05-11 16:55:55 +01001329Location CriticalNativeCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
1330 DCHECK_NE(type, DataType::Type::kReference);
1331
1332 Location location;
1333 if (DataType::Is64BitType(type)) {
1334 location = Location::DoubleStackSlot(stack_offset_);
1335 stack_offset_ += 2 * kFramePointerSize;
1336 } else {
1337 location = Location::StackSlot(stack_offset_);
1338 stack_offset_ += kFramePointerSize;
1339 }
1340 if (for_register_allocation_) {
1341 location = Location::Any();
1342 }
1343 return location;
1344}
1345
1346Location CriticalNativeCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
1347 // We perform conversion to the managed ABI return register after the call if needed.
1348 InvokeDexCallingConventionVisitorX86 dex_calling_convention;
1349 return dex_calling_convention.GetReturnLocation(type);
1350}
1351
1352Location CriticalNativeCallingConventionVisitorX86::GetMethodLocation() const {
1353 // Pass the method in the hidden argument EAX.
1354 return Location::RegisterLocation(EAX);
1355}
1356
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001357void CodeGeneratorX86::Move32(Location destination, Location source) {
1358 if (source.Equals(destination)) {
1359 return;
1360 }
1361 if (destination.IsRegister()) {
1362 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001363 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001364 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001365 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Andra Danciu5e13d452020-09-08 14:35:09 +00001366 } else if (source.IsConstant()) {
1367 int32_t value = GetInt32ValueOf(source.GetConstant());
1368 __ movl(destination.AsRegister<Register>(), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001369 } else {
1370 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001371 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001372 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001373 } else if (destination.IsFpuRegister()) {
1374 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001375 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001376 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001377 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001378 } else {
1379 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001380 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001381 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001382 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001383 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001384 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001385 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001386 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001387 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001388 } else if (source.IsConstant()) {
1389 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001390 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001391 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001392 } else {
1393 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001394 __ pushl(Address(ESP, source.GetStackIndex()));
1395 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001396 }
1397 }
1398}
1399
1400void CodeGeneratorX86::Move64(Location destination, Location source) {
1401 if (source.Equals(destination)) {
1402 return;
1403 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001404 if (destination.IsRegisterPair()) {
1405 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001406 EmitParallelMoves(
1407 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1408 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001409 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001410 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001411 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001412 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001413 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001414 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1415 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1416 __ psrlq(src_reg, Immediate(32));
1417 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001418 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001419 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001420 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001421 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1422 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001423 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1424 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001425 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001426 if (source.IsFpuRegister()) {
1427 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1428 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001429 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001430 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001431 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Vladimir Markodec78172020-06-19 15:31:23 +01001432 // Push the 2 source registers to the stack.
1433 __ pushl(source.AsRegisterPairHigh<Register>());
1434 __ cfi().AdjustCFAOffset(elem_size);
1435 __ pushl(source.AsRegisterPairLow<Register>());
1436 __ cfi().AdjustCFAOffset(elem_size);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001437 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1438 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01001439 DecreaseFrame(2 * elem_size);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001440 } else {
1441 LOG(FATAL) << "Unimplemented";
1442 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001443 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001444 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001445 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001446 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001447 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001448 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001449 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001450 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001451 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001452 } else if (source.IsConstant()) {
1453 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001454 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1455 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001456 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001457 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1458 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001459 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001460 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001461 EmitParallelMoves(
1462 Location::StackSlot(source.GetStackIndex()),
1463 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001464 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001465 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001466 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001467 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001468 }
1469 }
1470}
1471
Andra Danciu1ca6f322020-08-12 08:58:07 +00001472static Address CreateAddress(Register base,
1473 Register index = Register::kNoRegister,
1474 ScaleFactor scale = TIMES_1,
1475 int32_t disp = 0) {
1476 if (index == Register::kNoRegister) {
1477 return Address(base, disp);
1478 }
1479
1480 return Address(base, index, scale, disp);
1481}
1482
Andra Danciud0f71f22020-09-17 09:00:15 +00001483void CodeGeneratorX86::LoadFromMemoryNoBarrier(DataType::Type dst_type,
1484 Location dst,
1485 Address src,
Ulya Trafimovich322eced2021-06-02 15:39:36 +01001486 HInstruction* instr,
Andra Danciud0f71f22020-09-17 09:00:15 +00001487 XmmRegister temp,
1488 bool is_atomic_load) {
Andra Danciu1ca6f322020-08-12 08:58:07 +00001489 switch (dst_type) {
1490 case DataType::Type::kBool:
1491 case DataType::Type::kUint8:
1492 __ movzxb(dst.AsRegister<Register>(), src);
1493 break;
1494 case DataType::Type::kInt8:
1495 __ movsxb(dst.AsRegister<Register>(), src);
1496 break;
1497 case DataType::Type::kInt16:
1498 __ movsxw(dst.AsRegister<Register>(), src);
1499 break;
1500 case DataType::Type::kUint16:
1501 __ movzxw(dst.AsRegister<Register>(), src);
1502 break;
1503 case DataType::Type::kInt32:
Andra Danciu1ca6f322020-08-12 08:58:07 +00001504 __ movl(dst.AsRegister<Register>(), src);
1505 break;
Andra Danciud0f71f22020-09-17 09:00:15 +00001506 case DataType::Type::kInt64: {
1507 if (is_atomic_load) {
1508 __ movsd(temp, src);
Ulya Trafimovich322eced2021-06-02 15:39:36 +01001509 if (instr != nullptr) {
1510 MaybeRecordImplicitNullCheck(instr);
1511 }
Andra Danciud0f71f22020-09-17 09:00:15 +00001512 __ movd(dst.AsRegisterPairLow<Register>(), temp);
1513 __ psrlq(temp, Immediate(32));
1514 __ movd(dst.AsRegisterPairHigh<Register>(), temp);
1515 } else {
1516 DCHECK_NE(src.GetBaseRegister(), dst.AsRegisterPairLow<Register>());
Ulya Trafimovich893e2ed2021-06-10 16:18:12 +01001517 Address src_high = Address::displace(src, kX86WordSize);
Andra Danciud0f71f22020-09-17 09:00:15 +00001518 __ movl(dst.AsRegisterPairLow<Register>(), src);
Ulya Trafimovich322eced2021-06-02 15:39:36 +01001519 if (instr != nullptr) {
1520 MaybeRecordImplicitNullCheck(instr);
1521 }
Andra Danciud0f71f22020-09-17 09:00:15 +00001522 __ movl(dst.AsRegisterPairHigh<Register>(), src_high);
1523 }
Andra Danciu1ca6f322020-08-12 08:58:07 +00001524 break;
1525 }
1526 case DataType::Type::kFloat32:
1527 __ movss(dst.AsFpuRegister<XmmRegister>(), src);
1528 break;
1529 case DataType::Type::kFloat64:
1530 __ movsd(dst.AsFpuRegister<XmmRegister>(), src);
1531 break;
Andra Danciu1ca6f322020-08-12 08:58:07 +00001532 case DataType::Type::kReference:
Ulya Trafimovich322eced2021-06-02 15:39:36 +01001533 DCHECK(!kEmitCompilerReadBarrier);
Andra Danciud0f71f22020-09-17 09:00:15 +00001534 __ movl(dst.AsRegister<Register>(), src);
1535 __ MaybeUnpoisonHeapReference(dst.AsRegister<Register>());
1536 break;
1537 default:
Andra Danciu1ca6f322020-08-12 08:58:07 +00001538 LOG(FATAL) << "Unreachable type " << dst_type;
1539 }
Ulya Trafimovich322eced2021-06-02 15:39:36 +01001540 if (instr != nullptr && dst_type != DataType::Type::kInt64) {
1541 // kInt64 needs special handling that is done in the above switch.
1542 MaybeRecordImplicitNullCheck(instr);
1543 }
Andra Danciu1ca6f322020-08-12 08:58:07 +00001544}
1545
Andra Danciu73c31802020-09-01 13:17:05 +00001546void CodeGeneratorX86::MoveToMemory(DataType::Type src_type,
1547 Location src,
1548 Register dst_base,
1549 Register dst_index,
1550 ScaleFactor dst_scale,
1551 int32_t dst_disp) {
1552 DCHECK(dst_base != Register::kNoRegister);
1553 Address dst = CreateAddress(dst_base, dst_index, dst_scale, dst_disp);
1554
1555 switch (src_type) {
1556 case DataType::Type::kBool:
1557 case DataType::Type::kUint8:
1558 case DataType::Type::kInt8: {
1559 if (src.IsConstant()) {
1560 __ movb(dst, Immediate(CodeGenerator::GetInt8ValueOf(src.GetConstant())));
1561 } else {
1562 __ movb(dst, src.AsRegister<ByteRegister>());
1563 }
1564 break;
1565 }
1566 case DataType::Type::kUint16:
1567 case DataType::Type::kInt16: {
1568 if (src.IsConstant()) {
1569 __ movw(dst, Immediate(CodeGenerator::GetInt16ValueOf(src.GetConstant())));
1570 } else {
1571 __ movw(dst, src.AsRegister<Register>());
1572 }
1573 break;
1574 }
1575 case DataType::Type::kUint32:
1576 case DataType::Type::kInt32: {
1577 if (src.IsConstant()) {
1578 int32_t v = CodeGenerator::GetInt32ValueOf(src.GetConstant());
1579 __ movl(dst, Immediate(v));
1580 } else {
1581 __ movl(dst, src.AsRegister<Register>());
1582 }
1583 break;
1584 }
1585 case DataType::Type::kUint64:
1586 case DataType::Type::kInt64: {
1587 Address dst_next_4_bytes = CreateAddress(dst_base, dst_index, dst_scale, dst_disp + 4);
1588 if (src.IsConstant()) {
1589 int64_t v = CodeGenerator::GetInt64ValueOf(src.GetConstant());
1590 __ movl(dst, Immediate(Low32Bits(v)));
1591 __ movl(dst_next_4_bytes, Immediate(High32Bits(v)));
1592 } else {
1593 __ movl(dst, src.AsRegisterPairLow<Register>());
1594 __ movl(dst_next_4_bytes, src.AsRegisterPairHigh<Register>());
1595 }
1596 break;
1597 }
1598 case DataType::Type::kFloat32: {
1599 if (src.IsConstant()) {
1600 int32_t v = CodeGenerator::GetInt32ValueOf(src.GetConstant());
1601 __ movl(dst, Immediate(v));
1602 } else {
1603 __ movss(dst, src.AsFpuRegister<XmmRegister>());
1604 }
1605 break;
1606 }
1607 case DataType::Type::kFloat64: {
1608 Address dst_next_4_bytes = CreateAddress(dst_base, dst_index, dst_scale, dst_disp + 4);
1609 if (src.IsConstant()) {
1610 int64_t v = CodeGenerator::GetInt64ValueOf(src.GetConstant());
1611 __ movl(dst, Immediate(Low32Bits(v)));
1612 __ movl(dst_next_4_bytes, Immediate(High32Bits(v)));
1613 } else {
1614 __ movsd(dst, src.AsFpuRegister<XmmRegister>());
1615 }
1616 break;
1617 }
1618 case DataType::Type::kVoid:
1619 case DataType::Type::kReference:
1620 LOG(FATAL) << "Unreachable type " << src_type;
1621 }
1622}
1623
Calin Juravle175dc732015-08-25 15:42:32 +01001624void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1625 DCHECK(location.IsRegister());
1626 __ movl(location.AsRegister<Register>(), Immediate(value));
1627}
1628
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001629void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001630 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001631 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1632 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1633 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001634 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001635 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001636 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001637 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001638}
1639
1640void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1641 if (location.IsRegister()) {
1642 locations->AddTemp(location);
1643 } else if (location.IsRegisterPair()) {
1644 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1645 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1646 } else {
1647 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1648 }
1649}
1650
David Brazdilfc6a86a2015-06-26 10:33:45 +00001651void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001652 if (successor->IsExitBlock()) {
1653 DCHECK(got->GetPrevious()->AlwaysThrows());
1654 return; // no code needed
1655 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001656
1657 HBasicBlock* block = got->GetBlock();
1658 HInstruction* previous = got->GetPrevious();
1659
1660 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001661 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001662 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001663 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1664 return;
1665 }
1666
1667 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1668 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1669 }
1670 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001671 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001672 }
1673}
1674
David Brazdilfc6a86a2015-06-26 10:33:45 +00001675void LocationsBuilderX86::VisitGoto(HGoto* got) {
1676 got->SetLocations(nullptr);
1677}
1678
1679void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1680 HandleGoto(got, got->GetSuccessor());
1681}
1682
1683void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1684 try_boundary->SetLocations(nullptr);
1685}
1686
1687void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1688 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1689 if (!successor->IsExitBlock()) {
1690 HandleGoto(try_boundary, successor);
1691 }
1692}
1693
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001694void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001695 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001696}
1697
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001698void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001699}
1700
Mark Mendell152408f2015-12-31 12:28:50 -05001701template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001702void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001703 LabelType* true_label,
1704 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001705 if (cond->IsFPConditionTrueIfNaN()) {
1706 __ j(kUnordered, true_label);
1707 } else if (cond->IsFPConditionFalseIfNaN()) {
1708 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001709 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001710 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001711}
1712
Mark Mendell152408f2015-12-31 12:28:50 -05001713template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001714void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001715 LabelType* true_label,
1716 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001717 LocationSummary* locations = cond->GetLocations();
1718 Location left = locations->InAt(0);
1719 Location right = locations->InAt(1);
1720 IfCondition if_cond = cond->GetCondition();
1721
Mark Mendellc4701932015-04-10 13:18:51 -04001722 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001723 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001724 IfCondition true_high_cond = if_cond;
1725 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001726 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001727
1728 // Set the conditions for the test, remembering that == needs to be
1729 // decided using the low words.
1730 switch (if_cond) {
1731 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001732 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001733 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001734 break;
1735 case kCondLT:
1736 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001737 break;
1738 case kCondLE:
1739 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001740 break;
1741 case kCondGT:
1742 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001743 break;
1744 case kCondGE:
1745 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001746 break;
Aart Bike9f37602015-10-09 11:15:55 -07001747 case kCondB:
1748 false_high_cond = kCondA;
1749 break;
1750 case kCondBE:
1751 true_high_cond = kCondB;
1752 break;
1753 case kCondA:
1754 false_high_cond = kCondB;
1755 break;
1756 case kCondAE:
1757 true_high_cond = kCondA;
1758 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001759 }
1760
1761 if (right.IsConstant()) {
1762 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001763 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001764 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001765
Aart Bika19616e2016-02-01 18:57:58 -08001766 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001767 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001768 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001769 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001770 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001771 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001772 __ j(X86Condition(true_high_cond), true_label);
1773 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001774 }
1775 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001776 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001777 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001778 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001779 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001780
1781 __ cmpl(left_high, right_high);
1782 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001783 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001784 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001785 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001786 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001787 __ j(X86Condition(true_high_cond), true_label);
1788 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001789 }
1790 // Must be equal high, so compare the lows.
1791 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001792 } else {
1793 DCHECK(right.IsDoubleStackSlot());
1794 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1795 if (if_cond == kCondNE) {
1796 __ j(X86Condition(true_high_cond), true_label);
1797 } else if (if_cond == kCondEQ) {
1798 __ j(X86Condition(false_high_cond), false_label);
1799 } else {
1800 __ j(X86Condition(true_high_cond), true_label);
1801 __ j(X86Condition(false_high_cond), false_label);
1802 }
1803 // Must be equal high, so compare the lows.
1804 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001805 }
1806 // The last comparison might be unsigned.
1807 __ j(final_condition, true_label);
1808}
1809
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001810void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1811 Location rhs,
1812 HInstruction* insn,
1813 bool is_double) {
1814 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1815 if (is_double) {
1816 if (rhs.IsFpuRegister()) {
1817 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1818 } else if (const_area != nullptr) {
1819 DCHECK(const_area->IsEmittedAtUseSite());
1820 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1821 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001822 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1823 const_area->GetBaseMethodAddress(),
1824 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001825 } else {
1826 DCHECK(rhs.IsDoubleStackSlot());
1827 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1828 }
1829 } else {
1830 if (rhs.IsFpuRegister()) {
1831 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1832 } else if (const_area != nullptr) {
1833 DCHECK(const_area->IsEmittedAtUseSite());
1834 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1835 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001836 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1837 const_area->GetBaseMethodAddress(),
1838 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001839 } else {
1840 DCHECK(rhs.IsStackSlot());
1841 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1842 }
1843 }
1844}
1845
Mark Mendell152408f2015-12-31 12:28:50 -05001846template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001847void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001848 LabelType* true_target_in,
1849 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001850 // Generated branching requires both targets to be explicit. If either of the
1851 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001852 LabelType fallthrough_target;
1853 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1854 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001855
Mark Mendellc4701932015-04-10 13:18:51 -04001856 LocationSummary* locations = condition->GetLocations();
1857 Location left = locations->InAt(0);
1858 Location right = locations->InAt(1);
1859
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001860 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001861 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001862 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001863 GenerateLongComparesAndJumps(condition, true_target, false_target);
1864 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001865 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001866 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001867 GenerateFPJumps(condition, true_target, false_target);
1868 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001869 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001870 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001871 GenerateFPJumps(condition, true_target, false_target);
1872 break;
1873 default:
1874 LOG(FATAL) << "Unexpected compare type " << type;
1875 }
1876
David Brazdil0debae72015-11-12 18:37:00 +00001877 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001878 __ jmp(false_target);
1879 }
David Brazdil0debae72015-11-12 18:37:00 +00001880
1881 if (fallthrough_target.IsLinked()) {
1882 __ Bind(&fallthrough_target);
1883 }
Mark Mendellc4701932015-04-10 13:18:51 -04001884}
1885
David Brazdil0debae72015-11-12 18:37:00 +00001886static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1887 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1888 // are set only strictly before `branch`. We can't use the eflags on long/FP
1889 // conditions if they are materialized due to the complex branching.
1890 return cond->IsCondition() &&
1891 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001892 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1893 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001894}
1895
Mark Mendell152408f2015-12-31 12:28:50 -05001896template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001897void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001898 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001899 LabelType* true_target,
1900 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001901 HInstruction* cond = instruction->InputAt(condition_input_index);
1902
1903 if (true_target == nullptr && false_target == nullptr) {
1904 // Nothing to do. The code always falls through.
1905 return;
1906 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001907 // Constant condition, statically compared against "true" (integer value 1).
1908 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001909 if (true_target != nullptr) {
1910 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001911 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001912 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001913 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001914 if (false_target != nullptr) {
1915 __ jmp(false_target);
1916 }
1917 }
1918 return;
1919 }
1920
1921 // The following code generates these patterns:
1922 // (1) true_target == nullptr && false_target != nullptr
1923 // - opposite condition true => branch to false_target
1924 // (2) true_target != nullptr && false_target == nullptr
1925 // - condition true => branch to true_target
1926 // (3) true_target != nullptr && false_target != nullptr
1927 // - condition true => branch to true_target
1928 // - branch to false_target
1929 if (IsBooleanValueOrMaterializedCondition(cond)) {
1930 if (AreEflagsSetFrom(cond, instruction)) {
1931 if (true_target == nullptr) {
1932 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1933 } else {
1934 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1935 }
1936 } else {
1937 // Materialized condition, compare against 0.
1938 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1939 if (lhs.IsRegister()) {
1940 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1941 } else {
1942 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1943 }
1944 if (true_target == nullptr) {
1945 __ j(kEqual, false_target);
1946 } else {
1947 __ j(kNotEqual, true_target);
1948 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001949 }
1950 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001951 // Condition has not been materialized, use its inputs as the comparison and
1952 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001953 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001954
1955 // If this is a long or FP comparison that has been folded into
1956 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001957 DataType::Type type = condition->InputAt(0)->GetType();
1958 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001959 GenerateCompareTestAndBranch(condition, true_target, false_target);
1960 return;
1961 }
1962
1963 Location lhs = condition->GetLocations()->InAt(0);
1964 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001965 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001966 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001967 if (true_target == nullptr) {
1968 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1969 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001970 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001971 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001972 }
David Brazdil0debae72015-11-12 18:37:00 +00001973
1974 // If neither branch falls through (case 3), the conditional branch to `true_target`
1975 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1976 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001977 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001978 }
1979}
1980
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001981void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001982 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001983 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001984 locations->SetInAt(0, Location::Any());
1985 }
1986}
1987
1988void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001989 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1990 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1991 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1992 nullptr : codegen_->GetLabelOf(true_successor);
1993 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1994 nullptr : codegen_->GetLabelOf(false_successor);
Andreas Gampe3db70682018-12-26 15:12:03 -08001995 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001996}
1997
1998void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001999 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002000 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01002001 InvokeRuntimeCallingConvention calling_convention;
2002 RegisterSet caller_saves = RegisterSet::Empty();
2003 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2004 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00002005 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002006 locations->SetInAt(0, Location::Any());
2007 }
2008}
2009
2010void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08002011 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00002012 GenerateTestAndBranch<Label>(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08002013 /* condition_input_index= */ 0,
David Brazdil74eb1b22015-12-14 11:44:01 +00002014 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08002015 /* false_target= */ nullptr);
David Brazdil74eb1b22015-12-14 11:44:01 +00002016}
2017
Mingyao Yang063fc772016-08-02 11:02:54 -07002018void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002019 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07002020 LocationSummary(flag, LocationSummary::kNoCall);
2021 locations->SetOut(Location::RequiresRegister());
2022}
2023
2024void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2025 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
2026 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
2027}
2028
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002029static bool SelectCanUseCMOV(HSelect* select) {
2030 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002031 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002032 return false;
2033 }
2034
2035 // A FP condition doesn't generate the single CC that we need.
2036 // In 32 bit mode, a long condition doesn't generate a single CC either.
2037 HInstruction* condition = select->GetCondition();
2038 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002039 DataType::Type compare_type = condition->InputAt(0)->GetType();
2040 if (compare_type == DataType::Type::kInt64 ||
2041 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002042 return false;
2043 }
2044 }
2045
2046 // We can generate a CMOV for this Select.
2047 return true;
2048}
2049
David Brazdil74eb1b22015-12-14 11:44:01 +00002050void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002051 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002052 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00002053 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002054 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00002055 } else {
2056 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002057 if (SelectCanUseCMOV(select)) {
2058 if (select->InputAt(1)->IsConstant()) {
2059 // Cmov can't handle a constant value.
2060 locations->SetInAt(1, Location::RequiresRegister());
2061 } else {
2062 locations->SetInAt(1, Location::Any());
2063 }
2064 } else {
2065 locations->SetInAt(1, Location::Any());
2066 }
David Brazdil74eb1b22015-12-14 11:44:01 +00002067 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002068 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2069 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00002070 }
2071 locations->SetOut(Location::SameAsFirstInput());
2072}
2073
2074void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
2075 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002076 DCHECK(locations->InAt(0).Equals(locations->Out()));
2077 if (SelectCanUseCMOV(select)) {
2078 // If both the condition and the source types are integer, we can generate
2079 // a CMOV to implement Select.
2080
2081 HInstruction* select_condition = select->GetCondition();
2082 Condition cond = kNotEqual;
2083
2084 // Figure out how to test the 'condition'.
2085 if (select_condition->IsCondition()) {
2086 HCondition* condition = select_condition->AsCondition();
2087 if (!condition->IsEmittedAtUseSite()) {
2088 // This was a previously materialized condition.
2089 // Can we use the existing condition code?
2090 if (AreEflagsSetFrom(condition, select)) {
2091 // Materialization was the previous instruction. Condition codes are right.
2092 cond = X86Condition(condition->GetCondition());
2093 } else {
2094 // No, we have to recreate the condition code.
2095 Register cond_reg = locations->InAt(2).AsRegister<Register>();
2096 __ testl(cond_reg, cond_reg);
2097 }
2098 } else {
2099 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002100 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
2101 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002102 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01002103 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002104 cond = X86Condition(condition->GetCondition());
2105 }
2106 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002107 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002108 Register cond_reg = locations->InAt(2).AsRegister<Register>();
2109 __ testl(cond_reg, cond_reg);
2110 }
2111
2112 // If the condition is true, overwrite the output, which already contains false.
2113 Location false_loc = locations->InAt(0);
2114 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002115 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002116 // 64 bit conditional move.
2117 Register false_high = false_loc.AsRegisterPairHigh<Register>();
2118 Register false_low = false_loc.AsRegisterPairLow<Register>();
2119 if (true_loc.IsRegisterPair()) {
2120 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
2121 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
2122 } else {
2123 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
2124 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
2125 }
2126 } else {
2127 // 32 bit conditional move.
2128 Register false_reg = false_loc.AsRegister<Register>();
2129 if (true_loc.IsRegister()) {
2130 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
2131 } else {
2132 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
2133 }
2134 }
2135 } else {
2136 NearLabel false_target;
2137 GenerateTestAndBranch<NearLabel>(
Andreas Gampe3db70682018-12-26 15:12:03 -08002138 select, /* condition_input_index= */ 2, /* true_target= */ nullptr, &false_target);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002139 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2140 __ Bind(&false_target);
2141 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002142}
2143
David Srbecky0cf44932015-12-09 14:09:59 +00002144void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002145 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00002146}
2147
David Srbeckyd28f4a02016-03-14 17:14:24 +00002148void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
2149 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002150}
2151
Vladimir Markodec78172020-06-19 15:31:23 +01002152void CodeGeneratorX86::IncreaseFrame(size_t adjustment) {
2153 __ subl(ESP, Immediate(adjustment));
2154 __ cfi().AdjustCFAOffset(adjustment);
2155}
2156
2157void CodeGeneratorX86::DecreaseFrame(size_t adjustment) {
2158 __ addl(ESP, Immediate(adjustment));
2159 __ cfi().AdjustCFAOffset(-adjustment);
2160}
2161
David Srbeckyc7098ff2016-02-09 14:30:11 +00002162void CodeGeneratorX86::GenerateNop() {
2163 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002164}
2165
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002166void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002167 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002168 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04002169 // Handle the long/FP comparisons made in instruction simplification.
2170 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002171 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04002172 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05002173 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00002174 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002175 locations->SetOut(Location::RequiresRegister());
2176 }
2177 break;
2178 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002179 case DataType::Type::kFloat32:
2180 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04002181 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002182 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
2183 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
2184 } else if (cond->InputAt(1)->IsConstant()) {
2185 locations->SetInAt(1, Location::RequiresFpuRegister());
2186 } else {
2187 locations->SetInAt(1, Location::Any());
2188 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002189 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002190 locations->SetOut(Location::RequiresRegister());
2191 }
2192 break;
2193 }
2194 default:
2195 locations->SetInAt(0, Location::RequiresRegister());
2196 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00002197 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002198 // We need a byte register.
2199 locations->SetOut(Location::RegisterLocation(ECX));
2200 }
2201 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002202 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002203}
2204
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002205void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002206 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002207 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002208 }
Mark Mendellc4701932015-04-10 13:18:51 -04002209
2210 LocationSummary* locations = cond->GetLocations();
2211 Location lhs = locations->InAt(0);
2212 Location rhs = locations->InAt(1);
2213 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05002214 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002215
2216 switch (cond->InputAt(0)->GetType()) {
2217 default: {
2218 // Integer case.
2219
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002220 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04002221 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01002222 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07002223 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04002224 return;
2225 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002226 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04002227 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2228 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002229 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002230 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04002231 GenerateFPJumps(cond, &true_label, &false_label);
2232 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002233 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002234 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04002235 GenerateFPJumps(cond, &true_label, &false_label);
2236 break;
2237 }
2238
2239 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002240 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002241
Roland Levillain4fa13f62015-07-06 18:11:54 +01002242 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04002243 __ Bind(&false_label);
2244 __ xorl(reg, reg);
2245 __ jmp(&done_label);
2246
Roland Levillain4fa13f62015-07-06 18:11:54 +01002247 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04002248 __ Bind(&true_label);
2249 __ movl(reg, Immediate(1));
2250 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002251}
2252
2253void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002254 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002255}
2256
2257void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002258 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002259}
2260
2261void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002262 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002263}
2264
2265void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002266 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002267}
2268
2269void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002270 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002271}
2272
2273void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002274 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002275}
2276
2277void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002278 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002279}
2280
2281void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002282 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002283}
2284
2285void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002286 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002287}
2288
2289void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002290 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002291}
2292
2293void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002294 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002295}
2296
2297void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002298 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002299}
2300
Aart Bike9f37602015-10-09 11:15:55 -07002301void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002302 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002303}
2304
2305void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002306 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002307}
2308
2309void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002310 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002311}
2312
2313void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002314 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002315}
2316
2317void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002318 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002319}
2320
2321void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002322 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002323}
2324
2325void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002326 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002327}
2328
2329void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002330 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002331}
2332
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002333void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002334 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002335 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002336 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002337}
2338
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002339void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002340 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002341}
2342
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002343void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2344 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002345 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002346 locations->SetOut(Location::ConstantLocation(constant));
2347}
2348
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002349void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002350 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002351}
2352
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002353void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002354 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002355 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002356 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002357}
2358
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002359void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002360 // Will be generated at use site.
2361}
2362
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002363void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2364 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002365 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002366 locations->SetOut(Location::ConstantLocation(constant));
2367}
2368
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002369void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002370 // Will be generated at use site.
2371}
2372
2373void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2374 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002375 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002376 locations->SetOut(Location::ConstantLocation(constant));
2377}
2378
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002379void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002380 // Will be generated at use site.
2381}
2382
Igor Murashkind01745e2017-04-05 16:40:31 -07002383void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2384 constructor_fence->SetLocations(nullptr);
2385}
2386
2387void InstructionCodeGeneratorX86::VisitConstructorFence(
2388 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2389 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2390}
2391
Calin Juravle27df7582015-04-17 19:12:31 +01002392void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2393 memory_barrier->SetLocations(nullptr);
2394}
2395
2396void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002397 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002398}
2399
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002400void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002401 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002402}
2403
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002404void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002405 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002406}
2407
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002408void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002409 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002410 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002411 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002412 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002413 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002414 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002415 case DataType::Type::kInt8:
2416 case DataType::Type::kUint16:
2417 case DataType::Type::kInt16:
2418 case DataType::Type::kInt32:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002419 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002420 break;
2421
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002422 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002423 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002424 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002425 break;
2426
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002427 case DataType::Type::kFloat32:
2428 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002429 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002430 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002431 break;
2432
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002433 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002434 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002435 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002436}
2437
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002438void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002439 switch (ret->InputAt(0)->GetType()) {
2440 case DataType::Type::kReference:
2441 case DataType::Type::kBool:
2442 case DataType::Type::kUint8:
2443 case DataType::Type::kInt8:
2444 case DataType::Type::kUint16:
2445 case DataType::Type::kInt16:
2446 case DataType::Type::kInt32:
2447 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
2448 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002449
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002450 case DataType::Type::kInt64:
2451 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2452 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
2453 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002454
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002455 case DataType::Type::kFloat32:
2456 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2457 if (GetGraph()->IsCompilingOsr()) {
2458 // To simplify callers of an OSR method, we put the return value in both
2459 // floating point and core registers.
2460 __ movd(EAX, XMM0);
2461 }
2462 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002463
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002464 case DataType::Type::kFloat64:
2465 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2466 if (GetGraph()->IsCompilingOsr()) {
2467 // To simplify callers of an OSR method, we put the return value in both
2468 // floating point and core registers.
2469 __ movd(EAX, XMM0);
2470 // Use XMM1 as temporary register to not clobber XMM0.
2471 __ movaps(XMM1, XMM0);
2472 __ psrlq(XMM1, Immediate(32));
2473 __ movd(EDX, XMM1);
2474 }
2475 break;
2476
2477 default:
2478 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002479 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002480 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002481}
2482
Calin Juravle175dc732015-08-25 15:42:32 +01002483void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2484 // The trampoline uses the same calling convention as dex calling conventions,
2485 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2486 // the method_idx.
2487 HandleInvoke(invoke);
2488}
2489
2490void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2491 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2492}
2493
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002494void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002495 // Explicit clinit checks triggered by static invokes must have been pruned by
2496 // art::PrepareForRegisterAllocation.
2497 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002498
Mark Mendellfb8d2792015-03-31 22:16:59 -04002499 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002500 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002501 if (invoke->GetLocations()->CanCall() &&
2502 invoke->HasPcRelativeMethodLoadKind() &&
2503 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).IsInvalid()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002504 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002505 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002506 return;
2507 }
2508
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01002509 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
Vladimir Marko86c87522020-05-11 16:55:55 +01002510 CriticalNativeCallingConventionVisitorX86 calling_convention_visitor(
2511 /*for_register_allocation=*/ true);
2512 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2513 } else {
2514 HandleInvoke(invoke);
2515 }
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002516
Vladimir Marko86c87522020-05-11 16:55:55 +01002517 // For PC-relative load kinds the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002518 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002519 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002520 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002521}
2522
Mark Mendell09ed1a32015-03-25 08:30:06 -04002523static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2524 if (invoke->GetLocations()->Intrinsified()) {
2525 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2526 intrinsic.Dispatch(invoke);
2527 return true;
2528 }
2529 return false;
2530}
2531
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002532void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002533 // Explicit clinit checks triggered by static invokes must have been pruned by
2534 // art::PrepareForRegisterAllocation.
2535 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002536
Mark Mendell09ed1a32015-03-25 08:30:06 -04002537 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2538 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002539 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002540
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002541 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002542 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002543 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002544}
2545
2546void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002547 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2548 if (intrinsic.TryDispatch(invoke)) {
2549 return;
2550 }
2551
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002552 HandleInvoke(invoke);
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002553
2554 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002555 // Add one temporary for inline cache update.
2556 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2557 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002558}
2559
2560void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002561 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002562 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002563}
2564
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002565void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002566 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2567 return;
2568 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002569
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002570 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002571 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002572}
2573
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002574void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002575 // This call to HandleInvoke allocates a temporary (core) register
2576 // which is also used to transfer the hidden argument from FP to
2577 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002578 HandleInvoke(invoke);
2579 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002580 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002581
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002582 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002583 // Add one temporary for inline cache update.
2584 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2585 }
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01002586
2587 // For PC-relative load kinds the invoke has an extra input, the PC-relative address base.
2588 if (IsPcRelativeMethodLoadKind(invoke->GetHiddenArgumentLoadKind())) {
2589 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2590 }
2591
2592 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
2593 invoke->GetLocations()->SetInAt(invoke->GetNumberOfArguments() - 1,
2594 Location::RequiresRegister());
2595 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002596}
2597
2598void CodeGeneratorX86::MaybeGenerateInlineCacheCheck(HInstruction* instruction, Register klass) {
2599 DCHECK_EQ(EAX, klass);
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002600 // We know the destination of an intrinsic, so no need to record inline
2601 // caches (also the intrinsic location builder doesn't request an additional
2602 // temporary).
2603 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002604 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002605 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002606 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
Nicolas Geoffray095dc462020-08-17 16:40:28 +01002607 ScopedProfilingInfoUse spiu(
2608 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
2609 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002610 if (info != nullptr) {
2611 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
2612 uint32_t address = reinterpret_cast32<uint32_t>(cache);
2613 if (kIsDebugBuild) {
2614 uint32_t temp_index = instruction->GetLocations()->GetTempCount() - 1u;
2615 CHECK_EQ(EBP, instruction->GetLocations()->GetTemp(temp_index).AsRegister<Register>());
2616 }
2617 Register temp = EBP;
2618 NearLabel done;
2619 __ movl(temp, Immediate(address));
2620 // Fast path for a monomorphic cache.
2621 __ cmpl(klass, Address(temp, InlineCache::ClassesOffset().Int32Value()));
2622 __ j(kEqual, &done);
2623 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(kQuickUpdateInlineCache).Int32Value());
2624 __ Bind(&done);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002625 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002626 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002627}
2628
2629void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2630 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002631 LocationSummary* locations = invoke->GetLocations();
2632 Register temp = locations->GetTemp(0).AsRegister<Register>();
2633 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002634 Location receiver = locations->InAt(0);
2635 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2636
Roland Levillain0d5a2812015-11-13 10:07:31 +00002637 // Set the hidden argument. This is safe to do this here, as XMM7
2638 // won't be modified thereafter, before the `call` instruction.
2639 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01002640 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
2641 __ movd(hidden_reg, locations->InAt(invoke->GetNumberOfArguments() - 1).AsRegister<Register>());
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00002642 } else if (invoke->GetHiddenArgumentLoadKind() != MethodLoadKind::kRuntimeCall) {
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01002643 codegen_->LoadMethod(invoke->GetHiddenArgumentLoadKind(), locations->GetTemp(0), invoke);
2644 __ movd(hidden_reg, temp);
2645 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002646
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002647 if (receiver.IsStackSlot()) {
2648 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002649 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002650 __ movl(temp, Address(temp, class_offset));
2651 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002652 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002653 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002654 }
Roland Levillain4d027112015-07-01 15:41:14 +01002655 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002656 // Instead of simply (possibly) unpoisoning `temp` here, we should
2657 // emit a read barrier for the previous class reference load.
2658 // However this is not required in practice, as this is an
2659 // intermediate/temporary reference and because the current
2660 // concurrent copying collector keeps the from-space memory
2661 // intact/accessible until the end of the marking phase (the
2662 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002663 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002664
2665 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
2666
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002667 // temp = temp->GetAddressOfIMT()
2668 __ movl(temp,
2669 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002670 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002671 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002672 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002673 __ movl(temp, Address(temp, method_offset));
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00002674 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRuntimeCall) {
2675 // We pass the method from the IMT in case of a conflict. This will ensure
2676 // we go into the runtime to resolve the actual method.
2677 __ movd(hidden_reg, temp);
2678 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002679 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002680 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002681 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002682
2683 DCHECK(!codegen_->IsLeafMethod());
2684 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2685}
2686
Orion Hodsonac141392017-01-13 11:53:47 +00002687void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00002688 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2689 if (intrinsic.TryDispatch(invoke)) {
2690 return;
2691 }
Orion Hodsonac141392017-01-13 11:53:47 +00002692 HandleInvoke(invoke);
2693}
2694
2695void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00002696 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2697 return;
2698 }
Orion Hodsonac141392017-01-13 11:53:47 +00002699 codegen_->GenerateInvokePolymorphicCall(invoke);
2700}
2701
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002702void LocationsBuilderX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2703 HandleInvoke(invoke);
2704}
2705
2706void InstructionCodeGeneratorX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2707 codegen_->GenerateInvokeCustomCall(invoke);
2708}
2709
Roland Levillain88cb1752014-10-20 16:36:47 +01002710void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2711 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002712 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002713 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002714 case DataType::Type::kInt32:
2715 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002716 locations->SetInAt(0, Location::RequiresRegister());
2717 locations->SetOut(Location::SameAsFirstInput());
2718 break;
2719
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002720 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002721 locations->SetInAt(0, Location::RequiresFpuRegister());
2722 locations->SetOut(Location::SameAsFirstInput());
2723 locations->AddTemp(Location::RequiresRegister());
2724 locations->AddTemp(Location::RequiresFpuRegister());
2725 break;
2726
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002727 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002728 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002729 locations->SetOut(Location::SameAsFirstInput());
2730 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002731 break;
2732
2733 default:
2734 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2735 }
2736}
2737
2738void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2739 LocationSummary* locations = neg->GetLocations();
2740 Location out = locations->Out();
2741 Location in = locations->InAt(0);
2742 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002743 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002744 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002745 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002746 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002747 break;
2748
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002749 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002750 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002751 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002752 __ negl(out.AsRegisterPairLow<Register>());
2753 // Negation is similar to subtraction from zero. The least
2754 // significant byte triggers a borrow when it is different from
2755 // zero; to take it into account, add 1 to the most significant
2756 // byte if the carry flag (CF) is set to 1 after the first NEGL
2757 // operation.
2758 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2759 __ negl(out.AsRegisterPairHigh<Register>());
2760 break;
2761
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002762 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002763 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002764 Register constant = locations->GetTemp(0).AsRegister<Register>();
2765 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002766 // Implement float negation with an exclusive or with value
2767 // 0x80000000 (mask for bit 31, representing the sign of a
2768 // single-precision floating-point number).
2769 __ movl(constant, Immediate(INT32_C(0x80000000)));
2770 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002771 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002772 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002773 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002774
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002775 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002776 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002777 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002778 // Implement double negation with an exclusive or with value
2779 // 0x8000000000000000 (mask for bit 63, representing the sign of
2780 // a double-precision floating-point number).
2781 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002782 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002783 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002784 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002785
2786 default:
2787 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2788 }
2789}
2790
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002791void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2792 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002793 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002794 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002795 locations->SetInAt(0, Location::RequiresFpuRegister());
2796 locations->SetInAt(1, Location::RequiresRegister());
2797 locations->SetOut(Location::SameAsFirstInput());
2798 locations->AddTemp(Location::RequiresFpuRegister());
2799}
2800
2801void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2802 LocationSummary* locations = neg->GetLocations();
2803 Location out = locations->Out();
2804 DCHECK(locations->InAt(0).Equals(out));
2805
2806 Register constant_area = locations->InAt(1).AsRegister<Register>();
2807 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002808 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002809 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2810 neg->GetBaseMethodAddress(),
2811 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002812 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2813 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002814 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2815 neg->GetBaseMethodAddress(),
2816 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002817 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2818 }
2819}
2820
Roland Levillaindff1f282014-11-05 14:15:05 +00002821void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002822 DataType::Type result_type = conversion->GetResultType();
2823 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002824 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2825 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002826
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002827 // The float-to-long and double-to-long type conversions rely on a
2828 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002829 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002830 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2831 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002832 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002833 : LocationSummary::kNoCall;
2834 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002835 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002836
Roland Levillaindff1f282014-11-05 14:15:05 +00002837 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002838 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002839 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002840 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002841 case DataType::Type::kUint8:
2842 case DataType::Type::kInt8:
2843 case DataType::Type::kUint16:
2844 case DataType::Type::kInt16:
2845 case DataType::Type::kInt32:
2846 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2847 // Make the output overlap to please the register allocator. This greatly simplifies
2848 // the validation of the linear scan implementation
2849 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2850 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002851 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002852 HInstruction* input = conversion->InputAt(0);
2853 Location input_location = input->IsConstant()
2854 ? Location::ConstantLocation(input->AsConstant())
2855 : Location::RegisterPairLocation(EAX, EDX);
2856 locations->SetInAt(0, input_location);
2857 // Make the output overlap to please the register allocator. This greatly simplifies
2858 // the validation of the linear scan implementation
2859 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2860 break;
2861 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002862
2863 default:
2864 LOG(FATAL) << "Unexpected type conversion from " << input_type
2865 << " to " << result_type;
2866 }
2867 break;
2868
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002869 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002870 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002871 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2872 locations->SetInAt(0, Location::Any());
2873 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002874 break;
2875
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002876 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002877 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002878 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002879 locations->SetInAt(0, Location::Any());
2880 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2881 break;
2882
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002883 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002884 locations->SetInAt(0, Location::RequiresFpuRegister());
2885 locations->SetOut(Location::RequiresRegister());
2886 locations->AddTemp(Location::RequiresFpuRegister());
2887 break;
2888
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002889 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002890 locations->SetInAt(0, Location::RequiresFpuRegister());
2891 locations->SetOut(Location::RequiresRegister());
2892 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002893 break;
2894
2895 default:
2896 LOG(FATAL) << "Unexpected type conversion from " << input_type
2897 << " to " << result_type;
2898 }
2899 break;
2900
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002901 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002902 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002903 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002904 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002905 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002906 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002907 case DataType::Type::kInt16:
2908 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002909 locations->SetInAt(0, Location::RegisterLocation(EAX));
2910 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2911 break;
2912
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002913 case DataType::Type::kFloat32:
2914 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002915 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002916 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2917 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2918
Vladimir Marko949c91f2015-01-27 10:48:44 +00002919 // The runtime helper puts the result in EAX, EDX.
2920 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002921 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002922 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002923
2924 default:
2925 LOG(FATAL) << "Unexpected type conversion from " << input_type
2926 << " to " << result_type;
2927 }
2928 break;
2929
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002930 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002931 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002932 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002933 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002934 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002935 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002936 case DataType::Type::kInt16:
2937 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002938 locations->SetInAt(0, Location::RequiresRegister());
2939 locations->SetOut(Location::RequiresFpuRegister());
2940 break;
2941
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002942 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002943 locations->SetInAt(0, Location::Any());
2944 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002945 break;
2946
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002947 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002948 locations->SetInAt(0, Location::RequiresFpuRegister());
2949 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002950 break;
2951
2952 default:
2953 LOG(FATAL) << "Unexpected type conversion from " << input_type
2954 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002955 }
Roland Levillaincff13742014-11-17 14:32:17 +00002956 break;
2957
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002958 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002959 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002960 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002961 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002962 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002963 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002964 case DataType::Type::kInt16:
2965 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002966 locations->SetInAt(0, Location::RequiresRegister());
2967 locations->SetOut(Location::RequiresFpuRegister());
2968 break;
2969
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002970 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002971 locations->SetInAt(0, Location::Any());
2972 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002973 break;
2974
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002975 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002976 locations->SetInAt(0, Location::RequiresFpuRegister());
2977 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002978 break;
2979
2980 default:
2981 LOG(FATAL) << "Unexpected type conversion from " << input_type
2982 << " to " << result_type;
2983 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002984 break;
2985
2986 default:
2987 LOG(FATAL) << "Unexpected type conversion from " << input_type
2988 << " to " << result_type;
2989 }
2990}
2991
2992void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2993 LocationSummary* locations = conversion->GetLocations();
2994 Location out = locations->Out();
2995 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002996 DataType::Type result_type = conversion->GetResultType();
2997 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002998 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2999 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00003000 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003001 case DataType::Type::kUint8:
3002 switch (input_type) {
3003 case DataType::Type::kInt8:
3004 case DataType::Type::kUint16:
3005 case DataType::Type::kInt16:
3006 case DataType::Type::kInt32:
3007 if (in.IsRegister()) {
3008 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
3009 } else {
3010 DCHECK(in.GetConstant()->IsIntConstant());
3011 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
3012 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
3013 }
3014 break;
3015 case DataType::Type::kInt64:
3016 if (in.IsRegisterPair()) {
3017 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
3018 } else {
3019 DCHECK(in.GetConstant()->IsLongConstant());
3020 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3021 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
3022 }
3023 break;
3024
3025 default:
3026 LOG(FATAL) << "Unexpected type conversion from " << input_type
3027 << " to " << result_type;
3028 }
3029 break;
3030
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003031 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00003032 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003033 case DataType::Type::kUint8:
3034 case DataType::Type::kUint16:
3035 case DataType::Type::kInt16:
3036 case DataType::Type::kInt32:
3037 if (in.IsRegister()) {
3038 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
3039 } else {
3040 DCHECK(in.GetConstant()->IsIntConstant());
3041 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
3042 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
3043 }
3044 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003045 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003046 if (in.IsRegisterPair()) {
3047 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
3048 } else {
3049 DCHECK(in.GetConstant()->IsLongConstant());
3050 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3051 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
3052 }
3053 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003054
3055 default:
3056 LOG(FATAL) << "Unexpected type conversion from " << input_type
3057 << " to " << result_type;
3058 }
3059 break;
3060
3061 case DataType::Type::kUint16:
3062 switch (input_type) {
3063 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003064 case DataType::Type::kInt16:
3065 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003066 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003067 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
3068 } else if (in.IsStackSlot()) {
3069 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003070 } else {
3071 DCHECK(in.GetConstant()->IsIntConstant());
3072 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003073 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
3074 }
3075 break;
3076 case DataType::Type::kInt64:
3077 if (in.IsRegisterPair()) {
3078 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
3079 } else if (in.IsDoubleStackSlot()) {
3080 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
3081 } else {
3082 DCHECK(in.GetConstant()->IsLongConstant());
3083 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3084 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003085 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00003086 break;
3087
3088 default:
3089 LOG(FATAL) << "Unexpected type conversion from " << input_type
3090 << " to " << result_type;
3091 }
3092 break;
3093
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003094 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00003095 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003096 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003097 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00003098 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003099 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00003100 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003101 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00003102 } else {
3103 DCHECK(in.GetConstant()->IsIntConstant());
3104 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003105 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00003106 }
3107 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003108 case DataType::Type::kInt64:
3109 if (in.IsRegisterPair()) {
3110 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
3111 } else if (in.IsDoubleStackSlot()) {
3112 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
3113 } else {
3114 DCHECK(in.GetConstant()->IsLongConstant());
3115 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3116 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
3117 }
3118 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00003119
3120 default:
3121 LOG(FATAL) << "Unexpected type conversion from " << input_type
3122 << " to " << result_type;
3123 }
3124 break;
3125
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003126 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00003127 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003128 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00003129 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003130 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00003131 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003132 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00003133 } else {
3134 DCHECK(in.IsConstant());
3135 DCHECK(in.GetConstant()->IsLongConstant());
3136 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003137 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00003138 }
3139 break;
3140
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003141 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00003142 XmmRegister input = in.AsFpuRegister<XmmRegister>();
3143 Register output = out.AsRegister<Register>();
3144 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04003145 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00003146
3147 __ movl(output, Immediate(kPrimIntMax));
3148 // temp = int-to-float(output)
3149 __ cvtsi2ss(temp, output);
3150 // if input >= temp goto done
3151 __ comiss(input, temp);
3152 __ j(kAboveEqual, &done);
3153 // if input == NaN goto nan
3154 __ j(kUnordered, &nan);
3155 // output = float-to-int-truncate(input)
3156 __ cvttss2si(output, input);
3157 __ jmp(&done);
3158 __ Bind(&nan);
3159 // output = 0
3160 __ xorl(output, output);
3161 __ Bind(&done);
3162 break;
3163 }
3164
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003165 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003166 XmmRegister input = in.AsFpuRegister<XmmRegister>();
3167 Register output = out.AsRegister<Register>();
3168 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04003169 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003170
3171 __ movl(output, Immediate(kPrimIntMax));
3172 // temp = int-to-double(output)
3173 __ cvtsi2sd(temp, output);
3174 // if input >= temp goto done
3175 __ comisd(input, temp);
3176 __ j(kAboveEqual, &done);
3177 // if input == NaN goto nan
3178 __ j(kUnordered, &nan);
3179 // output = double-to-int-truncate(input)
3180 __ cvttsd2si(output, input);
3181 __ jmp(&done);
3182 __ Bind(&nan);
3183 // output = 0
3184 __ xorl(output, output);
3185 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00003186 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003187 }
Roland Levillain946e1432014-11-11 17:35:19 +00003188
3189 default:
3190 LOG(FATAL) << "Unexpected type conversion from " << input_type
3191 << " to " << result_type;
3192 }
3193 break;
3194
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003195 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00003196 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003197 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003198 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003199 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003200 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003201 case DataType::Type::kInt16:
3202 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00003203 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
3204 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003205 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00003206 __ cdq();
3207 break;
3208
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003209 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01003210 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003211 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00003212 break;
3213
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003214 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01003215 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003216 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00003217 break;
3218
3219 default:
3220 LOG(FATAL) << "Unexpected type conversion from " << input_type
3221 << " to " << result_type;
3222 }
3223 break;
3224
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003225 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00003226 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003227 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003228 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003229 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003230 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003231 case DataType::Type::kInt16:
3232 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003233 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003234 break;
3235
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003236 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003237 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00003238
Roland Levillain232ade02015-04-20 15:14:36 +01003239 // Create stack space for the call to
3240 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
3241 // TODO: enhance register allocator to ask for stack temporaries.
3242 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003243 adjustment = DataType::Size(DataType::Type::kInt64);
Vladimir Markodec78172020-06-19 15:31:23 +01003244 codegen_->IncreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003245 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003246
Roland Levillain232ade02015-04-20 15:14:36 +01003247 // Load the value to the FP stack, using temporaries if needed.
3248 PushOntoFPStack(in, 0, adjustment, false, true);
3249
3250 if (out.IsStackSlot()) {
3251 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
3252 } else {
3253 __ fstps(Address(ESP, 0));
3254 Location stack_temp = Location::StackSlot(0);
3255 codegen_->Move32(out, stack_temp);
3256 }
3257
3258 // Remove the temporary stack space we allocated.
3259 if (adjustment != 0) {
Vladimir Markodec78172020-06-19 15:31:23 +01003260 codegen_->DecreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003261 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003262 break;
3263 }
3264
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003265 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003266 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003267 break;
3268
3269 default:
3270 LOG(FATAL) << "Unexpected type conversion from " << input_type
3271 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003272 }
Roland Levillaincff13742014-11-17 14:32:17 +00003273 break;
3274
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003275 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00003276 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003277 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003278 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003279 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003280 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003281 case DataType::Type::kInt16:
3282 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003283 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003284 break;
3285
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003286 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003287 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00003288
Roland Levillain232ade02015-04-20 15:14:36 +01003289 // Create stack space for the call to
3290 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
3291 // TODO: enhance register allocator to ask for stack temporaries.
3292 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003293 adjustment = DataType::Size(DataType::Type::kInt64);
Vladimir Markodec78172020-06-19 15:31:23 +01003294 codegen_->IncreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003295 }
3296
3297 // Load the value to the FP stack, using temporaries if needed.
3298 PushOntoFPStack(in, 0, adjustment, false, true);
3299
3300 if (out.IsDoubleStackSlot()) {
3301 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
3302 } else {
3303 __ fstpl(Address(ESP, 0));
3304 Location stack_temp = Location::DoubleStackSlot(0);
3305 codegen_->Move64(out, stack_temp);
3306 }
3307
3308 // Remove the temporary stack space we allocated.
3309 if (adjustment != 0) {
Vladimir Markodec78172020-06-19 15:31:23 +01003310 codegen_->DecreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003311 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003312 break;
3313 }
3314
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003315 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003316 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003317 break;
3318
3319 default:
3320 LOG(FATAL) << "Unexpected type conversion from " << input_type
3321 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003322 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003323 break;
3324
3325 default:
3326 LOG(FATAL) << "Unexpected type conversion from " << input_type
3327 << " to " << result_type;
3328 }
3329}
3330
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003331void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003332 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003333 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003334 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003335 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05003336 locations->SetInAt(0, Location::RequiresRegister());
3337 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3338 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3339 break;
3340 }
3341
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003342 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003343 locations->SetInAt(0, Location::RequiresRegister());
3344 locations->SetInAt(1, Location::Any());
3345 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003346 break;
3347 }
3348
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003349 case DataType::Type::kFloat32:
3350 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003351 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003352 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3353 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003354 } else if (add->InputAt(1)->IsConstant()) {
3355 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003356 } else {
3357 locations->SetInAt(1, Location::Any());
3358 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003359 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003360 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003361 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003362
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003363 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003364 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Elliott Hughesc1896c92018-11-29 11:33:18 -08003365 UNREACHABLE();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003366 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003367}
3368
3369void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
3370 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003371 Location first = locations->InAt(0);
3372 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05003373 Location out = locations->Out();
3374
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003375 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003376 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003377 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003378 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3379 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003380 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3381 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05003382 } else {
3383 __ leal(out.AsRegister<Register>(), Address(
3384 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3385 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003386 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003387 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3388 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3389 __ addl(out.AsRegister<Register>(), Immediate(value));
3390 } else {
3391 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3392 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003393 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003394 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003395 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003396 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003397 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003398 }
3399
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003400 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003401 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003402 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3403 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003404 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003405 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3406 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003407 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003408 } else {
3409 DCHECK(second.IsConstant()) << second;
3410 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3411 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3412 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003413 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003414 break;
3415 }
3416
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003417 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003418 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003419 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003420 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3421 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003422 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003423 __ addss(first.AsFpuRegister<XmmRegister>(),
3424 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003425 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3426 const_area->GetBaseMethodAddress(),
3427 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003428 } else {
3429 DCHECK(second.IsStackSlot());
3430 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003431 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003432 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003433 }
3434
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003435 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003436 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003437 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003438 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3439 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003440 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003441 __ addsd(first.AsFpuRegister<XmmRegister>(),
3442 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003443 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3444 const_area->GetBaseMethodAddress(),
3445 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003446 } else {
3447 DCHECK(second.IsDoubleStackSlot());
3448 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003449 }
3450 break;
3451 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003452
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003453 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003454 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003455 }
3456}
3457
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003458void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003459 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003460 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003461 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003462 case DataType::Type::kInt32:
3463 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003464 locations->SetInAt(0, Location::RequiresRegister());
3465 locations->SetInAt(1, Location::Any());
3466 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003467 break;
3468 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003469 case DataType::Type::kFloat32:
3470 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003471 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003472 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3473 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003474 } else if (sub->InputAt(1)->IsConstant()) {
3475 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003476 } else {
3477 locations->SetInAt(1, Location::Any());
3478 }
Calin Juravle11351682014-10-23 15:38:15 +01003479 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003480 break;
Calin Juravle11351682014-10-23 15:38:15 +01003481 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003482
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003483 default:
Calin Juravle11351682014-10-23 15:38:15 +01003484 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003485 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003486}
3487
3488void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3489 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003490 Location first = locations->InAt(0);
3491 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003492 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003493 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003494 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003495 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003496 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003497 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003498 __ subl(first.AsRegister<Register>(),
3499 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003500 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003501 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003502 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003503 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003504 }
3505
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003506 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003507 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003508 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3509 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003510 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003511 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003512 __ sbbl(first.AsRegisterPairHigh<Register>(),
3513 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003514 } else {
3515 DCHECK(second.IsConstant()) << second;
3516 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3517 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3518 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003519 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003520 break;
3521 }
3522
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003523 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003524 if (second.IsFpuRegister()) {
3525 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3526 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3527 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003528 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003529 __ subss(first.AsFpuRegister<XmmRegister>(),
3530 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003531 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3532 const_area->GetBaseMethodAddress(),
3533 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003534 } else {
3535 DCHECK(second.IsStackSlot());
3536 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3537 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003538 break;
Calin Juravle11351682014-10-23 15:38:15 +01003539 }
3540
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003541 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003542 if (second.IsFpuRegister()) {
3543 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3544 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3545 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003546 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003547 __ subsd(first.AsFpuRegister<XmmRegister>(),
3548 codegen_->LiteralDoubleAddress(
3549 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003550 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003551 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3552 } else {
3553 DCHECK(second.IsDoubleStackSlot());
3554 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3555 }
Calin Juravle11351682014-10-23 15:38:15 +01003556 break;
3557 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003558
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003559 default:
Calin Juravle11351682014-10-23 15:38:15 +01003560 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003561 }
3562}
3563
Calin Juravle34bacdf2014-10-07 20:23:36 +01003564void LocationsBuilderX86::VisitMul(HMul* mul) {
3565 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003566 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003567 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003568 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003569 locations->SetInAt(0, Location::RequiresRegister());
3570 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003571 if (mul->InputAt(1)->IsIntConstant()) {
3572 // Can use 3 operand multiply.
3573 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3574 } else {
3575 locations->SetOut(Location::SameAsFirstInput());
3576 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003577 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003578 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003579 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003580 locations->SetInAt(1, Location::Any());
3581 locations->SetOut(Location::SameAsFirstInput());
3582 // Needed for imul on 32bits with 64bits output.
3583 locations->AddTemp(Location::RegisterLocation(EAX));
3584 locations->AddTemp(Location::RegisterLocation(EDX));
3585 break;
3586 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003587 case DataType::Type::kFloat32:
3588 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003589 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003590 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3591 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003592 } else if (mul->InputAt(1)->IsConstant()) {
3593 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003594 } else {
3595 locations->SetInAt(1, Location::Any());
3596 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003597 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003598 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003599 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003600
3601 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003602 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003603 }
3604}
3605
3606void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3607 LocationSummary* locations = mul->GetLocations();
3608 Location first = locations->InAt(0);
3609 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003610 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003611
3612 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003613 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003614 // The constant may have ended up in a register, so test explicitly to avoid
3615 // problems where the output may not be the same as the first operand.
3616 if (mul->InputAt(1)->IsIntConstant()) {
3617 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3618 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3619 } else if (second.IsRegister()) {
3620 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003621 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003622 } else {
3623 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003624 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003625 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003626 }
3627 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003628
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003629 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003630 Register in1_hi = first.AsRegisterPairHigh<Register>();
3631 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003632 Register eax = locations->GetTemp(0).AsRegister<Register>();
3633 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003634
3635 DCHECK_EQ(EAX, eax);
3636 DCHECK_EQ(EDX, edx);
3637
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003638 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003639 // output: in1
3640 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3641 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3642 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003643 if (second.IsConstant()) {
3644 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003645
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003646 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3647 int32_t low_value = Low32Bits(value);
3648 int32_t high_value = High32Bits(value);
3649 Immediate low(low_value);
3650 Immediate high(high_value);
3651
3652 __ movl(eax, high);
3653 // eax <- in1.lo * in2.hi
3654 __ imull(eax, in1_lo);
3655 // in1.hi <- in1.hi * in2.lo
3656 __ imull(in1_hi, low);
3657 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3658 __ addl(in1_hi, eax);
3659 // move in2_lo to eax to prepare for double precision
3660 __ movl(eax, low);
3661 // edx:eax <- in1.lo * in2.lo
3662 __ mull(in1_lo);
3663 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3664 __ addl(in1_hi, edx);
3665 // in1.lo <- (in1.lo * in2.lo)[31:0];
3666 __ movl(in1_lo, eax);
3667 } else if (second.IsRegisterPair()) {
3668 Register in2_hi = second.AsRegisterPairHigh<Register>();
3669 Register in2_lo = second.AsRegisterPairLow<Register>();
3670
3671 __ movl(eax, in2_hi);
3672 // eax <- in1.lo * in2.hi
3673 __ imull(eax, in1_lo);
3674 // in1.hi <- in1.hi * in2.lo
3675 __ imull(in1_hi, in2_lo);
3676 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3677 __ addl(in1_hi, eax);
3678 // move in1_lo to eax to prepare for double precision
3679 __ movl(eax, in1_lo);
3680 // edx:eax <- in1.lo * in2.lo
3681 __ mull(in2_lo);
3682 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3683 __ addl(in1_hi, edx);
3684 // in1.lo <- (in1.lo * in2.lo)[31:0];
3685 __ movl(in1_lo, eax);
3686 } else {
3687 DCHECK(second.IsDoubleStackSlot()) << second;
3688 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3689 Address in2_lo(ESP, second.GetStackIndex());
3690
3691 __ movl(eax, in2_hi);
3692 // eax <- in1.lo * in2.hi
3693 __ imull(eax, in1_lo);
3694 // in1.hi <- in1.hi * in2.lo
3695 __ imull(in1_hi, in2_lo);
3696 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3697 __ addl(in1_hi, eax);
3698 // move in1_lo to eax to prepare for double precision
3699 __ movl(eax, in1_lo);
3700 // edx:eax <- in1.lo * in2.lo
3701 __ mull(in2_lo);
3702 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3703 __ addl(in1_hi, edx);
3704 // in1.lo <- (in1.lo * in2.lo)[31:0];
3705 __ movl(in1_lo, eax);
3706 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003707
3708 break;
3709 }
3710
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003711 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003712 DCHECK(first.Equals(locations->Out()));
3713 if (second.IsFpuRegister()) {
3714 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3715 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3716 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003717 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003718 __ mulss(first.AsFpuRegister<XmmRegister>(),
3719 codegen_->LiteralFloatAddress(
3720 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003721 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003722 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3723 } else {
3724 DCHECK(second.IsStackSlot());
3725 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3726 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003727 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003728 }
3729
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003730 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003731 DCHECK(first.Equals(locations->Out()));
3732 if (second.IsFpuRegister()) {
3733 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3734 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3735 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003736 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003737 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3738 codegen_->LiteralDoubleAddress(
3739 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003740 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003741 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3742 } else {
3743 DCHECK(second.IsDoubleStackSlot());
3744 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3745 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003746 break;
3747 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003748
3749 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003750 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003751 }
3752}
3753
Roland Levillain232ade02015-04-20 15:14:36 +01003754void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3755 uint32_t temp_offset,
3756 uint32_t stack_adjustment,
3757 bool is_fp,
3758 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003759 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003760 DCHECK(!is_wide);
3761 if (is_fp) {
3762 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3763 } else {
3764 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3765 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003766 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003767 DCHECK(is_wide);
3768 if (is_fp) {
3769 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3770 } else {
3771 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3772 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003773 } else {
3774 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003775 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003776 Location stack_temp = Location::StackSlot(temp_offset);
3777 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003778 if (is_fp) {
3779 __ flds(Address(ESP, temp_offset));
3780 } else {
3781 __ filds(Address(ESP, temp_offset));
3782 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003783 } else {
3784 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3785 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003786 if (is_fp) {
3787 __ fldl(Address(ESP, temp_offset));
3788 } else {
3789 __ fildl(Address(ESP, temp_offset));
3790 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003791 }
3792 }
3793}
3794
3795void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003796 DataType::Type type = rem->GetResultType();
3797 bool is_float = type == DataType::Type::kFloat32;
3798 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003799 LocationSummary* locations = rem->GetLocations();
3800 Location first = locations->InAt(0);
3801 Location second = locations->InAt(1);
3802 Location out = locations->Out();
3803
3804 // Create stack space for 2 elements.
3805 // TODO: enhance register allocator to ask for stack temporaries.
Vladimir Markodec78172020-06-19 15:31:23 +01003806 codegen_->IncreaseFrame(2 * elem_size);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003807
3808 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003809 const bool is_wide = !is_float;
Andreas Gampe3db70682018-12-26 15:12:03 -08003810 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp= */ true, is_wide);
3811 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp= */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003812
3813 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003814 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003815 __ Bind(&retry);
3816 __ fprem();
3817
3818 // Move FP status to AX.
3819 __ fstsw();
3820
3821 // And see if the argument reduction is complete. This is signaled by the
3822 // C2 FPU flag bit set to 0.
3823 __ andl(EAX, Immediate(kC2ConditionMask));
3824 __ j(kNotEqual, &retry);
3825
3826 // We have settled on the final value. Retrieve it into an XMM register.
3827 // Store FP top of stack to real stack.
3828 if (is_float) {
3829 __ fsts(Address(ESP, 0));
3830 } else {
3831 __ fstl(Address(ESP, 0));
3832 }
3833
3834 // Pop the 2 items from the FP stack.
3835 __ fucompp();
3836
3837 // Load the value from the stack into an XMM register.
3838 DCHECK(out.IsFpuRegister()) << out;
3839 if (is_float) {
3840 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3841 } else {
3842 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3843 }
3844
3845 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01003846 codegen_->DecreaseFrame(2 * elem_size);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003847}
3848
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003849
3850void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3851 DCHECK(instruction->IsDiv() || instruction->IsRem());
3852
3853 LocationSummary* locations = instruction->GetLocations();
3854 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003855 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003856
3857 Register out_register = locations->Out().AsRegister<Register>();
3858 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003859 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003860
3861 DCHECK(imm == 1 || imm == -1);
3862
3863 if (instruction->IsRem()) {
3864 __ xorl(out_register, out_register);
3865 } else {
3866 __ movl(out_register, input_register);
3867 if (imm == -1) {
3868 __ negl(out_register);
3869 }
3870 }
3871}
3872
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303873void InstructionCodeGeneratorX86::RemByPowerOfTwo(HRem* instruction) {
3874 LocationSummary* locations = instruction->GetLocations();
3875 Location second = locations->InAt(1);
3876
3877 Register out = locations->Out().AsRegister<Register>();
3878 Register numerator = locations->InAt(0).AsRegister<Register>();
3879
3880 int32_t imm = Int64FromConstant(second.GetConstant());
3881 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3882 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3883
3884 Register tmp = locations->GetTemp(0).AsRegister<Register>();
3885 NearLabel done;
3886 __ movl(out, numerator);
3887 __ andl(out, Immediate(abs_imm-1));
3888 __ j(Condition::kZero, &done);
3889 __ leal(tmp, Address(out, static_cast<int32_t>(~(abs_imm-1))));
3890 __ testl(numerator, numerator);
3891 __ cmovl(Condition::kLess, out, tmp);
3892 __ Bind(&done);
3893}
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003894
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003895void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003896 LocationSummary* locations = instruction->GetLocations();
3897
3898 Register out_register = locations->Out().AsRegister<Register>();
3899 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003900 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003901 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3902 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003903
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003904 Register num = locations->GetTemp(0).AsRegister<Register>();
3905
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003906 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003907 __ testl(input_register, input_register);
3908 __ cmovl(kGreaterEqual, num, input_register);
3909 int shift = CTZ(imm);
3910 __ sarl(num, Immediate(shift));
3911
3912 if (imm < 0) {
3913 __ negl(num);
3914 }
3915
3916 __ movl(out_register, num);
3917}
3918
3919void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3920 DCHECK(instruction->IsDiv() || instruction->IsRem());
3921
3922 LocationSummary* locations = instruction->GetLocations();
3923 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3924
3925 Register eax = locations->InAt(0).AsRegister<Register>();
3926 Register out = locations->Out().AsRegister<Register>();
3927 Register num;
3928 Register edx;
3929
3930 if (instruction->IsDiv()) {
3931 edx = locations->GetTemp(0).AsRegister<Register>();
3932 num = locations->GetTemp(1).AsRegister<Register>();
3933 } else {
3934 edx = locations->Out().AsRegister<Register>();
3935 num = locations->GetTemp(0).AsRegister<Register>();
3936 }
3937
3938 DCHECK_EQ(EAX, eax);
3939 DCHECK_EQ(EDX, edx);
3940 if (instruction->IsDiv()) {
3941 DCHECK_EQ(EAX, out);
3942 } else {
3943 DCHECK_EQ(EDX, out);
3944 }
3945
3946 int64_t magic;
3947 int shift;
Andreas Gampe3db70682018-12-26 15:12:03 -08003948 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003949
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003950 // Save the numerator.
3951 __ movl(num, eax);
3952
3953 // EAX = magic
3954 __ movl(eax, Immediate(magic));
3955
3956 // EDX:EAX = magic * numerator
3957 __ imull(num);
3958
3959 if (imm > 0 && magic < 0) {
3960 // EDX += num
3961 __ addl(edx, num);
3962 } else if (imm < 0 && magic > 0) {
3963 __ subl(edx, num);
3964 }
3965
3966 // Shift if needed.
3967 if (shift != 0) {
3968 __ sarl(edx, Immediate(shift));
3969 }
3970
3971 // EDX += 1 if EDX < 0
3972 __ movl(eax, edx);
3973 __ shrl(edx, Immediate(31));
3974 __ addl(edx, eax);
3975
3976 if (instruction->IsRem()) {
3977 __ movl(eax, num);
3978 __ imull(edx, Immediate(imm));
3979 __ subl(eax, edx);
3980 __ movl(edx, eax);
3981 } else {
3982 __ movl(eax, edx);
3983 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003984}
3985
Calin Juravlebacfec32014-11-14 15:54:36 +00003986void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3987 DCHECK(instruction->IsDiv() || instruction->IsRem());
3988
3989 LocationSummary* locations = instruction->GetLocations();
3990 Location out = locations->Out();
3991 Location first = locations->InAt(0);
3992 Location second = locations->InAt(1);
3993 bool is_div = instruction->IsDiv();
3994
3995 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003996 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003997 DCHECK_EQ(EAX, first.AsRegister<Register>());
3998 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003999
Vladimir Marko13c86fd2015-11-11 12:37:46 +00004000 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01004001 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004002
4003 if (imm == 0) {
4004 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
4005 } else if (imm == 1 || imm == -1) {
4006 DivRemOneOrMinusOne(instruction);
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05304007 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
4008 if (is_div) {
4009 DivByPowerOfTwo(instruction->AsDiv());
4010 } else {
4011 RemByPowerOfTwo(instruction->AsRem());
4012 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004013 } else {
4014 DCHECK(imm <= -2 || imm >= 2);
4015 GenerateDivRemWithAnyConstant(instruction);
4016 }
4017 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004018 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00004019 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004020 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00004021
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004022 Register second_reg = second.AsRegister<Register>();
4023 // 0x80000000/-1 triggers an arithmetic exception!
4024 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
4025 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00004026
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004027 __ cmpl(second_reg, Immediate(-1));
4028 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00004029
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004030 // edx:eax <- sign-extended of eax
4031 __ cdq();
4032 // eax = quotient, edx = remainder
4033 __ idivl(second_reg);
4034 __ Bind(slow_path->GetExitLabel());
4035 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004036 break;
4037 }
4038
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004039 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004040 InvokeRuntimeCallingConvention calling_convention;
4041 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
4042 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
4043 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
4044 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
4045 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
4046 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
4047
4048 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004049 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004050 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004051 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004052 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004053 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004054 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004055 break;
4056 }
4057
4058 default:
4059 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
4060 }
4061}
4062
Calin Juravle7c4954d2014-10-28 16:57:40 +00004063void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004064 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004065 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004066 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01004067 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004068
Calin Juravle7c4954d2014-10-28 16:57:40 +00004069 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004070 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00004071 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004072 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00004073 locations->SetOut(Location::SameAsFirstInput());
4074 // Intel uses edx:eax as the dividend.
4075 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004076 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
4077 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
4078 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01004079 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004080 locations->AddTemp(Location::RequiresRegister());
4081 }
Calin Juravled0d48522014-11-04 16:40:20 +00004082 break;
4083 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004084 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004085 InvokeRuntimeCallingConvention calling_convention;
4086 locations->SetInAt(0, Location::RegisterPairLocation(
4087 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4088 locations->SetInAt(1, Location::RegisterPairLocation(
4089 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4090 // Runtime helper puts the result in EAX, EDX.
4091 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00004092 break;
4093 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004094 case DataType::Type::kFloat32:
4095 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004096 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00004097 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
4098 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00004099 } else if (div->InputAt(1)->IsConstant()) {
4100 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00004101 } else {
4102 locations->SetInAt(1, Location::Any());
4103 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004104 locations->SetOut(Location::SameAsFirstInput());
4105 break;
4106 }
4107
4108 default:
4109 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4110 }
4111}
4112
4113void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
4114 LocationSummary* locations = div->GetLocations();
4115 Location first = locations->InAt(0);
4116 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004117
4118 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004119 case DataType::Type::kInt32:
4120 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004121 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004122 break;
4123 }
4124
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004125 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04004126 if (second.IsFpuRegister()) {
4127 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
4128 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
4129 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00004130 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04004131 __ divss(first.AsFpuRegister<XmmRegister>(),
4132 codegen_->LiteralFloatAddress(
4133 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004134 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04004135 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
4136 } else {
4137 DCHECK(second.IsStackSlot());
4138 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
4139 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004140 break;
4141 }
4142
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004143 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04004144 if (second.IsFpuRegister()) {
4145 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
4146 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
4147 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00004148 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04004149 __ divsd(first.AsFpuRegister<XmmRegister>(),
4150 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004151 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
4152 const_area->GetBaseMethodAddress(),
4153 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04004154 } else {
4155 DCHECK(second.IsDoubleStackSlot());
4156 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
4157 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004158 break;
4159 }
4160
4161 default:
4162 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4163 }
4164}
4165
Calin Juravlebacfec32014-11-14 15:54:36 +00004166void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004167 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004168
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004169 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004170 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004171 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01004172 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00004173
Calin Juravled2ec87d2014-12-08 14:24:46 +00004174 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004175 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004176 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004177 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00004178 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004179 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
4180 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
4181 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01004182 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004183 locations->AddTemp(Location::RequiresRegister());
4184 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004185 break;
4186 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004187 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004188 InvokeRuntimeCallingConvention calling_convention;
4189 locations->SetInAt(0, Location::RegisterPairLocation(
4190 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4191 locations->SetInAt(1, Location::RegisterPairLocation(
4192 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4193 // Runtime helper puts the result in EAX, EDX.
4194 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
4195 break;
4196 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004197 case DataType::Type::kFloat64:
4198 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05004199 locations->SetInAt(0, Location::Any());
4200 locations->SetInAt(1, Location::Any());
4201 locations->SetOut(Location::RequiresFpuRegister());
4202 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00004203 break;
4204 }
4205
4206 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004207 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004208 }
4209}
4210
4211void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004212 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00004213 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004214 case DataType::Type::kInt32:
4215 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004216 GenerateDivRemIntegral(rem);
4217 break;
4218 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004219 case DataType::Type::kFloat32:
4220 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05004221 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00004222 break;
4223 }
4224 default:
4225 LOG(FATAL) << "Unexpected rem type " << type;
4226 }
4227}
4228
Aart Bik1f8d51b2018-02-15 10:42:37 -08004229static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
4230 LocationSummary* locations = new (allocator) LocationSummary(minmax);
4231 switch (minmax->GetResultType()) {
4232 case DataType::Type::kInt32:
4233 locations->SetInAt(0, Location::RequiresRegister());
4234 locations->SetInAt(1, Location::RequiresRegister());
4235 locations->SetOut(Location::SameAsFirstInput());
4236 break;
4237 case DataType::Type::kInt64:
4238 locations->SetInAt(0, Location::RequiresRegister());
4239 locations->SetInAt(1, Location::RequiresRegister());
4240 locations->SetOut(Location::SameAsFirstInput());
4241 // Register to use to perform a long subtract to set cc.
4242 locations->AddTemp(Location::RequiresRegister());
4243 break;
4244 case DataType::Type::kFloat32:
4245 locations->SetInAt(0, Location::RequiresFpuRegister());
4246 locations->SetInAt(1, Location::RequiresFpuRegister());
4247 locations->SetOut(Location::SameAsFirstInput());
4248 locations->AddTemp(Location::RequiresRegister());
4249 break;
4250 case DataType::Type::kFloat64:
4251 locations->SetInAt(0, Location::RequiresFpuRegister());
4252 locations->SetInAt(1, Location::RequiresFpuRegister());
4253 locations->SetOut(Location::SameAsFirstInput());
4254 break;
4255 default:
4256 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
4257 }
4258}
4259
Aart Bik351df3e2018-03-07 11:54:57 -08004260void InstructionCodeGeneratorX86::GenerateMinMaxInt(LocationSummary* locations,
4261 bool is_min,
4262 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08004263 Location op1_loc = locations->InAt(0);
4264 Location op2_loc = locations->InAt(1);
4265
4266 // Shortcut for same input locations.
4267 if (op1_loc.Equals(op2_loc)) {
4268 // Can return immediately, as op1_loc == out_loc.
4269 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
4270 // a copy here.
4271 DCHECK(locations->Out().Equals(op1_loc));
4272 return;
4273 }
4274
4275 if (type == DataType::Type::kInt64) {
4276 // Need to perform a subtract to get the sign right.
4277 // op1 is already in the same location as the output.
4278 Location output = locations->Out();
4279 Register output_lo = output.AsRegisterPairLow<Register>();
4280 Register output_hi = output.AsRegisterPairHigh<Register>();
4281
4282 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
4283 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
4284
4285 // The comparison is performed by subtracting the second operand from
4286 // the first operand and then setting the status flags in the same
4287 // manner as the SUB instruction."
4288 __ cmpl(output_lo, op2_lo);
4289
4290 // Now use a temp and the borrow to finish the subtraction of op2_hi.
4291 Register temp = locations->GetTemp(0).AsRegister<Register>();
4292 __ movl(temp, output_hi);
4293 __ sbbl(temp, op2_hi);
4294
4295 // Now the condition code is correct.
4296 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
4297 __ cmovl(cond, output_lo, op2_lo);
4298 __ cmovl(cond, output_hi, op2_hi);
4299 } else {
4300 DCHECK_EQ(type, DataType::Type::kInt32);
4301 Register out = locations->Out().AsRegister<Register>();
4302 Register op2 = op2_loc.AsRegister<Register>();
4303
4304 // (out := op1)
4305 // out <=? op2
4306 // if out is min jmp done
4307 // out := op2
4308 // done:
4309
4310 __ cmpl(out, op2);
4311 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
4312 __ cmovl(cond, out, op2);
4313 }
4314}
4315
4316void InstructionCodeGeneratorX86::GenerateMinMaxFP(LocationSummary* locations,
4317 bool is_min,
4318 DataType::Type type) {
4319 Location op1_loc = locations->InAt(0);
4320 Location op2_loc = locations->InAt(1);
4321 Location out_loc = locations->Out();
4322 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
4323
4324 // Shortcut for same input locations.
4325 if (op1_loc.Equals(op2_loc)) {
4326 DCHECK(out_loc.Equals(op1_loc));
4327 return;
4328 }
4329
4330 // (out := op1)
4331 // out <=? op2
4332 // if Nan jmp Nan_label
4333 // if out is min jmp done
4334 // if op2 is min jmp op2_label
4335 // handle -0/+0
4336 // jmp done
4337 // Nan_label:
4338 // out := NaN
4339 // op2_label:
4340 // out := op2
4341 // done:
4342 //
4343 // This removes one jmp, but needs to copy one input (op1) to out.
4344 //
4345 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
4346
4347 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
4348
4349 NearLabel nan, done, op2_label;
4350 if (type == DataType::Type::kFloat64) {
4351 __ ucomisd(out, op2);
4352 } else {
4353 DCHECK_EQ(type, DataType::Type::kFloat32);
4354 __ ucomiss(out, op2);
4355 }
4356
4357 __ j(Condition::kParityEven, &nan);
4358
4359 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
4360 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
4361
4362 // Handle 0.0/-0.0.
4363 if (is_min) {
4364 if (type == DataType::Type::kFloat64) {
4365 __ orpd(out, op2);
4366 } else {
4367 __ orps(out, op2);
4368 }
4369 } else {
4370 if (type == DataType::Type::kFloat64) {
4371 __ andpd(out, op2);
4372 } else {
4373 __ andps(out, op2);
4374 }
4375 }
4376 __ jmp(&done);
4377
4378 // NaN handling.
4379 __ Bind(&nan);
4380 if (type == DataType::Type::kFloat64) {
4381 // TODO: Use a constant from the constant table (requires extra input).
4382 __ LoadLongConstant(out, kDoubleNaN);
4383 } else {
4384 Register constant = locations->GetTemp(0).AsRegister<Register>();
4385 __ movl(constant, Immediate(kFloatNaN));
4386 __ movd(out, constant);
4387 }
4388 __ jmp(&done);
4389
4390 // out := op2;
4391 __ Bind(&op2_label);
4392 if (type == DataType::Type::kFloat64) {
4393 __ movsd(out, op2);
4394 } else {
4395 __ movss(out, op2);
4396 }
4397
4398 // Done.
4399 __ Bind(&done);
4400}
4401
Aart Bik351df3e2018-03-07 11:54:57 -08004402void InstructionCodeGeneratorX86::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4403 DataType::Type type = minmax->GetResultType();
4404 switch (type) {
4405 case DataType::Type::kInt32:
4406 case DataType::Type::kInt64:
4407 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
4408 break;
4409 case DataType::Type::kFloat32:
4410 case DataType::Type::kFloat64:
4411 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
4412 break;
4413 default:
4414 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4415 }
4416}
4417
Aart Bik1f8d51b2018-02-15 10:42:37 -08004418void LocationsBuilderX86::VisitMin(HMin* min) {
4419 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4420}
4421
4422void InstructionCodeGeneratorX86::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004423 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004424}
4425
4426void LocationsBuilderX86::VisitMax(HMax* max) {
4427 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4428}
4429
4430void InstructionCodeGeneratorX86::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004431 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004432}
4433
Aart Bik3dad3412018-02-28 12:01:46 -08004434void LocationsBuilderX86::VisitAbs(HAbs* abs) {
4435 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4436 switch (abs->GetResultType()) {
4437 case DataType::Type::kInt32:
4438 locations->SetInAt(0, Location::RegisterLocation(EAX));
4439 locations->SetOut(Location::SameAsFirstInput());
4440 locations->AddTemp(Location::RegisterLocation(EDX));
4441 break;
4442 case DataType::Type::kInt64:
4443 locations->SetInAt(0, Location::RequiresRegister());
4444 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4445 locations->AddTemp(Location::RequiresRegister());
4446 break;
4447 case DataType::Type::kFloat32:
4448 locations->SetInAt(0, Location::RequiresFpuRegister());
4449 locations->SetOut(Location::SameAsFirstInput());
4450 locations->AddTemp(Location::RequiresFpuRegister());
4451 locations->AddTemp(Location::RequiresRegister());
4452 break;
4453 case DataType::Type::kFloat64:
4454 locations->SetInAt(0, Location::RequiresFpuRegister());
4455 locations->SetOut(Location::SameAsFirstInput());
4456 locations->AddTemp(Location::RequiresFpuRegister());
4457 break;
4458 default:
4459 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4460 }
4461}
4462
4463void InstructionCodeGeneratorX86::VisitAbs(HAbs* abs) {
4464 LocationSummary* locations = abs->GetLocations();
4465 switch (abs->GetResultType()) {
4466 case DataType::Type::kInt32: {
4467 Register out = locations->Out().AsRegister<Register>();
4468 DCHECK_EQ(out, EAX);
4469 Register temp = locations->GetTemp(0).AsRegister<Register>();
4470 DCHECK_EQ(temp, EDX);
4471 // Sign extend EAX into EDX.
4472 __ cdq();
4473 // XOR EAX with sign.
4474 __ xorl(EAX, EDX);
4475 // Subtract out sign to correct.
4476 __ subl(EAX, EDX);
4477 // The result is in EAX.
4478 break;
4479 }
4480 case DataType::Type::kInt64: {
4481 Location input = locations->InAt(0);
4482 Register input_lo = input.AsRegisterPairLow<Register>();
4483 Register input_hi = input.AsRegisterPairHigh<Register>();
4484 Location output = locations->Out();
4485 Register output_lo = output.AsRegisterPairLow<Register>();
4486 Register output_hi = output.AsRegisterPairHigh<Register>();
4487 Register temp = locations->GetTemp(0).AsRegister<Register>();
4488 // Compute the sign into the temporary.
4489 __ movl(temp, input_hi);
4490 __ sarl(temp, Immediate(31));
4491 // Store the sign into the output.
4492 __ movl(output_lo, temp);
4493 __ movl(output_hi, temp);
4494 // XOR the input to the output.
4495 __ xorl(output_lo, input_lo);
4496 __ xorl(output_hi, input_hi);
4497 // Subtract the sign.
4498 __ subl(output_lo, temp);
4499 __ sbbl(output_hi, temp);
4500 break;
4501 }
4502 case DataType::Type::kFloat32: {
4503 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4504 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4505 Register constant = locations->GetTemp(1).AsRegister<Register>();
4506 __ movl(constant, Immediate(INT32_C(0x7FFFFFFF)));
4507 __ movd(temp, constant);
4508 __ andps(out, temp);
4509 break;
4510 }
4511 case DataType::Type::kFloat64: {
4512 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4513 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4514 // TODO: Use a constant from the constant table (requires extra input).
4515 __ LoadLongConstant(temp, INT64_C(0x7FFFFFFFFFFFFFFF));
4516 __ andpd(out, temp);
4517 break;
4518 }
4519 default:
4520 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4521 }
4522}
4523
Calin Juravled0d48522014-11-04 16:40:20 +00004524void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004525 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004526 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004527 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004528 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004529 case DataType::Type::kInt8:
4530 case DataType::Type::kUint16:
4531 case DataType::Type::kInt16:
4532 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004533 locations->SetInAt(0, Location::Any());
4534 break;
4535 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004536 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004537 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
4538 if (!instruction->IsConstant()) {
4539 locations->AddTemp(Location::RequiresRegister());
4540 }
4541 break;
4542 }
4543 default:
4544 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4545 }
Calin Juravled0d48522014-11-04 16:40:20 +00004546}
4547
4548void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004549 SlowPathCode* slow_path =
4550 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004551 codegen_->AddSlowPath(slow_path);
4552
4553 LocationSummary* locations = instruction->GetLocations();
4554 Location value = locations->InAt(0);
4555
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004556 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004557 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004558 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004559 case DataType::Type::kInt8:
4560 case DataType::Type::kUint16:
4561 case DataType::Type::kInt16:
4562 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004563 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004564 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004565 __ j(kEqual, slow_path->GetEntryLabel());
4566 } else if (value.IsStackSlot()) {
4567 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
4568 __ j(kEqual, slow_path->GetEntryLabel());
4569 } else {
4570 DCHECK(value.IsConstant()) << value;
4571 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004572 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004573 }
4574 }
4575 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004576 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004577 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004578 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004579 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004580 __ movl(temp, value.AsRegisterPairLow<Register>());
4581 __ orl(temp, value.AsRegisterPairHigh<Register>());
4582 __ j(kEqual, slow_path->GetEntryLabel());
4583 } else {
4584 DCHECK(value.IsConstant()) << value;
4585 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4586 __ jmp(slow_path->GetEntryLabel());
4587 }
4588 }
4589 break;
4590 }
4591 default:
4592 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004593 }
Calin Juravled0d48522014-11-04 16:40:20 +00004594}
4595
Calin Juravle9aec02f2014-11-18 23:06:35 +00004596void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
4597 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4598
4599 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004600 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004601
4602 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004603 case DataType::Type::kInt32:
4604 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004605 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00004606 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00004607 // The shift count needs to be in CL or a constant.
4608 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004609 locations->SetOut(Location::SameAsFirstInput());
4610 break;
4611 }
4612 default:
4613 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4614 }
4615}
4616
4617void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
4618 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4619
4620 LocationSummary* locations = op->GetLocations();
4621 Location first = locations->InAt(0);
4622 Location second = locations->InAt(1);
4623 DCHECK(first.Equals(locations->Out()));
4624
4625 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004626 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00004627 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004628 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004629 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004630 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004631 DCHECK_EQ(ECX, second_reg);
4632 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004633 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004634 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004635 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004636 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004637 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004638 }
4639 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004640 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004641 if (shift == 0) {
4642 return;
4643 }
4644 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004645 if (op->IsShl()) {
4646 __ shll(first_reg, imm);
4647 } else if (op->IsShr()) {
4648 __ sarl(first_reg, imm);
4649 } else {
4650 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004651 }
4652 }
4653 break;
4654 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004655 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004656 if (second.IsRegister()) {
4657 Register second_reg = second.AsRegister<Register>();
4658 DCHECK_EQ(ECX, second_reg);
4659 if (op->IsShl()) {
4660 GenerateShlLong(first, second_reg);
4661 } else if (op->IsShr()) {
4662 GenerateShrLong(first, second_reg);
4663 } else {
4664 GenerateUShrLong(first, second_reg);
4665 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004666 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00004667 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004668 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004669 // Nothing to do if the shift is 0, as the input is already the output.
4670 if (shift != 0) {
4671 if (op->IsShl()) {
4672 GenerateShlLong(first, shift);
4673 } else if (op->IsShr()) {
4674 GenerateShrLong(first, shift);
4675 } else {
4676 GenerateUShrLong(first, shift);
4677 }
4678 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004679 }
4680 break;
4681 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004682 default:
4683 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4684 }
4685}
4686
Mark P Mendell73945692015-04-29 14:56:17 +00004687void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
4688 Register low = loc.AsRegisterPairLow<Register>();
4689 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04004690 if (shift == 1) {
4691 // This is just an addition.
4692 __ addl(low, low);
4693 __ adcl(high, high);
4694 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00004695 // Shift by 32 is easy. High gets low, and low gets 0.
4696 codegen_->EmitParallelMoves(
4697 loc.ToLow(),
4698 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004699 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004700 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4701 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004702 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004703 } else if (shift > 32) {
4704 // Low part becomes 0. High part is low part << (shift-32).
4705 __ movl(high, low);
4706 __ shll(high, Immediate(shift - 32));
4707 __ xorl(low, low);
4708 } else {
4709 // Between 1 and 31.
4710 __ shld(high, low, Immediate(shift));
4711 __ shll(low, Immediate(shift));
4712 }
4713}
4714
Calin Juravle9aec02f2014-11-18 23:06:35 +00004715void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004716 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004717 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4718 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4719 __ testl(shifter, Immediate(32));
4720 __ j(kEqual, &done);
4721 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4722 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4723 __ Bind(&done);
4724}
4725
Mark P Mendell73945692015-04-29 14:56:17 +00004726void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4727 Register low = loc.AsRegisterPairLow<Register>();
4728 Register high = loc.AsRegisterPairHigh<Register>();
4729 if (shift == 32) {
4730 // Need to copy the sign.
4731 DCHECK_NE(low, high);
4732 __ movl(low, high);
4733 __ sarl(high, Immediate(31));
4734 } else if (shift > 32) {
4735 DCHECK_NE(low, high);
4736 // High part becomes sign. Low part is shifted by shift - 32.
4737 __ movl(low, high);
4738 __ sarl(high, Immediate(31));
4739 __ sarl(low, Immediate(shift - 32));
4740 } else {
4741 // Between 1 and 31.
4742 __ shrd(low, high, Immediate(shift));
4743 __ sarl(high, Immediate(shift));
4744 }
4745}
4746
Calin Juravle9aec02f2014-11-18 23:06:35 +00004747void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004748 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004749 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4750 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4751 __ testl(shifter, Immediate(32));
4752 __ j(kEqual, &done);
4753 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4754 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4755 __ Bind(&done);
4756}
4757
Mark P Mendell73945692015-04-29 14:56:17 +00004758void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4759 Register low = loc.AsRegisterPairLow<Register>();
4760 Register high = loc.AsRegisterPairHigh<Register>();
4761 if (shift == 32) {
4762 // Shift by 32 is easy. Low gets high, and high gets 0.
4763 codegen_->EmitParallelMoves(
4764 loc.ToHigh(),
4765 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004766 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004767 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4768 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004769 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004770 } else if (shift > 32) {
4771 // Low part is high >> (shift - 32). High part becomes 0.
4772 __ movl(low, high);
4773 __ shrl(low, Immediate(shift - 32));
4774 __ xorl(high, high);
4775 } else {
4776 // Between 1 and 31.
4777 __ shrd(low, high, Immediate(shift));
4778 __ shrl(high, Immediate(shift));
4779 }
4780}
4781
Calin Juravle9aec02f2014-11-18 23:06:35 +00004782void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004783 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004784 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4785 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4786 __ testl(shifter, Immediate(32));
4787 __ j(kEqual, &done);
4788 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4789 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4790 __ Bind(&done);
4791}
4792
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004793void LocationsBuilderX86::VisitRor(HRor* ror) {
4794 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004795 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004796
4797 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004798 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004799 // Add the temporary needed.
4800 locations->AddTemp(Location::RequiresRegister());
4801 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004802 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004803 locations->SetInAt(0, Location::RequiresRegister());
4804 // The shift count needs to be in CL (unless it is a constant).
4805 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4806 locations->SetOut(Location::SameAsFirstInput());
4807 break;
4808 default:
4809 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4810 UNREACHABLE();
4811 }
4812}
4813
4814void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4815 LocationSummary* locations = ror->GetLocations();
4816 Location first = locations->InAt(0);
4817 Location second = locations->InAt(1);
4818
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004819 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004820 Register first_reg = first.AsRegister<Register>();
4821 if (second.IsRegister()) {
4822 Register second_reg = second.AsRegister<Register>();
4823 __ rorl(first_reg, second_reg);
4824 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004825 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004826 __ rorl(first_reg, imm);
4827 }
4828 return;
4829 }
4830
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004831 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004832 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4833 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4834 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4835 if (second.IsRegister()) {
4836 Register second_reg = second.AsRegister<Register>();
4837 DCHECK_EQ(second_reg, ECX);
4838 __ movl(temp_reg, first_reg_hi);
4839 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4840 __ shrd(first_reg_lo, temp_reg, second_reg);
4841 __ movl(temp_reg, first_reg_hi);
4842 __ testl(second_reg, Immediate(32));
4843 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4844 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4845 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004846 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004847 if (shift_amt == 0) {
4848 // Already fine.
4849 return;
4850 }
4851 if (shift_amt == 32) {
4852 // Just swap.
4853 __ movl(temp_reg, first_reg_lo);
4854 __ movl(first_reg_lo, first_reg_hi);
4855 __ movl(first_reg_hi, temp_reg);
4856 return;
4857 }
4858
4859 Immediate imm(shift_amt);
4860 // Save the constents of the low value.
4861 __ movl(temp_reg, first_reg_lo);
4862
4863 // Shift right into low, feeding bits from high.
4864 __ shrd(first_reg_lo, first_reg_hi, imm);
4865
4866 // Shift right into high, feeding bits from the original low.
4867 __ shrd(first_reg_hi, temp_reg, imm);
4868
4869 // Swap if needed.
4870 if (shift_amt > 32) {
4871 __ movl(temp_reg, first_reg_lo);
4872 __ movl(first_reg_lo, first_reg_hi);
4873 __ movl(first_reg_hi, temp_reg);
4874 }
4875 }
4876}
4877
Calin Juravle9aec02f2014-11-18 23:06:35 +00004878void LocationsBuilderX86::VisitShl(HShl* shl) {
4879 HandleShift(shl);
4880}
4881
4882void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4883 HandleShift(shl);
4884}
4885
4886void LocationsBuilderX86::VisitShr(HShr* shr) {
4887 HandleShift(shr);
4888}
4889
4890void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4891 HandleShift(shr);
4892}
4893
4894void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4895 HandleShift(ushr);
4896}
4897
4898void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4899 HandleShift(ushr);
4900}
4901
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004902void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004903 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4904 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004905 locations->SetOut(Location::RegisterLocation(EAX));
Alex Lightd109e302018-06-27 10:25:41 -07004906 InvokeRuntimeCallingConvention calling_convention;
4907 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004908}
4909
4910void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07004911 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
4912 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4913 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004914}
4915
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004916void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004917 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4918 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004919 locations->SetOut(Location::RegisterLocation(EAX));
4920 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004921 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4922 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004923}
4924
4925void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01004926 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
4927 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004928 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004929 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004930 DCHECK(!codegen_->IsLeafMethod());
4931}
4932
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004933void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004934 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004935 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004936 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4937 if (location.IsStackSlot()) {
4938 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4939 } else if (location.IsDoubleStackSlot()) {
4940 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004941 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004942 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004943}
4944
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004945void InstructionCodeGeneratorX86::VisitParameterValue(
4946 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4947}
4948
4949void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4950 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004951 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004952 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4953}
4954
4955void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004956}
4957
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004958void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4959 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004960 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004961 locations->SetInAt(0, Location::RequiresRegister());
4962 locations->SetOut(Location::RequiresRegister());
4963}
4964
4965void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4966 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004967 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004968 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004969 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004970 __ movl(locations->Out().AsRegister<Register>(),
4971 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004972 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004973 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004974 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004975 __ movl(locations->Out().AsRegister<Register>(),
4976 Address(locations->InAt(0).AsRegister<Register>(),
4977 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4978 // temp = temp->GetImtEntryAt(method_offset);
4979 __ movl(locations->Out().AsRegister<Register>(),
4980 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004981 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004982}
4983
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004984void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004985 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004986 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004987 locations->SetInAt(0, Location::RequiresRegister());
4988 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004989}
4990
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004991void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4992 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004993 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004994 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004995 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004996 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004997 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004998 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004999 break;
5000
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005001 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01005002 __ notl(out.AsRegisterPairLow<Register>());
5003 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005004 break;
5005
5006 default:
5007 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
5008 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005009}
5010
David Brazdil66d126e2015-04-03 16:02:44 +01005011void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
5012 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005013 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01005014 locations->SetInAt(0, Location::RequiresRegister());
5015 locations->SetOut(Location::SameAsFirstInput());
5016}
5017
5018void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01005019 LocationSummary* locations = bool_not->GetLocations();
5020 Location in = locations->InAt(0);
5021 Location out = locations->Out();
5022 DCHECK(in.Equals(out));
5023 __ xorl(out.AsRegister<Register>(), Immediate(1));
5024}
5025
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005026void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005027 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005028 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00005029 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005030 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005031 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005032 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005033 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005034 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005035 case DataType::Type::kInt32:
5036 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00005037 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00005038 locations->SetInAt(1, Location::Any());
5039 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5040 break;
5041 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005042 case DataType::Type::kFloat32:
5043 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00005044 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00005045 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
5046 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
5047 } else if (compare->InputAt(1)->IsConstant()) {
5048 locations->SetInAt(1, Location::RequiresFpuRegister());
5049 } else {
5050 locations->SetInAt(1, Location::Any());
5051 }
Calin Juravleddb7df22014-11-25 20:56:51 +00005052 locations->SetOut(Location::RequiresRegister());
5053 break;
5054 }
5055 default:
5056 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5057 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005058}
5059
5060void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005061 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005062 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00005063 Location left = locations->InAt(0);
5064 Location right = locations->InAt(1);
5065
Mark Mendell0c9497d2015-08-21 09:30:05 -04005066 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08005067 Condition less_cond = kLess;
5068
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005069 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005070 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005071 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005072 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005073 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005074 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005075 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01005076 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08005077 break;
5078 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005079 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005080 Register left_low = left.AsRegisterPairLow<Register>();
5081 Register left_high = left.AsRegisterPairHigh<Register>();
5082 int32_t val_low = 0;
5083 int32_t val_high = 0;
5084 bool right_is_const = false;
5085
5086 if (right.IsConstant()) {
5087 DCHECK(right.GetConstant()->IsLongConstant());
5088 right_is_const = true;
5089 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
5090 val_low = Low32Bits(val);
5091 val_high = High32Bits(val);
5092 }
5093
Calin Juravleddb7df22014-11-25 20:56:51 +00005094 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005095 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005096 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005097 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005098 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005099 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08005100 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005101 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005102 __ j(kLess, &less); // Signed compare.
5103 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005104 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005105 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005106 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005107 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005108 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005109 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08005110 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005111 }
Aart Bika19616e2016-02-01 18:57:58 -08005112 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00005113 break;
5114 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005115 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00005116 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00005117 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08005118 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00005119 break;
5120 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005121 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00005122 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00005123 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08005124 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005125 break;
5126 }
5127 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00005128 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005129 }
Aart Bika19616e2016-02-01 18:57:58 -08005130
Calin Juravleddb7df22014-11-25 20:56:51 +00005131 __ movl(out, Immediate(0));
5132 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08005133 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00005134
5135 __ Bind(&greater);
5136 __ movl(out, Immediate(1));
5137 __ jmp(&done);
5138
5139 __ Bind(&less);
5140 __ movl(out, Immediate(-1));
5141
5142 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005143}
5144
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005145void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005146 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005147 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005148 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01005149 locations->SetInAt(i, Location::Any());
5150 }
5151 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005152}
5153
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005154void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005155 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005156}
5157
Roland Levillain7c1559a2015-12-15 10:55:36 +00005158void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00005159 /*
5160 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
5161 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
5162 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
5163 */
5164 switch (kind) {
5165 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00005166 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00005167 break;
5168 }
5169 case MemBarrierKind::kAnyStore:
5170 case MemBarrierKind::kLoadAny:
5171 case MemBarrierKind::kStoreStore: {
5172 // nop
5173 break;
5174 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05005175 case MemBarrierKind::kNTStoreStore:
5176 // Non-Temporal Store/Store needs an explicit fence.
Andreas Gampe3db70682018-12-26 15:12:03 -08005177 MemoryFence(/* non-temporal= */ true);
Mark Mendell7aa04a12016-01-27 22:39:07 -05005178 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005179 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005180}
5181
Vladimir Markodc151b22015-10-15 18:02:30 +01005182HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
5183 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01005184 ArtMethod* method ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005185 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005186}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005187
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005188Register CodeGeneratorX86::GetInvokeExtraParameter(HInvoke* invoke, Register temp) {
5189 if (invoke->IsInvokeStaticOrDirect()) {
5190 return GetInvokeStaticOrDirectExtraParameter(invoke->AsInvokeStaticOrDirect(), temp);
5191 }
5192 DCHECK(invoke->IsInvokeInterface());
5193 Location location =
5194 invoke->GetLocations()->InAt(invoke->AsInvokeInterface()->GetSpecialInputIndex());
5195 return location.AsRegister<Register>();
5196}
5197
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005198Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
5199 Register temp) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00005200 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005201 if (!invoke->GetLocations()->Intrinsified()) {
5202 return location.AsRegister<Register>();
5203 }
5204 // For intrinsics we allow any location, so it may be on the stack.
5205 if (!location.IsRegister()) {
5206 __ movl(temp, Address(ESP, location.GetStackIndex()));
5207 return temp;
5208 }
5209 // For register locations, check if the register was saved. If so, get it from the stack.
5210 // Note: There is a chance that the register was saved but not overwritten, so we could
5211 // save one load. However, since this is just an intrinsic slow path we prefer this
5212 // simple and more robust approach rather that trying to determine if that's the case.
5213 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00005214 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
5215 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
5216 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
5217 __ movl(temp, Address(ESP, stack_offset));
5218 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005219 }
5220 return location.AsRegister<Register>();
5221}
5222
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005223void CodeGeneratorX86::LoadMethod(MethodLoadKind load_kind, Location temp, HInvoke* invoke) {
5224 switch (load_kind) {
5225 case MethodLoadKind::kBootImageLinkTimePcRelative: {
5226 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
5227 Register base_reg = GetInvokeExtraParameter(invoke, temp.AsRegister<Register>());
5228 __ leal(temp.AsRegister<Register>(),
5229 Address(base_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
5230 RecordBootImageMethodPatch(invoke);
5231 break;
5232 }
5233 case MethodLoadKind::kBootImageRelRo: {
5234 size_t index = invoke->IsInvokeInterface()
5235 ? invoke->AsInvokeInterface()->GetSpecialInputIndex()
5236 : invoke->AsInvokeStaticOrDirect()->GetSpecialInputIndex();
5237 Register base_reg = GetInvokeExtraParameter(invoke, temp.AsRegister<Register>());
5238 __ movl(temp.AsRegister<Register>(), Address(base_reg, kPlaceholder32BitOffset));
5239 RecordBootImageRelRoPatch(
5240 invoke->InputAt(index)->AsX86ComputeBaseMethodAddress(),
5241 GetBootImageOffset(invoke));
5242 break;
5243 }
5244 case MethodLoadKind::kBssEntry: {
5245 Register base_reg = GetInvokeExtraParameter(invoke, temp.AsRegister<Register>());
5246 __ movl(temp.AsRegister<Register>(), Address(base_reg, kPlaceholder32BitOffset));
5247 RecordMethodBssEntryPatch(invoke);
5248 // No need for memory fence, thanks to the x86 memory model.
5249 break;
5250 }
5251 case MethodLoadKind::kJitDirectAddress: {
5252 __ movl(temp.AsRegister<Register>(),
5253 Immediate(reinterpret_cast32<uint32_t>(invoke->GetResolvedMethod())));
5254 break;
5255 }
5256 case MethodLoadKind::kRuntimeCall: {
5257 // Test situation, don't do anything.
5258 break;
5259 }
5260 default: {
5261 LOG(FATAL) << "Load kind should have already been handled " << load_kind;
5262 UNREACHABLE();
5263 }
5264 }
5265}
5266
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005267void CodeGeneratorX86::GenerateStaticOrDirectCall(
5268 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00005269 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
5270 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005271 case MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005272 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005273 uint32_t offset =
5274 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
5275 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00005276 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005277 }
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005278 case MethodLoadKind::kRecursive: {
Vladimir Marko86c87522020-05-11 16:55:55 +01005279 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005280 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005281 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005282 case MethodLoadKind::kRuntimeCall: {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005283 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5284 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01005285 }
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005286 case MethodLoadKind::kBootImageLinkTimePcRelative:
5287 // For kCallCriticalNative we skip loading the method and do the call directly.
5288 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
5289 break;
5290 }
5291 FALLTHROUGH_INTENDED;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005292 default: {
5293 LoadMethod(invoke->GetMethodLoadKind(), callee_method, invoke);
5294 }
Vladimir Marko58155012015-08-19 12:49:41 +00005295 }
5296
5297 switch (invoke->GetCodePtrLocation()) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005298 case CodePtrLocation::kCallSelf:
Nicolas Geoffray282795c2021-09-24 18:16:41 +01005299 DCHECK(!GetGraph()->HasShouldDeoptimizeFlag());
Vladimir Marko58155012015-08-19 12:49:41 +00005300 __ call(GetFrameEntryLabel());
Vladimir Marko86c87522020-05-11 16:55:55 +01005301 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko58155012015-08-19 12:49:41 +00005302 break;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005303 case CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01005304 size_t out_frame_size =
5305 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorX86,
5306 kNativeStackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01005307 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005308 if (invoke->GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative) {
5309 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
5310 Register base_reg = GetInvokeExtraParameter(invoke, temp.AsRegister<Register>());
5311 __ call(Address(base_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
5312 RecordBootImageJniEntrypointPatch(invoke);
5313 } else {
5314 // (callee_method + offset_of_jni_entry_point)()
5315 __ call(Address(callee_method.AsRegister<Register>(),
5316 ArtMethod::EntryPointFromJniOffset(kX86PointerSize).Int32Value()));
5317 }
Vladimir Marko86c87522020-05-11 16:55:55 +01005318 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5319 if (out_frame_size == 0u && DataType::IsFloatingPointType(invoke->GetType())) {
5320 // Create space for conversion.
5321 out_frame_size = 8u;
Vladimir Markodec78172020-06-19 15:31:23 +01005322 IncreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01005323 }
5324 // Zero-/sign-extend or move the result when needed due to native and managed ABI mismatch.
5325 switch (invoke->GetType()) {
5326 case DataType::Type::kBool:
5327 __ movzxb(EAX, AL);
5328 break;
5329 case DataType::Type::kInt8:
5330 __ movsxb(EAX, AL);
5331 break;
5332 case DataType::Type::kUint16:
5333 __ movzxw(EAX, EAX);
5334 break;
5335 case DataType::Type::kInt16:
5336 __ movsxw(EAX, EAX);
5337 break;
5338 case DataType::Type::kFloat32:
5339 __ fstps(Address(ESP, 0));
5340 __ movss(XMM0, Address(ESP, 0));
5341 break;
5342 case DataType::Type::kFloat64:
5343 __ fstpl(Address(ESP, 0));
5344 __ movsd(XMM0, Address(ESP, 0));
5345 break;
5346 case DataType::Type::kInt32:
5347 case DataType::Type::kInt64:
5348 case DataType::Type::kVoid:
5349 break;
5350 default:
5351 DCHECK(false) << invoke->GetType();
5352 break;
5353 }
5354 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01005355 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01005356 }
5357 break;
5358 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005359 case CodePtrLocation::kCallArtMethod:
Vladimir Marko58155012015-08-19 12:49:41 +00005360 // (callee_method + offset_of_quick_compiled_code)()
5361 __ call(Address(callee_method.AsRegister<Register>(),
5362 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005363 kX86PointerSize).Int32Value()));
Vladimir Marko86c87522020-05-11 16:55:55 +01005364 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko58155012015-08-19 12:49:41 +00005365 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04005366 }
5367
5368 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04005369}
5370
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005371void CodeGeneratorX86::GenerateVirtualCall(
5372 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005373 Register temp = temp_in.AsRegister<Register>();
5374 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5375 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005376
5377 // Use the calling convention instead of the location of the receiver, as
5378 // intrinsics may have put the receiver in a different register. In the intrinsics
5379 // slow path, the arguments have been moved to the right place, so here we are
5380 // guaranteed that the receiver is the first register of the calling convention.
5381 InvokeDexCallingConvention calling_convention;
5382 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005383 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005384 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005385 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005386 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005387 // Instead of simply (possibly) unpoisoning `temp` here, we should
5388 // emit a read barrier for the previous class reference load.
5389 // However this is not required in practice, as this is an
5390 // intermediate/temporary reference and because the current
5391 // concurrent copying collector keeps the from-space memory
5392 // intact/accessible until the end of the marking phase (the
5393 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005394 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00005395
5396 MaybeGenerateInlineCacheCheck(invoke, temp);
5397
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005398 // temp = temp->GetMethodAt(method_offset);
5399 __ movl(temp, Address(temp, method_offset));
5400 // call temp->GetEntryPoint();
5401 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07005402 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005403 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005404}
5405
Vladimir Marko6fd16062018-06-26 11:02:04 +01005406void CodeGeneratorX86::RecordBootImageIntrinsicPatch(HX86ComputeBaseMethodAddress* method_address,
5407 uint32_t intrinsic_data) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005408 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005409 method_address, /* target_dex_file= */ nullptr, intrinsic_data);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005410 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005411}
5412
Vladimir Markob066d432018-01-03 13:14:37 +00005413void CodeGeneratorX86::RecordBootImageRelRoPatch(HX86ComputeBaseMethodAddress* method_address,
5414 uint32_t boot_image_offset) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005415 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005416 method_address, /* target_dex_file= */ nullptr, boot_image_offset);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005417 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Markob066d432018-01-03 13:14:37 +00005418}
5419
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005420void CodeGeneratorX86::RecordBootImageMethodPatch(HInvoke* invoke) {
5421 size_t index = invoke->IsInvokeInterface()
5422 ? invoke->AsInvokeInterface()->GetSpecialInputIndex()
5423 : invoke->AsInvokeStaticOrDirect()->GetSpecialInputIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005424 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005425 invoke->InputAt(index)->AsX86ComputeBaseMethodAddress();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005426 boot_image_method_patches_.emplace_back(
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005427 method_address,
5428 invoke->GetResolvedMethodReference().dex_file,
5429 invoke->GetResolvedMethodReference().index);
Vladimir Marko65979462017-05-19 17:25:12 +01005430 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005431}
5432
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005433void CodeGeneratorX86::RecordMethodBssEntryPatch(HInvoke* invoke) {
5434 size_t index = invoke->IsInvokeInterface()
5435 ? invoke->AsInvokeInterface()->GetSpecialInputIndex()
5436 : invoke->AsInvokeStaticOrDirect()->GetSpecialInputIndex();
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005437 DCHECK(IsSameDexFile(GetGraph()->GetDexFile(), *invoke->GetMethodReference().dex_file));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005438 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005439 invoke->InputAt(index)->AsX86ComputeBaseMethodAddress();
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005440 // Add the patch entry and bind its label at the end of the instruction.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005441 method_bss_entry_patches_.emplace_back(
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005442 method_address,
5443 invoke->GetMethodReference().dex_file,
5444 invoke->GetMethodReference().index);
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005445 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005446}
5447
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005448void CodeGeneratorX86::RecordBootImageTypePatch(HLoadClass* load_class) {
5449 HX86ComputeBaseMethodAddress* method_address =
5450 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5451 boot_image_type_patches_.emplace_back(
5452 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005453 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005454}
5455
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005456Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005457 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005458 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko8f63f102020-09-28 12:10:28 +01005459 ArenaDeque<X86PcRelativePatchInfo>* patches = nullptr;
5460 switch (load_class->GetLoadKind()) {
5461 case HLoadClass::LoadKind::kBssEntry:
5462 patches = &type_bss_entry_patches_;
5463 break;
5464 case HLoadClass::LoadKind::kBssEntryPublic:
5465 patches = &public_type_bss_entry_patches_;
5466 break;
5467 case HLoadClass::LoadKind::kBssEntryPackage:
5468 patches = &package_type_bss_entry_patches_;
5469 break;
5470 default:
5471 LOG(FATAL) << "Unexpected load kind: " << load_class->GetLoadKind();
5472 UNREACHABLE();
5473 }
5474 patches->emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005475 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005476 return &patches->back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005477}
5478
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005479void CodeGeneratorX86::RecordBootImageStringPatch(HLoadString* load_string) {
5480 HX86ComputeBaseMethodAddress* method_address =
5481 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
5482 boot_image_string_patches_.emplace_back(
5483 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
5484 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01005485}
5486
Vladimir Markoaad75c62016-10-03 08:46:48 +00005487Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005488 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005489 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005490 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005491 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005492 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005493}
5494
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005495void CodeGeneratorX86::RecordBootImageJniEntrypointPatch(HInvokeStaticOrDirect* invoke) {
5496 HX86ComputeBaseMethodAddress* method_address =
5497 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5498 boot_image_jni_entrypoint_patches_.emplace_back(
5499 method_address,
5500 invoke->GetResolvedMethodReference().dex_file,
5501 invoke->GetResolvedMethodReference().index);
5502 __ Bind(&boot_image_jni_entrypoint_patches_.back().label);
5503}
5504
Vladimir Markoeebb8212018-06-05 14:57:24 +01005505void CodeGeneratorX86::LoadBootImageAddress(Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01005506 uint32_t boot_image_reference,
Vladimir Markoeebb8212018-06-05 14:57:24 +01005507 HInvokeStaticOrDirect* invoke) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005508 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005509 HX86ComputeBaseMethodAddress* method_address =
5510 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5511 DCHECK(method_address != nullptr);
5512 Register method_address_reg =
5513 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005514 __ leal(reg, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005515 RecordBootImageIntrinsicPatch(method_address, boot_image_reference);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01005516 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01005517 HX86ComputeBaseMethodAddress* method_address =
5518 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5519 DCHECK(method_address != nullptr);
5520 Register method_address_reg =
5521 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005522 __ movl(reg, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005523 RecordBootImageRelRoPatch(method_address, boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01005524 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01005525 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01005526 gc::Heap* heap = Runtime::Current()->GetHeap();
5527 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01005528 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01005529 __ movl(reg, Immediate(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address))));
5530 }
5531}
5532
Vladimir Markode91ca92020-10-27 13:41:40 +00005533void CodeGeneratorX86::LoadIntrinsicDeclaringClass(Register reg, HInvokeStaticOrDirect* invoke) {
5534 DCHECK_NE(invoke->GetIntrinsic(), Intrinsics::kNone);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005535 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005536 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
Vladimir Marko6fd16062018-06-26 11:02:04 +01005537 HX86ComputeBaseMethodAddress* method_address =
5538 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5539 DCHECK(method_address != nullptr);
5540 Register method_address_reg =
5541 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Markode91ca92020-10-27 13:41:40 +00005542 __ leal(reg, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005543 MethodReference target_method = invoke->GetResolvedMethodReference();
Vladimir Marko6fd16062018-06-26 11:02:04 +01005544 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
5545 boot_image_type_patches_.emplace_back(method_address, target_method.dex_file, type_idx.index_);
5546 __ Bind(&boot_image_type_patches_.back().label);
5547 } else {
Vladimir Markode91ca92020-10-27 13:41:40 +00005548 uint32_t boot_image_offset = GetBootImageOffsetOfIntrinsicDeclaringClass(invoke);
5549 LoadBootImageAddress(reg, boot_image_offset, invoke);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005550 }
Vladimir Marko6fd16062018-06-26 11:02:04 +01005551}
5552
Vladimir Markoaad75c62016-10-03 08:46:48 +00005553// The label points to the end of the "movl" or another instruction but the literal offset
5554// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
5555constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
5556
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005557template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005558inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005559 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005560 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005561 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005562 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005563 linker_patches->push_back(Factory(literal_offset,
5564 info.target_dex_file,
5565 GetMethodAddressOffset(info.method_address),
5566 info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005567 }
5568}
5569
Vladimir Marko6fd16062018-06-26 11:02:04 +01005570template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
5571linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
5572 const DexFile* target_dex_file,
5573 uint32_t pc_insn_offset,
5574 uint32_t boot_image_offset) {
5575 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5576 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005577}
5578
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005579void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005580 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005581 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01005582 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005583 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00005584 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01005585 type_bss_entry_patches_.size() +
Vladimir Marko8f63f102020-09-28 12:10:28 +01005586 public_type_bss_entry_patches_.size() +
5587 package_type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005588 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01005589 string_bss_entry_patches_.size() +
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005590 boot_image_jni_entrypoint_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01005591 boot_image_other_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005592 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01005593 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005594 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
5595 boot_image_method_patches_, linker_patches);
5596 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
5597 boot_image_type_patches_, linker_patches);
5598 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005599 boot_image_string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005600 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005601 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005602 DCHECK(boot_image_type_patches_.empty());
5603 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01005604 }
5605 if (GetCompilerOptions().IsBootImage()) {
5606 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5607 boot_image_other_patches_, linker_patches);
5608 } else {
5609 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
5610 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005611 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005612 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5613 method_bss_entry_patches_, linker_patches);
5614 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5615 type_bss_entry_patches_, linker_patches);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005616 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PublicTypeBssEntryPatch>(
5617 public_type_bss_entry_patches_, linker_patches);
5618 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PackageTypeBssEntryPatch>(
5619 package_type_bss_entry_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005620 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5621 string_bss_entry_patches_, linker_patches);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005622 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeJniEntrypointPatch>(
5623 boot_image_jni_entrypoint_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005624 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005625}
5626
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005627void CodeGeneratorX86::MarkGCCard(Register temp,
5628 Register card,
5629 Register object,
5630 Register value,
5631 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005632 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005633 if (value_can_be_null) {
5634 __ testl(value, value);
5635 __ j(kEqual, &is_null);
5636 }
Roland Levillainc73f0522018-08-14 15:16:50 +01005637 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07005638 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01005639 // Calculate the offset (in the card table) of the card corresponding to
5640 // `object`.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005641 __ movl(temp, object);
5642 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01005643 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
5644 // `object`'s card.
5645 //
5646 // Register `card` contains the address of the card table. Note that the card
5647 // table's base is biased during its creation so that it always starts at an
5648 // address whose least-significant byte is equal to `kCardDirty` (see
5649 // art::gc::accounting::CardTable::Create). Therefore the MOVB instruction
5650 // below writes the `kCardDirty` (byte) value into the `object`'s card
5651 // (located at `card + object >> kCardShift`).
5652 //
5653 // This dual use of the value in register `card` (1. to calculate the location
5654 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
5655 // (no need to explicitly load `kCardDirty` as an immediate value).
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00005656 __ movb(Address(temp, card, TIMES_1, 0),
5657 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005658 if (value_can_be_null) {
5659 __ Bind(&is_null);
5660 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005661}
5662
Calin Juravle52c48962014-12-16 17:02:57 +00005663void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Alex Light3a73ffb2021-01-25 14:11:05 +00005664 DCHECK(instruction->IsInstanceFieldGet() ||
5665 instruction->IsStaticFieldGet() ||
5666 instruction->IsPredicatedInstanceFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005667
5668 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005669 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Alex Light3a73ffb2021-01-25 14:11:05 +00005670 bool is_predicated = instruction->IsPredicatedInstanceFieldGet();
Nicolas Geoffray39468442014-09-02 15:17:15 +01005671 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005672 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5673 kEmitCompilerReadBarrier
5674 ? LocationSummary::kCallOnSlowPath
5675 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005676 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005677 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005678 }
Alex Light3a73ffb2021-01-25 14:11:05 +00005679 // receiver_input
5680 locations->SetInAt(is_predicated ? 1 : 0, Location::RequiresRegister());
5681 if (is_predicated) {
5682 if (DataType::IsFloatingPointType(instruction->GetType())) {
5683 locations->SetInAt(0, Location::RequiresFpuRegister());
5684 } else {
5685 locations->SetInAt(0, Location::RequiresRegister());
5686 }
5687 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005688 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alex Light3a73ffb2021-01-25 14:11:05 +00005689 locations->SetOut(is_predicated ? Location::SameAsFirstInput()
5690 : Location::RequiresFpuRegister());
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005691 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005692 // The output overlaps in case of long: we don't want the low move
5693 // to overwrite the object's location. Likewise, in the case of
5694 // an object field get with read barriers enabled, we do not want
5695 // the move to overwrite the object's location, as we need it to emit
5696 // the read barrier.
Alex Light3a73ffb2021-01-25 14:11:05 +00005697 locations->SetOut(is_predicated ? Location::SameAsFirstInput() : Location::RequiresRegister(),
5698 (object_field_get_with_read_barrier ||
5699 instruction->GetType() == DataType::Type::kInt64 ||
5700 is_predicated)
5701 ? Location::kOutputOverlap
5702 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005703 }
Calin Juravle52c48962014-12-16 17:02:57 +00005704
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005705 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00005706 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005707 // So we use an XMM register as a temp to achieve atomicity (first
5708 // load the temp into the XMM and then copy the XMM into the
5709 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00005710 locations->AddTemp(Location::RequiresFpuRegister());
5711 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005712}
5713
Calin Juravle52c48962014-12-16 17:02:57 +00005714void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
5715 const FieldInfo& field_info) {
Alex Light3a73ffb2021-01-25 14:11:05 +00005716 DCHECK(instruction->IsInstanceFieldGet() ||
5717 instruction->IsStaticFieldGet() ||
5718 instruction->IsPredicatedInstanceFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005719
Calin Juravle52c48962014-12-16 17:02:57 +00005720 LocationSummary* locations = instruction->GetLocations();
Alex Light3a73ffb2021-01-25 14:11:05 +00005721 Location base_loc = locations->InAt(instruction->IsPredicatedInstanceFieldGet() ? 1 : 0);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005722 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005723 Location out = locations->Out();
5724 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01005725 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5726 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00005727 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5728
Ulya Trafimovich322eced2021-06-02 15:39:36 +01005729 if (load_type == DataType::Type::kReference) {
5730 // /* HeapReference<Object> */ out = *(base + offset)
5731 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5732 // Note that a potential implicit null check is handled in this
5733 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
5734 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5735 instruction, out, base, offset, /* needs_null_check= */ true);
Calin Juravle52c48962014-12-16 17:02:57 +00005736 if (is_volatile) {
Ulya Trafimovich322eced2021-06-02 15:39:36 +01005737 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005738 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00005739 } else {
Ulya Trafimovich322eced2021-06-02 15:39:36 +01005740 __ movl(out.AsRegister<Register>(), Address(base, offset));
5741 codegen_->MaybeRecordImplicitNullCheck(instruction);
5742 if (is_volatile) {
5743 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5744 }
5745 // If read barriers are enabled, emit read barriers other than
5746 // Baker's using a slow path (and also unpoison the loaded
5747 // reference, if heap poisoning is enabled).
5748 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5749 }
5750 } else {
5751 Address src(base, offset);
5752 XmmRegister temp = (load_type == DataType::Type::kInt64 && is_volatile)
5753 ? locations->GetTemp(0).AsFpuRegister<XmmRegister>()
5754 : kNoXmmRegister;
5755 codegen_->LoadFromMemoryNoBarrier(load_type, out, src, instruction, temp, is_volatile);
5756 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005757 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5758 }
Roland Levillain4d027112015-07-01 15:41:14 +01005759 }
Calin Juravle52c48962014-12-16 17:02:57 +00005760}
5761
5762void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5763 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5764
5765 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005766 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00005767 locations->SetInAt(0, Location::RequiresRegister());
5768 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005769 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005770 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00005771
5772 // The register allocator does not support multiple
5773 // inputs that die at entry with one in a specific register.
5774 if (is_byte_type) {
5775 // Ensure the value is in a byte register.
5776 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005777 } else if (DataType::IsFloatingPointType(field_type)) {
5778 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05005779 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5780 locations->SetInAt(1, Location::RequiresFpuRegister());
5781 } else {
5782 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
5783 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005784 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05005785 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00005786 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005787
Calin Juravle52c48962014-12-16 17:02:57 +00005788 // 64bits value can be atomically written to an address with movsd and an XMM register.
5789 // We need two XMM registers because there's no easier way to (bit) copy a register pair
5790 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
5791 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
5792 // isolated cases when we need this it isn't worth adding the extra complexity.
5793 locations->AddTemp(Location::RequiresFpuRegister());
5794 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005795 } else {
5796 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5797
5798 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5799 // Temporary registers for the write barrier.
5800 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
5801 // Ensure the card is in a byte register.
5802 locations->AddTemp(Location::RegisterLocation(ECX));
5803 }
Calin Juravle52c48962014-12-16 17:02:57 +00005804 }
5805}
5806
5807void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Andra Danciucde98192020-09-13 12:32:09 +00005808 uint32_t value_index,
5809 DataType::Type field_type,
5810 Address field_addr,
5811 Register base,
5812 bool is_volatile,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005813 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005814 LocationSummary* locations = instruction->GetLocations();
Andra Danciucde98192020-09-13 12:32:09 +00005815 Location value = locations->InAt(value_index);
Roland Levillain4d027112015-07-01 15:41:14 +01005816 bool needs_write_barrier =
Andra Danciucde98192020-09-13 12:32:09 +00005817 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(value_index));
Calin Juravle52c48962014-12-16 17:02:57 +00005818
5819 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005820 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005821 }
5822
Mark Mendell81489372015-11-04 11:30:41 -05005823 bool maybe_record_implicit_null_check_done = false;
5824
Calin Juravle52c48962014-12-16 17:02:57 +00005825 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005826 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005827 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005828 case DataType::Type::kInt8: {
Andra Danciucde98192020-09-13 12:32:09 +00005829 if (value.IsConstant()) {
5830 __ movb(field_addr, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
5831 } else {
5832 __ movb(field_addr, value.AsRegister<ByteRegister>());
5833 }
Calin Juravle52c48962014-12-16 17:02:57 +00005834 break;
5835 }
5836
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005837 case DataType::Type::kUint16:
5838 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05005839 if (value.IsConstant()) {
Andra Danciucde98192020-09-13 12:32:09 +00005840 __ movw(field_addr, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05005841 } else {
Andra Danciucde98192020-09-13 12:32:09 +00005842 __ movw(field_addr, value.AsRegister<Register>());
Mark Mendell81489372015-11-04 11:30:41 -05005843 }
Calin Juravle52c48962014-12-16 17:02:57 +00005844 break;
5845 }
5846
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005847 case DataType::Type::kInt32:
5848 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01005849 if (kPoisonHeapReferences && needs_write_barrier) {
5850 // Note that in the case where `value` is a null reference,
5851 // we do not enter this block, as the reference does not
5852 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005853 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01005854 Register temp = locations->GetTemp(0).AsRegister<Register>();
5855 __ movl(temp, value.AsRegister<Register>());
5856 __ PoisonHeapReference(temp);
Andra Danciucde98192020-09-13 12:32:09 +00005857 __ movl(field_addr, temp);
Mark Mendell81489372015-11-04 11:30:41 -05005858 } else if (value.IsConstant()) {
5859 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Andra Danciucde98192020-09-13 12:32:09 +00005860 __ movl(field_addr, Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005861 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005862 DCHECK(value.IsRegister()) << value;
Andra Danciucde98192020-09-13 12:32:09 +00005863 __ movl(field_addr, value.AsRegister<Register>());
Roland Levillain4d027112015-07-01 15:41:14 +01005864 }
Calin Juravle52c48962014-12-16 17:02:57 +00005865 break;
5866 }
5867
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005868 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005869 if (is_volatile) {
5870 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5871 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5872 __ movd(temp1, value.AsRegisterPairLow<Register>());
5873 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5874 __ punpckldq(temp1, temp2);
Andra Danciucde98192020-09-13 12:32:09 +00005875 __ movsd(field_addr, temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005876 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005877 } else if (value.IsConstant()) {
5878 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
Andra Danciucde98192020-09-13 12:32:09 +00005879 __ movl(field_addr, Immediate(Low32Bits(v)));
Mark Mendell81489372015-11-04 11:30:41 -05005880 codegen_->MaybeRecordImplicitNullCheck(instruction);
Ulya Trafimovich893e2ed2021-06-10 16:18:12 +01005881 __ movl(Address::displace(field_addr, kX86WordSize), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005882 } else {
Andra Danciucde98192020-09-13 12:32:09 +00005883 __ movl(field_addr, value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005884 codegen_->MaybeRecordImplicitNullCheck(instruction);
Ulya Trafimovich893e2ed2021-06-10 16:18:12 +01005885 __ movl(Address::displace(field_addr, kX86WordSize), value.AsRegisterPairHigh<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00005886 }
Mark Mendell81489372015-11-04 11:30:41 -05005887 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005888 break;
5889 }
5890
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005891 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05005892 if (value.IsConstant()) {
5893 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Andra Danciucde98192020-09-13 12:32:09 +00005894 __ movl(field_addr, Immediate(v));
Mark Mendell81489372015-11-04 11:30:41 -05005895 } else {
Andra Danciucde98192020-09-13 12:32:09 +00005896 __ movss(field_addr, value.AsFpuRegister<XmmRegister>());
Mark Mendell81489372015-11-04 11:30:41 -05005897 }
Calin Juravle52c48962014-12-16 17:02:57 +00005898 break;
5899 }
5900
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005901 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005902 if (value.IsConstant()) {
Andra Danciuc992e422020-09-16 08:12:02 +00005903 DCHECK(!is_volatile);
Mark Mendell81489372015-11-04 11:30:41 -05005904 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
Andra Danciucde98192020-09-13 12:32:09 +00005905 __ movl(field_addr, Immediate(Low32Bits(v)));
Mark Mendell81489372015-11-04 11:30:41 -05005906 codegen_->MaybeRecordImplicitNullCheck(instruction);
Ulya Trafimovich893e2ed2021-06-10 16:18:12 +01005907 __ movl(Address::displace(field_addr, kX86WordSize), Immediate(High32Bits(v)));
Mark Mendell81489372015-11-04 11:30:41 -05005908 maybe_record_implicit_null_check_done = true;
5909 } else {
Andra Danciucde98192020-09-13 12:32:09 +00005910 __ movsd(field_addr, value.AsFpuRegister<XmmRegister>());
Mark Mendell81489372015-11-04 11:30:41 -05005911 }
Calin Juravle52c48962014-12-16 17:02:57 +00005912 break;
5913 }
5914
Aart Bik66c158e2018-01-31 12:55:04 -08005915 case DataType::Type::kUint32:
5916 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005917 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005918 LOG(FATAL) << "Unreachable type " << field_type;
5919 UNREACHABLE();
5920 }
5921
Mark Mendell81489372015-11-04 11:30:41 -05005922 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005923 codegen_->MaybeRecordImplicitNullCheck(instruction);
5924 }
5925
Roland Levillain4d027112015-07-01 15:41:14 +01005926 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005927 Register temp = locations->GetTemp(0).AsRegister<Register>();
5928 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005929 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005930 }
5931
Calin Juravle52c48962014-12-16 17:02:57 +00005932 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005933 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005934 }
5935}
5936
Andra Danciucde98192020-09-13 12:32:09 +00005937void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
5938 const FieldInfo& field_info,
5939 bool value_can_be_null) {
5940 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5941
5942 LocationSummary* locations = instruction->GetLocations();
5943 Register base = locations->InAt(0).AsRegister<Register>();
5944 bool is_volatile = field_info.IsVolatile();
5945 DataType::Type field_type = field_info.GetFieldType();
5946 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alex Light3a73ffb2021-01-25 14:11:05 +00005947 bool is_predicated =
5948 instruction->IsInstanceFieldSet() && instruction->AsInstanceFieldSet()->GetIsPredicatedSet();
Andra Danciucde98192020-09-13 12:32:09 +00005949
5950 Address field_addr(base, offset);
5951
Alex Light3a73ffb2021-01-25 14:11:05 +00005952 NearLabel pred_is_null;
5953 if (is_predicated) {
5954 __ testl(base, base);
5955 __ j(kEqual, &pred_is_null);
5956 }
5957
Andra Danciucde98192020-09-13 12:32:09 +00005958 HandleFieldSet(instruction,
5959 /* value_index= */ 1,
5960 field_type,
5961 field_addr,
5962 base,
5963 is_volatile,
5964 value_can_be_null);
Alex Light3a73ffb2021-01-25 14:11:05 +00005965
5966 if (is_predicated) {
5967 __ Bind(&pred_is_null);
5968 }
Andra Danciucde98192020-09-13 12:32:09 +00005969}
5970
Calin Juravle52c48962014-12-16 17:02:57 +00005971void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5972 HandleFieldGet(instruction, instruction->GetFieldInfo());
5973}
5974
5975void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5976 HandleFieldGet(instruction, instruction->GetFieldInfo());
5977}
5978
5979void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5980 HandleFieldSet(instruction, instruction->GetFieldInfo());
5981}
5982
5983void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005984 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005985}
5986
5987void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5988 HandleFieldSet(instruction, instruction->GetFieldInfo());
5989}
5990
5991void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005992 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005993}
5994
Alex Light3a73ffb2021-01-25 14:11:05 +00005995void LocationsBuilderX86::VisitPredicatedInstanceFieldGet(
5996 HPredicatedInstanceFieldGet* instruction) {
5997 HandleFieldGet(instruction, instruction->GetFieldInfo());
5998}
5999
Calin Juravle52c48962014-12-16 17:02:57 +00006000void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6001 HandleFieldGet(instruction, instruction->GetFieldInfo());
6002}
6003
Alex Light3a73ffb2021-01-25 14:11:05 +00006004void InstructionCodeGeneratorX86::VisitPredicatedInstanceFieldGet(
6005 HPredicatedInstanceFieldGet* instruction) {
6006 NearLabel finish;
6007 LocationSummary* locations = instruction->GetLocations();
6008 Register recv = locations->InAt(1).AsRegister<Register>();
6009 __ testl(recv, recv);
6010 __ j(kZero, &finish);
6011 HandleFieldGet(instruction, instruction->GetFieldInfo());
6012 __ Bind(&finish);
6013}
Calin Juravle52c48962014-12-16 17:02:57 +00006014void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6015 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006016}
6017
Vladimir Marko552a1342017-10-31 10:56:47 +00006018void LocationsBuilderX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6019 codegen_->CreateStringBuilderAppendLocations(instruction, Location::RegisterLocation(EAX));
6020}
6021
6022void InstructionCodeGeneratorX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6023 __ movl(EAX, Immediate(instruction->GetFormat()->GetValue()));
6024 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
6025}
6026
Calin Juravlee460d1d2015-09-29 04:52:17 +01006027void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
6028 HUnresolvedInstanceFieldGet* instruction) {
6029 FieldAccessCallingConventionX86 calling_convention;
6030 codegen_->CreateUnresolvedFieldLocationSummary(
6031 instruction, instruction->GetFieldType(), calling_convention);
6032}
6033
6034void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
6035 HUnresolvedInstanceFieldGet* instruction) {
6036 FieldAccessCallingConventionX86 calling_convention;
6037 codegen_->GenerateUnresolvedFieldAccess(instruction,
6038 instruction->GetFieldType(),
6039 instruction->GetFieldIndex(),
6040 instruction->GetDexPc(),
6041 calling_convention);
6042}
6043
6044void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
6045 HUnresolvedInstanceFieldSet* instruction) {
6046 FieldAccessCallingConventionX86 calling_convention;
6047 codegen_->CreateUnresolvedFieldLocationSummary(
6048 instruction, instruction->GetFieldType(), calling_convention);
6049}
6050
6051void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
6052 HUnresolvedInstanceFieldSet* instruction) {
6053 FieldAccessCallingConventionX86 calling_convention;
6054 codegen_->GenerateUnresolvedFieldAccess(instruction,
6055 instruction->GetFieldType(),
6056 instruction->GetFieldIndex(),
6057 instruction->GetDexPc(),
6058 calling_convention);
6059}
6060
6061void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
6062 HUnresolvedStaticFieldGet* instruction) {
6063 FieldAccessCallingConventionX86 calling_convention;
6064 codegen_->CreateUnresolvedFieldLocationSummary(
6065 instruction, instruction->GetFieldType(), calling_convention);
6066}
6067
6068void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
6069 HUnresolvedStaticFieldGet* instruction) {
6070 FieldAccessCallingConventionX86 calling_convention;
6071 codegen_->GenerateUnresolvedFieldAccess(instruction,
6072 instruction->GetFieldType(),
6073 instruction->GetFieldIndex(),
6074 instruction->GetDexPc(),
6075 calling_convention);
6076}
6077
6078void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
6079 HUnresolvedStaticFieldSet* instruction) {
6080 FieldAccessCallingConventionX86 calling_convention;
6081 codegen_->CreateUnresolvedFieldLocationSummary(
6082 instruction, instruction->GetFieldType(), calling_convention);
6083}
6084
6085void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
6086 HUnresolvedStaticFieldSet* instruction) {
6087 FieldAccessCallingConventionX86 calling_convention;
6088 codegen_->GenerateUnresolvedFieldAccess(instruction,
6089 instruction->GetFieldType(),
6090 instruction->GetFieldIndex(),
6091 instruction->GetDexPc(),
6092 calling_convention);
6093}
6094
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006095void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006096 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6097 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
6098 ? Location::RequiresRegister()
6099 : Location::Any();
6100 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006101}
6102
Calin Juravle2ae48182016-03-16 14:05:09 +00006103void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
6104 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00006105 return;
6106 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006107 LocationSummary* locations = instruction->GetLocations();
6108 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00006109
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006110 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00006111 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006112}
6113
Calin Juravle2ae48182016-03-16 14:05:09 +00006114void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006115 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006116 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006117
6118 LocationSummary* locations = instruction->GetLocations();
6119 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006120
6121 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04006122 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006123 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006124 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006125 } else {
6126 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00006127 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006128 __ jmp(slow_path->GetEntryLabel());
6129 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006130 }
6131 __ j(kEqual, slow_path->GetEntryLabel());
6132}
6133
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006134void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006135 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006136}
6137
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006138void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006139 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006140 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006141 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006142 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
6143 object_array_get_with_read_barrier
6144 ? LocationSummary::kCallOnSlowPath
6145 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01006146 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006147 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006148 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006149 locations->SetInAt(0, Location::RequiresRegister());
6150 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006151 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01006152 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6153 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006154 // The output overlaps in case of long: we don't want the low move
6155 // to overwrite the array's location. Likewise, in the case of an
6156 // object array get with read barriers enabled, we do not want the
6157 // move to overwrite the array's location, as we need it to emit
6158 // the read barrier.
6159 locations->SetOut(
6160 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006161 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
6162 ? Location::kOutputOverlap
6163 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01006164 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006165}
6166
6167void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
6168 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006169 Location obj_loc = locations->InAt(0);
6170 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006171 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006172 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006173 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006174
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006175 DataType::Type type = instruction->GetType();
Ulya Trafimovich322eced2021-06-02 15:39:36 +01006176 if (type == DataType::Type::kReference) {
6177 static_assert(
6178 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6179 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6180 // /* HeapReference<Object> */ out =
6181 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6182 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
6183 // Note that a potential implicit null check is handled in this
6184 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
6185 codegen_->GenerateArrayLoadWithBakerReadBarrier(
6186 instruction, out_loc, obj, data_offset, index, /* needs_null_check= */ true);
6187 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006188 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006189 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006190 codegen_->MaybeRecordImplicitNullCheck(instruction);
Ulya Trafimovich322eced2021-06-02 15:39:36 +01006191 // If read barriers are enabled, emit read barriers other than
6192 // Baker's using a slow path (and also unpoison the loaded
6193 // reference, if heap poisoning is enabled).
6194 if (index.IsConstant()) {
6195 uint32_t offset =
6196 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
6197 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6198 } else {
6199 codegen_->MaybeGenerateReadBarrierSlow(
6200 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6201 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006202 }
Ulya Trafimovich322eced2021-06-02 15:39:36 +01006203 } else if (type == DataType::Type::kUint16
6204 && mirror::kUseStringCompression
6205 && instruction->IsStringCharAt()) {
6206 // Branch cases into compressed and uncompressed for each index's type.
6207 Register out = out_loc.AsRegister<Register>();
6208 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
6209 NearLabel done, not_compressed;
6210 __ testb(Address(obj, count_offset), Immediate(1));
Calin Juravle77520bc2015-01-12 18:45:46 +00006211 codegen_->MaybeRecordImplicitNullCheck(instruction);
Ulya Trafimovich322eced2021-06-02 15:39:36 +01006212 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6213 "Expecting 0=compressed, 1=uncompressed");
6214 __ j(kNotZero, &not_compressed);
6215 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
6216 __ jmp(&done);
6217 __ Bind(&not_compressed);
6218 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
6219 __ Bind(&done);
6220 } else {
Ulya Trafimovichc8451cb2021-06-02 17:35:16 +01006221 ScaleFactor scale = CodeGenerator::ScaleFactorForType(type);
6222 Address src = CodeGeneratorX86::ArrayAddress(obj, index, scale, data_offset);
Ulya Trafimovich322eced2021-06-02 15:39:36 +01006223 codegen_->LoadFromMemoryNoBarrier(type, out_loc, src, instruction);
Calin Juravle77520bc2015-01-12 18:45:46 +00006224 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006225}
6226
6227void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006228 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006229
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006230 bool needs_write_barrier =
6231 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006232 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006233
Vladimir Markoca6fff82017-10-03 14:49:14 +01006234 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01006235 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006236 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006237
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006238 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006239 // We need the inputs to be different than the output in case of long operation.
6240 // In case of a byte operation, the register allocator does not support multiple
6241 // inputs that die at entry with one in a specific register.
6242 locations->SetInAt(0, Location::RequiresRegister());
6243 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6244 if (is_byte_type) {
6245 // Ensure the value is in a byte register.
6246 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006247 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05006248 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006249 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006250 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
6251 }
6252 if (needs_write_barrier) {
6253 // Temporary registers for the write barrier.
6254 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6255 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00006256 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006257 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006258}
6259
6260void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
6261 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006262 Location array_loc = locations->InAt(0);
6263 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006264 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006265 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006266 DataType::Type value_type = instruction->GetComponentType();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006267 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006268 bool needs_write_barrier =
6269 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006270
6271 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006272 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006273 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006274 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006275 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006276 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006277 if (value.IsRegister()) {
6278 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006279 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01006280 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006281 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006282 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006283 break;
6284 }
6285
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006286 case DataType::Type::kUint16:
6287 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006288 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006289 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006290 if (value.IsRegister()) {
6291 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006292 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01006293 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006294 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006295 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006296 break;
6297 }
6298
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006299 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006300 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006301 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006302
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006303 if (!value.IsRegister()) {
6304 // Just setting null.
6305 DCHECK(instruction->InputAt(2)->IsNullConstant());
6306 DCHECK(value.IsConstant()) << value;
6307 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00006308 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006309 DCHECK(!needs_write_barrier);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006310 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006311 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006312 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006313
6314 DCHECK(needs_write_barrier);
6315 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01006316 Location temp_loc = locations->GetTemp(0);
6317 Register temp = temp_loc.AsRegister<Register>();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006318
6319 bool can_value_be_null = instruction->GetValueCanBeNull();
6320 NearLabel do_store;
6321 if (can_value_be_null) {
6322 __ testl(register_value, register_value);
6323 __ j(kEqual, &do_store);
6324 }
6325
6326 SlowPathCode* slow_path = nullptr;
6327 if (needs_type_check) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006328 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006329 codegen_->AddSlowPath(slow_path);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006330
6331 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6332 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6333 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006334
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006335 // Note that when Baker read barriers are enabled, the type
6336 // checks are performed without read barriers. This is fine,
6337 // even in the case where a class object is in the from-space
6338 // after the flip, as a comparison involving such a type would
6339 // not produce a false positive; it may of course produce a
6340 // false negative, in which case we would take the ArraySet
6341 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01006342
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006343 // /* HeapReference<Class> */ temp = array->klass_
6344 __ movl(temp, Address(array, class_offset));
6345 codegen_->MaybeRecordImplicitNullCheck(instruction);
6346 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01006347
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006348 // /* HeapReference<Class> */ temp = temp->component_type_
6349 __ movl(temp, Address(temp, component_offset));
6350 // If heap poisoning is enabled, no need to unpoison `temp`
6351 // nor the object reference in `register_value->klass`, as
6352 // we are comparing two poisoned references.
6353 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01006354
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006355 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006356 NearLabel do_put;
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006357 __ j(kEqual, &do_put);
6358 // If heap poisoning is enabled, the `temp` reference has
6359 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006360 __ MaybeUnpoisonHeapReference(temp);
6361
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006362 // If heap poisoning is enabled, no need to unpoison the
6363 // heap reference loaded below, as it is only used for a
6364 // comparison with null.
6365 __ cmpl(Address(temp, super_offset), Immediate(0));
6366 __ j(kNotEqual, slow_path->GetEntryLabel());
6367 __ Bind(&do_put);
6368 } else {
6369 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006370 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006371 }
6372
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006373 Register card = locations->GetTemp(1).AsRegister<Register>();
6374 codegen_->MarkGCCard(
6375 temp, card, array, value.AsRegister<Register>(), /* value_can_be_null= */ false);
6376
6377 if (can_value_be_null) {
6378 DCHECK(do_store.IsLinked());
6379 __ Bind(&do_store);
6380 }
6381
6382 Register source = register_value;
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006383 if (kPoisonHeapReferences) {
6384 __ movl(temp, register_value);
6385 __ PoisonHeapReference(temp);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006386 source = temp;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006387 }
6388
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006389 __ movl(address, source);
6390
6391 if (can_value_be_null || !needs_type_check) {
6392 codegen_->MaybeRecordImplicitNullCheck(instruction);
6393 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006394
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006395 if (slow_path != nullptr) {
6396 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006397 }
6398
6399 break;
6400 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006401
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006402 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006403 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006404 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006405 if (value.IsRegister()) {
6406 __ movl(address, value.AsRegister<Register>());
6407 } else {
6408 DCHECK(value.IsConstant()) << value;
6409 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
6410 __ movl(address, Immediate(v));
6411 }
6412 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::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006417 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006418 if (value.IsRegisterPair()) {
6419 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6420 value.AsRegisterPairLow<Register>());
6421 codegen_->MaybeRecordImplicitNullCheck(instruction);
6422 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6423 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006424 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006425 DCHECK(value.IsConstant());
6426 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
6427 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6428 Immediate(Low32Bits(val)));
6429 codegen_->MaybeRecordImplicitNullCheck(instruction);
6430 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6431 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006432 }
6433 break;
6434 }
6435
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006436 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006437 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006438 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006439 if (value.IsFpuRegister()) {
6440 __ movss(address, value.AsFpuRegister<XmmRegister>());
6441 } else {
6442 DCHECK(value.IsConstant());
6443 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
6444 __ movl(address, Immediate(v));
6445 }
6446 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006447 break;
6448 }
6449
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006450 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006451 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006452 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006453 if (value.IsFpuRegister()) {
6454 __ movsd(address, value.AsFpuRegister<XmmRegister>());
6455 } else {
6456 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006457 Address address_hi =
6458 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05006459 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
6460 __ movl(address, Immediate(Low32Bits(v)));
6461 codegen_->MaybeRecordImplicitNullCheck(instruction);
6462 __ movl(address_hi, Immediate(High32Bits(v)));
6463 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006464 break;
6465 }
6466
Aart Bik66c158e2018-01-31 12:55:04 -08006467 case DataType::Type::kUint32:
6468 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006469 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006470 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07006471 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006472 }
6473}
6474
6475void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006476 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006477 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04006478 if (!instruction->IsEmittedAtUseSite()) {
6479 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6480 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006481}
6482
6483void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04006484 if (instruction->IsEmittedAtUseSite()) {
6485 return;
6486 }
6487
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006488 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006489 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006490 Register obj = locations->InAt(0).AsRegister<Register>();
6491 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006492 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00006493 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07006494 // Mask out most significant bit in case the array is String's array of char.
6495 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006496 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006497 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006498}
6499
6500void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006501 RegisterSet caller_saves = RegisterSet::Empty();
6502 InvokeRuntimeCallingConvention calling_convention;
6503 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6504 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6505 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05006506 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04006507 HInstruction* length = instruction->InputAt(1);
6508 if (!length->IsEmittedAtUseSite()) {
6509 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6510 }
jessicahandojo4877b792016-09-08 19:49:13 -07006511 // Need register to see array's length.
6512 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6513 locations->AddTemp(Location::RequiresRegister());
6514 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006515}
6516
6517void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07006518 const bool is_string_compressed_char_at =
6519 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006520 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05006521 Location index_loc = locations->InAt(0);
6522 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006523 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006524 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006525
Mark Mendell99dbd682015-04-22 16:18:52 -04006526 if (length_loc.IsConstant()) {
6527 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
6528 if (index_loc.IsConstant()) {
6529 // BCE will remove the bounds check if we are guarenteed to pass.
6530 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6531 if (index < 0 || index >= length) {
6532 codegen_->AddSlowPath(slow_path);
6533 __ jmp(slow_path->GetEntryLabel());
6534 } else {
6535 // Some optimization after BCE may have generated this, and we should not
6536 // generate a bounds check if it is a valid range.
6537 }
6538 return;
6539 }
6540
6541 // We have to reverse the jump condition because the length is the constant.
6542 Register index_reg = index_loc.AsRegister<Register>();
6543 __ cmpl(index_reg, Immediate(length));
6544 codegen_->AddSlowPath(slow_path);
6545 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006546 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04006547 HInstruction* array_length = instruction->InputAt(1);
6548 if (array_length->IsEmittedAtUseSite()) {
6549 // Address the length field in the array.
6550 DCHECK(array_length->IsArrayLength());
6551 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
6552 Location array_loc = array_length->GetLocations()->InAt(0);
6553 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07006554 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006555 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
6556 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07006557 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
6558 __ movl(length_reg, array_len);
6559 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006560 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006561 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04006562 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006563 // Checking bounds for general case:
6564 // Array of char or string's array with feature compression off.
6565 if (index_loc.IsConstant()) {
6566 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6567 __ cmpl(array_len, Immediate(value));
6568 } else {
6569 __ cmpl(array_len, index_loc.AsRegister<Register>());
6570 }
6571 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04006572 }
Mark Mendell99dbd682015-04-22 16:18:52 -04006573 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006574 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04006575 }
6576 codegen_->AddSlowPath(slow_path);
6577 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006578 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006579}
6580
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006581void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006582 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006583}
6584
6585void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006586 if (instruction->GetNext()->IsSuspendCheck() &&
6587 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6588 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6589 // The back edge will generate the suspend check.
6590 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6591 }
6592
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006593 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6594}
6595
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006596void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006597 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6598 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07006599 // In suspend check slow path, usually there are no caller-save registers at all.
6600 // If SIMD instructions are present, however, we force spilling all live SIMD
6601 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07006602 locations->SetCustomSlowPathCallerSaves(
6603 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006604}
6605
6606void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006607 HBasicBlock* block = instruction->GetBlock();
6608 if (block->GetLoopInformation() != nullptr) {
6609 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6610 // The back edge will generate the suspend check.
6611 return;
6612 }
6613 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6614 // The goto will generate the suspend check.
6615 return;
6616 }
6617 GenerateSuspendCheck(instruction, nullptr);
6618}
6619
6620void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
6621 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006622 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006623 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
6624 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006625 slow_path =
6626 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006627 instruction->SetSlowPath(slow_path);
6628 codegen_->AddSlowPath(slow_path);
6629 if (successor != nullptr) {
6630 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006631 }
6632 } else {
6633 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6634 }
6635
Andreas Gampe542451c2016-07-26 09:02:02 -07006636 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006637 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006638 if (successor == nullptr) {
6639 __ j(kNotEqual, slow_path->GetEntryLabel());
6640 __ Bind(slow_path->GetReturnLabel());
6641 } else {
6642 __ j(kEqual, codegen_->GetLabelOf(successor));
6643 __ jmp(slow_path->GetEntryLabel());
6644 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006645}
6646
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006647X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
6648 return codegen_->GetAssembler();
6649}
6650
Aart Bikcfe50bb2017-12-12 14:54:12 -08006651void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006652 ScratchRegisterScope ensure_scratch(
6653 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6654 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6655 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05006656
Aart Bikcfe50bb2017-12-12 14:54:12 -08006657 // Now that temp register is available (possibly spilled), move blocks of memory.
6658 for (int i = 0; i < number_of_words; i++) {
6659 __ movl(temp_reg, Address(ESP, src + stack_offset));
6660 __ movl(Address(ESP, dst + stack_offset), temp_reg);
6661 stack_offset += kX86WordSize;
6662 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006663}
6664
6665void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006666 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006667 Location source = move->GetSource();
6668 Location destination = move->GetDestination();
6669
6670 if (source.IsRegister()) {
6671 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006672 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006673 } else if (destination.IsFpuRegister()) {
6674 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006675 } else {
6676 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006677 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006678 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006679 } else if (source.IsRegisterPair()) {
Vladimir Marko86c87522020-05-11 16:55:55 +01006680 if (destination.IsRegisterPair()) {
6681 __ movl(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
6682 DCHECK_NE(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
6683 __ movl(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
6684 } else if (destination.IsFpuRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006685 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Vladimir Markodec78172020-06-19 15:31:23 +01006686 // Push the 2 source registers to the stack.
Vladimir Marko86c87522020-05-11 16:55:55 +01006687 __ pushl(source.AsRegisterPairHigh<Register>());
6688 __ cfi().AdjustCFAOffset(elem_size);
6689 __ pushl(source.AsRegisterPairLow<Register>());
6690 __ cfi().AdjustCFAOffset(elem_size);
6691 // Load the destination register.
David Brazdil74eb1b22015-12-14 11:44:01 +00006692 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
6693 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01006694 codegen_->DecreaseFrame(2 * elem_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01006695 } else {
6696 DCHECK(destination.IsDoubleStackSlot());
6697 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
6698 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
6699 source.AsRegisterPairHigh<Register>());
6700 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006701 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006702 if (destination.IsRegister()) {
6703 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
6704 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006705 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006706 } else if (destination.IsRegisterPair()) {
Vladimir Marko86c87522020-05-11 16:55:55 +01006707 size_t elem_size = DataType::Size(DataType::Type::kInt32);
6708 // Create stack space for 2 elements.
Vladimir Markodec78172020-06-19 15:31:23 +01006709 codegen_->IncreaseFrame(2 * elem_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01006710 // Store the source register.
6711 __ movsd(Address(ESP, 0), source.AsFpuRegister<XmmRegister>());
6712 // And pop the values into destination registers.
6713 __ popl(destination.AsRegisterPairLow<Register>());
6714 __ cfi().AdjustCFAOffset(-elem_size);
6715 __ popl(destination.AsRegisterPairHigh<Register>());
6716 __ cfi().AdjustCFAOffset(-elem_size);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006717 } else if (destination.IsStackSlot()) {
6718 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006719 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006720 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006721 } else {
6722 DCHECK(destination.IsSIMDStackSlot());
6723 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05006724 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006725 } else if (source.IsStackSlot()) {
6726 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006727 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006728 } else if (destination.IsFpuRegister()) {
6729 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006730 } else {
6731 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08006732 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006733 }
6734 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006735 if (destination.IsRegisterPair()) {
6736 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
6737 __ movl(destination.AsRegisterPairHigh<Register>(),
6738 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
6739 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006740 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6741 } else {
6742 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006743 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006744 }
Aart Bik5576f372017-03-23 16:17:37 -07006745 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006746 if (destination.IsFpuRegister()) {
6747 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6748 } else {
6749 DCHECK(destination.IsSIMDStackSlot());
6750 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6751 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006752 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006753 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006754 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006755 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006756 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006757 if (value == 0) {
6758 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
6759 } else {
6760 __ movl(destination.AsRegister<Register>(), Immediate(value));
6761 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006762 } else {
6763 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05006764 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006765 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006766 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006767 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006768 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006769 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006770 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006771 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6772 if (value == 0) {
6773 // Easy handling of 0.0.
6774 __ xorps(dest, dest);
6775 } else {
6776 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006777 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6778 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
6779 __ movl(temp, Immediate(value));
6780 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006781 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006782 } else {
6783 DCHECK(destination.IsStackSlot()) << destination;
6784 __ movl(Address(ESP, destination.GetStackIndex()), imm);
6785 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006786 } else if (constant->IsLongConstant()) {
6787 int64_t value = constant->AsLongConstant()->GetValue();
6788 int32_t low_value = Low32Bits(value);
6789 int32_t high_value = High32Bits(value);
6790 Immediate low(low_value);
6791 Immediate high(high_value);
6792 if (destination.IsDoubleStackSlot()) {
6793 __ movl(Address(ESP, destination.GetStackIndex()), low);
6794 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6795 } else {
6796 __ movl(destination.AsRegisterPairLow<Register>(), low);
6797 __ movl(destination.AsRegisterPairHigh<Register>(), high);
6798 }
6799 } else {
6800 DCHECK(constant->IsDoubleConstant());
6801 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006802 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006803 int32_t low_value = Low32Bits(value);
6804 int32_t high_value = High32Bits(value);
6805 Immediate low(low_value);
6806 Immediate high(high_value);
6807 if (destination.IsFpuRegister()) {
6808 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6809 if (value == 0) {
6810 // Easy handling of 0.0.
6811 __ xorpd(dest, dest);
6812 } else {
6813 __ pushl(high);
Vladimir Marko86c87522020-05-11 16:55:55 +01006814 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006815 __ pushl(low);
Vladimir Marko86c87522020-05-11 16:55:55 +01006816 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006817 __ movsd(dest, Address(ESP, 0));
Vladimir Markodec78172020-06-19 15:31:23 +01006818 codegen_->DecreaseFrame(8);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006819 }
6820 } else {
6821 DCHECK(destination.IsDoubleStackSlot()) << destination;
6822 __ movl(Address(ESP, destination.GetStackIndex()), low);
6823 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6824 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006825 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006826 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006827 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006828 }
6829}
6830
Mark Mendella5c19ce2015-04-01 12:51:05 -04006831void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006832 Register suggested_scratch = reg == EAX ? EBX : EAX;
6833 ScratchRegisterScope ensure_scratch(
6834 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
6835
6836 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6837 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
6838 __ movl(Address(ESP, mem + stack_offset), reg);
6839 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006840}
6841
Mark Mendell7c8d0092015-01-26 11:21:33 -05006842void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006843 ScratchRegisterScope ensure_scratch(
6844 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6845
6846 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6847 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6848 __ movl(temp_reg, Address(ESP, mem + stack_offset));
6849 __ movss(Address(ESP, mem + stack_offset), reg);
6850 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006851}
6852
Aart Bikcfe50bb2017-12-12 14:54:12 -08006853void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
6854 size_t extra_slot = 4 * kX86WordSize;
Vladimir Markodec78172020-06-19 15:31:23 +01006855 codegen_->IncreaseFrame(extra_slot);
Aart Bikcfe50bb2017-12-12 14:54:12 -08006856 __ movups(Address(ESP, 0), XmmRegister(reg));
6857 ExchangeMemory(0, mem + extra_slot, 4);
6858 __ movups(XmmRegister(reg), Address(ESP, 0));
Vladimir Markodec78172020-06-19 15:31:23 +01006859 codegen_->DecreaseFrame(extra_slot);
Aart Bikcfe50bb2017-12-12 14:54:12 -08006860}
6861
6862void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006863 ScratchRegisterScope ensure_scratch1(
6864 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006865
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006866 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
6867 ScratchRegisterScope ensure_scratch2(
6868 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006869
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006870 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
6871 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006872
6873 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
6874 for (int i = 0; i < number_of_words; i++) {
6875 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
6876 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
6877 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
6878 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
6879 stack_offset += kX86WordSize;
6880 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006881}
6882
6883void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006884 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006885 Location source = move->GetSource();
6886 Location destination = move->GetDestination();
6887
6888 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04006889 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
6890 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
6891 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
6892 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
6893 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006894 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006895 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006896 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006897 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006898 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006899 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006900 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
6901 // Use XOR Swap algorithm to avoid a temporary.
6902 DCHECK_NE(source.reg(), destination.reg());
6903 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6904 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
6905 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6906 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
6907 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6908 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
6909 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006910 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
6911 // Take advantage of the 16 bytes in the XMM register.
6912 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
6913 Address stack(ESP, destination.GetStackIndex());
6914 // Load the double into the high doubleword.
6915 __ movhpd(reg, stack);
6916
6917 // Store the low double into the destination.
6918 __ movsd(stack, reg);
6919
6920 // Move the high double to the low double.
6921 __ psrldq(reg, Immediate(8));
6922 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6923 // Take advantage of the 16 bytes in the XMM register.
6924 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6925 Address stack(ESP, source.GetStackIndex());
6926 // Load the double into the high doubleword.
6927 __ movhpd(reg, stack);
6928
6929 // Store the low double into the destination.
6930 __ movsd(stack, reg);
6931
6932 // Move the high double to the low double.
6933 __ psrldq(reg, Immediate(8));
6934 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006935 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
6936 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
6937 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6938 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
6939 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6940 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
6941 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006942 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006943 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006944 }
6945}
6946
6947void ParallelMoveResolverX86::SpillScratch(int reg) {
6948 __ pushl(static_cast<Register>(reg));
6949}
6950
6951void ParallelMoveResolverX86::RestoreScratch(int reg) {
6952 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006953}
6954
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006955HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6956 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006957 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006958 case HLoadClass::LoadKind::kInvalid:
6959 LOG(FATAL) << "UNREACHABLE";
6960 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006961 case HLoadClass::LoadKind::kReferrersClass:
6962 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006963 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006964 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006965 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko8f63f102020-09-28 12:10:28 +01006966 case HLoadClass::LoadKind::kBssEntryPublic:
6967 case HLoadClass::LoadKind::kBssEntryPackage:
Vladimir Marko695348f2020-05-19 14:42:02 +01006968 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006969 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006970 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006971 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01006972 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006973 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006974 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006975 break;
6976 }
6977 return desired_class_load_kind;
6978}
6979
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006980void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006981 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006982 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006983 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006984 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006985 cls,
6986 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006987 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006988 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006989 return;
6990 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01006991 DCHECK_EQ(cls->NeedsAccessCheck(),
6992 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
6993 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006994
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006995 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6996 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006997 ? LocationSummary::kCallOnSlowPath
6998 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006999 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007000 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007001 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007002 }
7003
Vladimir Marko8f63f102020-09-28 12:10:28 +01007004 if (load_kind == HLoadClass::LoadKind::kReferrersClass || cls->HasPcRelativeLoadKind()) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007005 locations->SetInAt(0, Location::RequiresRegister());
7006 }
7007 locations->SetOut(Location::RequiresRegister());
Vladimir Marko8f63f102020-09-28 12:10:28 +01007008 if (call_kind == LocationSummary::kCallOnSlowPath && cls->HasPcRelativeLoadKind()) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007009 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7010 // Rely on the type resolution and/or initialization to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007011 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007012 } else {
7013 // For non-Baker read barrier we have a temp-clobbering call.
7014 }
7015 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007016}
7017
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007018Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01007019 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007020 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007021 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007022 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007023 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007024 PatchInfo<Label>* info = &jit_class_patches_.back();
7025 return &info->label;
7026}
7027
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007028// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7029// move.
7030void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007031 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007032 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007033 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01007034 return;
7035 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01007036 DCHECK_EQ(cls->NeedsAccessCheck(),
7037 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
7038 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Calin Juravle580b6092015-10-06 17:35:58 +01007039
Vladimir Marko41559982017-01-06 14:04:23 +00007040 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007041 Location out_loc = locations->Out();
7042 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007043
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007044 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007045 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7046 ? kWithoutReadBarrier
7047 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00007048 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007049 case HLoadClass::LoadKind::kReferrersClass: {
7050 DCHECK(!cls->CanCallRuntime());
7051 DCHECK(!cls->MustGenerateClinitCheck());
7052 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7053 Register current_method = locations->InAt(0).AsRegister<Register>();
7054 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007055 cls,
7056 out_loc,
7057 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Andreas Gampe3db70682018-12-26 15:12:03 -08007058 /* fixup_label= */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007059 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007060 break;
7061 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007062 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007063 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7064 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007065 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007066 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007067 __ leal(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007068 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007069 break;
7070 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007071 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007072 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7073 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007074 __ movl(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007075 codegen_->RecordBootImageRelRoPatch(cls->InputAt(0)->AsX86ComputeBaseMethodAddress(),
Vladimir Markode91ca92020-10-27 13:41:40 +00007076 CodeGenerator::GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007077 break;
7078 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01007079 case HLoadClass::LoadKind::kBssEntry:
7080 case HLoadClass::LoadKind::kBssEntryPublic:
7081 case HLoadClass::LoadKind::kBssEntryPackage: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007082 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007083 Address address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007084 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
7085 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007086 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007087 generate_null_check = true;
7088 break;
7089 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007090 case HLoadClass::LoadKind::kJitBootImageAddress: {
7091 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7092 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
7093 DCHECK_NE(address, 0u);
7094 __ movl(out, Immediate(address));
7095 break;
7096 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007097 case HLoadClass::LoadKind::kJitTableAddress: {
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007098 Address address = Address::Absolute(CodeGeneratorX86::kPlaceholder32BitOffset);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007099 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007100 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007101 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00007102 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007103 break;
7104 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007105 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007106 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007107 LOG(FATAL) << "UNREACHABLE";
7108 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007109 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007110
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007111 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7112 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007113 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007114 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007115
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007116 if (generate_null_check) {
7117 __ testl(out, out);
7118 __ j(kEqual, slow_path->GetEntryLabel());
7119 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007120
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007121 if (cls->MustGenerateClinitCheck()) {
7122 GenerateClassInitializationCheck(slow_path, out);
7123 } else {
7124 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007125 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007126 }
7127}
7128
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007129void LocationsBuilderX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7130 InvokeRuntimeCallingConvention calling_convention;
7131 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7132 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
7133}
7134
7135void InstructionCodeGeneratorX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7136 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
7137}
7138
Orion Hodson18259d72018-04-12 11:18:23 +01007139void LocationsBuilderX86::VisitLoadMethodType(HLoadMethodType* load) {
7140 InvokeRuntimeCallingConvention calling_convention;
7141 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7142 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
7143}
7144
7145void InstructionCodeGeneratorX86::VisitLoadMethodType(HLoadMethodType* load) {
7146 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
7147}
7148
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007149void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
7150 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007151 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007152 locations->SetInAt(0, Location::RequiresRegister());
7153 if (check->HasUses()) {
7154 locations->SetOut(Location::SameAsFirstInput());
7155 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007156 // Rely on the type initialization to save everything we need.
7157 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007158}
7159
7160void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007161 // We assume the class to not be null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007162 SlowPathCode* slow_path =
7163 new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(check->GetLoadClass(), check);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007164 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00007165 GenerateClassInitializationCheck(slow_path,
7166 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007167}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007168
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007169void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07007170 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00007171 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
7172 const size_t status_byte_offset =
7173 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
Vladimir Markobf121912019-06-04 13:49:05 +01007174 constexpr uint32_t shifted_visibly_initialized_value =
7175 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << (status_lsb_position % kBitsPerByte);
Vladimir Markodc682aa2018-01-04 18:42:57 +00007176
Vladimir Markobf121912019-06-04 13:49:05 +01007177 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_visibly_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00007178 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007179 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007180}
7181
Vladimir Marko175e7862018-03-27 09:03:13 +00007182void InstructionCodeGeneratorX86::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
7183 Register temp) {
7184 uint32_t path_to_root = check->GetBitstringPathToRoot();
7185 uint32_t mask = check->GetBitstringMask();
7186 DCHECK(IsPowerOfTwo(mask + 1));
7187 size_t mask_bits = WhichPowerOf2(mask + 1);
7188
7189 if (mask_bits == 16u) {
7190 // Compare the bitstring in memory.
7191 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
7192 } else {
7193 // /* uint32_t */ temp = temp->status_
7194 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
7195 // Compare the bitstring bits using SUB.
7196 __ subl(temp, Immediate(path_to_root));
7197 // Shift out bits that do not contribute to the comparison.
7198 __ shll(temp, Immediate(32u - mask_bits));
7199 }
7200}
7201
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007202HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
7203 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007204 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007205 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007206 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007207 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01007208 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007209 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007210 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007211 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01007212 DCHECK(GetCompilerOptions().IsJitCompiler());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007213 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007214 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007215 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007216 }
7217 return desired_string_load_kind;
7218}
7219
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007220void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007221 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007222 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007223 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007224 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007225 load_kind == HLoadString::LoadKind::kBootImageRelRo ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00007226 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007227 locations->SetInAt(0, Location::RequiresRegister());
7228 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007229 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007230 locations->SetOut(Location::RegisterLocation(EAX));
7231 } else {
7232 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007233 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7234 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007235 // Rely on the pResolveString to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007236 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007237 } else {
7238 // For non-Baker read barrier we have a temp-clobbering call.
7239 }
7240 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007241 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007242}
7243
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007244Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01007245 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007246 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007247 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007248 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007249 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007250 PatchInfo<Label>* info = &jit_string_patches_.back();
7251 return &info->label;
7252}
7253
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007254// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7255// move.
7256void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01007257 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007258 Location out_loc = locations->Out();
7259 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007260
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007261 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007262 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007263 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7264 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007265 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007266 __ leal(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007267 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007268 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007269 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007270 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007271 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7272 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007273 __ movl(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007274 codegen_->RecordBootImageRelRoPatch(load->InputAt(0)->AsX86ComputeBaseMethodAddress(),
Vladimir Markode91ca92020-10-27 13:41:40 +00007275 CodeGenerator::GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007276 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007277 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007278 case HLoadString::LoadKind::kBssEntry: {
7279 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007280 Address address = Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007281 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007282 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007283 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007284 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007285 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007286 codegen_->AddSlowPath(slow_path);
7287 __ testl(out, out);
7288 __ j(kEqual, slow_path->GetEntryLabel());
7289 __ Bind(slow_path->GetExitLabel());
7290 return;
7291 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007292 case HLoadString::LoadKind::kJitBootImageAddress: {
7293 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
7294 DCHECK_NE(address, 0u);
7295 __ movl(out, Immediate(address));
7296 return;
7297 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007298 case HLoadString::LoadKind::kJitTableAddress: {
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007299 Address address = Address::Absolute(CodeGeneratorX86::kPlaceholder32BitOffset);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007300 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007301 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007302 // /* GcRoot<mirror::String> */ out = *address
7303 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
7304 return;
7305 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007306 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007307 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007308 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007309
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007310 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007311 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007312 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007313 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007314 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7315 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007316}
7317
David Brazdilcb1c0552015-08-04 16:22:25 +01007318static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007319 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01007320}
7321
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007322void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
7323 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007324 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007325 locations->SetOut(Location::RequiresRegister());
7326}
7327
7328void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01007329 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
7330}
7331
7332void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007333 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01007334}
7335
7336void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7337 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007338}
7339
7340void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007341 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7342 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007343 InvokeRuntimeCallingConvention calling_convention;
7344 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7345}
7346
7347void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007348 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007349 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007350}
7351
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007352// Temp is used for read barrier.
7353static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7354 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00007355 !kUseBakerReadBarrier &&
7356 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00007357 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007358 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7359 return 1;
7360 }
7361 return 0;
7362}
7363
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007364// Interface case has 2 temps, one for holding the number of interfaces, one for the current
7365// interface pointer, the current interface is compared in memory.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007366// The other checks have one temp for loading the object's class.
7367static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007368 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007369 return 2;
7370 }
7371 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007372}
7373
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007374void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007375 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007376 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01007377 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007378 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007379 case TypeCheckKind::kExactCheck:
7380 case TypeCheckKind::kAbstractClassCheck:
7381 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007382 case TypeCheckKind::kArrayObjectCheck: {
7383 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7384 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7385 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007386 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007387 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007388 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007389 case TypeCheckKind::kUnresolvedCheck:
7390 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007391 call_kind = LocationSummary::kCallOnSlowPath;
7392 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007393 case TypeCheckKind::kBitstringCheck:
7394 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007395 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007396
Vladimir Markoca6fff82017-10-03 14:49:14 +01007397 LocationSummary* locations =
7398 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01007399 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007400 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007401 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007402 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007403 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7404 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7405 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7406 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7407 } else {
7408 locations->SetInAt(1, Location::Any());
7409 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007410 // Note that TypeCheckSlowPathX86 uses this "out" register too.
7411 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007412 // When read barriers are enabled, we need a temporary register for some cases.
7413 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007414}
7415
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007416void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007417 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007418 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007419 Location obj_loc = locations->InAt(0);
7420 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007421 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007422 Location out_loc = locations->Out();
7423 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007424 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7425 DCHECK_LE(num_temps, 1u);
7426 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007427 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007428 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7429 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7430 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07007431 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007432 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007433
7434 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007435 // Avoid null check if we know obj is not null.
7436 if (instruction->MustDoNullCheck()) {
7437 __ testl(obj, obj);
7438 __ j(kEqual, &zero);
7439 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007440
Roland Levillain7c1559a2015-12-15 10:55:36 +00007441 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007442 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007443 ReadBarrierOption read_barrier_option =
7444 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007445 // /* HeapReference<Class> */ out = obj->klass_
7446 GenerateReferenceLoadTwoRegisters(instruction,
7447 out_loc,
7448 obj_loc,
7449 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007450 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007451 if (cls.IsRegister()) {
7452 __ cmpl(out, cls.AsRegister<Register>());
7453 } else {
7454 DCHECK(cls.IsStackSlot()) << cls;
7455 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7456 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007457
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007458 // Classes must be equal for the instanceof to succeed.
7459 __ j(kNotEqual, &zero);
7460 __ movl(out, Immediate(1));
7461 __ jmp(&done);
7462 break;
7463 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007464
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007465 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007466 ReadBarrierOption read_barrier_option =
7467 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007468 // /* HeapReference<Class> */ out = obj->klass_
7469 GenerateReferenceLoadTwoRegisters(instruction,
7470 out_loc,
7471 obj_loc,
7472 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007473 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007474 // If the class is abstract, we eagerly fetch the super class of the
7475 // object to avoid doing a comparison we know will fail.
7476 NearLabel loop;
7477 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007478 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007479 GenerateReferenceLoadOneRegister(instruction,
7480 out_loc,
7481 super_offset,
7482 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007483 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007484 __ testl(out, out);
7485 // If `out` is null, we use it for the result, and jump to `done`.
7486 __ j(kEqual, &done);
7487 if (cls.IsRegister()) {
7488 __ cmpl(out, cls.AsRegister<Register>());
7489 } else {
7490 DCHECK(cls.IsStackSlot()) << cls;
7491 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7492 }
7493 __ j(kNotEqual, &loop);
7494 __ movl(out, Immediate(1));
7495 if (zero.IsLinked()) {
7496 __ jmp(&done);
7497 }
7498 break;
7499 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007500
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007501 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007502 ReadBarrierOption read_barrier_option =
7503 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007504 // /* HeapReference<Class> */ out = obj->klass_
7505 GenerateReferenceLoadTwoRegisters(instruction,
7506 out_loc,
7507 obj_loc,
7508 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007509 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007510 // Walk over the class hierarchy to find a match.
7511 NearLabel loop, success;
7512 __ Bind(&loop);
7513 if (cls.IsRegister()) {
7514 __ cmpl(out, cls.AsRegister<Register>());
7515 } else {
7516 DCHECK(cls.IsStackSlot()) << cls;
7517 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7518 }
7519 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007520 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007521 GenerateReferenceLoadOneRegister(instruction,
7522 out_loc,
7523 super_offset,
7524 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007525 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007526 __ testl(out, out);
7527 __ j(kNotEqual, &loop);
7528 // If `out` is null, we use it for the result, and jump to `done`.
7529 __ jmp(&done);
7530 __ Bind(&success);
7531 __ movl(out, Immediate(1));
7532 if (zero.IsLinked()) {
7533 __ jmp(&done);
7534 }
7535 break;
7536 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007537
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007538 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007539 ReadBarrierOption read_barrier_option =
7540 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007541 // /* HeapReference<Class> */ out = obj->klass_
7542 GenerateReferenceLoadTwoRegisters(instruction,
7543 out_loc,
7544 obj_loc,
7545 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007546 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007547 // Do an exact check.
7548 NearLabel exact_check;
7549 if (cls.IsRegister()) {
7550 __ cmpl(out, cls.AsRegister<Register>());
7551 } else {
7552 DCHECK(cls.IsStackSlot()) << cls;
7553 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7554 }
7555 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007556 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007557 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007558 GenerateReferenceLoadOneRegister(instruction,
7559 out_loc,
7560 component_offset,
7561 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007562 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007563 __ testl(out, out);
7564 // If `out` is null, we use it for the result, and jump to `done`.
7565 __ j(kEqual, &done);
7566 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
7567 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007568 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007569 __ movl(out, Immediate(1));
7570 __ jmp(&done);
7571 break;
7572 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007573
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007574 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007575 // No read barrier since the slow path will retry upon failure.
7576 // /* HeapReference<Class> */ out = obj->klass_
7577 GenerateReferenceLoadTwoRegisters(instruction,
7578 out_loc,
7579 obj_loc,
7580 class_offset,
7581 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007582 if (cls.IsRegister()) {
7583 __ cmpl(out, cls.AsRegister<Register>());
7584 } else {
7585 DCHECK(cls.IsStackSlot()) << cls;
7586 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7587 }
7588 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007589 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007590 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007591 codegen_->AddSlowPath(slow_path);
7592 __ j(kNotEqual, slow_path->GetEntryLabel());
7593 __ movl(out, Immediate(1));
7594 if (zero.IsLinked()) {
7595 __ jmp(&done);
7596 }
7597 break;
7598 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007599
Calin Juravle98893e12015-10-02 21:05:03 +01007600 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007601 case TypeCheckKind::kInterfaceCheck: {
7602 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007603 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00007604 // cases.
7605 //
7606 // We cannot directly call the InstanceofNonTrivial runtime
7607 // entry point without resorting to a type checking slow path
7608 // here (i.e. by calling InvokeRuntime directly), as it would
7609 // require to assign fixed registers for the inputs of this
7610 // HInstanceOf instruction (following the runtime calling
7611 // convention), which might be cluttered by the potential first
7612 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007613 //
7614 // TODO: Introduce a new runtime entry point taking the object
7615 // to test (instead of its class) as argument, and let it deal
7616 // with the read barrier issues. This will let us refactor this
7617 // case of the `switch` code as it was previously (with a direct
7618 // call to the runtime not using a type checking slow path).
7619 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007620 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007621 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007622 instruction, /* is_fatal= */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007623 codegen_->AddSlowPath(slow_path);
7624 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007625 if (zero.IsLinked()) {
7626 __ jmp(&done);
7627 }
7628 break;
7629 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007630
7631 case TypeCheckKind::kBitstringCheck: {
7632 // /* HeapReference<Class> */ temp = obj->klass_
7633 GenerateReferenceLoadTwoRegisters(instruction,
7634 out_loc,
7635 obj_loc,
7636 class_offset,
7637 kWithoutReadBarrier);
7638
7639 GenerateBitstringTypeCheckCompare(instruction, out);
7640 __ j(kNotEqual, &zero);
7641 __ movl(out, Immediate(1));
7642 __ jmp(&done);
7643 break;
7644 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007645 }
7646
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007647 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007648 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007649 __ xorl(out, out);
7650 }
7651
7652 if (done.IsLinked()) {
7653 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007654 }
7655
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007656 if (slow_path != nullptr) {
7657 __ Bind(slow_path->GetExitLabel());
7658 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007659}
7660
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007661void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007662 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007663 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007664 LocationSummary* locations =
7665 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007666 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007667 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7668 // Require a register for the interface check since there is a loop that compares the class to
7669 // a memory address.
7670 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007671 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7672 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7673 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7674 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007675 } else {
7676 locations->SetInAt(1, Location::Any());
7677 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007678 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007679 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
7680}
7681
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007682void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007683 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007684 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007685 Location obj_loc = locations->InAt(0);
7686 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007687 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007688 Location temp_loc = locations->GetTemp(0);
7689 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007690 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7691 DCHECK_GE(num_temps, 1u);
7692 DCHECK_LE(num_temps, 2u);
7693 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7694 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7695 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7696 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7697 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7698 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7699 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7700 const uint32_t object_array_data_offset =
7701 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007702
Vladimir Marko87584542017-12-12 17:47:52 +00007703 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007704 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007705 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7706 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007707 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007708
Roland Levillain0d5a2812015-11-13 10:07:31 +00007709 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007710 // Avoid null check if we know obj is not null.
7711 if (instruction->MustDoNullCheck()) {
7712 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007713 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007714 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007715
Roland Levillain0d5a2812015-11-13 10:07:31 +00007716 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007717 case TypeCheckKind::kExactCheck:
7718 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007719 // /* HeapReference<Class> */ temp = obj->klass_
7720 GenerateReferenceLoadTwoRegisters(instruction,
7721 temp_loc,
7722 obj_loc,
7723 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007724 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007725
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007726 if (cls.IsRegister()) {
7727 __ cmpl(temp, cls.AsRegister<Register>());
7728 } else {
7729 DCHECK(cls.IsStackSlot()) << cls;
7730 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7731 }
7732 // Jump to slow path for throwing the exception or doing a
7733 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007734 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007735 break;
7736 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007737
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007738 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007739 // /* HeapReference<Class> */ temp = obj->klass_
7740 GenerateReferenceLoadTwoRegisters(instruction,
7741 temp_loc,
7742 obj_loc,
7743 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007744 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007745
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007746 // If the class is abstract, we eagerly fetch the super class of the
7747 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007748 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007749 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007750 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007751 GenerateReferenceLoadOneRegister(instruction,
7752 temp_loc,
7753 super_offset,
7754 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007755 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007756
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007757 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7758 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007759 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007760 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007761
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007762 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007763 if (cls.IsRegister()) {
7764 __ cmpl(temp, cls.AsRegister<Register>());
7765 } else {
7766 DCHECK(cls.IsStackSlot()) << cls;
7767 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7768 }
7769 __ j(kNotEqual, &loop);
7770 break;
7771 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007772
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007773 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007774 // /* HeapReference<Class> */ temp = obj->klass_
7775 GenerateReferenceLoadTwoRegisters(instruction,
7776 temp_loc,
7777 obj_loc,
7778 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007779 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007780
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007781 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007782 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007783 __ Bind(&loop);
7784 if (cls.IsRegister()) {
7785 __ cmpl(temp, cls.AsRegister<Register>());
7786 } else {
7787 DCHECK(cls.IsStackSlot()) << cls;
7788 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7789 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007790 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007791
Roland Levillain0d5a2812015-11-13 10:07:31 +00007792 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007793 GenerateReferenceLoadOneRegister(instruction,
7794 temp_loc,
7795 super_offset,
7796 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007797 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007798
7799 // If the class reference currently in `temp` is not null, jump
7800 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007801 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007802 __ j(kNotZero, &loop);
7803 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007804 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007805 break;
7806 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007807
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007808 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007809 // /* HeapReference<Class> */ temp = obj->klass_
7810 GenerateReferenceLoadTwoRegisters(instruction,
7811 temp_loc,
7812 obj_loc,
7813 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007814 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007815
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007816 // Do an exact check.
7817 if (cls.IsRegister()) {
7818 __ cmpl(temp, cls.AsRegister<Register>());
7819 } else {
7820 DCHECK(cls.IsStackSlot()) << cls;
7821 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7822 }
7823 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007824
7825 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007826 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007827 GenerateReferenceLoadOneRegister(instruction,
7828 temp_loc,
7829 component_offset,
7830 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007831 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007832
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007833 // If the component type is null (i.e. the object not an array), jump to the slow path to
7834 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007835 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007836 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007837
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007838 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007839 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007840 break;
7841 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007842
Calin Juravle98893e12015-10-02 21:05:03 +01007843 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007844 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007845 // We cannot directly call the CheckCast runtime entry point
7846 // without resorting to a type checking slow path here (i.e. by
7847 // calling InvokeRuntime directly), as it would require to
7848 // assign fixed registers for the inputs of this HInstanceOf
7849 // instruction (following the runtime calling convention), which
7850 // might be cluttered by the potential first read barrier
7851 // emission at the beginning of this method.
7852 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007853 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007854
7855 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007856 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
7857 // We can not get false positives by doing this.
7858 // /* HeapReference<Class> */ temp = obj->klass_
7859 GenerateReferenceLoadTwoRegisters(instruction,
7860 temp_loc,
7861 obj_loc,
7862 class_offset,
7863 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007864
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007865 // /* HeapReference<Class> */ temp = temp->iftable_
7866 GenerateReferenceLoadTwoRegisters(instruction,
7867 temp_loc,
7868 temp_loc,
7869 iftable_offset,
7870 kWithoutReadBarrier);
7871 // Iftable is never null.
7872 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
7873 // Maybe poison the `cls` for direct comparison with memory.
7874 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
7875 // Loop through the iftable and check if any class matches.
7876 NearLabel start_loop;
7877 __ Bind(&start_loop);
7878 // Need to subtract first to handle the empty array case.
7879 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
7880 __ j(kNegative, type_check_slow_path->GetEntryLabel());
7881 // Go to next interface if the classes do not match.
7882 __ cmpl(cls.AsRegister<Register>(),
7883 CodeGeneratorX86::ArrayAddress(temp,
7884 maybe_temp2_loc,
7885 TIMES_4,
7886 object_array_data_offset));
7887 __ j(kNotEqual, &start_loop);
7888 // If `cls` was poisoned above, unpoison it.
7889 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007890 break;
7891 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007892
7893 case TypeCheckKind::kBitstringCheck: {
7894 // /* HeapReference<Class> */ temp = obj->klass_
7895 GenerateReferenceLoadTwoRegisters(instruction,
7896 temp_loc,
7897 obj_loc,
7898 class_offset,
7899 kWithoutReadBarrier);
7900
7901 GenerateBitstringTypeCheckCompare(instruction, temp);
7902 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
7903 break;
7904 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007905 }
7906 __ Bind(&done);
7907
Roland Levillain0d5a2812015-11-13 10:07:31 +00007908 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007909}
7910
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007911void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007912 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7913 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007914 InvokeRuntimeCallingConvention calling_convention;
7915 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7916}
7917
7918void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007919 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
7920 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01007921 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01007922 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007923 if (instruction->IsEnter()) {
7924 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7925 } else {
7926 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7927 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007928}
7929
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307930void LocationsBuilderX86::VisitX86AndNot(HX86AndNot* instruction) {
7931 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7932 DCHECK(DataType::IsIntOrLongType(instruction->GetType())) << instruction->GetType();
7933 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7934 locations->SetInAt(0, Location::RequiresRegister());
7935 locations->SetInAt(1, Location::RequiresRegister());
7936 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7937}
7938
7939void InstructionCodeGeneratorX86::VisitX86AndNot(HX86AndNot* instruction) {
7940 LocationSummary* locations = instruction->GetLocations();
7941 Location first = locations->InAt(0);
7942 Location second = locations->InAt(1);
7943 Location dest = locations->Out();
7944 if (instruction->GetResultType() == DataType::Type::kInt32) {
7945 __ andn(dest.AsRegister<Register>(),
7946 first.AsRegister<Register>(),
7947 second.AsRegister<Register>());
7948 } else {
7949 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
7950 __ andn(dest.AsRegisterPairLow<Register>(),
7951 first.AsRegisterPairLow<Register>(),
7952 second.AsRegisterPairLow<Register>());
7953 __ andn(dest.AsRegisterPairHigh<Register>(),
7954 first.AsRegisterPairHigh<Register>(),
7955 second.AsRegisterPairHigh<Register>());
7956 }
7957}
7958
7959void LocationsBuilderX86::VisitX86MaskOrResetLeastSetBit(HX86MaskOrResetLeastSetBit* instruction) {
7960 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7961 DCHECK(instruction->GetType() == DataType::Type::kInt32) << instruction->GetType();
7962 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7963 locations->SetInAt(0, Location::RequiresRegister());
7964 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7965}
7966
7967void InstructionCodeGeneratorX86::VisitX86MaskOrResetLeastSetBit(
7968 HX86MaskOrResetLeastSetBit* instruction) {
7969 LocationSummary* locations = instruction->GetLocations();
7970 Location src = locations->InAt(0);
7971 Location dest = locations->Out();
7972 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
7973 switch (instruction->GetOpKind()) {
7974 case HInstruction::kAnd:
7975 __ blsr(dest.AsRegister<Register>(), src.AsRegister<Register>());
7976 break;
7977 case HInstruction::kXor:
7978 __ blsmsk(dest.AsRegister<Register>(), src.AsRegister<Register>());
7979 break;
7980 default:
7981 LOG(FATAL) << "Unreachable";
7982 }
7983}
7984
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007985void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
7986void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
7987void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
7988
7989void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7990 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007991 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007992 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7993 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007994 locations->SetInAt(0, Location::RequiresRegister());
7995 locations->SetInAt(1, Location::Any());
7996 locations->SetOut(Location::SameAsFirstInput());
7997}
7998
7999void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
8000 HandleBitwiseOperation(instruction);
8001}
8002
8003void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
8004 HandleBitwiseOperation(instruction);
8005}
8006
8007void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
8008 HandleBitwiseOperation(instruction);
8009}
8010
8011void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
8012 LocationSummary* locations = instruction->GetLocations();
8013 Location first = locations->InAt(0);
8014 Location second = locations->InAt(1);
8015 DCHECK(first.Equals(locations->Out()));
8016
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008017 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008018 if (second.IsRegister()) {
8019 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00008020 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008021 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00008022 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008023 } else {
8024 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00008025 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008026 }
8027 } else if (second.IsConstant()) {
8028 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00008029 __ andl(first.AsRegister<Register>(),
8030 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008031 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00008032 __ orl(first.AsRegister<Register>(),
8033 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008034 } else {
8035 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00008036 __ xorl(first.AsRegister<Register>(),
8037 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008038 }
8039 } else {
8040 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00008041 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008042 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00008043 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008044 } else {
8045 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00008046 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008047 }
8048 }
8049 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008050 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008051 if (second.IsRegisterPair()) {
8052 if (instruction->IsAnd()) {
8053 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
8054 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
8055 } else if (instruction->IsOr()) {
8056 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
8057 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
8058 } else {
8059 DCHECK(instruction->IsXor());
8060 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
8061 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
8062 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008063 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008064 if (instruction->IsAnd()) {
8065 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
8066 __ andl(first.AsRegisterPairHigh<Register>(),
8067 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
8068 } else if (instruction->IsOr()) {
8069 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
8070 __ orl(first.AsRegisterPairHigh<Register>(),
8071 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
8072 } else {
8073 DCHECK(instruction->IsXor());
8074 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
8075 __ xorl(first.AsRegisterPairHigh<Register>(),
8076 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
8077 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008078 } else {
8079 DCHECK(second.IsConstant()) << second;
8080 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008081 int32_t low_value = Low32Bits(value);
8082 int32_t high_value = High32Bits(value);
8083 Immediate low(low_value);
8084 Immediate high(high_value);
8085 Register first_low = first.AsRegisterPairLow<Register>();
8086 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008087 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008088 if (low_value == 0) {
8089 __ xorl(first_low, first_low);
8090 } else if (low_value != -1) {
8091 __ andl(first_low, low);
8092 }
8093 if (high_value == 0) {
8094 __ xorl(first_high, first_high);
8095 } else if (high_value != -1) {
8096 __ andl(first_high, high);
8097 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008098 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008099 if (low_value != 0) {
8100 __ orl(first_low, low);
8101 }
8102 if (high_value != 0) {
8103 __ orl(first_high, high);
8104 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008105 } else {
8106 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008107 if (low_value != 0) {
8108 __ xorl(first_low, low);
8109 }
8110 if (high_value != 0) {
8111 __ xorl(first_high, high);
8112 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008113 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008114 }
8115 }
8116}
8117
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008118void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
8119 HInstruction* instruction,
8120 Location out,
8121 uint32_t offset,
8122 Location maybe_temp,
8123 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008124 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008125 if (read_barrier_option == kWithReadBarrier) {
8126 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008127 if (kUseBakerReadBarrier) {
8128 // Load with fast path based Baker's read barrier.
8129 // /* HeapReference<Object> */ out = *(out + offset)
8130 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008131 instruction, out, out_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008132 } else {
8133 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008134 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00008135 // in the following move operation, as we will need it for the
8136 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00008137 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008138 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008139 // /* HeapReference<Object> */ out = *(out + offset)
8140 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008141 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008142 }
8143 } else {
8144 // Plain load with no read barrier.
8145 // /* HeapReference<Object> */ out = *(out + offset)
8146 __ movl(out_reg, Address(out_reg, offset));
8147 __ MaybeUnpoisonHeapReference(out_reg);
8148 }
8149}
8150
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008151void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
8152 HInstruction* instruction,
8153 Location out,
8154 Location obj,
8155 uint32_t offset,
8156 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008157 Register out_reg = out.AsRegister<Register>();
8158 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008159 if (read_barrier_option == kWithReadBarrier) {
8160 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008161 if (kUseBakerReadBarrier) {
8162 // Load with fast path based Baker's read barrier.
8163 // /* HeapReference<Object> */ out = *(obj + offset)
8164 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008165 instruction, out, obj_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008166 } else {
8167 // Load with slow path based read barrier.
8168 // /* HeapReference<Object> */ out = *(obj + offset)
8169 __ movl(out_reg, Address(obj_reg, offset));
8170 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8171 }
8172 } else {
8173 // Plain load with no read barrier.
8174 // /* HeapReference<Object> */ out = *(obj + offset)
8175 __ movl(out_reg, Address(obj_reg, offset));
8176 __ MaybeUnpoisonHeapReference(out_reg);
8177 }
8178}
8179
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008180void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
8181 HInstruction* instruction,
8182 Location root,
8183 const Address& address,
8184 Label* fixup_label,
8185 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008186 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008187 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07008188 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008189 if (kUseBakerReadBarrier) {
8190 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
8191 // Baker's read barrier are used:
8192 //
Roland Levillaind966ce72017-02-09 16:20:14 +00008193 // root = obj.field;
8194 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8195 // if (temp != null) {
8196 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00008197 // }
8198
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008199 // /* GcRoot<mirror::Object> */ root = *address
8200 __ movl(root_reg, address);
8201 if (fixup_label != nullptr) {
8202 __ Bind(fixup_label);
8203 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008204 static_assert(
8205 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8206 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8207 "have different sizes.");
8208 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8209 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8210 "have different sizes.");
8211
Vladimir Marko953437b2016-08-24 08:30:46 +00008212 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008213 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008214 instruction, root, /* unpoison_ref_before_marking= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008215 codegen_->AddSlowPath(slow_path);
8216
Roland Levillaind966ce72017-02-09 16:20:14 +00008217 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
8218 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01008219 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00008220 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
8221 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008222 __ j(kNotEqual, slow_path->GetEntryLabel());
8223 __ Bind(slow_path->GetExitLabel());
8224 } else {
8225 // GC root loaded through a slow path for read barriers other
8226 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008227 // /* GcRoot<mirror::Object>* */ root = address
8228 __ leal(root_reg, address);
8229 if (fixup_label != nullptr) {
8230 __ Bind(fixup_label);
8231 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008232 // /* mirror::Object* */ root = root->Read()
8233 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8234 }
8235 } else {
8236 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008237 // /* GcRoot<mirror::Object> */ root = *address
8238 __ movl(root_reg, address);
8239 if (fixup_label != nullptr) {
8240 __ Bind(fixup_label);
8241 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00008242 // Note that GC roots are not affected by heap poisoning, thus we
8243 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008244 }
8245}
8246
8247void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8248 Location ref,
8249 Register obj,
8250 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00008251 bool needs_null_check) {
8252 DCHECK(kEmitCompilerReadBarrier);
8253 DCHECK(kUseBakerReadBarrier);
8254
8255 // /* HeapReference<Object> */ ref = *(obj + offset)
8256 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00008257 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008258}
8259
8260void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8261 Location ref,
8262 Register obj,
8263 uint32_t data_offset,
8264 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00008265 bool needs_null_check) {
8266 DCHECK(kEmitCompilerReadBarrier);
8267 DCHECK(kUseBakerReadBarrier);
8268
Roland Levillain3d312422016-06-23 13:53:42 +01008269 static_assert(
8270 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8271 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00008272 // /* HeapReference<Object> */ ref =
8273 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008274 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00008275 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008276}
8277
8278void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8279 Location ref,
8280 Register obj,
8281 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008282 bool needs_null_check,
8283 bool always_update_field,
8284 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008285 DCHECK(kEmitCompilerReadBarrier);
8286 DCHECK(kUseBakerReadBarrier);
8287
8288 // In slow path based read barriers, the read barrier call is
8289 // inserted after the original load. However, in fast path based
8290 // Baker's read barriers, we need to perform the load of
8291 // mirror::Object::monitor_ *before* the original reference load.
8292 // This load-load ordering is required by the read barrier.
8293 // The fast path/slow path (for Baker's algorithm) should look like:
8294 //
8295 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8296 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8297 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008298 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00008299 // if (is_gray) {
8300 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
8301 // }
8302 //
8303 // Note: the original implementation in ReadBarrier::Barrier is
8304 // slightly more complex as:
8305 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00008306 // the high-bits of rb_state, which are expected to be all zeroes
8307 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
8308 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008309 // - it performs additional checks that we do not do here for
8310 // performance reasons.
8311
8312 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00008313 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
8314
Vladimir Marko953437b2016-08-24 08:30:46 +00008315 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01008316 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008317 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00008318 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
8319 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
8320 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
8321
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008322 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00008323 // ref = ReadBarrier::Mark(ref);
8324 // At this point, just do the "if" and make sure that flags are preserved until the branch.
8325 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00008326 if (needs_null_check) {
8327 MaybeRecordImplicitNullCheck(instruction);
8328 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008329
8330 // Load fence to prevent load-load reordering.
8331 // Note that this is a no-op, thanks to the x86 memory model.
8332 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
8333
8334 // The actual reference load.
8335 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00008336 __ movl(ref_reg, src); // Flags are unaffected.
8337
8338 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
8339 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008340 SlowPathCode* slow_path;
8341 if (always_update_field) {
8342 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01008343 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008344 instruction, ref, obj, src, /* unpoison_ref_before_marking= */ true, *temp);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008345 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008346 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008347 instruction, ref, /* unpoison_ref_before_marking= */ true);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008348 }
Vladimir Marko953437b2016-08-24 08:30:46 +00008349 AddSlowPath(slow_path);
8350
8351 // We have done the "if" of the gray bit check above, now branch based on the flags.
8352 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00008353
8354 // Object* ref = ref_addr->AsMirrorPtr()
8355 __ MaybeUnpoisonHeapReference(ref_reg);
8356
Roland Levillain7c1559a2015-12-15 10:55:36 +00008357 __ Bind(slow_path->GetExitLabel());
8358}
8359
8360void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
8361 Location out,
8362 Location ref,
8363 Location obj,
8364 uint32_t offset,
8365 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008366 DCHECK(kEmitCompilerReadBarrier);
8367
Roland Levillain7c1559a2015-12-15 10:55:36 +00008368 // Insert a slow path based read barrier *after* the reference load.
8369 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008370 // If heap poisoning is enabled, the unpoisoning of the loaded
8371 // reference will be carried out by the runtime within the slow
8372 // path.
8373 //
8374 // Note that `ref` currently does not get unpoisoned (when heap
8375 // poisoning is enabled), which is alright as the `ref` argument is
8376 // not used by the artReadBarrierSlow entry point.
8377 //
8378 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008379 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00008380 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
8381 AddSlowPath(slow_path);
8382
Roland Levillain0d5a2812015-11-13 10:07:31 +00008383 __ jmp(slow_path->GetEntryLabel());
8384 __ Bind(slow_path->GetExitLabel());
8385}
8386
Roland Levillain7c1559a2015-12-15 10:55:36 +00008387void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
8388 Location out,
8389 Location ref,
8390 Location obj,
8391 uint32_t offset,
8392 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008393 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008394 // Baker's read barriers shall be handled by the fast path
8395 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
8396 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008397 // If heap poisoning is enabled, unpoisoning will be taken care of
8398 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008399 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008400 } else if (kPoisonHeapReferences) {
8401 __ UnpoisonHeapReference(out.AsRegister<Register>());
8402 }
8403}
8404
Roland Levillain7c1559a2015-12-15 10:55:36 +00008405void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8406 Location out,
8407 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008408 DCHECK(kEmitCompilerReadBarrier);
8409
Roland Levillain7c1559a2015-12-15 10:55:36 +00008410 // Insert a slow path based read barrier *after* the GC root load.
8411 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008412 // Note that GC roots are not affected by heap poisoning, so we do
8413 // not need to do anything special for this here.
8414 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008415 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008416 AddSlowPath(slow_path);
8417
Roland Levillain0d5a2812015-11-13 10:07:31 +00008418 __ jmp(slow_path->GetEntryLabel());
8419 __ Bind(slow_path->GetExitLabel());
8420}
8421
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008422void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008423 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008424 LOG(FATAL) << "Unreachable";
8425}
8426
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008427void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008428 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008429 LOG(FATAL) << "Unreachable";
8430}
8431
Mark Mendellfe57faa2015-09-18 09:26:15 -04008432// Simple implementation of packed switch - generate cascaded compare/jumps.
8433void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8434 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008435 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04008436 locations->SetInAt(0, Location::RequiresRegister());
8437}
8438
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008439void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
8440 int32_t lower_bound,
8441 uint32_t num_entries,
8442 HBasicBlock* switch_block,
8443 HBasicBlock* default_block) {
8444 // Figure out the correct compare values and jump conditions.
8445 // Handle the first compare/branch as a special case because it might
8446 // jump to the default case.
8447 DCHECK_GT(num_entries, 2u);
8448 Condition first_condition;
8449 uint32_t index;
8450 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
8451 if (lower_bound != 0) {
8452 first_condition = kLess;
8453 __ cmpl(value_reg, Immediate(lower_bound));
8454 __ j(first_condition, codegen_->GetLabelOf(default_block));
8455 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008456
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008457 index = 1;
8458 } else {
8459 // Handle all the compare/jumps below.
8460 first_condition = kBelow;
8461 index = 0;
8462 }
8463
8464 // Handle the rest of the compare/jumps.
8465 for (; index + 1 < num_entries; index += 2) {
8466 int32_t compare_to_value = lower_bound + index + 1;
8467 __ cmpl(value_reg, Immediate(compare_to_value));
8468 // Jump to successors[index] if value < case_value[index].
8469 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
8470 // Jump to successors[index + 1] if value == case_value[index + 1].
8471 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
8472 }
8473
8474 if (index != num_entries) {
8475 // There are an odd number of entries. Handle the last one.
8476 DCHECK_EQ(index + 1, num_entries);
8477 __ cmpl(value_reg, Immediate(lower_bound + index));
8478 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008479 }
8480
8481 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008482 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
8483 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008484 }
8485}
8486
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008487void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8488 int32_t lower_bound = switch_instr->GetStartValue();
8489 uint32_t num_entries = switch_instr->GetNumEntries();
8490 LocationSummary* locations = switch_instr->GetLocations();
8491 Register value_reg = locations->InAt(0).AsRegister<Register>();
8492
8493 GenPackedSwitchWithCompares(value_reg,
8494 lower_bound,
8495 num_entries,
8496 switch_instr->GetBlock(),
8497 switch_instr->GetDefaultBlock());
8498}
8499
Mark Mendell805b3b52015-09-18 14:10:29 -04008500void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8501 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008502 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04008503 locations->SetInAt(0, Location::RequiresRegister());
8504
8505 // Constant area pointer.
8506 locations->SetInAt(1, Location::RequiresRegister());
8507
8508 // And the temporary we need.
8509 locations->AddTemp(Location::RequiresRegister());
8510}
8511
8512void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8513 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008514 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04008515 LocationSummary* locations = switch_instr->GetLocations();
8516 Register value_reg = locations->InAt(0).AsRegister<Register>();
8517 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8518
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008519 if (num_entries <= kPackedSwitchJumpTableThreshold) {
8520 GenPackedSwitchWithCompares(value_reg,
8521 lower_bound,
8522 num_entries,
8523 switch_instr->GetBlock(),
8524 default_block);
8525 return;
8526 }
8527
Mark Mendell805b3b52015-09-18 14:10:29 -04008528 // Optimizing has a jump area.
8529 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
8530 Register constant_area = locations->InAt(1).AsRegister<Register>();
8531
8532 // Remove the bias, if needed.
8533 if (lower_bound != 0) {
8534 __ leal(temp_reg, Address(value_reg, -lower_bound));
8535 value_reg = temp_reg;
8536 }
8537
8538 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008539 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04008540 __ cmpl(value_reg, Immediate(num_entries - 1));
8541 __ j(kAbove, codegen_->GetLabelOf(default_block));
8542
8543 // We are in the range of the table.
8544 // Load (target-constant_area) from the jump table, indexing by the value.
8545 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
8546
8547 // Compute the actual target address by adding in constant_area.
8548 __ addl(temp_reg, constant_area);
8549
8550 // And jump.
8551 __ jmp(temp_reg);
8552}
8553
Mark Mendell0616ae02015-04-17 12:49:27 -04008554void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
8555 HX86ComputeBaseMethodAddress* insn) {
8556 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008557 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008558 locations->SetOut(Location::RequiresRegister());
8559}
8560
8561void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
8562 HX86ComputeBaseMethodAddress* insn) {
8563 LocationSummary* locations = insn->GetLocations();
8564 Register reg = locations->Out().AsRegister<Register>();
8565
8566 // Generate call to next instruction.
8567 Label next_instruction;
8568 __ call(&next_instruction);
8569 __ Bind(&next_instruction);
8570
8571 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008572 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04008573
8574 // Grab the return address off the stack.
8575 __ popl(reg);
8576}
8577
8578void LocationsBuilderX86::VisitX86LoadFromConstantTable(
8579 HX86LoadFromConstantTable* insn) {
8580 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008581 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008582
8583 locations->SetInAt(0, Location::RequiresRegister());
8584 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
8585
8586 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00008587 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008588 return;
8589 }
8590
8591 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008592 case DataType::Type::kFloat32:
8593 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008594 locations->SetOut(Location::RequiresFpuRegister());
8595 break;
8596
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008597 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008598 locations->SetOut(Location::RequiresRegister());
8599 break;
8600
8601 default:
8602 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8603 }
8604}
8605
8606void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00008607 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008608 return;
8609 }
8610
8611 LocationSummary* locations = insn->GetLocations();
8612 Location out = locations->Out();
8613 Register const_area = locations->InAt(0).AsRegister<Register>();
8614 HConstant *value = insn->GetConstant();
8615
8616 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008617 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008618 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008619 codegen_->LiteralFloatAddress(
8620 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008621 break;
8622
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008623 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008624 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008625 codegen_->LiteralDoubleAddress(
8626 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008627 break;
8628
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008629 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008630 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008631 codegen_->LiteralInt32Address(
8632 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008633 break;
8634
8635 default:
8636 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8637 }
8638}
8639
Mark Mendell0616ae02015-04-17 12:49:27 -04008640/**
8641 * Class to handle late fixup of offsets into constant area.
8642 */
Vladimir Marko5233f932015-09-29 19:01:15 +01008643class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04008644 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008645 RIPFixup(CodeGeneratorX86& codegen,
8646 HX86ComputeBaseMethodAddress* base_method_address,
8647 size_t offset)
8648 : codegen_(&codegen),
8649 base_method_address_(base_method_address),
8650 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008651
8652 protected:
8653 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
8654
8655 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008656 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008657
8658 private:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008659 void Process(const MemoryRegion& region, int pos) override {
Mark Mendell0616ae02015-04-17 12:49:27 -04008660 // Patch the correct offset for the instruction. The place to patch is the
8661 // last 4 bytes of the instruction.
8662 // The value to patch is the distance from the offset in the constant area
8663 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04008664 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008665 int32_t relative_position =
8666 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04008667
8668 // Patch in the right value.
8669 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
8670 }
8671
Mark Mendell0616ae02015-04-17 12:49:27 -04008672 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04008673 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008674};
8675
Mark Mendell805b3b52015-09-18 14:10:29 -04008676/**
8677 * Class to handle late fixup of offsets to a jump table that will be created in the
8678 * constant area.
8679 */
8680class JumpTableRIPFixup : public RIPFixup {
8681 public:
8682 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008683 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
8684 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008685
8686 void CreateJumpTable() {
8687 X86Assembler* assembler = codegen_->GetAssembler();
8688
8689 // Ensure that the reference to the jump table has the correct offset.
8690 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
8691 SetOffset(offset_in_constant_table);
8692
8693 // The label values in the jump table are computed relative to the
8694 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008695 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04008696
8697 // Populate the jump table with the correct values for the jump table.
8698 int32_t num_entries = switch_instr_->GetNumEntries();
8699 HBasicBlock* block = switch_instr_->GetBlock();
8700 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
8701 // The value that we want is the target offset - the position of the table.
8702 for (int32_t i = 0; i < num_entries; i++) {
8703 HBasicBlock* b = successors[i];
8704 Label* l = codegen_->GetLabelOf(b);
8705 DCHECK(l->IsBound());
8706 int32_t offset_to_block = l->Position() - relative_offset;
8707 assembler->AppendInt32(offset_to_block);
8708 }
8709 }
8710
8711 private:
8712 const HX86PackedSwitch* switch_instr_;
8713};
8714
8715void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
8716 // Generate the constant area if needed.
8717 X86Assembler* assembler = GetAssembler();
jaishank20d1c942019-03-08 15:08:17 +05308718
Mark Mendell805b3b52015-09-18 14:10:29 -04008719 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
8720 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
8721 // byte values.
8722 assembler->Align(4, 0);
8723 constant_area_start_ = assembler->CodeSize();
8724
8725 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008726 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04008727 jump_table->CreateJumpTable();
8728 }
8729
8730 // And now add the constant area to the generated code.
8731 assembler->AddConstantArea();
8732 }
8733
8734 // And finish up.
8735 CodeGenerator::Finalize(allocator);
8736}
8737
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008738Address CodeGeneratorX86::LiteralDoubleAddress(double v,
8739 HX86ComputeBaseMethodAddress* method_base,
8740 Register reg) {
8741 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008742 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008743 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008744}
8745
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008746Address CodeGeneratorX86::LiteralFloatAddress(float v,
8747 HX86ComputeBaseMethodAddress* method_base,
8748 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008749 AssemblerFixup* fixup =
8750 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008751 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008752}
8753
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008754Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
8755 HX86ComputeBaseMethodAddress* method_base,
8756 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008757 AssemblerFixup* fixup =
8758 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008759 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008760}
8761
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008762Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
8763 HX86ComputeBaseMethodAddress* method_base,
8764 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008765 AssemblerFixup* fixup =
8766 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008767 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008768}
8769
Aart Bika19616e2016-02-01 18:57:58 -08008770void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
8771 if (value == 0) {
8772 __ xorl(dest, dest);
8773 } else {
8774 __ movl(dest, Immediate(value));
8775 }
8776}
8777
8778void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
8779 if (value == 0) {
8780 __ testl(dest, dest);
8781 } else {
8782 __ cmpl(dest, Immediate(value));
8783 }
8784}
8785
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008786void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
8787 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07008788 GenerateIntCompare(lhs_reg, rhs);
8789}
8790
8791void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008792 if (rhs.IsConstant()) {
8793 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07008794 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008795 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07008796 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008797 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07008798 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008799 }
8800}
8801
8802Address CodeGeneratorX86::ArrayAddress(Register obj,
8803 Location index,
8804 ScaleFactor scale,
8805 uint32_t data_offset) {
8806 return index.IsConstant() ?
8807 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
8808 Address(obj, index.AsRegister<Register>(), scale, data_offset);
8809}
8810
Mark Mendell805b3b52015-09-18 14:10:29 -04008811Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
8812 Register reg,
8813 Register value) {
8814 // Create a fixup to be used to create and address the jump table.
8815 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008816 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04008817
8818 // We have to populate the jump tables.
8819 fixups_to_jump_tables_.push_back(table_fixup);
8820
8821 // We want a scaled address, as we are extracting the correct offset from the table.
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008822 return Address(reg, value, TIMES_4, kPlaceholder32BitOffset, table_fixup);
Mark Mendell805b3b52015-09-18 14:10:29 -04008823}
8824
Andreas Gampe85b62f22015-09-09 13:15:38 -07008825// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008826void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07008827 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008828 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008829 return;
8830 }
8831
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008832 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008833
8834 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
8835 if (target.Equals(return_loc)) {
8836 return;
8837 }
8838
8839 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8840 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008841 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008842 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008843 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
8844 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008845 GetMoveResolver()->EmitNativeCode(&parallel_move);
8846 } else {
8847 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01008848 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07008849 parallel_move.AddMove(return_loc, target, type, nullptr);
8850 GetMoveResolver()->EmitNativeCode(&parallel_move);
8851 }
8852}
8853
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008854void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
8855 const uint8_t* roots_data,
8856 const PatchInfo<Label>& info,
8857 uint64_t index_in_table) const {
8858 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
8859 uintptr_t address =
8860 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
Andreas Gampec55bb392018-09-21 00:02:02 +00008861 using unaligned_uint32_t __attribute__((__aligned__(1))) = uint32_t;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008862 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
8863 dchecked_integral_cast<uint32_t>(address);
8864}
8865
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008866void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8867 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008868 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008869 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008870 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008871 }
8872
8873 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008874 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008875 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008876 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008877 }
8878}
8879
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008880void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8881 ATTRIBUTE_UNUSED) {
8882 LOG(FATAL) << "Unreachable";
8883}
8884
8885void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8886 ATTRIBUTE_UNUSED) {
8887 LOG(FATAL) << "Unreachable";
8888}
8889
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05308890bool LocationsBuilderX86::CpuHasAvxFeatureFlag() {
8891 return codegen_->GetInstructionSetFeatures().HasAVX();
8892}
8893bool LocationsBuilderX86::CpuHasAvx2FeatureFlag() {
8894 return codegen_->GetInstructionSetFeatures().HasAVX2();
8895}
8896bool InstructionCodeGeneratorX86::CpuHasAvxFeatureFlag() {
8897 return codegen_->GetInstructionSetFeatures().HasAVX();
8898}
8899bool InstructionCodeGeneratorX86::CpuHasAvx2FeatureFlag() {
8900 return codegen_->GetInstructionSetFeatures().HasAVX2();
8901}
8902
Roland Levillain4d027112015-07-01 15:41:14 +01008903#undef __
8904
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008905} // namespace x86
8906} // namespace art