blob: f6c027080422f8d9ee8ab751b375d2acdcf49320 [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() ||
492 instruction_->IsStaticFieldGet() ||
493 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100494 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000495 instruction_->IsLoadClass() ||
496 instruction_->IsLoadString() ||
497 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100498 instruction_->IsCheckCast() ||
Andra Danciu1ca6f322020-08-12 08:58:07 +0000499 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000500 << "Unexpected instruction in read barrier marking slow path: "
501 << instruction_->DebugName();
502
503 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100504 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000505 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100506 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000507 }
Roland Levillain4359e612016-07-20 11:32:19 +0100508 // No need to save live registers; it's taken care of by the
509 // entrypoint. Also, there is no need to update the stack mask,
510 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000511 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100512 DCHECK_NE(ref_reg, ESP);
513 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100514 // "Compact" slow path, saving two moves.
515 //
516 // Instead of using the standard runtime calling convention (input
517 // and output in EAX):
518 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100519 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100520 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100521 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100522 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100523 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100524 // of a dedicated entrypoint:
525 //
526 // rX <- ReadBarrierMarkRegX(rX)
527 //
Roland Levillain97c46462017-05-11 14:04:03 +0100528 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100529 // This runtime call does not require a stack map.
530 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000531 __ jmp(GetExitLabel());
532 }
533
534 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100535 // The location (register) of the marked object reference.
536 const Location ref_;
537 // Should the reference in `ref_` be unpoisoned prior to marking it?
538 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000539
540 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
541};
542
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100543// Slow path marking an object reference `ref` during a read barrier,
544// and if needed, atomically updating the field `obj.field` in the
545// object `obj` holding this reference after marking (contrary to
546// ReadBarrierMarkSlowPathX86 above, which never tries to update
547// `obj.field`).
548//
549// This means that after the execution of this slow path, both `ref`
550// and `obj.field` will be up-to-date; i.e., after the flip, both will
551// hold the same to-space reference (unless another thread installed
552// another object reference (different from `ref`) in `obj.field`).
553class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
554 public:
555 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
556 Location ref,
557 Register obj,
558 const Address& field_addr,
559 bool unpoison_ref_before_marking,
560 Register temp)
561 : SlowPathCode(instruction),
562 ref_(ref),
563 obj_(obj),
564 field_addr_(field_addr),
565 unpoison_ref_before_marking_(unpoison_ref_before_marking),
566 temp_(temp) {
567 DCHECK(kEmitCompilerReadBarrier);
568 }
569
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100570 const char* GetDescription() const override { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100571
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100572 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100573 LocationSummary* locations = instruction_->GetLocations();
574 Register ref_reg = ref_.AsRegister<Register>();
575 DCHECK(locations->CanCall());
576 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
577 // This slow path is only used by the UnsafeCASObject intrinsic.
Andra Danciu5e13d452020-09-08 14:35:09 +0000578 DCHECK((instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100579 << "Unexpected instruction in read barrier marking and field updating slow path: "
580 << instruction_->DebugName();
581 DCHECK(instruction_->GetLocations()->Intrinsified());
Andra Danciu52d2c0c2020-09-15 14:27:21 +0000582 Intrinsics intrinsic = instruction_->AsInvoke()->GetIntrinsic();
583 static constexpr auto kVarHandleCAS = mirror::VarHandle::AccessModeTemplate::kCompareAndSet;
Andra Danciu9dfb1a92020-09-22 13:27:18 +0000584 static constexpr auto kVarHandleGetAndSet =
585 mirror::VarHandle::AccessModeTemplate::kGetAndUpdate;
Andra Danciu370948e2020-09-23 08:07:25 +0000586 static constexpr auto kVarHandleCAX =
587 mirror::VarHandle::AccessModeTemplate::kCompareAndExchange;
Andra Danciu52d2c0c2020-09-15 14:27:21 +0000588 DCHECK(intrinsic == Intrinsics::kUnsafeCASObject ||
Andra Danciu9dfb1a92020-09-22 13:27:18 +0000589 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) == kVarHandleCAS ||
Andra Danciu370948e2020-09-23 08:07:25 +0000590 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) == kVarHandleGetAndSet ||
591 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) == kVarHandleCAX);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100592
593 __ Bind(GetEntryLabel());
594 if (unpoison_ref_before_marking_) {
595 // Object* ref = ref_addr->AsMirrorPtr()
596 __ MaybeUnpoisonHeapReference(ref_reg);
597 }
598
599 // Save the old (unpoisoned) reference.
600 __ movl(temp_, ref_reg);
601
602 // No need to save live registers; it's taken care of by the
603 // entrypoint. Also, there is no need to update the stack mask,
604 // as this runtime call will not trigger a garbage collection.
605 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
606 DCHECK_NE(ref_reg, ESP);
607 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
608 // "Compact" slow path, saving two moves.
609 //
610 // Instead of using the standard runtime calling convention (input
611 // and output in EAX):
612 //
613 // EAX <- ref
614 // EAX <- ReadBarrierMark(EAX)
615 // ref <- EAX
616 //
617 // we just use rX (the register containing `ref`) as input and output
618 // of a dedicated entrypoint:
619 //
620 // rX <- ReadBarrierMarkRegX(rX)
621 //
Roland Levillain97c46462017-05-11 14:04:03 +0100622 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100623 // This runtime call does not require a stack map.
624 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
625
626 // If the new reference is different from the old reference,
627 // update the field in the holder (`*field_addr`).
628 //
629 // Note that this field could also hold a different object, if
630 // another thread had concurrently changed it. In that case, the
631 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
632 // operation below would abort the CAS, leaving the field as-is.
633 NearLabel done;
634 __ cmpl(temp_, ref_reg);
635 __ j(kEqual, &done);
636
637 // Update the the holder's field atomically. This may fail if
638 // mutator updates before us, but it's OK. This is achieved
639 // using a strong compare-and-set (CAS) operation with relaxed
640 // memory synchronization ordering, where the expected value is
641 // the old reference and the desired value is the new reference.
642 // This operation is implemented with a 32-bit LOCK CMPXLCHG
643 // instruction, which requires the expected value (the old
644 // reference) to be in EAX. Save EAX beforehand, and move the
645 // expected value (stored in `temp_`) into EAX.
646 __ pushl(EAX);
647 __ movl(EAX, temp_);
648
649 // Convenience aliases.
650 Register base = obj_;
651 Register expected = EAX;
652 Register value = ref_reg;
653
654 bool base_equals_value = (base == value);
655 if (kPoisonHeapReferences) {
656 if (base_equals_value) {
657 // If `base` and `value` are the same register location, move
658 // `value` to a temporary register. This way, poisoning
659 // `value` won't invalidate `base`.
660 value = temp_;
661 __ movl(value, base);
662 }
663
664 // Check that the register allocator did not assign the location
665 // of `expected` (EAX) to `value` nor to `base`, so that heap
666 // poisoning (when enabled) works as intended below.
667 // - If `value` were equal to `expected`, both references would
668 // be poisoned twice, meaning they would not be poisoned at
669 // all, as heap poisoning uses address negation.
670 // - If `base` were equal to `expected`, poisoning `expected`
671 // would invalidate `base`.
672 DCHECK_NE(value, expected);
673 DCHECK_NE(base, expected);
674
675 __ PoisonHeapReference(expected);
676 __ PoisonHeapReference(value);
677 }
678
679 __ LockCmpxchgl(field_addr_, value);
680
681 // If heap poisoning is enabled, we need to unpoison the values
682 // that were poisoned earlier.
683 if (kPoisonHeapReferences) {
684 if (base_equals_value) {
685 // `value` has been moved to a temporary register, no need
686 // to unpoison it.
687 } else {
688 __ UnpoisonHeapReference(value);
689 }
690 // No need to unpoison `expected` (EAX), as it is be overwritten below.
691 }
692
693 // Restore EAX.
694 __ popl(EAX);
695
696 __ Bind(&done);
697 __ jmp(GetExitLabel());
698 }
699
700 private:
701 // The location (register) of the marked object reference.
702 const Location ref_;
703 // The register containing the object holding the marked object reference field.
704 const Register obj_;
705 // The address of the marked reference field. The base of this address must be `obj_`.
706 const Address field_addr_;
707
708 // Should the reference in `ref_` be unpoisoned prior to marking it?
709 const bool unpoison_ref_before_marking_;
710
711 const Register temp_;
712
713 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
714};
715
Roland Levillain0d5a2812015-11-13 10:07:31 +0000716// Slow path generating a read barrier for a heap reference.
717class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
718 public:
719 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
720 Location out,
721 Location ref,
722 Location obj,
723 uint32_t offset,
724 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000725 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000726 out_(out),
727 ref_(ref),
728 obj_(obj),
729 offset_(offset),
730 index_(index) {
731 DCHECK(kEmitCompilerReadBarrier);
732 // If `obj` is equal to `out` or `ref`, it means the initial object
733 // has been overwritten by (or after) the heap object reference load
734 // to be instrumented, e.g.:
735 //
736 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000737 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000738 //
739 // In that case, we have lost the information about the original
740 // object, and the emitted read barrier cannot work properly.
741 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
742 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
743 }
744
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100745 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000746 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
747 LocationSummary* locations = instruction_->GetLocations();
748 Register reg_out = out_.AsRegister<Register>();
749 DCHECK(locations->CanCall());
750 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100751 DCHECK(instruction_->IsInstanceFieldGet() ||
752 instruction_->IsStaticFieldGet() ||
753 instruction_->IsArrayGet() ||
754 instruction_->IsInstanceOf() ||
755 instruction_->IsCheckCast() ||
Vladimir Marko94d2c812020-11-05 10:04:45 +0000756 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000757 << "Unexpected instruction in read barrier for heap reference slow path: "
758 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000759
760 __ Bind(GetEntryLabel());
761 SaveLiveRegisters(codegen, locations);
762
763 // We may have to change the index's value, but as `index_` is a
764 // constant member (like other "inputs" of this slow path),
765 // introduce a copy of it, `index`.
766 Location index = index_;
767 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100768 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000769 if (instruction_->IsArrayGet()) {
770 // Compute the actual memory offset and store it in `index`.
771 Register index_reg = index_.AsRegister<Register>();
772 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
773 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
774 // We are about to change the value of `index_reg` (see the
775 // calls to art::x86::X86Assembler::shll and
776 // art::x86::X86Assembler::AddImmediate below), but it has
777 // not been saved by the previous call to
778 // art::SlowPathCode::SaveLiveRegisters, as it is a
779 // callee-save register --
780 // art::SlowPathCode::SaveLiveRegisters does not consider
781 // callee-save registers, as it has been designed with the
782 // assumption that callee-save registers are supposed to be
783 // handled by the called function. So, as a callee-save
784 // register, `index_reg` _would_ eventually be saved onto
785 // the stack, but it would be too late: we would have
786 // changed its value earlier. Therefore, we manually save
787 // it here into another freely available register,
788 // `free_reg`, chosen of course among the caller-save
789 // registers (as a callee-save `free_reg` register would
790 // exhibit the same problem).
791 //
792 // Note we could have requested a temporary register from
793 // the register allocator instead; but we prefer not to, as
794 // this is a slow path, and we know we can find a
795 // caller-save register that is available.
796 Register free_reg = FindAvailableCallerSaveRegister(codegen);
797 __ movl(free_reg, index_reg);
798 index_reg = free_reg;
799 index = Location::RegisterLocation(index_reg);
800 } else {
801 // The initial register stored in `index_` has already been
802 // saved in the call to art::SlowPathCode::SaveLiveRegisters
803 // (as it is not a callee-save register), so we can freely
804 // use it.
805 }
806 // Shifting the index value contained in `index_reg` by the scale
807 // factor (2) cannot overflow in practice, as the runtime is
808 // unable to allocate object arrays with a size larger than
809 // 2^26 - 1 (that is, 2^28 - 4 bytes).
810 __ shll(index_reg, Immediate(TIMES_4));
811 static_assert(
812 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
813 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
814 __ AddImmediate(index_reg, Immediate(offset_));
815 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100816 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
817 // intrinsics, `index_` is not shifted by a scale factor of 2
818 // (as in the case of ArrayGet), as it is actually an offset
819 // to an object field within an object.
820 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000821 DCHECK(instruction_->GetLocations()->Intrinsified());
822 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
823 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
824 << instruction_->AsInvoke()->GetIntrinsic();
825 DCHECK_EQ(offset_, 0U);
826 DCHECK(index_.IsRegisterPair());
827 // UnsafeGet's offset location is a register pair, the low
828 // part contains the correct offset.
829 index = index_.ToLow();
830 }
831 }
832
833 // We're moving two or three locations to locations that could
834 // overlap, so we need a parallel move resolver.
835 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100836 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000837 parallel_move.AddMove(ref_,
838 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100839 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000840 nullptr);
841 parallel_move.AddMove(obj_,
842 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100843 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000844 nullptr);
845 if (index.IsValid()) {
846 parallel_move.AddMove(index,
847 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100848 DataType::Type::kInt32,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000849 nullptr);
850 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
851 } else {
852 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
853 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
854 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100855 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000856 CheckEntrypointTypes<
857 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
858 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
859
860 RestoreLiveRegisters(codegen, locations);
861 __ jmp(GetExitLabel());
862 }
863
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100864 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathX86"; }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000865
866 private:
867 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
868 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
869 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
870 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
871 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
872 return static_cast<Register>(i);
873 }
874 }
875 // We shall never fail to find a free caller-save register, as
876 // there are more than two core caller-save registers on x86
877 // (meaning it is possible to find one which is different from
878 // `ref` and `obj`).
879 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
880 LOG(FATAL) << "Could not find a free caller-save register";
881 UNREACHABLE();
882 }
883
Roland Levillain0d5a2812015-11-13 10:07:31 +0000884 const Location out_;
885 const Location ref_;
886 const Location obj_;
887 const uint32_t offset_;
888 // An additional location containing an index to an array.
889 // Only used for HArrayGet and the UnsafeGetObject &
890 // UnsafeGetObjectVolatile intrinsics.
891 const Location index_;
892
893 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
894};
895
896// Slow path generating a read barrier for a GC root.
897class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
898 public:
899 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000900 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000901 DCHECK(kEmitCompilerReadBarrier);
902 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000903
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100904 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000905 LocationSummary* locations = instruction_->GetLocations();
906 Register reg_out = out_.AsRegister<Register>();
907 DCHECK(locations->CanCall());
908 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000909 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
910 << "Unexpected instruction in read barrier for GC root slow path: "
911 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000912
913 __ Bind(GetEntryLabel());
914 SaveLiveRegisters(codegen, locations);
915
916 InvokeRuntimeCallingConvention calling_convention;
917 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
918 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100919 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000920 instruction_,
921 instruction_->GetDexPc(),
922 this);
923 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
924 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
925
926 RestoreLiveRegisters(codegen, locations);
927 __ jmp(GetExitLabel());
928 }
929
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100930 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathX86"; }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000931
932 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000933 const Location out_;
934 const Location root_;
935
936 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
937};
938
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100939#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100940// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
941#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100942
Aart Bike9f37602015-10-09 11:15:55 -0700943inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700944 switch (cond) {
945 case kCondEQ: return kEqual;
946 case kCondNE: return kNotEqual;
947 case kCondLT: return kLess;
948 case kCondLE: return kLessEqual;
949 case kCondGT: return kGreater;
950 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700951 case kCondB: return kBelow;
952 case kCondBE: return kBelowEqual;
953 case kCondA: return kAbove;
954 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700955 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100956 LOG(FATAL) << "Unreachable";
957 UNREACHABLE();
958}
959
Aart Bike9f37602015-10-09 11:15:55 -0700960// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100961inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
962 switch (cond) {
963 case kCondEQ: return kEqual;
964 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700965 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100966 case kCondLT: return kBelow;
967 case kCondLE: return kBelowEqual;
968 case kCondGT: return kAbove;
969 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700970 // Unsigned remain unchanged.
971 case kCondB: return kBelow;
972 case kCondBE: return kBelowEqual;
973 case kCondA: return kAbove;
974 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100975 }
976 LOG(FATAL) << "Unreachable";
977 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700978}
979
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100980void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100981 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100982}
983
984void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100985 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100986}
987
Vladimir Markoa0431112018-06-25 09:32:54 +0100988const X86InstructionSetFeatures& CodeGeneratorX86::GetInstructionSetFeatures() const {
989 return *GetCompilerOptions().GetInstructionSetFeatures()->AsX86InstructionSetFeatures();
990}
991
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100992size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
993 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
994 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100995}
996
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100997size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
998 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
999 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001000}
1001
Mark Mendell7c8d0092015-01-26 11:21:33 -05001002size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001003 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001004 __ movups(Address(ESP, stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -07001005 } else {
1006 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
1007 }
Artem Serov6a0b6572019-07-26 20:38:37 +01001008 return GetSlowPathFPWidth();
Mark Mendell7c8d0092015-01-26 11:21:33 -05001009}
1010
1011size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001012 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001013 __ movups(XmmRegister(reg_id), Address(ESP, stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -07001014 } else {
1015 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
1016 }
Artem Serov6a0b6572019-07-26 20:38:37 +01001017 return GetSlowPathFPWidth();
Mark Mendell7c8d0092015-01-26 11:21:33 -05001018}
1019
Calin Juravle175dc732015-08-25 15:42:32 +01001020void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
1021 HInstruction* instruction,
1022 uint32_t dex_pc,
1023 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001024 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001025 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
1026 if (EntrypointRequiresStackMap(entrypoint)) {
1027 RecordPcInfo(instruction, dex_pc, slow_path);
1028 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001029}
1030
Roland Levillaindec8f632016-07-22 17:10:06 +01001031void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1032 HInstruction* instruction,
1033 SlowPathCode* slow_path) {
1034 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001035 GenerateInvokeRuntime(entry_point_offset);
1036}
1037
1038void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001039 __ fs()->call(Address::Absolute(entry_point_offset));
1040}
1041
Mark Mendellfb8d2792015-03-31 22:16:59 -04001042CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001043 const CompilerOptions& compiler_options,
1044 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001045 : CodeGenerator(graph,
1046 kNumberOfCpuRegisters,
1047 kNumberOfXmmRegisters,
1048 kNumberOfRegisterPairs,
1049 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1050 arraysize(kCoreCalleeSaves))
1051 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001052 0,
1053 compiler_options,
1054 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001055 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001056 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001057 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001058 move_resolver_(graph->GetAllocator(), this),
1059 assembler_(graph->GetAllocator()),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001060 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1061 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1062 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1063 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko8f63f102020-09-28 12:10:28 +01001064 public_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1065 package_type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001066 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001067 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoeb9eb002020-10-02 13:54:19 +01001068 boot_image_jni_entrypoint_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +01001069 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001070 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1071 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001072 constant_area_start_(-1),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001073 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001074 method_address_offset_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001075 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001076 // Use a fake return address register to mimic Quick.
1077 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001078}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001079
David Brazdil58282f42016-01-14 12:45:10 +00001080void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001081 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001082 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001083}
1084
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001085InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001086 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001087 assembler_(codegen->GetAssembler()),
1088 codegen_(codegen) {}
1089
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001090static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001091 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001092}
1093
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001094void CodeGeneratorX86::MaybeIncrementHotness(bool is_frame_entry) {
1095 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1096 Register reg = EAX;
1097 if (is_frame_entry) {
1098 reg = kMethodRegisterArgument;
1099 } else {
1100 __ pushl(EAX);
Vladimir Markodec78172020-06-19 15:31:23 +01001101 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001102 __ movl(EAX, Address(ESP, kX86WordSize));
1103 }
1104 NearLabel overflow;
1105 __ cmpw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()),
1106 Immediate(ArtMethod::MaxCounter()));
1107 __ j(kEqual, &overflow);
1108 __ addw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()),
1109 Immediate(1));
1110 __ Bind(&overflow);
1111 if (!is_frame_entry) {
1112 __ popl(EAX);
Vladimir Markodec78172020-06-19 15:31:23 +01001113 __ cfi().AdjustCFAOffset(-4);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001114 }
1115 }
1116
1117 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffray095dc462020-08-17 16:40:28 +01001118 ScopedProfilingInfoUse spiu(
1119 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
1120 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001121 if (info != nullptr) {
1122 uint32_t address = reinterpret_cast32<uint32_t>(info);
1123 NearLabel done;
1124 if (HasEmptyFrame()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001125 CHECK(is_frame_entry);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001126 // Alignment
Vladimir Markodec78172020-06-19 15:31:23 +01001127 IncreaseFrame(8);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001128 // We need a temporary. The stub also expects the method at bottom of stack.
1129 __ pushl(EAX);
1130 __ cfi().AdjustCFAOffset(4);
1131 __ movl(EAX, Immediate(address));
1132 __ addw(Address(EAX, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()),
1133 Immediate(1));
Nicolas Geoffray8b8d93d2020-09-17 14:30:01 +01001134 __ andw(Address(EAX, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()),
1135 Immediate(interpreter::kTieredHotnessMask));
1136 __ j(kNotZero, &done);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001137 GenerateInvokeRuntime(
1138 GetThreadOffset<kX86PointerSize>(kQuickCompileOptimized).Int32Value());
1139 __ Bind(&done);
1140 // We don't strictly require to restore EAX, but this makes the generated
1141 // code easier to reason about.
1142 __ popl(EAX);
1143 __ cfi().AdjustCFAOffset(-4);
Vladimir Markodec78172020-06-19 15:31:23 +01001144 DecreaseFrame(8);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001145 } else {
1146 if (!RequiresCurrentMethod()) {
1147 CHECK(is_frame_entry);
1148 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1149 }
1150 // We need a temporary.
1151 __ pushl(EAX);
1152 __ cfi().AdjustCFAOffset(4);
1153 __ movl(EAX, Immediate(address));
1154 __ addw(Address(EAX, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()),
1155 Immediate(1));
1156 __ popl(EAX); // Put stack as expected before exiting or calling stub.
1157 __ cfi().AdjustCFAOffset(-4);
1158 __ j(kCarryClear, &done);
1159 GenerateInvokeRuntime(
1160 GetThreadOffset<kX86PointerSize>(kQuickCompileOptimized).Int32Value());
1161 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001162 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001163 }
1164 }
1165}
1166
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001167void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001168 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001169 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001170 bool skip_overflow_check =
1171 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001172 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001173
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001174 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001175 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86);
1176 __ testl(EAX, Address(ESP, -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001177 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001178 }
1179
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001180 if (!HasEmptyFrame()) {
1181 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1182 Register reg = kCoreCalleeSaves[i];
1183 if (allocated_registers_.ContainsCoreRegister(reg)) {
1184 __ pushl(reg);
1185 __ cfi().AdjustCFAOffset(kX86WordSize);
1186 __ cfi().RelOffset(DWARFReg(reg), 0);
1187 }
1188 }
Mark Mendell5f874182015-03-04 15:42:45 -05001189
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001190 int adjust = GetFrameSize() - FrameEntrySpillSize();
Vladimir Markodec78172020-06-19 15:31:23 +01001191 IncreaseFrame(adjust);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001192 // Save the current method if we need it. Note that we do not
1193 // do this in HCurrentMethod, as the instruction might have been removed
1194 // in the SSA graph.
1195 if (RequiresCurrentMethod()) {
1196 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1197 }
1198
1199 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1200 // Initialize should_deoptimize flag to 0.
1201 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
Mark Mendell5f874182015-03-04 15:42:45 -05001202 }
1203 }
1204
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001205 MaybeIncrementHotness(/* is_frame_entry= */ true);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001206}
1207
1208void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001209 __ cfi().RememberState();
1210 if (!HasEmptyFrame()) {
1211 int adjust = GetFrameSize() - FrameEntrySpillSize();
Vladimir Markodec78172020-06-19 15:31:23 +01001212 DecreaseFrame(adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001213
David Srbeckyc34dc932015-04-12 09:27:43 +01001214 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1215 Register reg = kCoreCalleeSaves[i];
1216 if (allocated_registers_.ContainsCoreRegister(reg)) {
1217 __ popl(reg);
1218 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1219 __ cfi().Restore(DWARFReg(reg));
1220 }
Mark Mendell5f874182015-03-04 15:42:45 -05001221 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001222 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001223 __ ret();
1224 __ cfi().RestoreState();
1225 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001226}
1227
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001228void CodeGeneratorX86::Bind(HBasicBlock* block) {
1229 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001230}
1231
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001232Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001233 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001234 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001235 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001236 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001237 case DataType::Type::kInt8:
1238 case DataType::Type::kUint16:
1239 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08001240 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001241 case DataType::Type::kInt32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001242 return Location::RegisterLocation(EAX);
1243
Aart Bik66c158e2018-01-31 12:55:04 -08001244 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001245 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001246 return Location::RegisterPairLocation(EAX, EDX);
1247
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001248 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001249 return Location::NoLocation();
1250
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001251 case DataType::Type::kFloat64:
1252 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001253 return Location::FpuRegisterLocation(XMM0);
1254 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001255
1256 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001257}
1258
1259Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1260 return Location::RegisterLocation(kMethodRegisterArgument);
1261}
1262
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001263Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001264 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001265 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001266 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001267 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001268 case DataType::Type::kInt8:
1269 case DataType::Type::kUint16:
1270 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001271 case DataType::Type::kInt32: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001272 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001273 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001274 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001275 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001276 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001277 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001278 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001279 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001280
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001281 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001282 uint32_t index = gp_index_;
1283 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001284 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001285 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001286 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1287 calling_convention.GetRegisterPairAt(index));
1288 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001289 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001290 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1291 }
1292 }
1293
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001294 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001295 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001296 stack_index_++;
1297 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1298 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1299 } else {
1300 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1301 }
1302 }
1303
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001304 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001305 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001306 stack_index_ += 2;
1307 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1308 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1309 } else {
1310 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001311 }
1312 }
1313
Aart Bik66c158e2018-01-31 12:55:04 -08001314 case DataType::Type::kUint32:
1315 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001316 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001317 LOG(FATAL) << "Unexpected parameter type " << type;
Elliott Hughesc1896c92018-11-29 11:33:18 -08001318 UNREACHABLE();
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001319 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001320 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001321}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001322
Vladimir Marko86c87522020-05-11 16:55:55 +01001323Location CriticalNativeCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
1324 DCHECK_NE(type, DataType::Type::kReference);
1325
1326 Location location;
1327 if (DataType::Is64BitType(type)) {
1328 location = Location::DoubleStackSlot(stack_offset_);
1329 stack_offset_ += 2 * kFramePointerSize;
1330 } else {
1331 location = Location::StackSlot(stack_offset_);
1332 stack_offset_ += kFramePointerSize;
1333 }
1334 if (for_register_allocation_) {
1335 location = Location::Any();
1336 }
1337 return location;
1338}
1339
1340Location CriticalNativeCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
1341 // We perform conversion to the managed ABI return register after the call if needed.
1342 InvokeDexCallingConventionVisitorX86 dex_calling_convention;
1343 return dex_calling_convention.GetReturnLocation(type);
1344}
1345
1346Location CriticalNativeCallingConventionVisitorX86::GetMethodLocation() const {
1347 // Pass the method in the hidden argument EAX.
1348 return Location::RegisterLocation(EAX);
1349}
1350
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001351void CodeGeneratorX86::Move32(Location destination, Location source) {
1352 if (source.Equals(destination)) {
1353 return;
1354 }
1355 if (destination.IsRegister()) {
1356 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001357 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001358 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001359 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Andra Danciu5e13d452020-09-08 14:35:09 +00001360 } else if (source.IsConstant()) {
1361 int32_t value = GetInt32ValueOf(source.GetConstant());
1362 __ movl(destination.AsRegister<Register>(), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001363 } else {
1364 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001365 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001366 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001367 } else if (destination.IsFpuRegister()) {
1368 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001369 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001370 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001371 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001372 } else {
1373 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001374 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001375 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001376 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001377 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001378 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001379 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001380 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001381 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001382 } else if (source.IsConstant()) {
1383 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001384 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001385 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001386 } else {
1387 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001388 __ pushl(Address(ESP, source.GetStackIndex()));
1389 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001390 }
1391 }
1392}
1393
1394void CodeGeneratorX86::Move64(Location destination, Location source) {
1395 if (source.Equals(destination)) {
1396 return;
1397 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001398 if (destination.IsRegisterPair()) {
1399 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001400 EmitParallelMoves(
1401 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1402 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001403 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001404 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001405 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001406 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001407 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001408 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1409 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1410 __ psrlq(src_reg, Immediate(32));
1411 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001412 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001413 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001414 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001415 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1416 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001417 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1418 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001419 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001420 if (source.IsFpuRegister()) {
1421 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1422 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001423 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001424 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001425 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Vladimir Markodec78172020-06-19 15:31:23 +01001426 // Push the 2 source registers to the stack.
1427 __ pushl(source.AsRegisterPairHigh<Register>());
1428 __ cfi().AdjustCFAOffset(elem_size);
1429 __ pushl(source.AsRegisterPairLow<Register>());
1430 __ cfi().AdjustCFAOffset(elem_size);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001431 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1432 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01001433 DecreaseFrame(2 * elem_size);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001434 } else {
1435 LOG(FATAL) << "Unimplemented";
1436 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001437 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001438 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001439 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001440 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001441 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001442 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001443 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001444 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001445 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001446 } else if (source.IsConstant()) {
1447 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001448 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1449 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001450 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001451 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1452 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001453 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001454 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001455 EmitParallelMoves(
1456 Location::StackSlot(source.GetStackIndex()),
1457 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001458 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001459 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001460 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001461 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001462 }
1463 }
1464}
1465
Andra Danciu1ca6f322020-08-12 08:58:07 +00001466static Address CreateAddress(Register base,
1467 Register index = Register::kNoRegister,
1468 ScaleFactor scale = TIMES_1,
1469 int32_t disp = 0) {
1470 if (index == Register::kNoRegister) {
1471 return Address(base, disp);
1472 }
1473
1474 return Address(base, index, scale, disp);
1475}
1476
Andra Danciud0f71f22020-09-17 09:00:15 +00001477void CodeGeneratorX86::LoadFromMemoryNoBarrier(DataType::Type dst_type,
1478 Location dst,
1479 Address src,
1480 XmmRegister temp,
1481 bool is_atomic_load) {
Andra Danciu1ca6f322020-08-12 08:58:07 +00001482 switch (dst_type) {
1483 case DataType::Type::kBool:
1484 case DataType::Type::kUint8:
1485 __ movzxb(dst.AsRegister<Register>(), src);
1486 break;
1487 case DataType::Type::kInt8:
1488 __ movsxb(dst.AsRegister<Register>(), src);
1489 break;
1490 case DataType::Type::kInt16:
1491 __ movsxw(dst.AsRegister<Register>(), src);
1492 break;
1493 case DataType::Type::kUint16:
1494 __ movzxw(dst.AsRegister<Register>(), src);
1495 break;
1496 case DataType::Type::kInt32:
Andra Danciu1ca6f322020-08-12 08:58:07 +00001497 __ movl(dst.AsRegister<Register>(), src);
1498 break;
Andra Danciud0f71f22020-09-17 09:00:15 +00001499 case DataType::Type::kInt64: {
1500 if (is_atomic_load) {
1501 __ movsd(temp, src);
1502 __ movd(dst.AsRegisterPairLow<Register>(), temp);
1503 __ psrlq(temp, Immediate(32));
1504 __ movd(dst.AsRegisterPairHigh<Register>(), temp);
1505 } else {
1506 DCHECK_NE(src.GetBaseRegister(), dst.AsRegisterPairLow<Register>());
1507 Address src_high = src.displaceBy(kX86WordSize);
1508 __ movl(dst.AsRegisterPairLow<Register>(), src);
1509 __ movl(dst.AsRegisterPairHigh<Register>(), src_high);
1510 }
Andra Danciu1ca6f322020-08-12 08:58:07 +00001511 break;
1512 }
1513 case DataType::Type::kFloat32:
1514 __ movss(dst.AsFpuRegister<XmmRegister>(), src);
1515 break;
1516 case DataType::Type::kFloat64:
1517 __ movsd(dst.AsFpuRegister<XmmRegister>(), src);
1518 break;
Andra Danciu1ca6f322020-08-12 08:58:07 +00001519 case DataType::Type::kReference:
Andra Danciud0f71f22020-09-17 09:00:15 +00001520 __ movl(dst.AsRegister<Register>(), src);
1521 __ MaybeUnpoisonHeapReference(dst.AsRegister<Register>());
1522 break;
1523 default:
Andra Danciu1ca6f322020-08-12 08:58:07 +00001524 LOG(FATAL) << "Unreachable type " << dst_type;
1525 }
1526}
1527
Andra Danciu73c31802020-09-01 13:17:05 +00001528void CodeGeneratorX86::MoveToMemory(DataType::Type src_type,
1529 Location src,
1530 Register dst_base,
1531 Register dst_index,
1532 ScaleFactor dst_scale,
1533 int32_t dst_disp) {
1534 DCHECK(dst_base != Register::kNoRegister);
1535 Address dst = CreateAddress(dst_base, dst_index, dst_scale, dst_disp);
1536
1537 switch (src_type) {
1538 case DataType::Type::kBool:
1539 case DataType::Type::kUint8:
1540 case DataType::Type::kInt8: {
1541 if (src.IsConstant()) {
1542 __ movb(dst, Immediate(CodeGenerator::GetInt8ValueOf(src.GetConstant())));
1543 } else {
1544 __ movb(dst, src.AsRegister<ByteRegister>());
1545 }
1546 break;
1547 }
1548 case DataType::Type::kUint16:
1549 case DataType::Type::kInt16: {
1550 if (src.IsConstant()) {
1551 __ movw(dst, Immediate(CodeGenerator::GetInt16ValueOf(src.GetConstant())));
1552 } else {
1553 __ movw(dst, src.AsRegister<Register>());
1554 }
1555 break;
1556 }
1557 case DataType::Type::kUint32:
1558 case DataType::Type::kInt32: {
1559 if (src.IsConstant()) {
1560 int32_t v = CodeGenerator::GetInt32ValueOf(src.GetConstant());
1561 __ movl(dst, Immediate(v));
1562 } else {
1563 __ movl(dst, src.AsRegister<Register>());
1564 }
1565 break;
1566 }
1567 case DataType::Type::kUint64:
1568 case DataType::Type::kInt64: {
1569 Address dst_next_4_bytes = CreateAddress(dst_base, dst_index, dst_scale, dst_disp + 4);
1570 if (src.IsConstant()) {
1571 int64_t v = CodeGenerator::GetInt64ValueOf(src.GetConstant());
1572 __ movl(dst, Immediate(Low32Bits(v)));
1573 __ movl(dst_next_4_bytes, Immediate(High32Bits(v)));
1574 } else {
1575 __ movl(dst, src.AsRegisterPairLow<Register>());
1576 __ movl(dst_next_4_bytes, src.AsRegisterPairHigh<Register>());
1577 }
1578 break;
1579 }
1580 case DataType::Type::kFloat32: {
1581 if (src.IsConstant()) {
1582 int32_t v = CodeGenerator::GetInt32ValueOf(src.GetConstant());
1583 __ movl(dst, Immediate(v));
1584 } else {
1585 __ movss(dst, src.AsFpuRegister<XmmRegister>());
1586 }
1587 break;
1588 }
1589 case DataType::Type::kFloat64: {
1590 Address dst_next_4_bytes = CreateAddress(dst_base, dst_index, dst_scale, dst_disp + 4);
1591 if (src.IsConstant()) {
1592 int64_t v = CodeGenerator::GetInt64ValueOf(src.GetConstant());
1593 __ movl(dst, Immediate(Low32Bits(v)));
1594 __ movl(dst_next_4_bytes, Immediate(High32Bits(v)));
1595 } else {
1596 __ movsd(dst, src.AsFpuRegister<XmmRegister>());
1597 }
1598 break;
1599 }
1600 case DataType::Type::kVoid:
1601 case DataType::Type::kReference:
1602 LOG(FATAL) << "Unreachable type " << src_type;
1603 }
1604}
1605
Calin Juravle175dc732015-08-25 15:42:32 +01001606void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1607 DCHECK(location.IsRegister());
1608 __ movl(location.AsRegister<Register>(), Immediate(value));
1609}
1610
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001611void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001612 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001613 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1614 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1615 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001616 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001617 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001618 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001619 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001620}
1621
1622void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1623 if (location.IsRegister()) {
1624 locations->AddTemp(location);
1625 } else if (location.IsRegisterPair()) {
1626 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1627 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1628 } else {
1629 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1630 }
1631}
1632
David Brazdilfc6a86a2015-06-26 10:33:45 +00001633void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001634 if (successor->IsExitBlock()) {
1635 DCHECK(got->GetPrevious()->AlwaysThrows());
1636 return; // no code needed
1637 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001638
1639 HBasicBlock* block = got->GetBlock();
1640 HInstruction* previous = got->GetPrevious();
1641
1642 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001643 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001644 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001645 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1646 return;
1647 }
1648
1649 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1650 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1651 }
1652 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001653 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001654 }
1655}
1656
David Brazdilfc6a86a2015-06-26 10:33:45 +00001657void LocationsBuilderX86::VisitGoto(HGoto* got) {
1658 got->SetLocations(nullptr);
1659}
1660
1661void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1662 HandleGoto(got, got->GetSuccessor());
1663}
1664
1665void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1666 try_boundary->SetLocations(nullptr);
1667}
1668
1669void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1670 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1671 if (!successor->IsExitBlock()) {
1672 HandleGoto(try_boundary, successor);
1673 }
1674}
1675
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001676void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001677 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001678}
1679
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001680void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001681}
1682
Mark Mendell152408f2015-12-31 12:28:50 -05001683template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001684void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001685 LabelType* true_label,
1686 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001687 if (cond->IsFPConditionTrueIfNaN()) {
1688 __ j(kUnordered, true_label);
1689 } else if (cond->IsFPConditionFalseIfNaN()) {
1690 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001691 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001692 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001693}
1694
Mark Mendell152408f2015-12-31 12:28:50 -05001695template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001696void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001697 LabelType* true_label,
1698 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001699 LocationSummary* locations = cond->GetLocations();
1700 Location left = locations->InAt(0);
1701 Location right = locations->InAt(1);
1702 IfCondition if_cond = cond->GetCondition();
1703
Mark Mendellc4701932015-04-10 13:18:51 -04001704 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001705 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001706 IfCondition true_high_cond = if_cond;
1707 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001708 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001709
1710 // Set the conditions for the test, remembering that == needs to be
1711 // decided using the low words.
1712 switch (if_cond) {
1713 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001714 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001715 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001716 break;
1717 case kCondLT:
1718 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001719 break;
1720 case kCondLE:
1721 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001722 break;
1723 case kCondGT:
1724 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001725 break;
1726 case kCondGE:
1727 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001728 break;
Aart Bike9f37602015-10-09 11:15:55 -07001729 case kCondB:
1730 false_high_cond = kCondA;
1731 break;
1732 case kCondBE:
1733 true_high_cond = kCondB;
1734 break;
1735 case kCondA:
1736 false_high_cond = kCondB;
1737 break;
1738 case kCondAE:
1739 true_high_cond = kCondA;
1740 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001741 }
1742
1743 if (right.IsConstant()) {
1744 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001745 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001746 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001747
Aart Bika19616e2016-02-01 18:57:58 -08001748 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001749 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001750 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001751 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001752 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001753 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001754 __ j(X86Condition(true_high_cond), true_label);
1755 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001756 }
1757 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001758 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001759 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001760 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001761 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001762
1763 __ cmpl(left_high, right_high);
1764 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001765 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001766 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001767 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001768 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001769 __ j(X86Condition(true_high_cond), true_label);
1770 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001771 }
1772 // Must be equal high, so compare the lows.
1773 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001774 } else {
1775 DCHECK(right.IsDoubleStackSlot());
1776 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1777 if (if_cond == kCondNE) {
1778 __ j(X86Condition(true_high_cond), true_label);
1779 } else if (if_cond == kCondEQ) {
1780 __ j(X86Condition(false_high_cond), false_label);
1781 } else {
1782 __ j(X86Condition(true_high_cond), true_label);
1783 __ j(X86Condition(false_high_cond), false_label);
1784 }
1785 // Must be equal high, so compare the lows.
1786 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001787 }
1788 // The last comparison might be unsigned.
1789 __ j(final_condition, true_label);
1790}
1791
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001792void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1793 Location rhs,
1794 HInstruction* insn,
1795 bool is_double) {
1796 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1797 if (is_double) {
1798 if (rhs.IsFpuRegister()) {
1799 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1800 } else if (const_area != nullptr) {
1801 DCHECK(const_area->IsEmittedAtUseSite());
1802 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1803 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001804 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1805 const_area->GetBaseMethodAddress(),
1806 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001807 } else {
1808 DCHECK(rhs.IsDoubleStackSlot());
1809 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1810 }
1811 } else {
1812 if (rhs.IsFpuRegister()) {
1813 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1814 } else if (const_area != nullptr) {
1815 DCHECK(const_area->IsEmittedAtUseSite());
1816 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1817 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001818 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1819 const_area->GetBaseMethodAddress(),
1820 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001821 } else {
1822 DCHECK(rhs.IsStackSlot());
1823 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1824 }
1825 }
1826}
1827
Mark Mendell152408f2015-12-31 12:28:50 -05001828template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001829void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001830 LabelType* true_target_in,
1831 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001832 // Generated branching requires both targets to be explicit. If either of the
1833 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001834 LabelType fallthrough_target;
1835 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1836 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001837
Mark Mendellc4701932015-04-10 13:18:51 -04001838 LocationSummary* locations = condition->GetLocations();
1839 Location left = locations->InAt(0);
1840 Location right = locations->InAt(1);
1841
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001842 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001843 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001844 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001845 GenerateLongComparesAndJumps(condition, true_target, false_target);
1846 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001847 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001848 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001849 GenerateFPJumps(condition, true_target, false_target);
1850 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001851 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001852 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001853 GenerateFPJumps(condition, true_target, false_target);
1854 break;
1855 default:
1856 LOG(FATAL) << "Unexpected compare type " << type;
1857 }
1858
David Brazdil0debae72015-11-12 18:37:00 +00001859 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001860 __ jmp(false_target);
1861 }
David Brazdil0debae72015-11-12 18:37:00 +00001862
1863 if (fallthrough_target.IsLinked()) {
1864 __ Bind(&fallthrough_target);
1865 }
Mark Mendellc4701932015-04-10 13:18:51 -04001866}
1867
David Brazdil0debae72015-11-12 18:37:00 +00001868static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1869 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1870 // are set only strictly before `branch`. We can't use the eflags on long/FP
1871 // conditions if they are materialized due to the complex branching.
1872 return cond->IsCondition() &&
1873 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001874 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1875 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001876}
1877
Mark Mendell152408f2015-12-31 12:28:50 -05001878template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001879void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001880 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001881 LabelType* true_target,
1882 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001883 HInstruction* cond = instruction->InputAt(condition_input_index);
1884
1885 if (true_target == nullptr && false_target == nullptr) {
1886 // Nothing to do. The code always falls through.
1887 return;
1888 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001889 // Constant condition, statically compared against "true" (integer value 1).
1890 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001891 if (true_target != nullptr) {
1892 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001893 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001894 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001895 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001896 if (false_target != nullptr) {
1897 __ jmp(false_target);
1898 }
1899 }
1900 return;
1901 }
1902
1903 // The following code generates these patterns:
1904 // (1) true_target == nullptr && false_target != nullptr
1905 // - opposite condition true => branch to false_target
1906 // (2) true_target != nullptr && false_target == nullptr
1907 // - condition true => branch to true_target
1908 // (3) true_target != nullptr && false_target != nullptr
1909 // - condition true => branch to true_target
1910 // - branch to false_target
1911 if (IsBooleanValueOrMaterializedCondition(cond)) {
1912 if (AreEflagsSetFrom(cond, instruction)) {
1913 if (true_target == nullptr) {
1914 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1915 } else {
1916 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1917 }
1918 } else {
1919 // Materialized condition, compare against 0.
1920 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1921 if (lhs.IsRegister()) {
1922 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1923 } else {
1924 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1925 }
1926 if (true_target == nullptr) {
1927 __ j(kEqual, false_target);
1928 } else {
1929 __ j(kNotEqual, true_target);
1930 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001931 }
1932 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001933 // Condition has not been materialized, use its inputs as the comparison and
1934 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001935 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001936
1937 // If this is a long or FP comparison that has been folded into
1938 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001939 DataType::Type type = condition->InputAt(0)->GetType();
1940 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001941 GenerateCompareTestAndBranch(condition, true_target, false_target);
1942 return;
1943 }
1944
1945 Location lhs = condition->GetLocations()->InAt(0);
1946 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001947 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001948 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001949 if (true_target == nullptr) {
1950 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1951 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001952 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001953 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001954 }
David Brazdil0debae72015-11-12 18:37:00 +00001955
1956 // If neither branch falls through (case 3), the conditional branch to `true_target`
1957 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1958 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001959 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001960 }
1961}
1962
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001963void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001964 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001965 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001966 locations->SetInAt(0, Location::Any());
1967 }
1968}
1969
1970void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001971 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1972 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1973 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1974 nullptr : codegen_->GetLabelOf(true_successor);
1975 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1976 nullptr : codegen_->GetLabelOf(false_successor);
Andreas Gampe3db70682018-12-26 15:12:03 -08001977 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001978}
1979
1980void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001981 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001982 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001983 InvokeRuntimeCallingConvention calling_convention;
1984 RegisterSet caller_saves = RegisterSet::Empty();
1985 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1986 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001987 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001988 locations->SetInAt(0, Location::Any());
1989 }
1990}
1991
1992void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001993 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001994 GenerateTestAndBranch<Label>(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08001995 /* condition_input_index= */ 0,
David Brazdil74eb1b22015-12-14 11:44:01 +00001996 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08001997 /* false_target= */ nullptr);
David Brazdil74eb1b22015-12-14 11:44:01 +00001998}
1999
Mingyao Yang063fc772016-08-02 11:02:54 -07002000void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002001 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07002002 LocationSummary(flag, LocationSummary::kNoCall);
2003 locations->SetOut(Location::RequiresRegister());
2004}
2005
2006void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2007 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
2008 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
2009}
2010
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002011static bool SelectCanUseCMOV(HSelect* select) {
2012 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002013 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002014 return false;
2015 }
2016
2017 // A FP condition doesn't generate the single CC that we need.
2018 // In 32 bit mode, a long condition doesn't generate a single CC either.
2019 HInstruction* condition = select->GetCondition();
2020 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002021 DataType::Type compare_type = condition->InputAt(0)->GetType();
2022 if (compare_type == DataType::Type::kInt64 ||
2023 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002024 return false;
2025 }
2026 }
2027
2028 // We can generate a CMOV for this Select.
2029 return true;
2030}
2031
David Brazdil74eb1b22015-12-14 11:44:01 +00002032void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002033 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002034 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00002035 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002036 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00002037 } else {
2038 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002039 if (SelectCanUseCMOV(select)) {
2040 if (select->InputAt(1)->IsConstant()) {
2041 // Cmov can't handle a constant value.
2042 locations->SetInAt(1, Location::RequiresRegister());
2043 } else {
2044 locations->SetInAt(1, Location::Any());
2045 }
2046 } else {
2047 locations->SetInAt(1, Location::Any());
2048 }
David Brazdil74eb1b22015-12-14 11:44:01 +00002049 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002050 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2051 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00002052 }
2053 locations->SetOut(Location::SameAsFirstInput());
2054}
2055
2056void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
2057 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002058 DCHECK(locations->InAt(0).Equals(locations->Out()));
2059 if (SelectCanUseCMOV(select)) {
2060 // If both the condition and the source types are integer, we can generate
2061 // a CMOV to implement Select.
2062
2063 HInstruction* select_condition = select->GetCondition();
2064 Condition cond = kNotEqual;
2065
2066 // Figure out how to test the 'condition'.
2067 if (select_condition->IsCondition()) {
2068 HCondition* condition = select_condition->AsCondition();
2069 if (!condition->IsEmittedAtUseSite()) {
2070 // This was a previously materialized condition.
2071 // Can we use the existing condition code?
2072 if (AreEflagsSetFrom(condition, select)) {
2073 // Materialization was the previous instruction. Condition codes are right.
2074 cond = X86Condition(condition->GetCondition());
2075 } else {
2076 // No, we have to recreate the condition code.
2077 Register cond_reg = locations->InAt(2).AsRegister<Register>();
2078 __ testl(cond_reg, cond_reg);
2079 }
2080 } else {
2081 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002082 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
2083 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002084 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01002085 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002086 cond = X86Condition(condition->GetCondition());
2087 }
2088 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002089 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002090 Register cond_reg = locations->InAt(2).AsRegister<Register>();
2091 __ testl(cond_reg, cond_reg);
2092 }
2093
2094 // If the condition is true, overwrite the output, which already contains false.
2095 Location false_loc = locations->InAt(0);
2096 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002097 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002098 // 64 bit conditional move.
2099 Register false_high = false_loc.AsRegisterPairHigh<Register>();
2100 Register false_low = false_loc.AsRegisterPairLow<Register>();
2101 if (true_loc.IsRegisterPair()) {
2102 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
2103 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
2104 } else {
2105 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
2106 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
2107 }
2108 } else {
2109 // 32 bit conditional move.
2110 Register false_reg = false_loc.AsRegister<Register>();
2111 if (true_loc.IsRegister()) {
2112 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
2113 } else {
2114 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
2115 }
2116 }
2117 } else {
2118 NearLabel false_target;
2119 GenerateTestAndBranch<NearLabel>(
Andreas Gampe3db70682018-12-26 15:12:03 -08002120 select, /* condition_input_index= */ 2, /* true_target= */ nullptr, &false_target);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002121 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2122 __ Bind(&false_target);
2123 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002124}
2125
David Srbecky0cf44932015-12-09 14:09:59 +00002126void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002127 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00002128}
2129
David Srbeckyd28f4a02016-03-14 17:14:24 +00002130void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
2131 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002132}
2133
Vladimir Markodec78172020-06-19 15:31:23 +01002134void CodeGeneratorX86::IncreaseFrame(size_t adjustment) {
2135 __ subl(ESP, Immediate(adjustment));
2136 __ cfi().AdjustCFAOffset(adjustment);
2137}
2138
2139void CodeGeneratorX86::DecreaseFrame(size_t adjustment) {
2140 __ addl(ESP, Immediate(adjustment));
2141 __ cfi().AdjustCFAOffset(-adjustment);
2142}
2143
David Srbeckyc7098ff2016-02-09 14:30:11 +00002144void CodeGeneratorX86::GenerateNop() {
2145 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002146}
2147
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002148void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002149 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002150 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04002151 // Handle the long/FP comparisons made in instruction simplification.
2152 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002153 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04002154 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05002155 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00002156 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002157 locations->SetOut(Location::RequiresRegister());
2158 }
2159 break;
2160 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002161 case DataType::Type::kFloat32:
2162 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04002163 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002164 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
2165 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
2166 } else if (cond->InputAt(1)->IsConstant()) {
2167 locations->SetInAt(1, Location::RequiresFpuRegister());
2168 } else {
2169 locations->SetInAt(1, Location::Any());
2170 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002171 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002172 locations->SetOut(Location::RequiresRegister());
2173 }
2174 break;
2175 }
2176 default:
2177 locations->SetInAt(0, Location::RequiresRegister());
2178 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00002179 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002180 // We need a byte register.
2181 locations->SetOut(Location::RegisterLocation(ECX));
2182 }
2183 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002184 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002185}
2186
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002187void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002188 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002189 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002190 }
Mark Mendellc4701932015-04-10 13:18:51 -04002191
2192 LocationSummary* locations = cond->GetLocations();
2193 Location lhs = locations->InAt(0);
2194 Location rhs = locations->InAt(1);
2195 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05002196 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002197
2198 switch (cond->InputAt(0)->GetType()) {
2199 default: {
2200 // Integer case.
2201
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002202 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04002203 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01002204 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07002205 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04002206 return;
2207 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002208 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04002209 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2210 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002211 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002212 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04002213 GenerateFPJumps(cond, &true_label, &false_label);
2214 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002215 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002216 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04002217 GenerateFPJumps(cond, &true_label, &false_label);
2218 break;
2219 }
2220
2221 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002222 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002223
Roland Levillain4fa13f62015-07-06 18:11:54 +01002224 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04002225 __ Bind(&false_label);
2226 __ xorl(reg, reg);
2227 __ jmp(&done_label);
2228
Roland Levillain4fa13f62015-07-06 18:11:54 +01002229 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04002230 __ Bind(&true_label);
2231 __ movl(reg, Immediate(1));
2232 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002233}
2234
2235void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002236 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002237}
2238
2239void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002240 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002241}
2242
2243void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002244 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002245}
2246
2247void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002248 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002249}
2250
2251void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002252 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002253}
2254
2255void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002256 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002257}
2258
2259void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002260 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002261}
2262
2263void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002264 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002265}
2266
2267void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002268 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002269}
2270
2271void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002272 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002273}
2274
2275void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002276 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002277}
2278
2279void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002280 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002281}
2282
Aart Bike9f37602015-10-09 11:15:55 -07002283void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002284 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002285}
2286
2287void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002288 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002289}
2290
2291void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002292 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002293}
2294
2295void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002296 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002297}
2298
2299void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002300 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002301}
2302
2303void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002304 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002305}
2306
2307void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002308 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002309}
2310
2311void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002312 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002313}
2314
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002315void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002316 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002317 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002318 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002319}
2320
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002321void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002322 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002323}
2324
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002325void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2326 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002327 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002328 locations->SetOut(Location::ConstantLocation(constant));
2329}
2330
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002331void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002332 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002333}
2334
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002335void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002336 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002337 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002338 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002339}
2340
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002341void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002342 // Will be generated at use site.
2343}
2344
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002345void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2346 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002347 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002348 locations->SetOut(Location::ConstantLocation(constant));
2349}
2350
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002351void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002352 // Will be generated at use site.
2353}
2354
2355void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2356 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002357 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002358 locations->SetOut(Location::ConstantLocation(constant));
2359}
2360
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002361void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002362 // Will be generated at use site.
2363}
2364
Igor Murashkind01745e2017-04-05 16:40:31 -07002365void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2366 constructor_fence->SetLocations(nullptr);
2367}
2368
2369void InstructionCodeGeneratorX86::VisitConstructorFence(
2370 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2371 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2372}
2373
Calin Juravle27df7582015-04-17 19:12:31 +01002374void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2375 memory_barrier->SetLocations(nullptr);
2376}
2377
2378void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002379 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002380}
2381
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002382void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002383 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002384}
2385
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002386void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002387 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002388}
2389
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002390void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002391 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002392 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002393 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002394 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002395 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002396 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002397 case DataType::Type::kInt8:
2398 case DataType::Type::kUint16:
2399 case DataType::Type::kInt16:
2400 case DataType::Type::kInt32:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002401 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002402 break;
2403
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002404 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002405 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002406 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002407 break;
2408
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002409 case DataType::Type::kFloat32:
2410 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002411 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002412 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002413 break;
2414
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002415 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002416 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002417 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002418}
2419
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002420void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002421 switch (ret->InputAt(0)->GetType()) {
2422 case DataType::Type::kReference:
2423 case DataType::Type::kBool:
2424 case DataType::Type::kUint8:
2425 case DataType::Type::kInt8:
2426 case DataType::Type::kUint16:
2427 case DataType::Type::kInt16:
2428 case DataType::Type::kInt32:
2429 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
2430 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002431
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002432 case DataType::Type::kInt64:
2433 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2434 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
2435 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002436
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002437 case DataType::Type::kFloat32:
2438 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2439 if (GetGraph()->IsCompilingOsr()) {
2440 // To simplify callers of an OSR method, we put the return value in both
2441 // floating point and core registers.
2442 __ movd(EAX, XMM0);
2443 }
2444 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002445
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002446 case DataType::Type::kFloat64:
2447 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2448 if (GetGraph()->IsCompilingOsr()) {
2449 // To simplify callers of an OSR method, we put the return value in both
2450 // floating point and core registers.
2451 __ movd(EAX, XMM0);
2452 // Use XMM1 as temporary register to not clobber XMM0.
2453 __ movaps(XMM1, XMM0);
2454 __ psrlq(XMM1, Immediate(32));
2455 __ movd(EDX, XMM1);
2456 }
2457 break;
2458
2459 default:
2460 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002461 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002462 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002463}
2464
Calin Juravle175dc732015-08-25 15:42:32 +01002465void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2466 // The trampoline uses the same calling convention as dex calling conventions,
2467 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2468 // the method_idx.
2469 HandleInvoke(invoke);
2470}
2471
2472void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2473 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2474}
2475
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002476void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002477 // Explicit clinit checks triggered by static invokes must have been pruned by
2478 // art::PrepareForRegisterAllocation.
2479 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002480
Mark Mendellfb8d2792015-03-31 22:16:59 -04002481 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002482 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002483 if (invoke->GetLocations()->CanCall() &&
2484 invoke->HasPcRelativeMethodLoadKind() &&
2485 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).IsInvalid()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002486 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002487 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002488 return;
2489 }
2490
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01002491 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
Vladimir Marko86c87522020-05-11 16:55:55 +01002492 CriticalNativeCallingConventionVisitorX86 calling_convention_visitor(
2493 /*for_register_allocation=*/ true);
2494 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2495 } else {
2496 HandleInvoke(invoke);
2497 }
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002498
Vladimir Marko86c87522020-05-11 16:55:55 +01002499 // For PC-relative load kinds the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002500 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002501 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002502 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002503}
2504
Mark Mendell09ed1a32015-03-25 08:30:06 -04002505static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2506 if (invoke->GetLocations()->Intrinsified()) {
2507 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2508 intrinsic.Dispatch(invoke);
2509 return true;
2510 }
2511 return false;
2512}
2513
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002514void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002515 // Explicit clinit checks triggered by static invokes must have been pruned by
2516 // art::PrepareForRegisterAllocation.
2517 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002518
Mark Mendell09ed1a32015-03-25 08:30:06 -04002519 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2520 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002521 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002522
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002523 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002524 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002525 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002526}
2527
2528void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002529 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2530 if (intrinsic.TryDispatch(invoke)) {
2531 return;
2532 }
2533
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002534 HandleInvoke(invoke);
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002535
2536 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002537 // Add one temporary for inline cache update.
2538 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2539 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002540}
2541
2542void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002543 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002544 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002545}
2546
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002547void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002548 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2549 return;
2550 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002551
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002552 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002553 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002554}
2555
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002556void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002557 // This call to HandleInvoke allocates a temporary (core) register
2558 // which is also used to transfer the hidden argument from FP to
2559 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002560 HandleInvoke(invoke);
2561 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002562 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002563
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002564 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002565 // Add one temporary for inline cache update.
2566 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2567 }
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01002568
2569 // For PC-relative load kinds the invoke has an extra input, the PC-relative address base.
2570 if (IsPcRelativeMethodLoadKind(invoke->GetHiddenArgumentLoadKind())) {
2571 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2572 }
2573
2574 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
2575 invoke->GetLocations()->SetInAt(invoke->GetNumberOfArguments() - 1,
2576 Location::RequiresRegister());
2577 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002578}
2579
2580void CodeGeneratorX86::MaybeGenerateInlineCacheCheck(HInstruction* instruction, Register klass) {
2581 DCHECK_EQ(EAX, klass);
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002582 // We know the destination of an intrinsic, so no need to record inline
2583 // caches (also the intrinsic location builder doesn't request an additional
2584 // temporary).
2585 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002586 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002587 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002588 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
Nicolas Geoffray095dc462020-08-17 16:40:28 +01002589 ScopedProfilingInfoUse spiu(
2590 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
2591 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002592 if (info != nullptr) {
2593 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
2594 uint32_t address = reinterpret_cast32<uint32_t>(cache);
2595 if (kIsDebugBuild) {
2596 uint32_t temp_index = instruction->GetLocations()->GetTempCount() - 1u;
2597 CHECK_EQ(EBP, instruction->GetLocations()->GetTemp(temp_index).AsRegister<Register>());
2598 }
2599 Register temp = EBP;
2600 NearLabel done;
2601 __ movl(temp, Immediate(address));
2602 // Fast path for a monomorphic cache.
2603 __ cmpl(klass, Address(temp, InlineCache::ClassesOffset().Int32Value()));
2604 __ j(kEqual, &done);
2605 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(kQuickUpdateInlineCache).Int32Value());
2606 __ Bind(&done);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002607 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002608 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002609}
2610
2611void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2612 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002613 LocationSummary* locations = invoke->GetLocations();
2614 Register temp = locations->GetTemp(0).AsRegister<Register>();
2615 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002616 Location receiver = locations->InAt(0);
2617 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2618
Roland Levillain0d5a2812015-11-13 10:07:31 +00002619 // Set the hidden argument. This is safe to do this here, as XMM7
2620 // won't be modified thereafter, before the `call` instruction.
2621 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01002622 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRecursive) {
2623 __ movd(hidden_reg, locations->InAt(invoke->GetNumberOfArguments() - 1).AsRegister<Register>());
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00002624 } else if (invoke->GetHiddenArgumentLoadKind() != MethodLoadKind::kRuntimeCall) {
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01002625 codegen_->LoadMethod(invoke->GetHiddenArgumentLoadKind(), locations->GetTemp(0), invoke);
2626 __ movd(hidden_reg, temp);
2627 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002628
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002629 if (receiver.IsStackSlot()) {
2630 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002631 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002632 __ movl(temp, Address(temp, class_offset));
2633 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002634 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002635 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002636 }
Roland Levillain4d027112015-07-01 15:41:14 +01002637 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002638 // Instead of simply (possibly) unpoisoning `temp` here, we should
2639 // emit a read barrier for the previous class reference load.
2640 // However this is not required in practice, as this is an
2641 // intermediate/temporary reference and because the current
2642 // concurrent copying collector keeps the from-space memory
2643 // intact/accessible until the end of the marking phase (the
2644 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002645 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002646
2647 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
2648
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002649 // temp = temp->GetAddressOfIMT()
2650 __ movl(temp,
2651 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002652 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002653 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002654 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002655 __ movl(temp, Address(temp, method_offset));
Nicolas Geoffrayd6bd1072020-11-30 18:42:01 +00002656 if (invoke->GetHiddenArgumentLoadKind() == MethodLoadKind::kRuntimeCall) {
2657 // We pass the method from the IMT in case of a conflict. This will ensure
2658 // we go into the runtime to resolve the actual method.
2659 __ movd(hidden_reg, temp);
2660 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002661 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002662 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002663 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002664
2665 DCHECK(!codegen_->IsLeafMethod());
2666 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2667}
2668
Orion Hodsonac141392017-01-13 11:53:47 +00002669void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00002670 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2671 if (intrinsic.TryDispatch(invoke)) {
2672 return;
2673 }
Orion Hodsonac141392017-01-13 11:53:47 +00002674 HandleInvoke(invoke);
2675}
2676
2677void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00002678 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2679 return;
2680 }
Orion Hodsonac141392017-01-13 11:53:47 +00002681 codegen_->GenerateInvokePolymorphicCall(invoke);
2682}
2683
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002684void LocationsBuilderX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2685 HandleInvoke(invoke);
2686}
2687
2688void InstructionCodeGeneratorX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2689 codegen_->GenerateInvokeCustomCall(invoke);
2690}
2691
Roland Levillain88cb1752014-10-20 16:36:47 +01002692void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2693 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002694 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002695 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002696 case DataType::Type::kInt32:
2697 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002698 locations->SetInAt(0, Location::RequiresRegister());
2699 locations->SetOut(Location::SameAsFirstInput());
2700 break;
2701
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002702 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002703 locations->SetInAt(0, Location::RequiresFpuRegister());
2704 locations->SetOut(Location::SameAsFirstInput());
2705 locations->AddTemp(Location::RequiresRegister());
2706 locations->AddTemp(Location::RequiresFpuRegister());
2707 break;
2708
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002709 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002710 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002711 locations->SetOut(Location::SameAsFirstInput());
2712 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002713 break;
2714
2715 default:
2716 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2717 }
2718}
2719
2720void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2721 LocationSummary* locations = neg->GetLocations();
2722 Location out = locations->Out();
2723 Location in = locations->InAt(0);
2724 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002725 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002726 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002727 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002728 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002729 break;
2730
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002731 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002732 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002733 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002734 __ negl(out.AsRegisterPairLow<Register>());
2735 // Negation is similar to subtraction from zero. The least
2736 // significant byte triggers a borrow when it is different from
2737 // zero; to take it into account, add 1 to the most significant
2738 // byte if the carry flag (CF) is set to 1 after the first NEGL
2739 // operation.
2740 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2741 __ negl(out.AsRegisterPairHigh<Register>());
2742 break;
2743
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002744 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002745 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002746 Register constant = locations->GetTemp(0).AsRegister<Register>();
2747 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002748 // Implement float negation with an exclusive or with value
2749 // 0x80000000 (mask for bit 31, representing the sign of a
2750 // single-precision floating-point number).
2751 __ movl(constant, Immediate(INT32_C(0x80000000)));
2752 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002753 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002754 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002755 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002756
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002757 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002758 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002759 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002760 // Implement double negation with an exclusive or with value
2761 // 0x8000000000000000 (mask for bit 63, representing the sign of
2762 // a double-precision floating-point number).
2763 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002764 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002765 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002766 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002767
2768 default:
2769 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2770 }
2771}
2772
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002773void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2774 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002775 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002776 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002777 locations->SetInAt(0, Location::RequiresFpuRegister());
2778 locations->SetInAt(1, Location::RequiresRegister());
2779 locations->SetOut(Location::SameAsFirstInput());
2780 locations->AddTemp(Location::RequiresFpuRegister());
2781}
2782
2783void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2784 LocationSummary* locations = neg->GetLocations();
2785 Location out = locations->Out();
2786 DCHECK(locations->InAt(0).Equals(out));
2787
2788 Register constant_area = locations->InAt(1).AsRegister<Register>();
2789 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002790 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002791 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2792 neg->GetBaseMethodAddress(),
2793 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002794 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2795 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002796 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2797 neg->GetBaseMethodAddress(),
2798 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002799 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2800 }
2801}
2802
Roland Levillaindff1f282014-11-05 14:15:05 +00002803void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002804 DataType::Type result_type = conversion->GetResultType();
2805 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002806 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2807 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002808
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002809 // The float-to-long and double-to-long type conversions rely on a
2810 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002811 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002812 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2813 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002814 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002815 : LocationSummary::kNoCall;
2816 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002817 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002818
Roland Levillaindff1f282014-11-05 14:15:05 +00002819 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002820 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002821 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002822 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002823 case DataType::Type::kUint8:
2824 case DataType::Type::kInt8:
2825 case DataType::Type::kUint16:
2826 case DataType::Type::kInt16:
2827 case DataType::Type::kInt32:
2828 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2829 // Make the output overlap to please the register allocator. This greatly simplifies
2830 // the validation of the linear scan implementation
2831 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2832 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002833 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002834 HInstruction* input = conversion->InputAt(0);
2835 Location input_location = input->IsConstant()
2836 ? Location::ConstantLocation(input->AsConstant())
2837 : Location::RegisterPairLocation(EAX, EDX);
2838 locations->SetInAt(0, input_location);
2839 // Make the output overlap to please the register allocator. This greatly simplifies
2840 // the validation of the linear scan implementation
2841 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2842 break;
2843 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002844
2845 default:
2846 LOG(FATAL) << "Unexpected type conversion from " << input_type
2847 << " to " << result_type;
2848 }
2849 break;
2850
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002851 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002852 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002853 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2854 locations->SetInAt(0, Location::Any());
2855 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002856 break;
2857
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002858 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002859 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002860 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002861 locations->SetInAt(0, Location::Any());
2862 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2863 break;
2864
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002865 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002866 locations->SetInAt(0, Location::RequiresFpuRegister());
2867 locations->SetOut(Location::RequiresRegister());
2868 locations->AddTemp(Location::RequiresFpuRegister());
2869 break;
2870
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002871 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002872 locations->SetInAt(0, Location::RequiresFpuRegister());
2873 locations->SetOut(Location::RequiresRegister());
2874 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002875 break;
2876
2877 default:
2878 LOG(FATAL) << "Unexpected type conversion from " << input_type
2879 << " to " << result_type;
2880 }
2881 break;
2882
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002883 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002884 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002885 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002886 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002887 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002888 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002889 case DataType::Type::kInt16:
2890 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002891 locations->SetInAt(0, Location::RegisterLocation(EAX));
2892 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2893 break;
2894
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002895 case DataType::Type::kFloat32:
2896 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002897 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002898 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2899 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2900
Vladimir Marko949c91f2015-01-27 10:48:44 +00002901 // The runtime helper puts the result in EAX, EDX.
2902 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002903 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002904 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002905
2906 default:
2907 LOG(FATAL) << "Unexpected type conversion from " << input_type
2908 << " to " << result_type;
2909 }
2910 break;
2911
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002912 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002913 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002914 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002915 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002916 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002917 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002918 case DataType::Type::kInt16:
2919 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002920 locations->SetInAt(0, Location::RequiresRegister());
2921 locations->SetOut(Location::RequiresFpuRegister());
2922 break;
2923
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002924 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002925 locations->SetInAt(0, Location::Any());
2926 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002927 break;
2928
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002929 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002930 locations->SetInAt(0, Location::RequiresFpuRegister());
2931 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002932 break;
2933
2934 default:
2935 LOG(FATAL) << "Unexpected type conversion from " << input_type
2936 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002937 }
Roland Levillaincff13742014-11-17 14:32:17 +00002938 break;
2939
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002940 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002941 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002942 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002943 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002944 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002945 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002946 case DataType::Type::kInt16:
2947 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002948 locations->SetInAt(0, Location::RequiresRegister());
2949 locations->SetOut(Location::RequiresFpuRegister());
2950 break;
2951
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002952 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002953 locations->SetInAt(0, Location::Any());
2954 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002955 break;
2956
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002957 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002958 locations->SetInAt(0, Location::RequiresFpuRegister());
2959 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002960 break;
2961
2962 default:
2963 LOG(FATAL) << "Unexpected type conversion from " << input_type
2964 << " to " << result_type;
2965 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002966 break;
2967
2968 default:
2969 LOG(FATAL) << "Unexpected type conversion from " << input_type
2970 << " to " << result_type;
2971 }
2972}
2973
2974void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2975 LocationSummary* locations = conversion->GetLocations();
2976 Location out = locations->Out();
2977 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002978 DataType::Type result_type = conversion->GetResultType();
2979 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002980 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2981 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002982 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002983 case DataType::Type::kUint8:
2984 switch (input_type) {
2985 case DataType::Type::kInt8:
2986 case DataType::Type::kUint16:
2987 case DataType::Type::kInt16:
2988 case DataType::Type::kInt32:
2989 if (in.IsRegister()) {
2990 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2991 } else {
2992 DCHECK(in.GetConstant()->IsIntConstant());
2993 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2994 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2995 }
2996 break;
2997 case DataType::Type::kInt64:
2998 if (in.IsRegisterPair()) {
2999 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
3000 } else {
3001 DCHECK(in.GetConstant()->IsLongConstant());
3002 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3003 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
3004 }
3005 break;
3006
3007 default:
3008 LOG(FATAL) << "Unexpected type conversion from " << input_type
3009 << " to " << result_type;
3010 }
3011 break;
3012
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003013 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00003014 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003015 case DataType::Type::kUint8:
3016 case DataType::Type::kUint16:
3017 case DataType::Type::kInt16:
3018 case DataType::Type::kInt32:
3019 if (in.IsRegister()) {
3020 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
3021 } else {
3022 DCHECK(in.GetConstant()->IsIntConstant());
3023 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
3024 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
3025 }
3026 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003027 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00003028 if (in.IsRegisterPair()) {
3029 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
3030 } else {
3031 DCHECK(in.GetConstant()->IsLongConstant());
3032 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3033 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
3034 }
3035 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003036
3037 default:
3038 LOG(FATAL) << "Unexpected type conversion from " << input_type
3039 << " to " << result_type;
3040 }
3041 break;
3042
3043 case DataType::Type::kUint16:
3044 switch (input_type) {
3045 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003046 case DataType::Type::kInt16:
3047 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003048 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003049 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
3050 } else if (in.IsStackSlot()) {
3051 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003052 } else {
3053 DCHECK(in.GetConstant()->IsIntConstant());
3054 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003055 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
3056 }
3057 break;
3058 case DataType::Type::kInt64:
3059 if (in.IsRegisterPair()) {
3060 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
3061 } else if (in.IsDoubleStackSlot()) {
3062 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
3063 } else {
3064 DCHECK(in.GetConstant()->IsLongConstant());
3065 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3066 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003067 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00003068 break;
3069
3070 default:
3071 LOG(FATAL) << "Unexpected type conversion from " << input_type
3072 << " to " << result_type;
3073 }
3074 break;
3075
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003076 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00003077 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003078 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003079 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00003080 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003081 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00003082 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003083 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00003084 } else {
3085 DCHECK(in.GetConstant()->IsIntConstant());
3086 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003087 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00003088 }
3089 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003090 case DataType::Type::kInt64:
3091 if (in.IsRegisterPair()) {
3092 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
3093 } else if (in.IsDoubleStackSlot()) {
3094 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
3095 } else {
3096 DCHECK(in.GetConstant()->IsLongConstant());
3097 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3098 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
3099 }
3100 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00003101
3102 default:
3103 LOG(FATAL) << "Unexpected type conversion from " << input_type
3104 << " to " << result_type;
3105 }
3106 break;
3107
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003108 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00003109 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003110 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00003111 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003112 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00003113 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003114 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00003115 } else {
3116 DCHECK(in.IsConstant());
3117 DCHECK(in.GetConstant()->IsLongConstant());
3118 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003119 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00003120 }
3121 break;
3122
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003123 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00003124 XmmRegister input = in.AsFpuRegister<XmmRegister>();
3125 Register output = out.AsRegister<Register>();
3126 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04003127 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00003128
3129 __ movl(output, Immediate(kPrimIntMax));
3130 // temp = int-to-float(output)
3131 __ cvtsi2ss(temp, output);
3132 // if input >= temp goto done
3133 __ comiss(input, temp);
3134 __ j(kAboveEqual, &done);
3135 // if input == NaN goto nan
3136 __ j(kUnordered, &nan);
3137 // output = float-to-int-truncate(input)
3138 __ cvttss2si(output, input);
3139 __ jmp(&done);
3140 __ Bind(&nan);
3141 // output = 0
3142 __ xorl(output, output);
3143 __ Bind(&done);
3144 break;
3145 }
3146
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003147 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003148 XmmRegister input = in.AsFpuRegister<XmmRegister>();
3149 Register output = out.AsRegister<Register>();
3150 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04003151 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003152
3153 __ movl(output, Immediate(kPrimIntMax));
3154 // temp = int-to-double(output)
3155 __ cvtsi2sd(temp, output);
3156 // if input >= temp goto done
3157 __ comisd(input, temp);
3158 __ j(kAboveEqual, &done);
3159 // if input == NaN goto nan
3160 __ j(kUnordered, &nan);
3161 // output = double-to-int-truncate(input)
3162 __ cvttsd2si(output, input);
3163 __ jmp(&done);
3164 __ Bind(&nan);
3165 // output = 0
3166 __ xorl(output, output);
3167 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00003168 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003169 }
Roland Levillain946e1432014-11-11 17:35:19 +00003170
3171 default:
3172 LOG(FATAL) << "Unexpected type conversion from " << input_type
3173 << " to " << result_type;
3174 }
3175 break;
3176
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003177 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00003178 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003179 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003180 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003181 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003182 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003183 case DataType::Type::kInt16:
3184 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00003185 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
3186 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003187 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00003188 __ cdq();
3189 break;
3190
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003191 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01003192 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003193 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00003194 break;
3195
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003196 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01003197 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003198 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00003199 break;
3200
3201 default:
3202 LOG(FATAL) << "Unexpected type conversion from " << input_type
3203 << " to " << result_type;
3204 }
3205 break;
3206
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003207 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00003208 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003209 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003210 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003211 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003212 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003213 case DataType::Type::kInt16:
3214 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003215 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003216 break;
3217
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003218 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003219 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00003220
Roland Levillain232ade02015-04-20 15:14:36 +01003221 // Create stack space for the call to
3222 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
3223 // TODO: enhance register allocator to ask for stack temporaries.
3224 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003225 adjustment = DataType::Size(DataType::Type::kInt64);
Vladimir Markodec78172020-06-19 15:31:23 +01003226 codegen_->IncreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003227 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003228
Roland Levillain232ade02015-04-20 15:14:36 +01003229 // Load the value to the FP stack, using temporaries if needed.
3230 PushOntoFPStack(in, 0, adjustment, false, true);
3231
3232 if (out.IsStackSlot()) {
3233 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
3234 } else {
3235 __ fstps(Address(ESP, 0));
3236 Location stack_temp = Location::StackSlot(0);
3237 codegen_->Move32(out, stack_temp);
3238 }
3239
3240 // Remove the temporary stack space we allocated.
3241 if (adjustment != 0) {
Vladimir Markodec78172020-06-19 15:31:23 +01003242 codegen_->DecreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003243 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003244 break;
3245 }
3246
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003247 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003248 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003249 break;
3250
3251 default:
3252 LOG(FATAL) << "Unexpected type conversion from " << input_type
3253 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003254 }
Roland Levillaincff13742014-11-17 14:32:17 +00003255 break;
3256
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003257 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00003258 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003259 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003260 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003261 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003262 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003263 case DataType::Type::kInt16:
3264 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003265 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003266 break;
3267
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003268 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003269 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00003270
Roland Levillain232ade02015-04-20 15:14:36 +01003271 // Create stack space for the call to
3272 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
3273 // TODO: enhance register allocator to ask for stack temporaries.
3274 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003275 adjustment = DataType::Size(DataType::Type::kInt64);
Vladimir Markodec78172020-06-19 15:31:23 +01003276 codegen_->IncreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003277 }
3278
3279 // Load the value to the FP stack, using temporaries if needed.
3280 PushOntoFPStack(in, 0, adjustment, false, true);
3281
3282 if (out.IsDoubleStackSlot()) {
3283 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
3284 } else {
3285 __ fstpl(Address(ESP, 0));
3286 Location stack_temp = Location::DoubleStackSlot(0);
3287 codegen_->Move64(out, stack_temp);
3288 }
3289
3290 // Remove the temporary stack space we allocated.
3291 if (adjustment != 0) {
Vladimir Markodec78172020-06-19 15:31:23 +01003292 codegen_->DecreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003293 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003294 break;
3295 }
3296
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003297 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003298 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003299 break;
3300
3301 default:
3302 LOG(FATAL) << "Unexpected type conversion from " << input_type
3303 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003304 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003305 break;
3306
3307 default:
3308 LOG(FATAL) << "Unexpected type conversion from " << input_type
3309 << " to " << result_type;
3310 }
3311}
3312
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003313void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003314 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003315 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003316 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003317 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05003318 locations->SetInAt(0, Location::RequiresRegister());
3319 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3320 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3321 break;
3322 }
3323
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003324 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003325 locations->SetInAt(0, Location::RequiresRegister());
3326 locations->SetInAt(1, Location::Any());
3327 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003328 break;
3329 }
3330
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003331 case DataType::Type::kFloat32:
3332 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003333 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003334 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3335 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003336 } else if (add->InputAt(1)->IsConstant()) {
3337 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003338 } else {
3339 locations->SetInAt(1, Location::Any());
3340 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003341 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003342 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003343 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003344
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003345 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003346 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Elliott Hughesc1896c92018-11-29 11:33:18 -08003347 UNREACHABLE();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003348 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003349}
3350
3351void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
3352 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003353 Location first = locations->InAt(0);
3354 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05003355 Location out = locations->Out();
3356
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003357 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003358 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003359 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003360 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3361 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003362 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3363 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05003364 } else {
3365 __ leal(out.AsRegister<Register>(), Address(
3366 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3367 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003368 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003369 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3370 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3371 __ addl(out.AsRegister<Register>(), Immediate(value));
3372 } else {
3373 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3374 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003375 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003376 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003377 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003378 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003379 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003380 }
3381
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003382 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003383 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003384 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3385 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003386 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003387 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3388 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003389 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003390 } else {
3391 DCHECK(second.IsConstant()) << second;
3392 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3393 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3394 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003395 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003396 break;
3397 }
3398
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003399 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003400 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003401 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003402 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3403 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003404 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003405 __ addss(first.AsFpuRegister<XmmRegister>(),
3406 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003407 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3408 const_area->GetBaseMethodAddress(),
3409 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003410 } else {
3411 DCHECK(second.IsStackSlot());
3412 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003413 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003414 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003415 }
3416
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003417 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003418 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003419 __ addsd(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 __ addsd(first.AsFpuRegister<XmmRegister>(),
3424 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003425 const_area->GetConstant()->AsDoubleConstant()->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.IsDoubleStackSlot());
3430 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003431 }
3432 break;
3433 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003434
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003435 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003436 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003437 }
3438}
3439
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003440void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003441 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003442 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003443 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003444 case DataType::Type::kInt32:
3445 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003446 locations->SetInAt(0, Location::RequiresRegister());
3447 locations->SetInAt(1, Location::Any());
3448 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003449 break;
3450 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003451 case DataType::Type::kFloat32:
3452 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003453 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003454 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3455 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003456 } else if (sub->InputAt(1)->IsConstant()) {
3457 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003458 } else {
3459 locations->SetInAt(1, Location::Any());
3460 }
Calin Juravle11351682014-10-23 15:38:15 +01003461 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003462 break;
Calin Juravle11351682014-10-23 15:38:15 +01003463 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003464
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003465 default:
Calin Juravle11351682014-10-23 15:38:15 +01003466 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003467 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003468}
3469
3470void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3471 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003472 Location first = locations->InAt(0);
3473 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003474 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003475 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003476 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003477 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003478 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003479 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003480 __ subl(first.AsRegister<Register>(),
3481 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003482 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003483 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003484 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003485 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003486 }
3487
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003488 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003489 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003490 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3491 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003492 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003493 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003494 __ sbbl(first.AsRegisterPairHigh<Register>(),
3495 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003496 } else {
3497 DCHECK(second.IsConstant()) << second;
3498 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3499 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3500 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003501 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003502 break;
3503 }
3504
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003505 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003506 if (second.IsFpuRegister()) {
3507 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3508 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3509 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003510 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003511 __ subss(first.AsFpuRegister<XmmRegister>(),
3512 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003513 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3514 const_area->GetBaseMethodAddress(),
3515 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003516 } else {
3517 DCHECK(second.IsStackSlot());
3518 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3519 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003520 break;
Calin Juravle11351682014-10-23 15:38:15 +01003521 }
3522
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003523 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003524 if (second.IsFpuRegister()) {
3525 __ subsd(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 __ subsd(first.AsFpuRegister<XmmRegister>(),
3530 codegen_->LiteralDoubleAddress(
3531 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003532 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003533 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3534 } else {
3535 DCHECK(second.IsDoubleStackSlot());
3536 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3537 }
Calin Juravle11351682014-10-23 15:38:15 +01003538 break;
3539 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003540
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003541 default:
Calin Juravle11351682014-10-23 15:38:15 +01003542 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003543 }
3544}
3545
Calin Juravle34bacdf2014-10-07 20:23:36 +01003546void LocationsBuilderX86::VisitMul(HMul* mul) {
3547 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003548 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003549 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003550 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003551 locations->SetInAt(0, Location::RequiresRegister());
3552 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003553 if (mul->InputAt(1)->IsIntConstant()) {
3554 // Can use 3 operand multiply.
3555 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3556 } else {
3557 locations->SetOut(Location::SameAsFirstInput());
3558 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003559 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003560 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003561 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003562 locations->SetInAt(1, Location::Any());
3563 locations->SetOut(Location::SameAsFirstInput());
3564 // Needed for imul on 32bits with 64bits output.
3565 locations->AddTemp(Location::RegisterLocation(EAX));
3566 locations->AddTemp(Location::RegisterLocation(EDX));
3567 break;
3568 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003569 case DataType::Type::kFloat32:
3570 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003571 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003572 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3573 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003574 } else if (mul->InputAt(1)->IsConstant()) {
3575 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003576 } else {
3577 locations->SetInAt(1, Location::Any());
3578 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003579 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003580 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003581 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003582
3583 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003584 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003585 }
3586}
3587
3588void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3589 LocationSummary* locations = mul->GetLocations();
3590 Location first = locations->InAt(0);
3591 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003592 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003593
3594 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003595 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003596 // The constant may have ended up in a register, so test explicitly to avoid
3597 // problems where the output may not be the same as the first operand.
3598 if (mul->InputAt(1)->IsIntConstant()) {
3599 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3600 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3601 } else if (second.IsRegister()) {
3602 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003603 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003604 } else {
3605 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003606 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003607 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003608 }
3609 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003610
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003611 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003612 Register in1_hi = first.AsRegisterPairHigh<Register>();
3613 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003614 Register eax = locations->GetTemp(0).AsRegister<Register>();
3615 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003616
3617 DCHECK_EQ(EAX, eax);
3618 DCHECK_EQ(EDX, edx);
3619
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003620 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003621 // output: in1
3622 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3623 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3624 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003625 if (second.IsConstant()) {
3626 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003627
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003628 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3629 int32_t low_value = Low32Bits(value);
3630 int32_t high_value = High32Bits(value);
3631 Immediate low(low_value);
3632 Immediate high(high_value);
3633
3634 __ movl(eax, high);
3635 // eax <- in1.lo * in2.hi
3636 __ imull(eax, in1_lo);
3637 // in1.hi <- in1.hi * in2.lo
3638 __ imull(in1_hi, low);
3639 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3640 __ addl(in1_hi, eax);
3641 // move in2_lo to eax to prepare for double precision
3642 __ movl(eax, low);
3643 // edx:eax <- in1.lo * in2.lo
3644 __ mull(in1_lo);
3645 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3646 __ addl(in1_hi, edx);
3647 // in1.lo <- (in1.lo * in2.lo)[31:0];
3648 __ movl(in1_lo, eax);
3649 } else if (second.IsRegisterPair()) {
3650 Register in2_hi = second.AsRegisterPairHigh<Register>();
3651 Register in2_lo = second.AsRegisterPairLow<Register>();
3652
3653 __ movl(eax, in2_hi);
3654 // eax <- in1.lo * in2.hi
3655 __ imull(eax, in1_lo);
3656 // in1.hi <- in1.hi * in2.lo
3657 __ imull(in1_hi, in2_lo);
3658 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3659 __ addl(in1_hi, eax);
3660 // move in1_lo to eax to prepare for double precision
3661 __ movl(eax, in1_lo);
3662 // edx:eax <- in1.lo * in2.lo
3663 __ mull(in2_lo);
3664 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3665 __ addl(in1_hi, edx);
3666 // in1.lo <- (in1.lo * in2.lo)[31:0];
3667 __ movl(in1_lo, eax);
3668 } else {
3669 DCHECK(second.IsDoubleStackSlot()) << second;
3670 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3671 Address in2_lo(ESP, second.GetStackIndex());
3672
3673 __ movl(eax, in2_hi);
3674 // eax <- in1.lo * in2.hi
3675 __ imull(eax, in1_lo);
3676 // in1.hi <- in1.hi * in2.lo
3677 __ imull(in1_hi, in2_lo);
3678 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3679 __ addl(in1_hi, eax);
3680 // move in1_lo to eax to prepare for double precision
3681 __ movl(eax, in1_lo);
3682 // edx:eax <- in1.lo * in2.lo
3683 __ mull(in2_lo);
3684 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3685 __ addl(in1_hi, edx);
3686 // in1.lo <- (in1.lo * in2.lo)[31:0];
3687 __ movl(in1_lo, eax);
3688 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003689
3690 break;
3691 }
3692
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003693 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003694 DCHECK(first.Equals(locations->Out()));
3695 if (second.IsFpuRegister()) {
3696 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3697 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3698 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003699 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003700 __ mulss(first.AsFpuRegister<XmmRegister>(),
3701 codegen_->LiteralFloatAddress(
3702 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003703 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003704 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3705 } else {
3706 DCHECK(second.IsStackSlot());
3707 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3708 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003709 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003710 }
3711
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003712 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003713 DCHECK(first.Equals(locations->Out()));
3714 if (second.IsFpuRegister()) {
3715 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3716 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3717 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003718 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003719 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3720 codegen_->LiteralDoubleAddress(
3721 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003722 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003723 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3724 } else {
3725 DCHECK(second.IsDoubleStackSlot());
3726 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3727 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003728 break;
3729 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003730
3731 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003732 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003733 }
3734}
3735
Roland Levillain232ade02015-04-20 15:14:36 +01003736void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3737 uint32_t temp_offset,
3738 uint32_t stack_adjustment,
3739 bool is_fp,
3740 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003741 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003742 DCHECK(!is_wide);
3743 if (is_fp) {
3744 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3745 } else {
3746 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3747 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003748 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003749 DCHECK(is_wide);
3750 if (is_fp) {
3751 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3752 } else {
3753 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3754 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003755 } else {
3756 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003757 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003758 Location stack_temp = Location::StackSlot(temp_offset);
3759 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003760 if (is_fp) {
3761 __ flds(Address(ESP, temp_offset));
3762 } else {
3763 __ filds(Address(ESP, temp_offset));
3764 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003765 } else {
3766 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3767 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003768 if (is_fp) {
3769 __ fldl(Address(ESP, temp_offset));
3770 } else {
3771 __ fildl(Address(ESP, temp_offset));
3772 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003773 }
3774 }
3775}
3776
3777void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003778 DataType::Type type = rem->GetResultType();
3779 bool is_float = type == DataType::Type::kFloat32;
3780 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003781 LocationSummary* locations = rem->GetLocations();
3782 Location first = locations->InAt(0);
3783 Location second = locations->InAt(1);
3784 Location out = locations->Out();
3785
3786 // Create stack space for 2 elements.
3787 // TODO: enhance register allocator to ask for stack temporaries.
Vladimir Markodec78172020-06-19 15:31:23 +01003788 codegen_->IncreaseFrame(2 * elem_size);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003789
3790 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003791 const bool is_wide = !is_float;
Andreas Gampe3db70682018-12-26 15:12:03 -08003792 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp= */ true, is_wide);
3793 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp= */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003794
3795 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003796 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003797 __ Bind(&retry);
3798 __ fprem();
3799
3800 // Move FP status to AX.
3801 __ fstsw();
3802
3803 // And see if the argument reduction is complete. This is signaled by the
3804 // C2 FPU flag bit set to 0.
3805 __ andl(EAX, Immediate(kC2ConditionMask));
3806 __ j(kNotEqual, &retry);
3807
3808 // We have settled on the final value. Retrieve it into an XMM register.
3809 // Store FP top of stack to real stack.
3810 if (is_float) {
3811 __ fsts(Address(ESP, 0));
3812 } else {
3813 __ fstl(Address(ESP, 0));
3814 }
3815
3816 // Pop the 2 items from the FP stack.
3817 __ fucompp();
3818
3819 // Load the value from the stack into an XMM register.
3820 DCHECK(out.IsFpuRegister()) << out;
3821 if (is_float) {
3822 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3823 } else {
3824 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3825 }
3826
3827 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01003828 codegen_->DecreaseFrame(2 * elem_size);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003829}
3830
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003831
3832void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3833 DCHECK(instruction->IsDiv() || instruction->IsRem());
3834
3835 LocationSummary* locations = instruction->GetLocations();
3836 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003837 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003838
3839 Register out_register = locations->Out().AsRegister<Register>();
3840 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003841 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003842
3843 DCHECK(imm == 1 || imm == -1);
3844
3845 if (instruction->IsRem()) {
3846 __ xorl(out_register, out_register);
3847 } else {
3848 __ movl(out_register, input_register);
3849 if (imm == -1) {
3850 __ negl(out_register);
3851 }
3852 }
3853}
3854
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303855void InstructionCodeGeneratorX86::RemByPowerOfTwo(HRem* instruction) {
3856 LocationSummary* locations = instruction->GetLocations();
3857 Location second = locations->InAt(1);
3858
3859 Register out = locations->Out().AsRegister<Register>();
3860 Register numerator = locations->InAt(0).AsRegister<Register>();
3861
3862 int32_t imm = Int64FromConstant(second.GetConstant());
3863 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3864 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3865
3866 Register tmp = locations->GetTemp(0).AsRegister<Register>();
3867 NearLabel done;
3868 __ movl(out, numerator);
3869 __ andl(out, Immediate(abs_imm-1));
3870 __ j(Condition::kZero, &done);
3871 __ leal(tmp, Address(out, static_cast<int32_t>(~(abs_imm-1))));
3872 __ testl(numerator, numerator);
3873 __ cmovl(Condition::kLess, out, tmp);
3874 __ Bind(&done);
3875}
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003876
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003877void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003878 LocationSummary* locations = instruction->GetLocations();
3879
3880 Register out_register = locations->Out().AsRegister<Register>();
3881 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003882 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003883 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3884 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003885
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003886 Register num = locations->GetTemp(0).AsRegister<Register>();
3887
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003888 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003889 __ testl(input_register, input_register);
3890 __ cmovl(kGreaterEqual, num, input_register);
3891 int shift = CTZ(imm);
3892 __ sarl(num, Immediate(shift));
3893
3894 if (imm < 0) {
3895 __ negl(num);
3896 }
3897
3898 __ movl(out_register, num);
3899}
3900
3901void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3902 DCHECK(instruction->IsDiv() || instruction->IsRem());
3903
3904 LocationSummary* locations = instruction->GetLocations();
3905 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3906
3907 Register eax = locations->InAt(0).AsRegister<Register>();
3908 Register out = locations->Out().AsRegister<Register>();
3909 Register num;
3910 Register edx;
3911
3912 if (instruction->IsDiv()) {
3913 edx = locations->GetTemp(0).AsRegister<Register>();
3914 num = locations->GetTemp(1).AsRegister<Register>();
3915 } else {
3916 edx = locations->Out().AsRegister<Register>();
3917 num = locations->GetTemp(0).AsRegister<Register>();
3918 }
3919
3920 DCHECK_EQ(EAX, eax);
3921 DCHECK_EQ(EDX, edx);
3922 if (instruction->IsDiv()) {
3923 DCHECK_EQ(EAX, out);
3924 } else {
3925 DCHECK_EQ(EDX, out);
3926 }
3927
3928 int64_t magic;
3929 int shift;
Andreas Gampe3db70682018-12-26 15:12:03 -08003930 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003931
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003932 // Save the numerator.
3933 __ movl(num, eax);
3934
3935 // EAX = magic
3936 __ movl(eax, Immediate(magic));
3937
3938 // EDX:EAX = magic * numerator
3939 __ imull(num);
3940
3941 if (imm > 0 && magic < 0) {
3942 // EDX += num
3943 __ addl(edx, num);
3944 } else if (imm < 0 && magic > 0) {
3945 __ subl(edx, num);
3946 }
3947
3948 // Shift if needed.
3949 if (shift != 0) {
3950 __ sarl(edx, Immediate(shift));
3951 }
3952
3953 // EDX += 1 if EDX < 0
3954 __ movl(eax, edx);
3955 __ shrl(edx, Immediate(31));
3956 __ addl(edx, eax);
3957
3958 if (instruction->IsRem()) {
3959 __ movl(eax, num);
3960 __ imull(edx, Immediate(imm));
3961 __ subl(eax, edx);
3962 __ movl(edx, eax);
3963 } else {
3964 __ movl(eax, edx);
3965 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003966}
3967
Calin Juravlebacfec32014-11-14 15:54:36 +00003968void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3969 DCHECK(instruction->IsDiv() || instruction->IsRem());
3970
3971 LocationSummary* locations = instruction->GetLocations();
3972 Location out = locations->Out();
3973 Location first = locations->InAt(0);
3974 Location second = locations->InAt(1);
3975 bool is_div = instruction->IsDiv();
3976
3977 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003978 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003979 DCHECK_EQ(EAX, first.AsRegister<Register>());
3980 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003981
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003982 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003983 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003984
3985 if (imm == 0) {
3986 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3987 } else if (imm == 1 || imm == -1) {
3988 DivRemOneOrMinusOne(instruction);
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303989 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
3990 if (is_div) {
3991 DivByPowerOfTwo(instruction->AsDiv());
3992 } else {
3993 RemByPowerOfTwo(instruction->AsRem());
3994 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003995 } else {
3996 DCHECK(imm <= -2 || imm >= 2);
3997 GenerateDivRemWithAnyConstant(instruction);
3998 }
3999 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004000 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00004001 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004002 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00004003
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004004 Register second_reg = second.AsRegister<Register>();
4005 // 0x80000000/-1 triggers an arithmetic exception!
4006 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
4007 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00004008
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004009 __ cmpl(second_reg, Immediate(-1));
4010 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00004011
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004012 // edx:eax <- sign-extended of eax
4013 __ cdq();
4014 // eax = quotient, edx = remainder
4015 __ idivl(second_reg);
4016 __ Bind(slow_path->GetExitLabel());
4017 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004018 break;
4019 }
4020
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004021 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004022 InvokeRuntimeCallingConvention calling_convention;
4023 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
4024 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
4025 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
4026 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
4027 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
4028 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
4029
4030 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004031 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004032 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004033 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004034 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004035 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004036 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004037 break;
4038 }
4039
4040 default:
4041 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
4042 }
4043}
4044
Calin Juravle7c4954d2014-10-28 16:57:40 +00004045void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004046 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004047 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004048 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01004049 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004050
Calin Juravle7c4954d2014-10-28 16:57:40 +00004051 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004052 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00004053 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004054 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00004055 locations->SetOut(Location::SameAsFirstInput());
4056 // Intel uses edx:eax as the dividend.
4057 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004058 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
4059 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
4060 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01004061 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004062 locations->AddTemp(Location::RequiresRegister());
4063 }
Calin Juravled0d48522014-11-04 16:40:20 +00004064 break;
4065 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004066 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004067 InvokeRuntimeCallingConvention calling_convention;
4068 locations->SetInAt(0, Location::RegisterPairLocation(
4069 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4070 locations->SetInAt(1, Location::RegisterPairLocation(
4071 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4072 // Runtime helper puts the result in EAX, EDX.
4073 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00004074 break;
4075 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004076 case DataType::Type::kFloat32:
4077 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004078 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00004079 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
4080 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00004081 } else if (div->InputAt(1)->IsConstant()) {
4082 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00004083 } else {
4084 locations->SetInAt(1, Location::Any());
4085 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004086 locations->SetOut(Location::SameAsFirstInput());
4087 break;
4088 }
4089
4090 default:
4091 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4092 }
4093}
4094
4095void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
4096 LocationSummary* locations = div->GetLocations();
4097 Location first = locations->InAt(0);
4098 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004099
4100 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004101 case DataType::Type::kInt32:
4102 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004103 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004104 break;
4105 }
4106
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004107 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04004108 if (second.IsFpuRegister()) {
4109 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
4110 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
4111 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00004112 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04004113 __ divss(first.AsFpuRegister<XmmRegister>(),
4114 codegen_->LiteralFloatAddress(
4115 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004116 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04004117 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
4118 } else {
4119 DCHECK(second.IsStackSlot());
4120 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
4121 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004122 break;
4123 }
4124
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004125 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04004126 if (second.IsFpuRegister()) {
4127 __ divsd(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 __ divsd(first.AsFpuRegister<XmmRegister>(),
4132 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004133 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
4134 const_area->GetBaseMethodAddress(),
4135 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04004136 } else {
4137 DCHECK(second.IsDoubleStackSlot());
4138 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
4139 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004140 break;
4141 }
4142
4143 default:
4144 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4145 }
4146}
4147
Calin Juravlebacfec32014-11-14 15:54:36 +00004148void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004149 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004150
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004151 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004152 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004153 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01004154 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00004155
Calin Juravled2ec87d2014-12-08 14:24:46 +00004156 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004157 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004158 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004159 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00004160 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004161 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
4162 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
4163 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01004164 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004165 locations->AddTemp(Location::RequiresRegister());
4166 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004167 break;
4168 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004169 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004170 InvokeRuntimeCallingConvention calling_convention;
4171 locations->SetInAt(0, Location::RegisterPairLocation(
4172 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4173 locations->SetInAt(1, Location::RegisterPairLocation(
4174 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4175 // Runtime helper puts the result in EAX, EDX.
4176 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
4177 break;
4178 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004179 case DataType::Type::kFloat64:
4180 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05004181 locations->SetInAt(0, Location::Any());
4182 locations->SetInAt(1, Location::Any());
4183 locations->SetOut(Location::RequiresFpuRegister());
4184 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00004185 break;
4186 }
4187
4188 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004189 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004190 }
4191}
4192
4193void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004194 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00004195 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004196 case DataType::Type::kInt32:
4197 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004198 GenerateDivRemIntegral(rem);
4199 break;
4200 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004201 case DataType::Type::kFloat32:
4202 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05004203 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00004204 break;
4205 }
4206 default:
4207 LOG(FATAL) << "Unexpected rem type " << type;
4208 }
4209}
4210
Aart Bik1f8d51b2018-02-15 10:42:37 -08004211static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
4212 LocationSummary* locations = new (allocator) LocationSummary(minmax);
4213 switch (minmax->GetResultType()) {
4214 case DataType::Type::kInt32:
4215 locations->SetInAt(0, Location::RequiresRegister());
4216 locations->SetInAt(1, Location::RequiresRegister());
4217 locations->SetOut(Location::SameAsFirstInput());
4218 break;
4219 case DataType::Type::kInt64:
4220 locations->SetInAt(0, Location::RequiresRegister());
4221 locations->SetInAt(1, Location::RequiresRegister());
4222 locations->SetOut(Location::SameAsFirstInput());
4223 // Register to use to perform a long subtract to set cc.
4224 locations->AddTemp(Location::RequiresRegister());
4225 break;
4226 case DataType::Type::kFloat32:
4227 locations->SetInAt(0, Location::RequiresFpuRegister());
4228 locations->SetInAt(1, Location::RequiresFpuRegister());
4229 locations->SetOut(Location::SameAsFirstInput());
4230 locations->AddTemp(Location::RequiresRegister());
4231 break;
4232 case DataType::Type::kFloat64:
4233 locations->SetInAt(0, Location::RequiresFpuRegister());
4234 locations->SetInAt(1, Location::RequiresFpuRegister());
4235 locations->SetOut(Location::SameAsFirstInput());
4236 break;
4237 default:
4238 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
4239 }
4240}
4241
Aart Bik351df3e2018-03-07 11:54:57 -08004242void InstructionCodeGeneratorX86::GenerateMinMaxInt(LocationSummary* locations,
4243 bool is_min,
4244 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08004245 Location op1_loc = locations->InAt(0);
4246 Location op2_loc = locations->InAt(1);
4247
4248 // Shortcut for same input locations.
4249 if (op1_loc.Equals(op2_loc)) {
4250 // Can return immediately, as op1_loc == out_loc.
4251 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
4252 // a copy here.
4253 DCHECK(locations->Out().Equals(op1_loc));
4254 return;
4255 }
4256
4257 if (type == DataType::Type::kInt64) {
4258 // Need to perform a subtract to get the sign right.
4259 // op1 is already in the same location as the output.
4260 Location output = locations->Out();
4261 Register output_lo = output.AsRegisterPairLow<Register>();
4262 Register output_hi = output.AsRegisterPairHigh<Register>();
4263
4264 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
4265 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
4266
4267 // The comparison is performed by subtracting the second operand from
4268 // the first operand and then setting the status flags in the same
4269 // manner as the SUB instruction."
4270 __ cmpl(output_lo, op2_lo);
4271
4272 // Now use a temp and the borrow to finish the subtraction of op2_hi.
4273 Register temp = locations->GetTemp(0).AsRegister<Register>();
4274 __ movl(temp, output_hi);
4275 __ sbbl(temp, op2_hi);
4276
4277 // Now the condition code is correct.
4278 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
4279 __ cmovl(cond, output_lo, op2_lo);
4280 __ cmovl(cond, output_hi, op2_hi);
4281 } else {
4282 DCHECK_EQ(type, DataType::Type::kInt32);
4283 Register out = locations->Out().AsRegister<Register>();
4284 Register op2 = op2_loc.AsRegister<Register>();
4285
4286 // (out := op1)
4287 // out <=? op2
4288 // if out is min jmp done
4289 // out := op2
4290 // done:
4291
4292 __ cmpl(out, op2);
4293 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
4294 __ cmovl(cond, out, op2);
4295 }
4296}
4297
4298void InstructionCodeGeneratorX86::GenerateMinMaxFP(LocationSummary* locations,
4299 bool is_min,
4300 DataType::Type type) {
4301 Location op1_loc = locations->InAt(0);
4302 Location op2_loc = locations->InAt(1);
4303 Location out_loc = locations->Out();
4304 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
4305
4306 // Shortcut for same input locations.
4307 if (op1_loc.Equals(op2_loc)) {
4308 DCHECK(out_loc.Equals(op1_loc));
4309 return;
4310 }
4311
4312 // (out := op1)
4313 // out <=? op2
4314 // if Nan jmp Nan_label
4315 // if out is min jmp done
4316 // if op2 is min jmp op2_label
4317 // handle -0/+0
4318 // jmp done
4319 // Nan_label:
4320 // out := NaN
4321 // op2_label:
4322 // out := op2
4323 // done:
4324 //
4325 // This removes one jmp, but needs to copy one input (op1) to out.
4326 //
4327 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
4328
4329 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
4330
4331 NearLabel nan, done, op2_label;
4332 if (type == DataType::Type::kFloat64) {
4333 __ ucomisd(out, op2);
4334 } else {
4335 DCHECK_EQ(type, DataType::Type::kFloat32);
4336 __ ucomiss(out, op2);
4337 }
4338
4339 __ j(Condition::kParityEven, &nan);
4340
4341 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
4342 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
4343
4344 // Handle 0.0/-0.0.
4345 if (is_min) {
4346 if (type == DataType::Type::kFloat64) {
4347 __ orpd(out, op2);
4348 } else {
4349 __ orps(out, op2);
4350 }
4351 } else {
4352 if (type == DataType::Type::kFloat64) {
4353 __ andpd(out, op2);
4354 } else {
4355 __ andps(out, op2);
4356 }
4357 }
4358 __ jmp(&done);
4359
4360 // NaN handling.
4361 __ Bind(&nan);
4362 if (type == DataType::Type::kFloat64) {
4363 // TODO: Use a constant from the constant table (requires extra input).
4364 __ LoadLongConstant(out, kDoubleNaN);
4365 } else {
4366 Register constant = locations->GetTemp(0).AsRegister<Register>();
4367 __ movl(constant, Immediate(kFloatNaN));
4368 __ movd(out, constant);
4369 }
4370 __ jmp(&done);
4371
4372 // out := op2;
4373 __ Bind(&op2_label);
4374 if (type == DataType::Type::kFloat64) {
4375 __ movsd(out, op2);
4376 } else {
4377 __ movss(out, op2);
4378 }
4379
4380 // Done.
4381 __ Bind(&done);
4382}
4383
Aart Bik351df3e2018-03-07 11:54:57 -08004384void InstructionCodeGeneratorX86::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4385 DataType::Type type = minmax->GetResultType();
4386 switch (type) {
4387 case DataType::Type::kInt32:
4388 case DataType::Type::kInt64:
4389 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
4390 break;
4391 case DataType::Type::kFloat32:
4392 case DataType::Type::kFloat64:
4393 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
4394 break;
4395 default:
4396 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4397 }
4398}
4399
Aart Bik1f8d51b2018-02-15 10:42:37 -08004400void LocationsBuilderX86::VisitMin(HMin* min) {
4401 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4402}
4403
4404void InstructionCodeGeneratorX86::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004405 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004406}
4407
4408void LocationsBuilderX86::VisitMax(HMax* max) {
4409 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4410}
4411
4412void InstructionCodeGeneratorX86::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004413 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004414}
4415
Aart Bik3dad3412018-02-28 12:01:46 -08004416void LocationsBuilderX86::VisitAbs(HAbs* abs) {
4417 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4418 switch (abs->GetResultType()) {
4419 case DataType::Type::kInt32:
4420 locations->SetInAt(0, Location::RegisterLocation(EAX));
4421 locations->SetOut(Location::SameAsFirstInput());
4422 locations->AddTemp(Location::RegisterLocation(EDX));
4423 break;
4424 case DataType::Type::kInt64:
4425 locations->SetInAt(0, Location::RequiresRegister());
4426 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4427 locations->AddTemp(Location::RequiresRegister());
4428 break;
4429 case DataType::Type::kFloat32:
4430 locations->SetInAt(0, Location::RequiresFpuRegister());
4431 locations->SetOut(Location::SameAsFirstInput());
4432 locations->AddTemp(Location::RequiresFpuRegister());
4433 locations->AddTemp(Location::RequiresRegister());
4434 break;
4435 case DataType::Type::kFloat64:
4436 locations->SetInAt(0, Location::RequiresFpuRegister());
4437 locations->SetOut(Location::SameAsFirstInput());
4438 locations->AddTemp(Location::RequiresFpuRegister());
4439 break;
4440 default:
4441 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4442 }
4443}
4444
4445void InstructionCodeGeneratorX86::VisitAbs(HAbs* abs) {
4446 LocationSummary* locations = abs->GetLocations();
4447 switch (abs->GetResultType()) {
4448 case DataType::Type::kInt32: {
4449 Register out = locations->Out().AsRegister<Register>();
4450 DCHECK_EQ(out, EAX);
4451 Register temp = locations->GetTemp(0).AsRegister<Register>();
4452 DCHECK_EQ(temp, EDX);
4453 // Sign extend EAX into EDX.
4454 __ cdq();
4455 // XOR EAX with sign.
4456 __ xorl(EAX, EDX);
4457 // Subtract out sign to correct.
4458 __ subl(EAX, EDX);
4459 // The result is in EAX.
4460 break;
4461 }
4462 case DataType::Type::kInt64: {
4463 Location input = locations->InAt(0);
4464 Register input_lo = input.AsRegisterPairLow<Register>();
4465 Register input_hi = input.AsRegisterPairHigh<Register>();
4466 Location output = locations->Out();
4467 Register output_lo = output.AsRegisterPairLow<Register>();
4468 Register output_hi = output.AsRegisterPairHigh<Register>();
4469 Register temp = locations->GetTemp(0).AsRegister<Register>();
4470 // Compute the sign into the temporary.
4471 __ movl(temp, input_hi);
4472 __ sarl(temp, Immediate(31));
4473 // Store the sign into the output.
4474 __ movl(output_lo, temp);
4475 __ movl(output_hi, temp);
4476 // XOR the input to the output.
4477 __ xorl(output_lo, input_lo);
4478 __ xorl(output_hi, input_hi);
4479 // Subtract the sign.
4480 __ subl(output_lo, temp);
4481 __ sbbl(output_hi, temp);
4482 break;
4483 }
4484 case DataType::Type::kFloat32: {
4485 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4486 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4487 Register constant = locations->GetTemp(1).AsRegister<Register>();
4488 __ movl(constant, Immediate(INT32_C(0x7FFFFFFF)));
4489 __ movd(temp, constant);
4490 __ andps(out, temp);
4491 break;
4492 }
4493 case DataType::Type::kFloat64: {
4494 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4495 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4496 // TODO: Use a constant from the constant table (requires extra input).
4497 __ LoadLongConstant(temp, INT64_C(0x7FFFFFFFFFFFFFFF));
4498 __ andpd(out, temp);
4499 break;
4500 }
4501 default:
4502 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4503 }
4504}
4505
Calin Juravled0d48522014-11-04 16:40:20 +00004506void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004507 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004508 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004509 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004510 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004511 case DataType::Type::kInt8:
4512 case DataType::Type::kUint16:
4513 case DataType::Type::kInt16:
4514 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004515 locations->SetInAt(0, Location::Any());
4516 break;
4517 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004518 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004519 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
4520 if (!instruction->IsConstant()) {
4521 locations->AddTemp(Location::RequiresRegister());
4522 }
4523 break;
4524 }
4525 default:
4526 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4527 }
Calin Juravled0d48522014-11-04 16:40:20 +00004528}
4529
4530void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004531 SlowPathCode* slow_path =
4532 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004533 codegen_->AddSlowPath(slow_path);
4534
4535 LocationSummary* locations = instruction->GetLocations();
4536 Location value = locations->InAt(0);
4537
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004538 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004539 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004540 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004541 case DataType::Type::kInt8:
4542 case DataType::Type::kUint16:
4543 case DataType::Type::kInt16:
4544 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004545 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004546 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004547 __ j(kEqual, slow_path->GetEntryLabel());
4548 } else if (value.IsStackSlot()) {
4549 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
4550 __ j(kEqual, slow_path->GetEntryLabel());
4551 } else {
4552 DCHECK(value.IsConstant()) << value;
4553 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004554 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004555 }
4556 }
4557 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004558 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004559 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004560 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004561 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004562 __ movl(temp, value.AsRegisterPairLow<Register>());
4563 __ orl(temp, value.AsRegisterPairHigh<Register>());
4564 __ j(kEqual, slow_path->GetEntryLabel());
4565 } else {
4566 DCHECK(value.IsConstant()) << value;
4567 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4568 __ jmp(slow_path->GetEntryLabel());
4569 }
4570 }
4571 break;
4572 }
4573 default:
4574 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004575 }
Calin Juravled0d48522014-11-04 16:40:20 +00004576}
4577
Calin Juravle9aec02f2014-11-18 23:06:35 +00004578void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
4579 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4580
4581 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004582 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004583
4584 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004585 case DataType::Type::kInt32:
4586 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004587 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00004588 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00004589 // The shift count needs to be in CL or a constant.
4590 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004591 locations->SetOut(Location::SameAsFirstInput());
4592 break;
4593 }
4594 default:
4595 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4596 }
4597}
4598
4599void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
4600 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4601
4602 LocationSummary* locations = op->GetLocations();
4603 Location first = locations->InAt(0);
4604 Location second = locations->InAt(1);
4605 DCHECK(first.Equals(locations->Out()));
4606
4607 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004608 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00004609 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004610 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004611 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004612 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004613 DCHECK_EQ(ECX, second_reg);
4614 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004615 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004616 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004617 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004618 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004619 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004620 }
4621 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004622 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004623 if (shift == 0) {
4624 return;
4625 }
4626 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004627 if (op->IsShl()) {
4628 __ shll(first_reg, imm);
4629 } else if (op->IsShr()) {
4630 __ sarl(first_reg, imm);
4631 } else {
4632 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004633 }
4634 }
4635 break;
4636 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004637 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004638 if (second.IsRegister()) {
4639 Register second_reg = second.AsRegister<Register>();
4640 DCHECK_EQ(ECX, second_reg);
4641 if (op->IsShl()) {
4642 GenerateShlLong(first, second_reg);
4643 } else if (op->IsShr()) {
4644 GenerateShrLong(first, second_reg);
4645 } else {
4646 GenerateUShrLong(first, second_reg);
4647 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004648 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00004649 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004650 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004651 // Nothing to do if the shift is 0, as the input is already the output.
4652 if (shift != 0) {
4653 if (op->IsShl()) {
4654 GenerateShlLong(first, shift);
4655 } else if (op->IsShr()) {
4656 GenerateShrLong(first, shift);
4657 } else {
4658 GenerateUShrLong(first, shift);
4659 }
4660 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004661 }
4662 break;
4663 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004664 default:
4665 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4666 }
4667}
4668
Mark P Mendell73945692015-04-29 14:56:17 +00004669void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
4670 Register low = loc.AsRegisterPairLow<Register>();
4671 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04004672 if (shift == 1) {
4673 // This is just an addition.
4674 __ addl(low, low);
4675 __ adcl(high, high);
4676 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00004677 // Shift by 32 is easy. High gets low, and low gets 0.
4678 codegen_->EmitParallelMoves(
4679 loc.ToLow(),
4680 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004681 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004682 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4683 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004684 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004685 } else if (shift > 32) {
4686 // Low part becomes 0. High part is low part << (shift-32).
4687 __ movl(high, low);
4688 __ shll(high, Immediate(shift - 32));
4689 __ xorl(low, low);
4690 } else {
4691 // Between 1 and 31.
4692 __ shld(high, low, Immediate(shift));
4693 __ shll(low, Immediate(shift));
4694 }
4695}
4696
Calin Juravle9aec02f2014-11-18 23:06:35 +00004697void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004698 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004699 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4700 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4701 __ testl(shifter, Immediate(32));
4702 __ j(kEqual, &done);
4703 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4704 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4705 __ Bind(&done);
4706}
4707
Mark P Mendell73945692015-04-29 14:56:17 +00004708void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4709 Register low = loc.AsRegisterPairLow<Register>();
4710 Register high = loc.AsRegisterPairHigh<Register>();
4711 if (shift == 32) {
4712 // Need to copy the sign.
4713 DCHECK_NE(low, high);
4714 __ movl(low, high);
4715 __ sarl(high, Immediate(31));
4716 } else if (shift > 32) {
4717 DCHECK_NE(low, high);
4718 // High part becomes sign. Low part is shifted by shift - 32.
4719 __ movl(low, high);
4720 __ sarl(high, Immediate(31));
4721 __ sarl(low, Immediate(shift - 32));
4722 } else {
4723 // Between 1 and 31.
4724 __ shrd(low, high, Immediate(shift));
4725 __ sarl(high, Immediate(shift));
4726 }
4727}
4728
Calin Juravle9aec02f2014-11-18 23:06:35 +00004729void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004730 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004731 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4732 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4733 __ testl(shifter, Immediate(32));
4734 __ j(kEqual, &done);
4735 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4736 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4737 __ Bind(&done);
4738}
4739
Mark P Mendell73945692015-04-29 14:56:17 +00004740void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4741 Register low = loc.AsRegisterPairLow<Register>();
4742 Register high = loc.AsRegisterPairHigh<Register>();
4743 if (shift == 32) {
4744 // Shift by 32 is easy. Low gets high, and high gets 0.
4745 codegen_->EmitParallelMoves(
4746 loc.ToHigh(),
4747 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004748 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004749 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4750 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004751 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004752 } else if (shift > 32) {
4753 // Low part is high >> (shift - 32). High part becomes 0.
4754 __ movl(low, high);
4755 __ shrl(low, Immediate(shift - 32));
4756 __ xorl(high, high);
4757 } else {
4758 // Between 1 and 31.
4759 __ shrd(low, high, Immediate(shift));
4760 __ shrl(high, Immediate(shift));
4761 }
4762}
4763
Calin Juravle9aec02f2014-11-18 23:06:35 +00004764void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004765 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004766 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4767 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4768 __ testl(shifter, Immediate(32));
4769 __ j(kEqual, &done);
4770 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4771 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4772 __ Bind(&done);
4773}
4774
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004775void LocationsBuilderX86::VisitRor(HRor* ror) {
4776 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004777 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004778
4779 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004780 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004781 // Add the temporary needed.
4782 locations->AddTemp(Location::RequiresRegister());
4783 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004784 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004785 locations->SetInAt(0, Location::RequiresRegister());
4786 // The shift count needs to be in CL (unless it is a constant).
4787 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4788 locations->SetOut(Location::SameAsFirstInput());
4789 break;
4790 default:
4791 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4792 UNREACHABLE();
4793 }
4794}
4795
4796void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4797 LocationSummary* locations = ror->GetLocations();
4798 Location first = locations->InAt(0);
4799 Location second = locations->InAt(1);
4800
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004801 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004802 Register first_reg = first.AsRegister<Register>();
4803 if (second.IsRegister()) {
4804 Register second_reg = second.AsRegister<Register>();
4805 __ rorl(first_reg, second_reg);
4806 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004807 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004808 __ rorl(first_reg, imm);
4809 }
4810 return;
4811 }
4812
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004813 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004814 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4815 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4816 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4817 if (second.IsRegister()) {
4818 Register second_reg = second.AsRegister<Register>();
4819 DCHECK_EQ(second_reg, ECX);
4820 __ movl(temp_reg, first_reg_hi);
4821 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4822 __ shrd(first_reg_lo, temp_reg, second_reg);
4823 __ movl(temp_reg, first_reg_hi);
4824 __ testl(second_reg, Immediate(32));
4825 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4826 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4827 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004828 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004829 if (shift_amt == 0) {
4830 // Already fine.
4831 return;
4832 }
4833 if (shift_amt == 32) {
4834 // Just swap.
4835 __ movl(temp_reg, first_reg_lo);
4836 __ movl(first_reg_lo, first_reg_hi);
4837 __ movl(first_reg_hi, temp_reg);
4838 return;
4839 }
4840
4841 Immediate imm(shift_amt);
4842 // Save the constents of the low value.
4843 __ movl(temp_reg, first_reg_lo);
4844
4845 // Shift right into low, feeding bits from high.
4846 __ shrd(first_reg_lo, first_reg_hi, imm);
4847
4848 // Shift right into high, feeding bits from the original low.
4849 __ shrd(first_reg_hi, temp_reg, imm);
4850
4851 // Swap if needed.
4852 if (shift_amt > 32) {
4853 __ movl(temp_reg, first_reg_lo);
4854 __ movl(first_reg_lo, first_reg_hi);
4855 __ movl(first_reg_hi, temp_reg);
4856 }
4857 }
4858}
4859
Calin Juravle9aec02f2014-11-18 23:06:35 +00004860void LocationsBuilderX86::VisitShl(HShl* shl) {
4861 HandleShift(shl);
4862}
4863
4864void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4865 HandleShift(shl);
4866}
4867
4868void LocationsBuilderX86::VisitShr(HShr* shr) {
4869 HandleShift(shr);
4870}
4871
4872void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4873 HandleShift(shr);
4874}
4875
4876void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4877 HandleShift(ushr);
4878}
4879
4880void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4881 HandleShift(ushr);
4882}
4883
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004884void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004885 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4886 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004887 locations->SetOut(Location::RegisterLocation(EAX));
Alex Lightd109e302018-06-27 10:25:41 -07004888 InvokeRuntimeCallingConvention calling_convention;
4889 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004890}
4891
4892void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07004893 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
4894 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4895 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004896}
4897
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004898void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004899 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4900 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004901 locations->SetOut(Location::RegisterLocation(EAX));
4902 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004903 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4904 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004905}
4906
4907void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01004908 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
4909 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004910 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004911 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004912 DCHECK(!codegen_->IsLeafMethod());
4913}
4914
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004915void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004916 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004917 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004918 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4919 if (location.IsStackSlot()) {
4920 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4921 } else if (location.IsDoubleStackSlot()) {
4922 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004923 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004924 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004925}
4926
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004927void InstructionCodeGeneratorX86::VisitParameterValue(
4928 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4929}
4930
4931void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4932 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004933 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004934 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4935}
4936
4937void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004938}
4939
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004940void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4941 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004942 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004943 locations->SetInAt(0, Location::RequiresRegister());
4944 locations->SetOut(Location::RequiresRegister());
4945}
4946
4947void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4948 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004949 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004950 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004951 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004952 __ movl(locations->Out().AsRegister<Register>(),
4953 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004954 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004955 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004956 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004957 __ movl(locations->Out().AsRegister<Register>(),
4958 Address(locations->InAt(0).AsRegister<Register>(),
4959 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4960 // temp = temp->GetImtEntryAt(method_offset);
4961 __ movl(locations->Out().AsRegister<Register>(),
4962 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004963 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004964}
4965
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004966void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004967 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004968 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004969 locations->SetInAt(0, Location::RequiresRegister());
4970 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004971}
4972
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004973void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4974 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004975 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004976 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004977 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004978 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004979 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004980 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004981 break;
4982
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004983 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004984 __ notl(out.AsRegisterPairLow<Register>());
4985 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004986 break;
4987
4988 default:
4989 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4990 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004991}
4992
David Brazdil66d126e2015-04-03 16:02:44 +01004993void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4994 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004995 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004996 locations->SetInAt(0, Location::RequiresRegister());
4997 locations->SetOut(Location::SameAsFirstInput());
4998}
4999
5000void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01005001 LocationSummary* locations = bool_not->GetLocations();
5002 Location in = locations->InAt(0);
5003 Location out = locations->Out();
5004 DCHECK(in.Equals(out));
5005 __ xorl(out.AsRegister<Register>(), Immediate(1));
5006}
5007
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005008void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005009 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005010 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00005011 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005012 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005013 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005014 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005015 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005016 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005017 case DataType::Type::kInt32:
5018 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00005019 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00005020 locations->SetInAt(1, Location::Any());
5021 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5022 break;
5023 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005024 case DataType::Type::kFloat32:
5025 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00005026 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00005027 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
5028 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
5029 } else if (compare->InputAt(1)->IsConstant()) {
5030 locations->SetInAt(1, Location::RequiresFpuRegister());
5031 } else {
5032 locations->SetInAt(1, Location::Any());
5033 }
Calin Juravleddb7df22014-11-25 20:56:51 +00005034 locations->SetOut(Location::RequiresRegister());
5035 break;
5036 }
5037 default:
5038 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5039 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005040}
5041
5042void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005043 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005044 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00005045 Location left = locations->InAt(0);
5046 Location right = locations->InAt(1);
5047
Mark Mendell0c9497d2015-08-21 09:30:05 -04005048 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08005049 Condition less_cond = kLess;
5050
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005051 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005052 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005053 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005054 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005055 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005056 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005057 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01005058 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08005059 break;
5060 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005061 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005062 Register left_low = left.AsRegisterPairLow<Register>();
5063 Register left_high = left.AsRegisterPairHigh<Register>();
5064 int32_t val_low = 0;
5065 int32_t val_high = 0;
5066 bool right_is_const = false;
5067
5068 if (right.IsConstant()) {
5069 DCHECK(right.GetConstant()->IsLongConstant());
5070 right_is_const = true;
5071 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
5072 val_low = Low32Bits(val);
5073 val_high = High32Bits(val);
5074 }
5075
Calin Juravleddb7df22014-11-25 20:56:51 +00005076 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005077 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005078 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005079 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005080 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005081 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08005082 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005083 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005084 __ j(kLess, &less); // Signed compare.
5085 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005086 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005087 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005088 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005089 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005090 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005091 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08005092 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005093 }
Aart Bika19616e2016-02-01 18:57:58 -08005094 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00005095 break;
5096 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005097 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00005098 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00005099 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08005100 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00005101 break;
5102 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005103 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00005104 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00005105 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08005106 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005107 break;
5108 }
5109 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00005110 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005111 }
Aart Bika19616e2016-02-01 18:57:58 -08005112
Calin Juravleddb7df22014-11-25 20:56:51 +00005113 __ movl(out, Immediate(0));
5114 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08005115 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00005116
5117 __ Bind(&greater);
5118 __ movl(out, Immediate(1));
5119 __ jmp(&done);
5120
5121 __ Bind(&less);
5122 __ movl(out, Immediate(-1));
5123
5124 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005125}
5126
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005127void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005128 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005129 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005130 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01005131 locations->SetInAt(i, Location::Any());
5132 }
5133 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005134}
5135
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005136void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005137 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005138}
5139
Roland Levillain7c1559a2015-12-15 10:55:36 +00005140void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00005141 /*
5142 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
5143 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
5144 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
5145 */
5146 switch (kind) {
5147 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00005148 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00005149 break;
5150 }
5151 case MemBarrierKind::kAnyStore:
5152 case MemBarrierKind::kLoadAny:
5153 case MemBarrierKind::kStoreStore: {
5154 // nop
5155 break;
5156 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05005157 case MemBarrierKind::kNTStoreStore:
5158 // Non-Temporal Store/Store needs an explicit fence.
Andreas Gampe3db70682018-12-26 15:12:03 -08005159 MemoryFence(/* non-temporal= */ true);
Mark Mendell7aa04a12016-01-27 22:39:07 -05005160 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005161 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005162}
5163
Vladimir Markodc151b22015-10-15 18:02:30 +01005164HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
5165 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01005166 ArtMethod* method ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005167 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005168}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005169
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005170Register CodeGeneratorX86::GetInvokeExtraParameter(HInvoke* invoke, Register temp) {
5171 if (invoke->IsInvokeStaticOrDirect()) {
5172 return GetInvokeStaticOrDirectExtraParameter(invoke->AsInvokeStaticOrDirect(), temp);
5173 }
5174 DCHECK(invoke->IsInvokeInterface());
5175 Location location =
5176 invoke->GetLocations()->InAt(invoke->AsInvokeInterface()->GetSpecialInputIndex());
5177 return location.AsRegister<Register>();
5178}
5179
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005180Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
5181 Register temp) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00005182 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005183 if (!invoke->GetLocations()->Intrinsified()) {
5184 return location.AsRegister<Register>();
5185 }
5186 // For intrinsics we allow any location, so it may be on the stack.
5187 if (!location.IsRegister()) {
5188 __ movl(temp, Address(ESP, location.GetStackIndex()));
5189 return temp;
5190 }
5191 // For register locations, check if the register was saved. If so, get it from the stack.
5192 // Note: There is a chance that the register was saved but not overwritten, so we could
5193 // save one load. However, since this is just an intrinsic slow path we prefer this
5194 // simple and more robust approach rather that trying to determine if that's the case.
5195 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00005196 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
5197 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
5198 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
5199 __ movl(temp, Address(ESP, stack_offset));
5200 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005201 }
5202 return location.AsRegister<Register>();
5203}
5204
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005205void CodeGeneratorX86::LoadMethod(MethodLoadKind load_kind, Location temp, HInvoke* invoke) {
5206 switch (load_kind) {
5207 case MethodLoadKind::kBootImageLinkTimePcRelative: {
5208 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
5209 Register base_reg = GetInvokeExtraParameter(invoke, temp.AsRegister<Register>());
5210 __ leal(temp.AsRegister<Register>(),
5211 Address(base_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
5212 RecordBootImageMethodPatch(invoke);
5213 break;
5214 }
5215 case MethodLoadKind::kBootImageRelRo: {
5216 size_t index = invoke->IsInvokeInterface()
5217 ? invoke->AsInvokeInterface()->GetSpecialInputIndex()
5218 : invoke->AsInvokeStaticOrDirect()->GetSpecialInputIndex();
5219 Register base_reg = GetInvokeExtraParameter(invoke, temp.AsRegister<Register>());
5220 __ movl(temp.AsRegister<Register>(), Address(base_reg, kPlaceholder32BitOffset));
5221 RecordBootImageRelRoPatch(
5222 invoke->InputAt(index)->AsX86ComputeBaseMethodAddress(),
5223 GetBootImageOffset(invoke));
5224 break;
5225 }
5226 case MethodLoadKind::kBssEntry: {
5227 Register base_reg = GetInvokeExtraParameter(invoke, temp.AsRegister<Register>());
5228 __ movl(temp.AsRegister<Register>(), Address(base_reg, kPlaceholder32BitOffset));
5229 RecordMethodBssEntryPatch(invoke);
5230 // No need for memory fence, thanks to the x86 memory model.
5231 break;
5232 }
5233 case MethodLoadKind::kJitDirectAddress: {
5234 __ movl(temp.AsRegister<Register>(),
5235 Immediate(reinterpret_cast32<uint32_t>(invoke->GetResolvedMethod())));
5236 break;
5237 }
5238 case MethodLoadKind::kRuntimeCall: {
5239 // Test situation, don't do anything.
5240 break;
5241 }
5242 default: {
5243 LOG(FATAL) << "Load kind should have already been handled " << load_kind;
5244 UNREACHABLE();
5245 }
5246 }
5247}
5248
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005249void CodeGeneratorX86::GenerateStaticOrDirectCall(
5250 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00005251 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
5252 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005253 case MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005254 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005255 uint32_t offset =
5256 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
5257 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00005258 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005259 }
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005260 case MethodLoadKind::kRecursive: {
Vladimir Marko86c87522020-05-11 16:55:55 +01005261 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005262 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005263 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005264 case MethodLoadKind::kRuntimeCall: {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005265 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5266 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01005267 }
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005268 case MethodLoadKind::kBootImageLinkTimePcRelative:
5269 // For kCallCriticalNative we skip loading the method and do the call directly.
5270 if (invoke->GetCodePtrLocation() == CodePtrLocation::kCallCriticalNative) {
5271 break;
5272 }
5273 FALLTHROUGH_INTENDED;
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005274 default: {
5275 LoadMethod(invoke->GetMethodLoadKind(), callee_method, invoke);
5276 }
Vladimir Marko58155012015-08-19 12:49:41 +00005277 }
5278
5279 switch (invoke->GetCodePtrLocation()) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005280 case CodePtrLocation::kCallSelf:
Vladimir Marko58155012015-08-19 12:49:41 +00005281 __ call(GetFrameEntryLabel());
Vladimir Marko86c87522020-05-11 16:55:55 +01005282 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko58155012015-08-19 12:49:41 +00005283 break;
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005284 case CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01005285 size_t out_frame_size =
5286 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorX86,
5287 kNativeStackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01005288 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005289 if (invoke->GetMethodLoadKind() == MethodLoadKind::kBootImageLinkTimePcRelative) {
5290 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
5291 Register base_reg = GetInvokeExtraParameter(invoke, temp.AsRegister<Register>());
5292 __ call(Address(base_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
5293 RecordBootImageJniEntrypointPatch(invoke);
5294 } else {
5295 // (callee_method + offset_of_jni_entry_point)()
5296 __ call(Address(callee_method.AsRegister<Register>(),
5297 ArtMethod::EntryPointFromJniOffset(kX86PointerSize).Int32Value()));
5298 }
Vladimir Marko86c87522020-05-11 16:55:55 +01005299 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5300 if (out_frame_size == 0u && DataType::IsFloatingPointType(invoke->GetType())) {
5301 // Create space for conversion.
5302 out_frame_size = 8u;
Vladimir Markodec78172020-06-19 15:31:23 +01005303 IncreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01005304 }
5305 // Zero-/sign-extend or move the result when needed due to native and managed ABI mismatch.
5306 switch (invoke->GetType()) {
5307 case DataType::Type::kBool:
5308 __ movzxb(EAX, AL);
5309 break;
5310 case DataType::Type::kInt8:
5311 __ movsxb(EAX, AL);
5312 break;
5313 case DataType::Type::kUint16:
5314 __ movzxw(EAX, EAX);
5315 break;
5316 case DataType::Type::kInt16:
5317 __ movsxw(EAX, EAX);
5318 break;
5319 case DataType::Type::kFloat32:
5320 __ fstps(Address(ESP, 0));
5321 __ movss(XMM0, Address(ESP, 0));
5322 break;
5323 case DataType::Type::kFloat64:
5324 __ fstpl(Address(ESP, 0));
5325 __ movsd(XMM0, Address(ESP, 0));
5326 break;
5327 case DataType::Type::kInt32:
5328 case DataType::Type::kInt64:
5329 case DataType::Type::kVoid:
5330 break;
5331 default:
5332 DCHECK(false) << invoke->GetType();
5333 break;
5334 }
5335 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01005336 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01005337 }
5338 break;
5339 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +01005340 case CodePtrLocation::kCallArtMethod:
Vladimir Marko58155012015-08-19 12:49:41 +00005341 // (callee_method + offset_of_quick_compiled_code)()
5342 __ call(Address(callee_method.AsRegister<Register>(),
5343 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005344 kX86PointerSize).Int32Value()));
Vladimir Marko86c87522020-05-11 16:55:55 +01005345 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko58155012015-08-19 12:49:41 +00005346 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04005347 }
5348
5349 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04005350}
5351
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005352void CodeGeneratorX86::GenerateVirtualCall(
5353 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005354 Register temp = temp_in.AsRegister<Register>();
5355 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5356 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005357
5358 // Use the calling convention instead of the location of the receiver, as
5359 // intrinsics may have put the receiver in a different register. In the intrinsics
5360 // slow path, the arguments have been moved to the right place, so here we are
5361 // guaranteed that the receiver is the first register of the calling convention.
5362 InvokeDexCallingConvention calling_convention;
5363 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005364 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005365 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005366 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005367 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005368 // Instead of simply (possibly) unpoisoning `temp` here, we should
5369 // emit a read barrier for the previous class reference load.
5370 // However this is not required in practice, as this is an
5371 // intermediate/temporary reference and because the current
5372 // concurrent copying collector keeps the from-space memory
5373 // intact/accessible until the end of the marking phase (the
5374 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005375 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00005376
5377 MaybeGenerateInlineCacheCheck(invoke, temp);
5378
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005379 // temp = temp->GetMethodAt(method_offset);
5380 __ movl(temp, Address(temp, method_offset));
5381 // call temp->GetEntryPoint();
5382 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07005383 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005384 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005385}
5386
Vladimir Marko6fd16062018-06-26 11:02:04 +01005387void CodeGeneratorX86::RecordBootImageIntrinsicPatch(HX86ComputeBaseMethodAddress* method_address,
5388 uint32_t intrinsic_data) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005389 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005390 method_address, /* target_dex_file= */ nullptr, intrinsic_data);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005391 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005392}
5393
Vladimir Markob066d432018-01-03 13:14:37 +00005394void CodeGeneratorX86::RecordBootImageRelRoPatch(HX86ComputeBaseMethodAddress* method_address,
5395 uint32_t boot_image_offset) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005396 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005397 method_address, /* target_dex_file= */ nullptr, boot_image_offset);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005398 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Markob066d432018-01-03 13:14:37 +00005399}
5400
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005401void CodeGeneratorX86::RecordBootImageMethodPatch(HInvoke* invoke) {
5402 size_t index = invoke->IsInvokeInterface()
5403 ? invoke->AsInvokeInterface()->GetSpecialInputIndex()
5404 : invoke->AsInvokeStaticOrDirect()->GetSpecialInputIndex();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005405 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005406 invoke->InputAt(index)->AsX86ComputeBaseMethodAddress();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005407 boot_image_method_patches_.emplace_back(
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005408 method_address,
5409 invoke->GetResolvedMethodReference().dex_file,
5410 invoke->GetResolvedMethodReference().index);
Vladimir Marko65979462017-05-19 17:25:12 +01005411 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005412}
5413
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005414void CodeGeneratorX86::RecordMethodBssEntryPatch(HInvoke* invoke) {
5415 size_t index = invoke->IsInvokeInterface()
5416 ? invoke->AsInvokeInterface()->GetSpecialInputIndex()
5417 : invoke->AsInvokeStaticOrDirect()->GetSpecialInputIndex();
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005418 DCHECK(IsSameDexFile(GetGraph()->GetDexFile(), *invoke->GetMethodReference().dex_file));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005419 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray8d34a182020-09-16 09:46:58 +01005420 invoke->InputAt(index)->AsX86ComputeBaseMethodAddress();
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005421 // Add the patch entry and bind its label at the end of the instruction.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005422 method_bss_entry_patches_.emplace_back(
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005423 method_address,
5424 invoke->GetMethodReference().dex_file,
5425 invoke->GetMethodReference().index);
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005426 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005427}
5428
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005429void CodeGeneratorX86::RecordBootImageTypePatch(HLoadClass* load_class) {
5430 HX86ComputeBaseMethodAddress* method_address =
5431 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5432 boot_image_type_patches_.emplace_back(
5433 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005434 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005435}
5436
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005437Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005438 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005439 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko8f63f102020-09-28 12:10:28 +01005440 ArenaDeque<X86PcRelativePatchInfo>* patches = nullptr;
5441 switch (load_class->GetLoadKind()) {
5442 case HLoadClass::LoadKind::kBssEntry:
5443 patches = &type_bss_entry_patches_;
5444 break;
5445 case HLoadClass::LoadKind::kBssEntryPublic:
5446 patches = &public_type_bss_entry_patches_;
5447 break;
5448 case HLoadClass::LoadKind::kBssEntryPackage:
5449 patches = &package_type_bss_entry_patches_;
5450 break;
5451 default:
5452 LOG(FATAL) << "Unexpected load kind: " << load_class->GetLoadKind();
5453 UNREACHABLE();
5454 }
5455 patches->emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005456 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005457 return &patches->back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005458}
5459
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005460void CodeGeneratorX86::RecordBootImageStringPatch(HLoadString* load_string) {
5461 HX86ComputeBaseMethodAddress* method_address =
5462 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
5463 boot_image_string_patches_.emplace_back(
5464 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
5465 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01005466}
5467
Vladimir Markoaad75c62016-10-03 08:46:48 +00005468Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005469 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005470 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005471 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005472 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005473 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005474}
5475
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005476void CodeGeneratorX86::RecordBootImageJniEntrypointPatch(HInvokeStaticOrDirect* invoke) {
5477 HX86ComputeBaseMethodAddress* method_address =
5478 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5479 boot_image_jni_entrypoint_patches_.emplace_back(
5480 method_address,
5481 invoke->GetResolvedMethodReference().dex_file,
5482 invoke->GetResolvedMethodReference().index);
5483 __ Bind(&boot_image_jni_entrypoint_patches_.back().label);
5484}
5485
Vladimir Markoeebb8212018-06-05 14:57:24 +01005486void CodeGeneratorX86::LoadBootImageAddress(Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01005487 uint32_t boot_image_reference,
Vladimir Markoeebb8212018-06-05 14:57:24 +01005488 HInvokeStaticOrDirect* invoke) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005489 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005490 HX86ComputeBaseMethodAddress* method_address =
5491 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5492 DCHECK(method_address != nullptr);
5493 Register method_address_reg =
5494 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005495 __ leal(reg, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005496 RecordBootImageIntrinsicPatch(method_address, boot_image_reference);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01005497 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01005498 HX86ComputeBaseMethodAddress* method_address =
5499 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5500 DCHECK(method_address != nullptr);
5501 Register method_address_reg =
5502 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005503 __ movl(reg, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005504 RecordBootImageRelRoPatch(method_address, boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01005505 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01005506 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01005507 gc::Heap* heap = Runtime::Current()->GetHeap();
5508 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01005509 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01005510 __ movl(reg, Immediate(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address))));
5511 }
5512}
5513
Vladimir Markode91ca92020-10-27 13:41:40 +00005514void CodeGeneratorX86::LoadIntrinsicDeclaringClass(Register reg, HInvokeStaticOrDirect* invoke) {
5515 DCHECK_NE(invoke->GetIntrinsic(), Intrinsics::kNone);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005516 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005517 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
Vladimir Marko6fd16062018-06-26 11:02:04 +01005518 HX86ComputeBaseMethodAddress* method_address =
5519 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5520 DCHECK(method_address != nullptr);
5521 Register method_address_reg =
5522 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Markode91ca92020-10-27 13:41:40 +00005523 __ leal(reg, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005524 MethodReference target_method = invoke->GetResolvedMethodReference();
Vladimir Marko6fd16062018-06-26 11:02:04 +01005525 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
5526 boot_image_type_patches_.emplace_back(method_address, target_method.dex_file, type_idx.index_);
5527 __ Bind(&boot_image_type_patches_.back().label);
5528 } else {
Vladimir Markode91ca92020-10-27 13:41:40 +00005529 uint32_t boot_image_offset = GetBootImageOffsetOfIntrinsicDeclaringClass(invoke);
5530 LoadBootImageAddress(reg, boot_image_offset, invoke);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005531 }
Vladimir Marko6fd16062018-06-26 11:02:04 +01005532}
5533
Vladimir Markoaad75c62016-10-03 08:46:48 +00005534// The label points to the end of the "movl" or another instruction but the literal offset
5535// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
5536constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
5537
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005538template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005539inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005540 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005541 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005542 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005543 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005544 linker_patches->push_back(Factory(literal_offset,
5545 info.target_dex_file,
5546 GetMethodAddressOffset(info.method_address),
5547 info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005548 }
5549}
5550
Vladimir Marko6fd16062018-06-26 11:02:04 +01005551template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
5552linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
5553 const DexFile* target_dex_file,
5554 uint32_t pc_insn_offset,
5555 uint32_t boot_image_offset) {
5556 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5557 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005558}
5559
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005560void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005561 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005562 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01005563 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005564 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00005565 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01005566 type_bss_entry_patches_.size() +
Vladimir Marko8f63f102020-09-28 12:10:28 +01005567 public_type_bss_entry_patches_.size() +
5568 package_type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005569 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01005570 string_bss_entry_patches_.size() +
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005571 boot_image_jni_entrypoint_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01005572 boot_image_other_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005573 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01005574 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005575 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
5576 boot_image_method_patches_, linker_patches);
5577 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
5578 boot_image_type_patches_, linker_patches);
5579 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005580 boot_image_string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005581 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005582 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005583 DCHECK(boot_image_type_patches_.empty());
5584 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01005585 }
5586 if (GetCompilerOptions().IsBootImage()) {
5587 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5588 boot_image_other_patches_, linker_patches);
5589 } else {
5590 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
5591 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005592 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005593 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5594 method_bss_entry_patches_, linker_patches);
5595 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5596 type_bss_entry_patches_, linker_patches);
Vladimir Marko8f63f102020-09-28 12:10:28 +01005597 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PublicTypeBssEntryPatch>(
5598 public_type_bss_entry_patches_, linker_patches);
5599 EmitPcRelativeLinkerPatches<linker::LinkerPatch::PackageTypeBssEntryPatch>(
5600 package_type_bss_entry_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005601 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5602 string_bss_entry_patches_, linker_patches);
Vladimir Markoeb9eb002020-10-02 13:54:19 +01005603 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeJniEntrypointPatch>(
5604 boot_image_jni_entrypoint_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005605 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005606}
5607
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005608void CodeGeneratorX86::MarkGCCard(Register temp,
5609 Register card,
5610 Register object,
5611 Register value,
5612 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005613 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005614 if (value_can_be_null) {
5615 __ testl(value, value);
5616 __ j(kEqual, &is_null);
5617 }
Roland Levillainc73f0522018-08-14 15:16:50 +01005618 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07005619 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01005620 // Calculate the offset (in the card table) of the card corresponding to
5621 // `object`.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005622 __ movl(temp, object);
5623 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01005624 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
5625 // `object`'s card.
5626 //
5627 // Register `card` contains the address of the card table. Note that the card
5628 // table's base is biased during its creation so that it always starts at an
5629 // address whose least-significant byte is equal to `kCardDirty` (see
5630 // art::gc::accounting::CardTable::Create). Therefore the MOVB instruction
5631 // below writes the `kCardDirty` (byte) value into the `object`'s card
5632 // (located at `card + object >> kCardShift`).
5633 //
5634 // This dual use of the value in register `card` (1. to calculate the location
5635 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
5636 // (no need to explicitly load `kCardDirty` as an immediate value).
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00005637 __ movb(Address(temp, card, TIMES_1, 0),
5638 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005639 if (value_can_be_null) {
5640 __ Bind(&is_null);
5641 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005642}
5643
Calin Juravle52c48962014-12-16 17:02:57 +00005644void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5645 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005646
5647 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005648 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005649 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005650 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5651 kEmitCompilerReadBarrier
5652 ? LocationSummary::kCallOnSlowPath
5653 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005654 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005655 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005656 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005657 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005658
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005659 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005660 locations->SetOut(Location::RequiresFpuRegister());
5661 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005662 // The output overlaps in case of long: we don't want the low move
5663 // to overwrite the object's location. Likewise, in the case of
5664 // an object field get with read barriers enabled, we do not want
5665 // the move to overwrite the object's location, as we need it to emit
5666 // the read barrier.
5667 locations->SetOut(
5668 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005669 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005670 Location::kOutputOverlap :
5671 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005672 }
Calin Juravle52c48962014-12-16 17:02:57 +00005673
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005674 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00005675 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005676 // So we use an XMM register as a temp to achieve atomicity (first
5677 // load the temp into the XMM and then copy the XMM into the
5678 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00005679 locations->AddTemp(Location::RequiresFpuRegister());
5680 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005681}
5682
Calin Juravle52c48962014-12-16 17:02:57 +00005683void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
5684 const FieldInfo& field_info) {
5685 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005686
Calin Juravle52c48962014-12-16 17:02:57 +00005687 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005688 Location base_loc = locations->InAt(0);
5689 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005690 Location out = locations->Out();
5691 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01005692 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5693 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00005694 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5695
Vladimir Marko61b92282017-10-11 13:23:17 +01005696 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005697 case DataType::Type::kBool:
5698 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005699 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005700 break;
5701 }
5702
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005703 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005704 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005705 break;
5706 }
5707
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005708 case DataType::Type::kUint16: {
5709 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005710 break;
5711 }
5712
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005713 case DataType::Type::kInt16: {
5714 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005715 break;
5716 }
5717
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005718 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00005719 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005720 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005721
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005722 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005723 // /* HeapReference<Object> */ out = *(base + offset)
5724 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005725 // Note that a potential implicit null check is handled in this
5726 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
5727 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005728 instruction, out, base, offset, /* needs_null_check= */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005729 if (is_volatile) {
5730 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5731 }
5732 } else {
5733 __ movl(out.AsRegister<Register>(), Address(base, offset));
5734 codegen_->MaybeRecordImplicitNullCheck(instruction);
5735 if (is_volatile) {
5736 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5737 }
5738 // If read barriers are enabled, emit read barriers other than
5739 // Baker's using a slow path (and also unpoison the loaded
5740 // reference, if heap poisoning is enabled).
5741 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5742 }
5743 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005744 }
5745
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005746 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005747 if (is_volatile) {
5748 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5749 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005750 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005751 __ movd(out.AsRegisterPairLow<Register>(), temp);
5752 __ psrlq(temp, Immediate(32));
5753 __ movd(out.AsRegisterPairHigh<Register>(), temp);
5754 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005755 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00005756 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005757 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005758 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
5759 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005760 break;
5761 }
5762
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005763 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00005764 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005765 break;
5766 }
5767
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005768 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005769 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005770 break;
5771 }
5772
Aart Bik66c158e2018-01-31 12:55:04 -08005773 case DataType::Type::kUint32:
5774 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005775 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005776 LOG(FATAL) << "Unreachable type " << load_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005777 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005778 }
Calin Juravle52c48962014-12-16 17:02:57 +00005779
Vladimir Marko61b92282017-10-11 13:23:17 +01005780 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005781 // Potential implicit null checks, in the case of reference or
5782 // long fields, are handled in the previous switch statement.
5783 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005784 codegen_->MaybeRecordImplicitNullCheck(instruction);
5785 }
5786
Calin Juravle52c48962014-12-16 17:02:57 +00005787 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005788 if (load_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005789 // Memory barriers, in the case of references, are also handled
5790 // in the previous switch statement.
5791 } else {
5792 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5793 }
Roland Levillain4d027112015-07-01 15:41:14 +01005794 }
Calin Juravle52c48962014-12-16 17:02:57 +00005795}
5796
5797void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5798 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5799
5800 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005801 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00005802 locations->SetInAt(0, Location::RequiresRegister());
5803 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005804 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005805 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00005806
5807 // The register allocator does not support multiple
5808 // inputs that die at entry with one in a specific register.
5809 if (is_byte_type) {
5810 // Ensure the value is in a byte register.
5811 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005812 } else if (DataType::IsFloatingPointType(field_type)) {
5813 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05005814 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5815 locations->SetInAt(1, Location::RequiresFpuRegister());
5816 } else {
5817 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
5818 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005819 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05005820 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00005821 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005822
Calin Juravle52c48962014-12-16 17:02:57 +00005823 // 64bits value can be atomically written to an address with movsd and an XMM register.
5824 // We need two XMM registers because there's no easier way to (bit) copy a register pair
5825 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
5826 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
5827 // isolated cases when we need this it isn't worth adding the extra complexity.
5828 locations->AddTemp(Location::RequiresFpuRegister());
5829 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005830 } else {
5831 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5832
5833 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5834 // Temporary registers for the write barrier.
5835 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
5836 // Ensure the card is in a byte register.
5837 locations->AddTemp(Location::RegisterLocation(ECX));
5838 }
Calin Juravle52c48962014-12-16 17:02:57 +00005839 }
5840}
5841
5842void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Andra Danciucde98192020-09-13 12:32:09 +00005843 uint32_t value_index,
5844 DataType::Type field_type,
5845 Address field_addr,
5846 Register base,
5847 bool is_volatile,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005848 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005849 LocationSummary* locations = instruction->GetLocations();
Andra Danciucde98192020-09-13 12:32:09 +00005850 Location value = locations->InAt(value_index);
Roland Levillain4d027112015-07-01 15:41:14 +01005851 bool needs_write_barrier =
Andra Danciucde98192020-09-13 12:32:09 +00005852 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(value_index));
Calin Juravle52c48962014-12-16 17:02:57 +00005853
5854 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005855 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005856 }
5857
Mark Mendell81489372015-11-04 11:30:41 -05005858 bool maybe_record_implicit_null_check_done = false;
5859
Calin Juravle52c48962014-12-16 17:02:57 +00005860 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005861 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005862 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005863 case DataType::Type::kInt8: {
Andra Danciucde98192020-09-13 12:32:09 +00005864 if (value.IsConstant()) {
5865 __ movb(field_addr, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
5866 } else {
5867 __ movb(field_addr, value.AsRegister<ByteRegister>());
5868 }
Calin Juravle52c48962014-12-16 17:02:57 +00005869 break;
5870 }
5871
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005872 case DataType::Type::kUint16:
5873 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05005874 if (value.IsConstant()) {
Andra Danciucde98192020-09-13 12:32:09 +00005875 __ movw(field_addr, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05005876 } else {
Andra Danciucde98192020-09-13 12:32:09 +00005877 __ movw(field_addr, value.AsRegister<Register>());
Mark Mendell81489372015-11-04 11:30:41 -05005878 }
Calin Juravle52c48962014-12-16 17:02:57 +00005879 break;
5880 }
5881
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005882 case DataType::Type::kInt32:
5883 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01005884 if (kPoisonHeapReferences && needs_write_barrier) {
5885 // Note that in the case where `value` is a null reference,
5886 // we do not enter this block, as the reference does not
5887 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005888 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01005889 Register temp = locations->GetTemp(0).AsRegister<Register>();
5890 __ movl(temp, value.AsRegister<Register>());
5891 __ PoisonHeapReference(temp);
Andra Danciucde98192020-09-13 12:32:09 +00005892 __ movl(field_addr, temp);
Mark Mendell81489372015-11-04 11:30:41 -05005893 } else if (value.IsConstant()) {
5894 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Andra Danciucde98192020-09-13 12:32:09 +00005895 __ movl(field_addr, Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005896 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005897 DCHECK(value.IsRegister()) << value;
Andra Danciucde98192020-09-13 12:32:09 +00005898 __ movl(field_addr, value.AsRegister<Register>());
Roland Levillain4d027112015-07-01 15:41:14 +01005899 }
Calin Juravle52c48962014-12-16 17:02:57 +00005900 break;
5901 }
5902
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005903 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005904 if (is_volatile) {
5905 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5906 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5907 __ movd(temp1, value.AsRegisterPairLow<Register>());
5908 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5909 __ punpckldq(temp1, temp2);
Andra Danciucde98192020-09-13 12:32:09 +00005910 __ movsd(field_addr, temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005911 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005912 } else if (value.IsConstant()) {
5913 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
Andra Danciucde98192020-09-13 12:32:09 +00005914 __ movl(field_addr, Immediate(Low32Bits(v)));
Mark Mendell81489372015-11-04 11:30:41 -05005915 codegen_->MaybeRecordImplicitNullCheck(instruction);
Andra Danciucde98192020-09-13 12:32:09 +00005916 __ movl(field_addr.displaceBy(kX86WordSize), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005917 } else {
Andra Danciucde98192020-09-13 12:32:09 +00005918 __ movl(field_addr, value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005919 codegen_->MaybeRecordImplicitNullCheck(instruction);
Andra Danciucde98192020-09-13 12:32:09 +00005920 __ movl(field_addr.displaceBy(kX86WordSize), value.AsRegisterPairHigh<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00005921 }
Mark Mendell81489372015-11-04 11:30:41 -05005922 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005923 break;
5924 }
5925
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005926 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05005927 if (value.IsConstant()) {
5928 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Andra Danciucde98192020-09-13 12:32:09 +00005929 __ movl(field_addr, Immediate(v));
Mark Mendell81489372015-11-04 11:30:41 -05005930 } else {
Andra Danciucde98192020-09-13 12:32:09 +00005931 __ movss(field_addr, value.AsFpuRegister<XmmRegister>());
Mark Mendell81489372015-11-04 11:30:41 -05005932 }
Calin Juravle52c48962014-12-16 17:02:57 +00005933 break;
5934 }
5935
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005936 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005937 if (value.IsConstant()) {
Andra Danciuc992e422020-09-16 08:12:02 +00005938 DCHECK(!is_volatile);
Mark Mendell81489372015-11-04 11:30:41 -05005939 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
Andra Danciucde98192020-09-13 12:32:09 +00005940 __ movl(field_addr, Immediate(Low32Bits(v)));
Mark Mendell81489372015-11-04 11:30:41 -05005941 codegen_->MaybeRecordImplicitNullCheck(instruction);
Andra Danciucde98192020-09-13 12:32:09 +00005942 __ movl(field_addr.displaceBy(kX86WordSize), Immediate(High32Bits(v)));
Mark Mendell81489372015-11-04 11:30:41 -05005943 maybe_record_implicit_null_check_done = true;
5944 } else {
Andra Danciucde98192020-09-13 12:32:09 +00005945 __ movsd(field_addr, value.AsFpuRegister<XmmRegister>());
Mark Mendell81489372015-11-04 11:30:41 -05005946 }
Calin Juravle52c48962014-12-16 17:02:57 +00005947 break;
5948 }
5949
Aart Bik66c158e2018-01-31 12:55:04 -08005950 case DataType::Type::kUint32:
5951 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005952 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005953 LOG(FATAL) << "Unreachable type " << field_type;
5954 UNREACHABLE();
5955 }
5956
Mark Mendell81489372015-11-04 11:30:41 -05005957 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005958 codegen_->MaybeRecordImplicitNullCheck(instruction);
5959 }
5960
Roland Levillain4d027112015-07-01 15:41:14 +01005961 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005962 Register temp = locations->GetTemp(0).AsRegister<Register>();
5963 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005964 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005965 }
5966
Calin Juravle52c48962014-12-16 17:02:57 +00005967 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005968 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005969 }
5970}
5971
Andra Danciucde98192020-09-13 12:32:09 +00005972void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
5973 const FieldInfo& field_info,
5974 bool value_can_be_null) {
5975 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5976
5977 LocationSummary* locations = instruction->GetLocations();
5978 Register base = locations->InAt(0).AsRegister<Register>();
5979 bool is_volatile = field_info.IsVolatile();
5980 DataType::Type field_type = field_info.GetFieldType();
5981 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5982
5983 Address field_addr(base, offset);
5984
5985 HandleFieldSet(instruction,
5986 /* value_index= */ 1,
5987 field_type,
5988 field_addr,
5989 base,
5990 is_volatile,
5991 value_can_be_null);
5992}
5993
Calin Juravle52c48962014-12-16 17:02:57 +00005994void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5995 HandleFieldGet(instruction, instruction->GetFieldInfo());
5996}
5997
5998void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5999 HandleFieldGet(instruction, instruction->GetFieldInfo());
6000}
6001
6002void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
6003 HandleFieldSet(instruction, instruction->GetFieldInfo());
6004}
6005
6006void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006007 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00006008}
6009
6010void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6011 HandleFieldSet(instruction, instruction->GetFieldInfo());
6012}
6013
6014void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006015 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00006016}
6017
6018void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6019 HandleFieldGet(instruction, instruction->GetFieldInfo());
6020}
6021
6022void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6023 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006024}
6025
Vladimir Marko552a1342017-10-31 10:56:47 +00006026void LocationsBuilderX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6027 codegen_->CreateStringBuilderAppendLocations(instruction, Location::RegisterLocation(EAX));
6028}
6029
6030void InstructionCodeGeneratorX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
6031 __ movl(EAX, Immediate(instruction->GetFormat()->GetValue()));
6032 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
6033}
6034
Calin Juravlee460d1d2015-09-29 04:52:17 +01006035void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
6036 HUnresolvedInstanceFieldGet* instruction) {
6037 FieldAccessCallingConventionX86 calling_convention;
6038 codegen_->CreateUnresolvedFieldLocationSummary(
6039 instruction, instruction->GetFieldType(), calling_convention);
6040}
6041
6042void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
6043 HUnresolvedInstanceFieldGet* instruction) {
6044 FieldAccessCallingConventionX86 calling_convention;
6045 codegen_->GenerateUnresolvedFieldAccess(instruction,
6046 instruction->GetFieldType(),
6047 instruction->GetFieldIndex(),
6048 instruction->GetDexPc(),
6049 calling_convention);
6050}
6051
6052void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
6053 HUnresolvedInstanceFieldSet* instruction) {
6054 FieldAccessCallingConventionX86 calling_convention;
6055 codegen_->CreateUnresolvedFieldLocationSummary(
6056 instruction, instruction->GetFieldType(), calling_convention);
6057}
6058
6059void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
6060 HUnresolvedInstanceFieldSet* instruction) {
6061 FieldAccessCallingConventionX86 calling_convention;
6062 codegen_->GenerateUnresolvedFieldAccess(instruction,
6063 instruction->GetFieldType(),
6064 instruction->GetFieldIndex(),
6065 instruction->GetDexPc(),
6066 calling_convention);
6067}
6068
6069void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
6070 HUnresolvedStaticFieldGet* instruction) {
6071 FieldAccessCallingConventionX86 calling_convention;
6072 codegen_->CreateUnresolvedFieldLocationSummary(
6073 instruction, instruction->GetFieldType(), calling_convention);
6074}
6075
6076void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
6077 HUnresolvedStaticFieldGet* instruction) {
6078 FieldAccessCallingConventionX86 calling_convention;
6079 codegen_->GenerateUnresolvedFieldAccess(instruction,
6080 instruction->GetFieldType(),
6081 instruction->GetFieldIndex(),
6082 instruction->GetDexPc(),
6083 calling_convention);
6084}
6085
6086void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
6087 HUnresolvedStaticFieldSet* instruction) {
6088 FieldAccessCallingConventionX86 calling_convention;
6089 codegen_->CreateUnresolvedFieldLocationSummary(
6090 instruction, instruction->GetFieldType(), calling_convention);
6091}
6092
6093void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
6094 HUnresolvedStaticFieldSet* instruction) {
6095 FieldAccessCallingConventionX86 calling_convention;
6096 codegen_->GenerateUnresolvedFieldAccess(instruction,
6097 instruction->GetFieldType(),
6098 instruction->GetFieldIndex(),
6099 instruction->GetDexPc(),
6100 calling_convention);
6101}
6102
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006103void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006104 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6105 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
6106 ? Location::RequiresRegister()
6107 : Location::Any();
6108 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006109}
6110
Calin Juravle2ae48182016-03-16 14:05:09 +00006111void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
6112 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00006113 return;
6114 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006115 LocationSummary* locations = instruction->GetLocations();
6116 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00006117
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006118 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00006119 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006120}
6121
Calin Juravle2ae48182016-03-16 14:05:09 +00006122void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006123 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006124 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006125
6126 LocationSummary* locations = instruction->GetLocations();
6127 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006128
6129 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04006130 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006131 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006132 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006133 } else {
6134 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00006135 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006136 __ jmp(slow_path->GetEntryLabel());
6137 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006138 }
6139 __ j(kEqual, slow_path->GetEntryLabel());
6140}
6141
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006142void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006143 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006144}
6145
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006146void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006147 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006148 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006149 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006150 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
6151 object_array_get_with_read_barrier
6152 ? LocationSummary::kCallOnSlowPath
6153 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01006154 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006155 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006156 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006157 locations->SetInAt(0, Location::RequiresRegister());
6158 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006159 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01006160 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6161 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006162 // The output overlaps in case of long: we don't want the low move
6163 // to overwrite the array's location. Likewise, in the case of an
6164 // object array get with read barriers enabled, we do not want the
6165 // move to overwrite the array's location, as we need it to emit
6166 // the read barrier.
6167 locations->SetOut(
6168 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006169 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
6170 ? Location::kOutputOverlap
6171 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01006172 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006173}
6174
6175void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
6176 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006177 Location obj_loc = locations->InAt(0);
6178 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006179 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006180 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006181 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006182
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006183 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00006184 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006185 case DataType::Type::kBool:
6186 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006187 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006188 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006189 break;
6190 }
6191
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006192 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006193 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006194 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006195 break;
6196 }
6197
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006198 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006199 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07006200 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6201 // Branch cases into compressed and uncompressed for each index's type.
6202 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
6203 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00006204 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006205 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006206 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6207 "Expecting 0=compressed, 1=uncompressed");
6208 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07006209 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
6210 __ jmp(&done);
6211 __ Bind(&not_compressed);
6212 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
6213 __ Bind(&done);
6214 } else {
6215 // Common case for charAt of array of char or when string compression's
6216 // feature is turned off.
6217 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
6218 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006219 break;
6220 }
6221
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006222 case DataType::Type::kInt16: {
6223 Register out = out_loc.AsRegister<Register>();
6224 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
6225 break;
6226 }
6227
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006228 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006229 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006230 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006231 break;
6232 }
6233
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006234 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006235 static_assert(
6236 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6237 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00006238 // /* HeapReference<Object> */ out =
6239 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6240 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006241 // Note that a potential implicit null check is handled in this
6242 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
6243 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08006244 instruction, out_loc, obj, data_offset, index, /* needs_null_check= */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006245 } else {
6246 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006247 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
6248 codegen_->MaybeRecordImplicitNullCheck(instruction);
6249 // If read barriers are enabled, emit read barriers other than
6250 // Baker's using a slow path (and also unpoison the loaded
6251 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00006252 if (index.IsConstant()) {
6253 uint32_t offset =
6254 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006255 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6256 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006257 codegen_->MaybeGenerateReadBarrierSlow(
6258 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6259 }
6260 }
6261 break;
6262 }
6263
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006264 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006265 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006266 __ movl(out_loc.AsRegisterPairLow<Register>(),
6267 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
6268 codegen_->MaybeRecordImplicitNullCheck(instruction);
6269 __ movl(out_loc.AsRegisterPairHigh<Register>(),
6270 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006271 break;
6272 }
6273
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006274 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006275 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006276 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006277 break;
6278 }
6279
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006280 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006281 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006282 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006283 break;
6284 }
6285
Aart Bik66c158e2018-01-31 12:55:04 -08006286 case DataType::Type::kUint32:
6287 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006288 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00006289 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07006290 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006291 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006292
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006293 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006294 // Potential implicit null checks, in the case of reference or
6295 // long arrays, are handled in the previous switch statement.
6296 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00006297 codegen_->MaybeRecordImplicitNullCheck(instruction);
6298 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006299}
6300
6301void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006302 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006303
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006304 bool needs_write_barrier =
6305 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006306 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006307
Vladimir Markoca6fff82017-10-03 14:49:14 +01006308 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01006309 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006310 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006311
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006312 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006313 // We need the inputs to be different than the output in case of long operation.
6314 // In case of a byte operation, the register allocator does not support multiple
6315 // inputs that die at entry with one in a specific register.
6316 locations->SetInAt(0, Location::RequiresRegister());
6317 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6318 if (is_byte_type) {
6319 // Ensure the value is in a byte register.
6320 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006321 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05006322 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006323 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006324 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
6325 }
6326 if (needs_write_barrier) {
6327 // Temporary registers for the write barrier.
6328 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6329 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00006330 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006331 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006332}
6333
6334void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
6335 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006336 Location array_loc = locations->InAt(0);
6337 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006338 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006339 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006340 DataType::Type value_type = instruction->GetComponentType();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006341 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006342 bool needs_write_barrier =
6343 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006344
6345 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006346 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006347 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006348 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006349 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006350 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006351 if (value.IsRegister()) {
6352 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006353 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01006354 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006355 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006356 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006357 break;
6358 }
6359
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006360 case DataType::Type::kUint16:
6361 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006362 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006363 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006364 if (value.IsRegister()) {
6365 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006366 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01006367 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006368 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006369 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006370 break;
6371 }
6372
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006373 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006374 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006375 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006376
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006377 if (!value.IsRegister()) {
6378 // Just setting null.
6379 DCHECK(instruction->InputAt(2)->IsNullConstant());
6380 DCHECK(value.IsConstant()) << value;
6381 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00006382 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006383 DCHECK(!needs_write_barrier);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006384 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006385 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006386 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006387
6388 DCHECK(needs_write_barrier);
6389 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01006390 Location temp_loc = locations->GetTemp(0);
6391 Register temp = temp_loc.AsRegister<Register>();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006392
6393 bool can_value_be_null = instruction->GetValueCanBeNull();
6394 NearLabel do_store;
6395 if (can_value_be_null) {
6396 __ testl(register_value, register_value);
6397 __ j(kEqual, &do_store);
6398 }
6399
6400 SlowPathCode* slow_path = nullptr;
6401 if (needs_type_check) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006402 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006403 codegen_->AddSlowPath(slow_path);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006404
6405 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6406 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6407 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006408
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006409 // Note that when Baker read barriers are enabled, the type
6410 // checks are performed without read barriers. This is fine,
6411 // even in the case where a class object is in the from-space
6412 // after the flip, as a comparison involving such a type would
6413 // not produce a false positive; it may of course produce a
6414 // false negative, in which case we would take the ArraySet
6415 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01006416
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006417 // /* HeapReference<Class> */ temp = array->klass_
6418 __ movl(temp, Address(array, class_offset));
6419 codegen_->MaybeRecordImplicitNullCheck(instruction);
6420 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01006421
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006422 // /* HeapReference<Class> */ temp = temp->component_type_
6423 __ movl(temp, Address(temp, component_offset));
6424 // If heap poisoning is enabled, no need to unpoison `temp`
6425 // nor the object reference in `register_value->klass`, as
6426 // we are comparing two poisoned references.
6427 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01006428
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006429 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006430 NearLabel do_put;
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006431 __ j(kEqual, &do_put);
6432 // If heap poisoning is enabled, the `temp` reference has
6433 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006434 __ MaybeUnpoisonHeapReference(temp);
6435
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006436 // If heap poisoning is enabled, no need to unpoison the
6437 // heap reference loaded below, as it is only used for a
6438 // comparison with null.
6439 __ cmpl(Address(temp, super_offset), Immediate(0));
6440 __ j(kNotEqual, slow_path->GetEntryLabel());
6441 __ Bind(&do_put);
6442 } else {
6443 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006444 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006445 }
6446
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006447 Register card = locations->GetTemp(1).AsRegister<Register>();
6448 codegen_->MarkGCCard(
6449 temp, card, array, value.AsRegister<Register>(), /* value_can_be_null= */ false);
6450
6451 if (can_value_be_null) {
6452 DCHECK(do_store.IsLinked());
6453 __ Bind(&do_store);
6454 }
6455
6456 Register source = register_value;
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006457 if (kPoisonHeapReferences) {
6458 __ movl(temp, register_value);
6459 __ PoisonHeapReference(temp);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006460 source = temp;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006461 }
6462
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006463 __ movl(address, source);
6464
6465 if (can_value_be_null || !needs_type_check) {
6466 codegen_->MaybeRecordImplicitNullCheck(instruction);
6467 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006468
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006469 if (slow_path != nullptr) {
6470 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006471 }
6472
6473 break;
6474 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006475
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006476 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006477 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006478 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006479 if (value.IsRegister()) {
6480 __ movl(address, value.AsRegister<Register>());
6481 } else {
6482 DCHECK(value.IsConstant()) << value;
6483 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
6484 __ movl(address, Immediate(v));
6485 }
6486 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006487 break;
6488 }
6489
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006490 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006491 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006492 if (value.IsRegisterPair()) {
6493 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6494 value.AsRegisterPairLow<Register>());
6495 codegen_->MaybeRecordImplicitNullCheck(instruction);
6496 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6497 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006498 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006499 DCHECK(value.IsConstant());
6500 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
6501 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6502 Immediate(Low32Bits(val)));
6503 codegen_->MaybeRecordImplicitNullCheck(instruction);
6504 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6505 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006506 }
6507 break;
6508 }
6509
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006510 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006511 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006512 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006513 if (value.IsFpuRegister()) {
6514 __ movss(address, value.AsFpuRegister<XmmRegister>());
6515 } else {
6516 DCHECK(value.IsConstant());
6517 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
6518 __ movl(address, Immediate(v));
6519 }
6520 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006521 break;
6522 }
6523
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006524 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006525 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006526 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006527 if (value.IsFpuRegister()) {
6528 __ movsd(address, value.AsFpuRegister<XmmRegister>());
6529 } else {
6530 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006531 Address address_hi =
6532 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05006533 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
6534 __ movl(address, Immediate(Low32Bits(v)));
6535 codegen_->MaybeRecordImplicitNullCheck(instruction);
6536 __ movl(address_hi, Immediate(High32Bits(v)));
6537 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006538 break;
6539 }
6540
Aart Bik66c158e2018-01-31 12:55:04 -08006541 case DataType::Type::kUint32:
6542 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006543 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006544 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07006545 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006546 }
6547}
6548
6549void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006550 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006551 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04006552 if (!instruction->IsEmittedAtUseSite()) {
6553 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6554 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006555}
6556
6557void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04006558 if (instruction->IsEmittedAtUseSite()) {
6559 return;
6560 }
6561
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006562 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006563 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006564 Register obj = locations->InAt(0).AsRegister<Register>();
6565 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006566 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00006567 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07006568 // Mask out most significant bit in case the array is String's array of char.
6569 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006570 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006571 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006572}
6573
6574void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006575 RegisterSet caller_saves = RegisterSet::Empty();
6576 InvokeRuntimeCallingConvention calling_convention;
6577 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6578 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6579 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05006580 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04006581 HInstruction* length = instruction->InputAt(1);
6582 if (!length->IsEmittedAtUseSite()) {
6583 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6584 }
jessicahandojo4877b792016-09-08 19:49:13 -07006585 // Need register to see array's length.
6586 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6587 locations->AddTemp(Location::RequiresRegister());
6588 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006589}
6590
6591void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07006592 const bool is_string_compressed_char_at =
6593 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006594 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05006595 Location index_loc = locations->InAt(0);
6596 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006597 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006598 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006599
Mark Mendell99dbd682015-04-22 16:18:52 -04006600 if (length_loc.IsConstant()) {
6601 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
6602 if (index_loc.IsConstant()) {
6603 // BCE will remove the bounds check if we are guarenteed to pass.
6604 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6605 if (index < 0 || index >= length) {
6606 codegen_->AddSlowPath(slow_path);
6607 __ jmp(slow_path->GetEntryLabel());
6608 } else {
6609 // Some optimization after BCE may have generated this, and we should not
6610 // generate a bounds check if it is a valid range.
6611 }
6612 return;
6613 }
6614
6615 // We have to reverse the jump condition because the length is the constant.
6616 Register index_reg = index_loc.AsRegister<Register>();
6617 __ cmpl(index_reg, Immediate(length));
6618 codegen_->AddSlowPath(slow_path);
6619 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006620 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04006621 HInstruction* array_length = instruction->InputAt(1);
6622 if (array_length->IsEmittedAtUseSite()) {
6623 // Address the length field in the array.
6624 DCHECK(array_length->IsArrayLength());
6625 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
6626 Location array_loc = array_length->GetLocations()->InAt(0);
6627 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07006628 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006629 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
6630 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07006631 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
6632 __ movl(length_reg, array_len);
6633 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006634 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006635 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04006636 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006637 // Checking bounds for general case:
6638 // Array of char or string's array with feature compression off.
6639 if (index_loc.IsConstant()) {
6640 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6641 __ cmpl(array_len, Immediate(value));
6642 } else {
6643 __ cmpl(array_len, index_loc.AsRegister<Register>());
6644 }
6645 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04006646 }
Mark Mendell99dbd682015-04-22 16:18:52 -04006647 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006648 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04006649 }
6650 codegen_->AddSlowPath(slow_path);
6651 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006652 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006653}
6654
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006655void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006656 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006657}
6658
6659void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006660 if (instruction->GetNext()->IsSuspendCheck() &&
6661 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6662 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6663 // The back edge will generate the suspend check.
6664 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6665 }
6666
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006667 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6668}
6669
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006670void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006671 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6672 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07006673 // In suspend check slow path, usually there are no caller-save registers at all.
6674 // If SIMD instructions are present, however, we force spilling all live SIMD
6675 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07006676 locations->SetCustomSlowPathCallerSaves(
6677 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006678}
6679
6680void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006681 HBasicBlock* block = instruction->GetBlock();
6682 if (block->GetLoopInformation() != nullptr) {
6683 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6684 // The back edge will generate the suspend check.
6685 return;
6686 }
6687 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6688 // The goto will generate the suspend check.
6689 return;
6690 }
6691 GenerateSuspendCheck(instruction, nullptr);
6692}
6693
6694void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
6695 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006696 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006697 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
6698 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006699 slow_path =
6700 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006701 instruction->SetSlowPath(slow_path);
6702 codegen_->AddSlowPath(slow_path);
6703 if (successor != nullptr) {
6704 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006705 }
6706 } else {
6707 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6708 }
6709
Andreas Gampe542451c2016-07-26 09:02:02 -07006710 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006711 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006712 if (successor == nullptr) {
6713 __ j(kNotEqual, slow_path->GetEntryLabel());
6714 __ Bind(slow_path->GetReturnLabel());
6715 } else {
6716 __ j(kEqual, codegen_->GetLabelOf(successor));
6717 __ jmp(slow_path->GetEntryLabel());
6718 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006719}
6720
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006721X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
6722 return codegen_->GetAssembler();
6723}
6724
Aart Bikcfe50bb2017-12-12 14:54:12 -08006725void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006726 ScratchRegisterScope ensure_scratch(
6727 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6728 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6729 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05006730
Aart Bikcfe50bb2017-12-12 14:54:12 -08006731 // Now that temp register is available (possibly spilled), move blocks of memory.
6732 for (int i = 0; i < number_of_words; i++) {
6733 __ movl(temp_reg, Address(ESP, src + stack_offset));
6734 __ movl(Address(ESP, dst + stack_offset), temp_reg);
6735 stack_offset += kX86WordSize;
6736 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006737}
6738
6739void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006740 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006741 Location source = move->GetSource();
6742 Location destination = move->GetDestination();
6743
6744 if (source.IsRegister()) {
6745 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006746 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006747 } else if (destination.IsFpuRegister()) {
6748 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006749 } else {
6750 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006751 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006752 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006753 } else if (source.IsRegisterPair()) {
Vladimir Marko86c87522020-05-11 16:55:55 +01006754 if (destination.IsRegisterPair()) {
6755 __ movl(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
6756 DCHECK_NE(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
6757 __ movl(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
6758 } else if (destination.IsFpuRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006759 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Vladimir Markodec78172020-06-19 15:31:23 +01006760 // Push the 2 source registers to the stack.
Vladimir Marko86c87522020-05-11 16:55:55 +01006761 __ pushl(source.AsRegisterPairHigh<Register>());
6762 __ cfi().AdjustCFAOffset(elem_size);
6763 __ pushl(source.AsRegisterPairLow<Register>());
6764 __ cfi().AdjustCFAOffset(elem_size);
6765 // Load the destination register.
David Brazdil74eb1b22015-12-14 11:44:01 +00006766 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
6767 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01006768 codegen_->DecreaseFrame(2 * elem_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01006769 } else {
6770 DCHECK(destination.IsDoubleStackSlot());
6771 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
6772 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
6773 source.AsRegisterPairHigh<Register>());
6774 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006775 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006776 if (destination.IsRegister()) {
6777 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
6778 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006779 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006780 } else if (destination.IsRegisterPair()) {
Vladimir Marko86c87522020-05-11 16:55:55 +01006781 size_t elem_size = DataType::Size(DataType::Type::kInt32);
6782 // Create stack space for 2 elements.
Vladimir Markodec78172020-06-19 15:31:23 +01006783 codegen_->IncreaseFrame(2 * elem_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01006784 // Store the source register.
6785 __ movsd(Address(ESP, 0), source.AsFpuRegister<XmmRegister>());
6786 // And pop the values into destination registers.
6787 __ popl(destination.AsRegisterPairLow<Register>());
6788 __ cfi().AdjustCFAOffset(-elem_size);
6789 __ popl(destination.AsRegisterPairHigh<Register>());
6790 __ cfi().AdjustCFAOffset(-elem_size);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006791 } else if (destination.IsStackSlot()) {
6792 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006793 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006794 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006795 } else {
6796 DCHECK(destination.IsSIMDStackSlot());
6797 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05006798 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006799 } else if (source.IsStackSlot()) {
6800 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006801 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006802 } else if (destination.IsFpuRegister()) {
6803 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006804 } else {
6805 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08006806 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006807 }
6808 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006809 if (destination.IsRegisterPair()) {
6810 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
6811 __ movl(destination.AsRegisterPairHigh<Register>(),
6812 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
6813 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006814 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6815 } else {
6816 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006817 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006818 }
Aart Bik5576f372017-03-23 16:17:37 -07006819 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006820 if (destination.IsFpuRegister()) {
6821 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6822 } else {
6823 DCHECK(destination.IsSIMDStackSlot());
6824 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6825 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006826 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006827 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006828 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006829 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006830 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006831 if (value == 0) {
6832 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
6833 } else {
6834 __ movl(destination.AsRegister<Register>(), Immediate(value));
6835 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006836 } else {
6837 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05006838 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006839 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006840 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006841 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006842 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006843 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006844 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006845 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6846 if (value == 0) {
6847 // Easy handling of 0.0.
6848 __ xorps(dest, dest);
6849 } else {
6850 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006851 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6852 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
6853 __ movl(temp, Immediate(value));
6854 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006855 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006856 } else {
6857 DCHECK(destination.IsStackSlot()) << destination;
6858 __ movl(Address(ESP, destination.GetStackIndex()), imm);
6859 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006860 } else if (constant->IsLongConstant()) {
6861 int64_t value = constant->AsLongConstant()->GetValue();
6862 int32_t low_value = Low32Bits(value);
6863 int32_t high_value = High32Bits(value);
6864 Immediate low(low_value);
6865 Immediate high(high_value);
6866 if (destination.IsDoubleStackSlot()) {
6867 __ movl(Address(ESP, destination.GetStackIndex()), low);
6868 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6869 } else {
6870 __ movl(destination.AsRegisterPairLow<Register>(), low);
6871 __ movl(destination.AsRegisterPairHigh<Register>(), high);
6872 }
6873 } else {
6874 DCHECK(constant->IsDoubleConstant());
6875 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006876 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006877 int32_t low_value = Low32Bits(value);
6878 int32_t high_value = High32Bits(value);
6879 Immediate low(low_value);
6880 Immediate high(high_value);
6881 if (destination.IsFpuRegister()) {
6882 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6883 if (value == 0) {
6884 // Easy handling of 0.0.
6885 __ xorpd(dest, dest);
6886 } else {
6887 __ pushl(high);
Vladimir Marko86c87522020-05-11 16:55:55 +01006888 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006889 __ pushl(low);
Vladimir Marko86c87522020-05-11 16:55:55 +01006890 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006891 __ movsd(dest, Address(ESP, 0));
Vladimir Markodec78172020-06-19 15:31:23 +01006892 codegen_->DecreaseFrame(8);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006893 }
6894 } else {
6895 DCHECK(destination.IsDoubleStackSlot()) << destination;
6896 __ movl(Address(ESP, destination.GetStackIndex()), low);
6897 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6898 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006899 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006900 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006901 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006902 }
6903}
6904
Mark Mendella5c19ce2015-04-01 12:51:05 -04006905void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006906 Register suggested_scratch = reg == EAX ? EBX : EAX;
6907 ScratchRegisterScope ensure_scratch(
6908 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
6909
6910 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6911 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
6912 __ movl(Address(ESP, mem + stack_offset), reg);
6913 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006914}
6915
Mark Mendell7c8d0092015-01-26 11:21:33 -05006916void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006917 ScratchRegisterScope ensure_scratch(
6918 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6919
6920 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6921 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6922 __ movl(temp_reg, Address(ESP, mem + stack_offset));
6923 __ movss(Address(ESP, mem + stack_offset), reg);
6924 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006925}
6926
Aart Bikcfe50bb2017-12-12 14:54:12 -08006927void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
6928 size_t extra_slot = 4 * kX86WordSize;
Vladimir Markodec78172020-06-19 15:31:23 +01006929 codegen_->IncreaseFrame(extra_slot);
Aart Bikcfe50bb2017-12-12 14:54:12 -08006930 __ movups(Address(ESP, 0), XmmRegister(reg));
6931 ExchangeMemory(0, mem + extra_slot, 4);
6932 __ movups(XmmRegister(reg), Address(ESP, 0));
Vladimir Markodec78172020-06-19 15:31:23 +01006933 codegen_->DecreaseFrame(extra_slot);
Aart Bikcfe50bb2017-12-12 14:54:12 -08006934}
6935
6936void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006937 ScratchRegisterScope ensure_scratch1(
6938 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006939
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006940 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
6941 ScratchRegisterScope ensure_scratch2(
6942 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006943
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006944 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
6945 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006946
6947 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
6948 for (int i = 0; i < number_of_words; i++) {
6949 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
6950 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
6951 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
6952 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
6953 stack_offset += kX86WordSize;
6954 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006955}
6956
6957void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006958 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006959 Location source = move->GetSource();
6960 Location destination = move->GetDestination();
6961
6962 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04006963 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
6964 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
6965 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
6966 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
6967 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006968 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006969 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006970 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006971 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006972 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006973 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006974 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
6975 // Use XOR Swap algorithm to avoid a temporary.
6976 DCHECK_NE(source.reg(), destination.reg());
6977 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6978 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
6979 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6980 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
6981 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6982 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
6983 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006984 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
6985 // Take advantage of the 16 bytes in the XMM register.
6986 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
6987 Address stack(ESP, destination.GetStackIndex());
6988 // Load the double into the high doubleword.
6989 __ movhpd(reg, stack);
6990
6991 // Store the low double into the destination.
6992 __ movsd(stack, reg);
6993
6994 // Move the high double to the low double.
6995 __ psrldq(reg, Immediate(8));
6996 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6997 // Take advantage of the 16 bytes in the XMM register.
6998 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6999 Address stack(ESP, source.GetStackIndex());
7000 // Load the double into the high doubleword.
7001 __ movhpd(reg, stack);
7002
7003 // Store the low double into the destination.
7004 __ movsd(stack, reg);
7005
7006 // Move the high double to the low double.
7007 __ psrldq(reg, Immediate(8));
7008 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08007009 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
7010 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
7011 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
7012 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
7013 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
7014 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
7015 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01007016 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05007017 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01007018 }
7019}
7020
7021void ParallelMoveResolverX86::SpillScratch(int reg) {
7022 __ pushl(static_cast<Register>(reg));
7023}
7024
7025void ParallelMoveResolverX86::RestoreScratch(int reg) {
7026 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007027}
7028
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007029HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
7030 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007031 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007032 case HLoadClass::LoadKind::kInvalid:
7033 LOG(FATAL) << "UNREACHABLE";
7034 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007035 case HLoadClass::LoadKind::kReferrersClass:
7036 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007037 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007038 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007039 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko8f63f102020-09-28 12:10:28 +01007040 case HLoadClass::LoadKind::kBssEntryPublic:
7041 case HLoadClass::LoadKind::kBssEntryPackage:
Vladimir Marko695348f2020-05-19 14:42:02 +01007042 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007043 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007044 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007045 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01007046 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007047 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007048 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007049 break;
7050 }
7051 return desired_class_load_kind;
7052}
7053
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007054void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007055 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007056 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007057 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00007058 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007059 cls,
7060 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00007061 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00007062 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007063 return;
7064 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01007065 DCHECK_EQ(cls->NeedsAccessCheck(),
7066 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
7067 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007068
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007069 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7070 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007071 ? LocationSummary::kCallOnSlowPath
7072 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007073 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007074 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007075 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007076 }
7077
Vladimir Marko8f63f102020-09-28 12:10:28 +01007078 if (load_kind == HLoadClass::LoadKind::kReferrersClass || cls->HasPcRelativeLoadKind()) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007079 locations->SetInAt(0, Location::RequiresRegister());
7080 }
7081 locations->SetOut(Location::RequiresRegister());
Vladimir Marko8f63f102020-09-28 12:10:28 +01007082 if (call_kind == LocationSummary::kCallOnSlowPath && cls->HasPcRelativeLoadKind()) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007083 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7084 // Rely on the type resolution and/or initialization to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007085 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007086 } else {
7087 // For non-Baker read barrier we have a temp-clobbering call.
7088 }
7089 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007090}
7091
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007092Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01007093 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007094 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007095 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007096 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007097 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007098 PatchInfo<Label>* info = &jit_class_patches_.back();
7099 return &info->label;
7100}
7101
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007102// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7103// move.
7104void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007105 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007106 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007107 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01007108 return;
7109 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01007110 DCHECK_EQ(cls->NeedsAccessCheck(),
7111 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
7112 load_kind == HLoadClass::LoadKind::kBssEntryPackage);
Calin Juravle580b6092015-10-06 17:35:58 +01007113
Vladimir Marko41559982017-01-06 14:04:23 +00007114 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007115 Location out_loc = locations->Out();
7116 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007117
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007118 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007119 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7120 ? kWithoutReadBarrier
7121 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00007122 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007123 case HLoadClass::LoadKind::kReferrersClass: {
7124 DCHECK(!cls->CanCallRuntime());
7125 DCHECK(!cls->MustGenerateClinitCheck());
7126 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7127 Register current_method = locations->InAt(0).AsRegister<Register>();
7128 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007129 cls,
7130 out_loc,
7131 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Andreas Gampe3db70682018-12-26 15:12:03 -08007132 /* fixup_label= */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007133 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007134 break;
7135 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007136 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007137 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7138 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007139 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007140 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007141 __ leal(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007142 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007143 break;
7144 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007145 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007146 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7147 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007148 __ movl(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007149 codegen_->RecordBootImageRelRoPatch(cls->InputAt(0)->AsX86ComputeBaseMethodAddress(),
Vladimir Markode91ca92020-10-27 13:41:40 +00007150 CodeGenerator::GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007151 break;
7152 }
Vladimir Marko8f63f102020-09-28 12:10:28 +01007153 case HLoadClass::LoadKind::kBssEntry:
7154 case HLoadClass::LoadKind::kBssEntryPublic:
7155 case HLoadClass::LoadKind::kBssEntryPackage: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007156 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007157 Address address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007158 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
7159 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007160 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007161 generate_null_check = true;
7162 break;
7163 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007164 case HLoadClass::LoadKind::kJitBootImageAddress: {
7165 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7166 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
7167 DCHECK_NE(address, 0u);
7168 __ movl(out, Immediate(address));
7169 break;
7170 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007171 case HLoadClass::LoadKind::kJitTableAddress: {
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007172 Address address = Address::Absolute(CodeGeneratorX86::kPlaceholder32BitOffset);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007173 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007174 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007175 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00007176 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007177 break;
7178 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007179 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007180 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007181 LOG(FATAL) << "UNREACHABLE";
7182 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007183 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007184
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007185 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7186 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007187 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007188 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007189
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007190 if (generate_null_check) {
7191 __ testl(out, out);
7192 __ j(kEqual, slow_path->GetEntryLabel());
7193 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007194
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007195 if (cls->MustGenerateClinitCheck()) {
7196 GenerateClassInitializationCheck(slow_path, out);
7197 } else {
7198 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007199 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007200 }
7201}
7202
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007203void LocationsBuilderX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7204 InvokeRuntimeCallingConvention calling_convention;
7205 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7206 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
7207}
7208
7209void InstructionCodeGeneratorX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7210 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
7211}
7212
Orion Hodson18259d72018-04-12 11:18:23 +01007213void LocationsBuilderX86::VisitLoadMethodType(HLoadMethodType* load) {
7214 InvokeRuntimeCallingConvention calling_convention;
7215 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7216 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
7217}
7218
7219void InstructionCodeGeneratorX86::VisitLoadMethodType(HLoadMethodType* load) {
7220 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
7221}
7222
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007223void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
7224 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007225 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007226 locations->SetInAt(0, Location::RequiresRegister());
7227 if (check->HasUses()) {
7228 locations->SetOut(Location::SameAsFirstInput());
7229 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007230 // Rely on the type initialization to save everything we need.
7231 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007232}
7233
7234void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007235 // We assume the class to not be null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007236 SlowPathCode* slow_path =
7237 new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(check->GetLoadClass(), check);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007238 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00007239 GenerateClassInitializationCheck(slow_path,
7240 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007241}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007242
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007243void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07007244 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00007245 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
7246 const size_t status_byte_offset =
7247 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
Vladimir Markobf121912019-06-04 13:49:05 +01007248 constexpr uint32_t shifted_visibly_initialized_value =
7249 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << (status_lsb_position % kBitsPerByte);
Vladimir Markodc682aa2018-01-04 18:42:57 +00007250
Vladimir Markobf121912019-06-04 13:49:05 +01007251 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_visibly_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00007252 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007253 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007254}
7255
Vladimir Marko175e7862018-03-27 09:03:13 +00007256void InstructionCodeGeneratorX86::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
7257 Register temp) {
7258 uint32_t path_to_root = check->GetBitstringPathToRoot();
7259 uint32_t mask = check->GetBitstringMask();
7260 DCHECK(IsPowerOfTwo(mask + 1));
7261 size_t mask_bits = WhichPowerOf2(mask + 1);
7262
7263 if (mask_bits == 16u) {
7264 // Compare the bitstring in memory.
7265 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
7266 } else {
7267 // /* uint32_t */ temp = temp->status_
7268 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
7269 // Compare the bitstring bits using SUB.
7270 __ subl(temp, Immediate(path_to_root));
7271 // Shift out bits that do not contribute to the comparison.
7272 __ shll(temp, Immediate(32u - mask_bits));
7273 }
7274}
7275
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007276HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
7277 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007278 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007279 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007280 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007281 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01007282 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007283 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007284 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007285 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01007286 DCHECK(GetCompilerOptions().IsJitCompiler());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007287 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007288 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007289 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007290 }
7291 return desired_string_load_kind;
7292}
7293
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007294void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007295 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007296 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007297 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007298 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007299 load_kind == HLoadString::LoadKind::kBootImageRelRo ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00007300 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007301 locations->SetInAt(0, Location::RequiresRegister());
7302 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007303 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007304 locations->SetOut(Location::RegisterLocation(EAX));
7305 } else {
7306 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007307 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7308 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007309 // Rely on the pResolveString to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007310 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007311 } else {
7312 // For non-Baker read barrier we have a temp-clobbering call.
7313 }
7314 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007315 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007316}
7317
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007318Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01007319 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007320 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007321 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007322 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007323 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007324 PatchInfo<Label>* info = &jit_string_patches_.back();
7325 return &info->label;
7326}
7327
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007328// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7329// move.
7330void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01007331 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007332 Location out_loc = locations->Out();
7333 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007334
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007335 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007336 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007337 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7338 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007339 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007340 __ leal(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007341 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007342 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007343 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007344 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007345 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7346 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007347 __ movl(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007348 codegen_->RecordBootImageRelRoPatch(load->InputAt(0)->AsX86ComputeBaseMethodAddress(),
Vladimir Markode91ca92020-10-27 13:41:40 +00007349 CodeGenerator::GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007350 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007351 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007352 case HLoadString::LoadKind::kBssEntry: {
7353 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007354 Address address = Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007355 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007356 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007357 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007358 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007359 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007360 codegen_->AddSlowPath(slow_path);
7361 __ testl(out, out);
7362 __ j(kEqual, slow_path->GetEntryLabel());
7363 __ Bind(slow_path->GetExitLabel());
7364 return;
7365 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007366 case HLoadString::LoadKind::kJitBootImageAddress: {
7367 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
7368 DCHECK_NE(address, 0u);
7369 __ movl(out, Immediate(address));
7370 return;
7371 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007372 case HLoadString::LoadKind::kJitTableAddress: {
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007373 Address address = Address::Absolute(CodeGeneratorX86::kPlaceholder32BitOffset);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007374 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007375 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007376 // /* GcRoot<mirror::String> */ out = *address
7377 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
7378 return;
7379 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007380 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007381 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007382 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007383
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007384 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007385 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007386 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007387 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007388 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7389 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007390}
7391
David Brazdilcb1c0552015-08-04 16:22:25 +01007392static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007393 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01007394}
7395
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007396void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
7397 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007398 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007399 locations->SetOut(Location::RequiresRegister());
7400}
7401
7402void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01007403 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
7404}
7405
7406void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007407 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01007408}
7409
7410void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7411 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007412}
7413
7414void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007415 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7416 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007417 InvokeRuntimeCallingConvention calling_convention;
7418 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7419}
7420
7421void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007422 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007423 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007424}
7425
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007426// Temp is used for read barrier.
7427static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7428 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00007429 !kUseBakerReadBarrier &&
7430 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00007431 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007432 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7433 return 1;
7434 }
7435 return 0;
7436}
7437
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007438// Interface case has 2 temps, one for holding the number of interfaces, one for the current
7439// interface pointer, the current interface is compared in memory.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007440// The other checks have one temp for loading the object's class.
7441static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007442 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007443 return 2;
7444 }
7445 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007446}
7447
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007448void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007449 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007450 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01007451 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007452 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007453 case TypeCheckKind::kExactCheck:
7454 case TypeCheckKind::kAbstractClassCheck:
7455 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007456 case TypeCheckKind::kArrayObjectCheck: {
7457 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7458 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7459 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007460 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007461 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007462 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007463 case TypeCheckKind::kUnresolvedCheck:
7464 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007465 call_kind = LocationSummary::kCallOnSlowPath;
7466 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007467 case TypeCheckKind::kBitstringCheck:
7468 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007469 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007470
Vladimir Markoca6fff82017-10-03 14:49:14 +01007471 LocationSummary* locations =
7472 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01007473 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007474 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007475 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007476 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007477 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7478 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7479 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7480 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7481 } else {
7482 locations->SetInAt(1, Location::Any());
7483 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007484 // Note that TypeCheckSlowPathX86 uses this "out" register too.
7485 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007486 // When read barriers are enabled, we need a temporary register for some cases.
7487 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007488}
7489
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007490void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007491 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007492 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007493 Location obj_loc = locations->InAt(0);
7494 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007495 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007496 Location out_loc = locations->Out();
7497 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007498 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7499 DCHECK_LE(num_temps, 1u);
7500 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007501 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007502 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7503 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7504 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07007505 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007506 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007507
7508 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007509 // Avoid null check if we know obj is not null.
7510 if (instruction->MustDoNullCheck()) {
7511 __ testl(obj, obj);
7512 __ j(kEqual, &zero);
7513 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007514
Roland Levillain7c1559a2015-12-15 10:55:36 +00007515 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007516 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007517 ReadBarrierOption read_barrier_option =
7518 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007519 // /* HeapReference<Class> */ out = obj->klass_
7520 GenerateReferenceLoadTwoRegisters(instruction,
7521 out_loc,
7522 obj_loc,
7523 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007524 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007525 if (cls.IsRegister()) {
7526 __ cmpl(out, cls.AsRegister<Register>());
7527 } else {
7528 DCHECK(cls.IsStackSlot()) << cls;
7529 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7530 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007531
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007532 // Classes must be equal for the instanceof to succeed.
7533 __ j(kNotEqual, &zero);
7534 __ movl(out, Immediate(1));
7535 __ jmp(&done);
7536 break;
7537 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007538
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007539 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007540 ReadBarrierOption read_barrier_option =
7541 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007542 // /* HeapReference<Class> */ out = obj->klass_
7543 GenerateReferenceLoadTwoRegisters(instruction,
7544 out_loc,
7545 obj_loc,
7546 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007547 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007548 // If the class is abstract, we eagerly fetch the super class of the
7549 // object to avoid doing a comparison we know will fail.
7550 NearLabel loop;
7551 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007552 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007553 GenerateReferenceLoadOneRegister(instruction,
7554 out_loc,
7555 super_offset,
7556 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007557 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007558 __ testl(out, out);
7559 // If `out` is null, we use it for the result, and jump to `done`.
7560 __ j(kEqual, &done);
7561 if (cls.IsRegister()) {
7562 __ cmpl(out, cls.AsRegister<Register>());
7563 } else {
7564 DCHECK(cls.IsStackSlot()) << cls;
7565 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7566 }
7567 __ j(kNotEqual, &loop);
7568 __ movl(out, Immediate(1));
7569 if (zero.IsLinked()) {
7570 __ jmp(&done);
7571 }
7572 break;
7573 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007574
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007575 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007576 ReadBarrierOption read_barrier_option =
7577 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007578 // /* HeapReference<Class> */ out = obj->klass_
7579 GenerateReferenceLoadTwoRegisters(instruction,
7580 out_loc,
7581 obj_loc,
7582 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007583 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007584 // Walk over the class hierarchy to find a match.
7585 NearLabel loop, success;
7586 __ Bind(&loop);
7587 if (cls.IsRegister()) {
7588 __ cmpl(out, cls.AsRegister<Register>());
7589 } else {
7590 DCHECK(cls.IsStackSlot()) << cls;
7591 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7592 }
7593 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007594 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007595 GenerateReferenceLoadOneRegister(instruction,
7596 out_loc,
7597 super_offset,
7598 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007599 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007600 __ testl(out, out);
7601 __ j(kNotEqual, &loop);
7602 // If `out` is null, we use it for the result, and jump to `done`.
7603 __ jmp(&done);
7604 __ Bind(&success);
7605 __ movl(out, Immediate(1));
7606 if (zero.IsLinked()) {
7607 __ jmp(&done);
7608 }
7609 break;
7610 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007611
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007612 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007613 ReadBarrierOption read_barrier_option =
7614 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007615 // /* HeapReference<Class> */ out = obj->klass_
7616 GenerateReferenceLoadTwoRegisters(instruction,
7617 out_loc,
7618 obj_loc,
7619 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007620 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007621 // Do an exact check.
7622 NearLabel exact_check;
7623 if (cls.IsRegister()) {
7624 __ cmpl(out, cls.AsRegister<Register>());
7625 } else {
7626 DCHECK(cls.IsStackSlot()) << cls;
7627 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7628 }
7629 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007630 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007631 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007632 GenerateReferenceLoadOneRegister(instruction,
7633 out_loc,
7634 component_offset,
7635 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007636 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007637 __ testl(out, out);
7638 // If `out` is null, we use it for the result, and jump to `done`.
7639 __ j(kEqual, &done);
7640 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
7641 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007642 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007643 __ movl(out, Immediate(1));
7644 __ jmp(&done);
7645 break;
7646 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007647
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007648 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007649 // No read barrier since the slow path will retry upon failure.
7650 // /* HeapReference<Class> */ out = obj->klass_
7651 GenerateReferenceLoadTwoRegisters(instruction,
7652 out_loc,
7653 obj_loc,
7654 class_offset,
7655 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007656 if (cls.IsRegister()) {
7657 __ cmpl(out, cls.AsRegister<Register>());
7658 } else {
7659 DCHECK(cls.IsStackSlot()) << cls;
7660 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7661 }
7662 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007663 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007664 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007665 codegen_->AddSlowPath(slow_path);
7666 __ j(kNotEqual, slow_path->GetEntryLabel());
7667 __ movl(out, Immediate(1));
7668 if (zero.IsLinked()) {
7669 __ jmp(&done);
7670 }
7671 break;
7672 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007673
Calin Juravle98893e12015-10-02 21:05:03 +01007674 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007675 case TypeCheckKind::kInterfaceCheck: {
7676 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007677 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00007678 // cases.
7679 //
7680 // We cannot directly call the InstanceofNonTrivial runtime
7681 // entry point without resorting to a type checking slow path
7682 // here (i.e. by calling InvokeRuntime directly), as it would
7683 // require to assign fixed registers for the inputs of this
7684 // HInstanceOf instruction (following the runtime calling
7685 // convention), which might be cluttered by the potential first
7686 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007687 //
7688 // TODO: Introduce a new runtime entry point taking the object
7689 // to test (instead of its class) as argument, and let it deal
7690 // with the read barrier issues. This will let us refactor this
7691 // case of the `switch` code as it was previously (with a direct
7692 // call to the runtime not using a type checking slow path).
7693 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007694 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007695 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007696 instruction, /* is_fatal= */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007697 codegen_->AddSlowPath(slow_path);
7698 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007699 if (zero.IsLinked()) {
7700 __ jmp(&done);
7701 }
7702 break;
7703 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007704
7705 case TypeCheckKind::kBitstringCheck: {
7706 // /* HeapReference<Class> */ temp = obj->klass_
7707 GenerateReferenceLoadTwoRegisters(instruction,
7708 out_loc,
7709 obj_loc,
7710 class_offset,
7711 kWithoutReadBarrier);
7712
7713 GenerateBitstringTypeCheckCompare(instruction, out);
7714 __ j(kNotEqual, &zero);
7715 __ movl(out, Immediate(1));
7716 __ jmp(&done);
7717 break;
7718 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007719 }
7720
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007721 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007722 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007723 __ xorl(out, out);
7724 }
7725
7726 if (done.IsLinked()) {
7727 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007728 }
7729
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007730 if (slow_path != nullptr) {
7731 __ Bind(slow_path->GetExitLabel());
7732 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007733}
7734
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007735void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007736 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007737 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007738 LocationSummary* locations =
7739 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007740 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007741 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7742 // Require a register for the interface check since there is a loop that compares the class to
7743 // a memory address.
7744 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007745 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7746 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7747 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7748 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007749 } else {
7750 locations->SetInAt(1, Location::Any());
7751 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007752 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007753 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
7754}
7755
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007756void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007757 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007758 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007759 Location obj_loc = locations->InAt(0);
7760 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007761 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007762 Location temp_loc = locations->GetTemp(0);
7763 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007764 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7765 DCHECK_GE(num_temps, 1u);
7766 DCHECK_LE(num_temps, 2u);
7767 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7768 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7769 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7770 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7771 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7772 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7773 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7774 const uint32_t object_array_data_offset =
7775 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007776
Vladimir Marko87584542017-12-12 17:47:52 +00007777 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007778 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007779 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7780 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007781 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007782
Roland Levillain0d5a2812015-11-13 10:07:31 +00007783 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007784 // Avoid null check if we know obj is not null.
7785 if (instruction->MustDoNullCheck()) {
7786 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007787 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007788 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007789
Roland Levillain0d5a2812015-11-13 10:07:31 +00007790 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007791 case TypeCheckKind::kExactCheck:
7792 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007793 // /* HeapReference<Class> */ temp = obj->klass_
7794 GenerateReferenceLoadTwoRegisters(instruction,
7795 temp_loc,
7796 obj_loc,
7797 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007798 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007799
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007800 if (cls.IsRegister()) {
7801 __ cmpl(temp, cls.AsRegister<Register>());
7802 } else {
7803 DCHECK(cls.IsStackSlot()) << cls;
7804 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7805 }
7806 // Jump to slow path for throwing the exception or doing a
7807 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007808 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007809 break;
7810 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007811
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007812 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007813 // /* HeapReference<Class> */ temp = obj->klass_
7814 GenerateReferenceLoadTwoRegisters(instruction,
7815 temp_loc,
7816 obj_loc,
7817 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007818 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007819
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007820 // If the class is abstract, we eagerly fetch the super class of the
7821 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007822 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007823 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007824 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007825 GenerateReferenceLoadOneRegister(instruction,
7826 temp_loc,
7827 super_offset,
7828 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007829 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007830
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007831 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7832 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007833 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007834 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007835
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007836 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007837 if (cls.IsRegister()) {
7838 __ cmpl(temp, cls.AsRegister<Register>());
7839 } else {
7840 DCHECK(cls.IsStackSlot()) << cls;
7841 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7842 }
7843 __ j(kNotEqual, &loop);
7844 break;
7845 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007846
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007847 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007848 // /* HeapReference<Class> */ temp = obj->klass_
7849 GenerateReferenceLoadTwoRegisters(instruction,
7850 temp_loc,
7851 obj_loc,
7852 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007853 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007854
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007855 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007856 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007857 __ Bind(&loop);
7858 if (cls.IsRegister()) {
7859 __ cmpl(temp, cls.AsRegister<Register>());
7860 } else {
7861 DCHECK(cls.IsStackSlot()) << cls;
7862 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7863 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007864 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007865
Roland Levillain0d5a2812015-11-13 10:07:31 +00007866 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007867 GenerateReferenceLoadOneRegister(instruction,
7868 temp_loc,
7869 super_offset,
7870 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007871 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007872
7873 // If the class reference currently in `temp` is not null, jump
7874 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007875 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007876 __ j(kNotZero, &loop);
7877 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007878 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007879 break;
7880 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007881
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007882 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007883 // /* HeapReference<Class> */ temp = obj->klass_
7884 GenerateReferenceLoadTwoRegisters(instruction,
7885 temp_loc,
7886 obj_loc,
7887 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007888 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007889
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007890 // Do an exact check.
7891 if (cls.IsRegister()) {
7892 __ cmpl(temp, cls.AsRegister<Register>());
7893 } else {
7894 DCHECK(cls.IsStackSlot()) << cls;
7895 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7896 }
7897 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007898
7899 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007900 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007901 GenerateReferenceLoadOneRegister(instruction,
7902 temp_loc,
7903 component_offset,
7904 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007905 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007906
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007907 // If the component type is null (i.e. the object not an array), jump to the slow path to
7908 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007909 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007910 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007911
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007912 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007913 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007914 break;
7915 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007916
Calin Juravle98893e12015-10-02 21:05:03 +01007917 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007918 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007919 // We cannot directly call the CheckCast runtime entry point
7920 // without resorting to a type checking slow path here (i.e. by
7921 // calling InvokeRuntime directly), as it would require to
7922 // assign fixed registers for the inputs of this HInstanceOf
7923 // instruction (following the runtime calling convention), which
7924 // might be cluttered by the potential first read barrier
7925 // emission at the beginning of this method.
7926 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007927 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007928
7929 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007930 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
7931 // We can not get false positives by doing this.
7932 // /* HeapReference<Class> */ temp = obj->klass_
7933 GenerateReferenceLoadTwoRegisters(instruction,
7934 temp_loc,
7935 obj_loc,
7936 class_offset,
7937 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007938
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007939 // /* HeapReference<Class> */ temp = temp->iftable_
7940 GenerateReferenceLoadTwoRegisters(instruction,
7941 temp_loc,
7942 temp_loc,
7943 iftable_offset,
7944 kWithoutReadBarrier);
7945 // Iftable is never null.
7946 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
7947 // Maybe poison the `cls` for direct comparison with memory.
7948 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
7949 // Loop through the iftable and check if any class matches.
7950 NearLabel start_loop;
7951 __ Bind(&start_loop);
7952 // Need to subtract first to handle the empty array case.
7953 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
7954 __ j(kNegative, type_check_slow_path->GetEntryLabel());
7955 // Go to next interface if the classes do not match.
7956 __ cmpl(cls.AsRegister<Register>(),
7957 CodeGeneratorX86::ArrayAddress(temp,
7958 maybe_temp2_loc,
7959 TIMES_4,
7960 object_array_data_offset));
7961 __ j(kNotEqual, &start_loop);
7962 // If `cls` was poisoned above, unpoison it.
7963 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007964 break;
7965 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007966
7967 case TypeCheckKind::kBitstringCheck: {
7968 // /* HeapReference<Class> */ temp = obj->klass_
7969 GenerateReferenceLoadTwoRegisters(instruction,
7970 temp_loc,
7971 obj_loc,
7972 class_offset,
7973 kWithoutReadBarrier);
7974
7975 GenerateBitstringTypeCheckCompare(instruction, temp);
7976 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
7977 break;
7978 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007979 }
7980 __ Bind(&done);
7981
Roland Levillain0d5a2812015-11-13 10:07:31 +00007982 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007983}
7984
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007985void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007986 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7987 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007988 InvokeRuntimeCallingConvention calling_convention;
7989 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7990}
7991
7992void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007993 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
7994 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01007995 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01007996 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007997 if (instruction->IsEnter()) {
7998 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7999 } else {
8000 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8001 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008002}
8003
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05308004void LocationsBuilderX86::VisitX86AndNot(HX86AndNot* instruction) {
8005 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
8006 DCHECK(DataType::IsIntOrLongType(instruction->GetType())) << instruction->GetType();
8007 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
8008 locations->SetInAt(0, Location::RequiresRegister());
8009 locations->SetInAt(1, Location::RequiresRegister());
8010 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8011}
8012
8013void InstructionCodeGeneratorX86::VisitX86AndNot(HX86AndNot* instruction) {
8014 LocationSummary* locations = instruction->GetLocations();
8015 Location first = locations->InAt(0);
8016 Location second = locations->InAt(1);
8017 Location dest = locations->Out();
8018 if (instruction->GetResultType() == DataType::Type::kInt32) {
8019 __ andn(dest.AsRegister<Register>(),
8020 first.AsRegister<Register>(),
8021 second.AsRegister<Register>());
8022 } else {
8023 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
8024 __ andn(dest.AsRegisterPairLow<Register>(),
8025 first.AsRegisterPairLow<Register>(),
8026 second.AsRegisterPairLow<Register>());
8027 __ andn(dest.AsRegisterPairHigh<Register>(),
8028 first.AsRegisterPairHigh<Register>(),
8029 second.AsRegisterPairHigh<Register>());
8030 }
8031}
8032
8033void LocationsBuilderX86::VisitX86MaskOrResetLeastSetBit(HX86MaskOrResetLeastSetBit* instruction) {
8034 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
8035 DCHECK(instruction->GetType() == DataType::Type::kInt32) << instruction->GetType();
8036 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
8037 locations->SetInAt(0, Location::RequiresRegister());
8038 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8039}
8040
8041void InstructionCodeGeneratorX86::VisitX86MaskOrResetLeastSetBit(
8042 HX86MaskOrResetLeastSetBit* instruction) {
8043 LocationSummary* locations = instruction->GetLocations();
8044 Location src = locations->InAt(0);
8045 Location dest = locations->Out();
8046 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
8047 switch (instruction->GetOpKind()) {
8048 case HInstruction::kAnd:
8049 __ blsr(dest.AsRegister<Register>(), src.AsRegister<Register>());
8050 break;
8051 case HInstruction::kXor:
8052 __ blsmsk(dest.AsRegister<Register>(), src.AsRegister<Register>());
8053 break;
8054 default:
8055 LOG(FATAL) << "Unreachable";
8056 }
8057}
8058
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008059void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
8060void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
8061void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
8062
8063void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
8064 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008065 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008066 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
8067 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008068 locations->SetInAt(0, Location::RequiresRegister());
8069 locations->SetInAt(1, Location::Any());
8070 locations->SetOut(Location::SameAsFirstInput());
8071}
8072
8073void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
8074 HandleBitwiseOperation(instruction);
8075}
8076
8077void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
8078 HandleBitwiseOperation(instruction);
8079}
8080
8081void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
8082 HandleBitwiseOperation(instruction);
8083}
8084
8085void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
8086 LocationSummary* locations = instruction->GetLocations();
8087 Location first = locations->InAt(0);
8088 Location second = locations->InAt(1);
8089 DCHECK(first.Equals(locations->Out()));
8090
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008091 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008092 if (second.IsRegister()) {
8093 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00008094 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008095 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00008096 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008097 } else {
8098 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00008099 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008100 }
8101 } else if (second.IsConstant()) {
8102 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00008103 __ andl(first.AsRegister<Register>(),
8104 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008105 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00008106 __ orl(first.AsRegister<Register>(),
8107 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008108 } else {
8109 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00008110 __ xorl(first.AsRegister<Register>(),
8111 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008112 }
8113 } else {
8114 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00008115 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008116 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00008117 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008118 } else {
8119 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00008120 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008121 }
8122 }
8123 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008124 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008125 if (second.IsRegisterPair()) {
8126 if (instruction->IsAnd()) {
8127 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
8128 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
8129 } else if (instruction->IsOr()) {
8130 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
8131 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
8132 } else {
8133 DCHECK(instruction->IsXor());
8134 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
8135 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
8136 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008137 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008138 if (instruction->IsAnd()) {
8139 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
8140 __ andl(first.AsRegisterPairHigh<Register>(),
8141 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
8142 } else if (instruction->IsOr()) {
8143 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
8144 __ orl(first.AsRegisterPairHigh<Register>(),
8145 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
8146 } else {
8147 DCHECK(instruction->IsXor());
8148 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
8149 __ xorl(first.AsRegisterPairHigh<Register>(),
8150 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
8151 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008152 } else {
8153 DCHECK(second.IsConstant()) << second;
8154 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008155 int32_t low_value = Low32Bits(value);
8156 int32_t high_value = High32Bits(value);
8157 Immediate low(low_value);
8158 Immediate high(high_value);
8159 Register first_low = first.AsRegisterPairLow<Register>();
8160 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008161 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008162 if (low_value == 0) {
8163 __ xorl(first_low, first_low);
8164 } else if (low_value != -1) {
8165 __ andl(first_low, low);
8166 }
8167 if (high_value == 0) {
8168 __ xorl(first_high, first_high);
8169 } else if (high_value != -1) {
8170 __ andl(first_high, high);
8171 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008172 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008173 if (low_value != 0) {
8174 __ orl(first_low, low);
8175 }
8176 if (high_value != 0) {
8177 __ orl(first_high, high);
8178 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008179 } else {
8180 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008181 if (low_value != 0) {
8182 __ xorl(first_low, low);
8183 }
8184 if (high_value != 0) {
8185 __ xorl(first_high, high);
8186 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008187 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008188 }
8189 }
8190}
8191
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008192void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
8193 HInstruction* instruction,
8194 Location out,
8195 uint32_t offset,
8196 Location maybe_temp,
8197 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008198 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008199 if (read_barrier_option == kWithReadBarrier) {
8200 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008201 if (kUseBakerReadBarrier) {
8202 // Load with fast path based Baker's read barrier.
8203 // /* HeapReference<Object> */ out = *(out + offset)
8204 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008205 instruction, out, out_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008206 } else {
8207 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008208 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00008209 // in the following move operation, as we will need it for the
8210 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00008211 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008212 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008213 // /* HeapReference<Object> */ out = *(out + offset)
8214 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008215 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008216 }
8217 } else {
8218 // Plain load with no read barrier.
8219 // /* HeapReference<Object> */ out = *(out + offset)
8220 __ movl(out_reg, Address(out_reg, offset));
8221 __ MaybeUnpoisonHeapReference(out_reg);
8222 }
8223}
8224
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008225void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
8226 HInstruction* instruction,
8227 Location out,
8228 Location obj,
8229 uint32_t offset,
8230 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008231 Register out_reg = out.AsRegister<Register>();
8232 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008233 if (read_barrier_option == kWithReadBarrier) {
8234 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008235 if (kUseBakerReadBarrier) {
8236 // Load with fast path based Baker's read barrier.
8237 // /* HeapReference<Object> */ out = *(obj + offset)
8238 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008239 instruction, out, obj_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008240 } else {
8241 // Load with slow path based read barrier.
8242 // /* HeapReference<Object> */ out = *(obj + offset)
8243 __ movl(out_reg, Address(obj_reg, offset));
8244 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8245 }
8246 } else {
8247 // Plain load with no read barrier.
8248 // /* HeapReference<Object> */ out = *(obj + offset)
8249 __ movl(out_reg, Address(obj_reg, offset));
8250 __ MaybeUnpoisonHeapReference(out_reg);
8251 }
8252}
8253
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008254void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
8255 HInstruction* instruction,
8256 Location root,
8257 const Address& address,
8258 Label* fixup_label,
8259 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008260 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008261 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07008262 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008263 if (kUseBakerReadBarrier) {
8264 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
8265 // Baker's read barrier are used:
8266 //
Roland Levillaind966ce72017-02-09 16:20:14 +00008267 // root = obj.field;
8268 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8269 // if (temp != null) {
8270 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00008271 // }
8272
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008273 // /* GcRoot<mirror::Object> */ root = *address
8274 __ movl(root_reg, address);
8275 if (fixup_label != nullptr) {
8276 __ Bind(fixup_label);
8277 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008278 static_assert(
8279 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8280 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8281 "have different sizes.");
8282 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8283 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8284 "have different sizes.");
8285
Vladimir Marko953437b2016-08-24 08:30:46 +00008286 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008287 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008288 instruction, root, /* unpoison_ref_before_marking= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008289 codegen_->AddSlowPath(slow_path);
8290
Roland Levillaind966ce72017-02-09 16:20:14 +00008291 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
8292 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01008293 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00008294 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
8295 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008296 __ j(kNotEqual, slow_path->GetEntryLabel());
8297 __ Bind(slow_path->GetExitLabel());
8298 } else {
8299 // GC root loaded through a slow path for read barriers other
8300 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008301 // /* GcRoot<mirror::Object>* */ root = address
8302 __ leal(root_reg, address);
8303 if (fixup_label != nullptr) {
8304 __ Bind(fixup_label);
8305 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008306 // /* mirror::Object* */ root = root->Read()
8307 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8308 }
8309 } else {
8310 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008311 // /* GcRoot<mirror::Object> */ root = *address
8312 __ movl(root_reg, address);
8313 if (fixup_label != nullptr) {
8314 __ Bind(fixup_label);
8315 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00008316 // Note that GC roots are not affected by heap poisoning, thus we
8317 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008318 }
8319}
8320
8321void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8322 Location ref,
8323 Register obj,
8324 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00008325 bool needs_null_check) {
8326 DCHECK(kEmitCompilerReadBarrier);
8327 DCHECK(kUseBakerReadBarrier);
8328
8329 // /* HeapReference<Object> */ ref = *(obj + offset)
8330 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00008331 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008332}
8333
8334void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8335 Location ref,
8336 Register obj,
8337 uint32_t data_offset,
8338 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00008339 bool needs_null_check) {
8340 DCHECK(kEmitCompilerReadBarrier);
8341 DCHECK(kUseBakerReadBarrier);
8342
Roland Levillain3d312422016-06-23 13:53:42 +01008343 static_assert(
8344 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8345 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00008346 // /* HeapReference<Object> */ ref =
8347 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008348 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00008349 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008350}
8351
8352void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8353 Location ref,
8354 Register obj,
8355 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008356 bool needs_null_check,
8357 bool always_update_field,
8358 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008359 DCHECK(kEmitCompilerReadBarrier);
8360 DCHECK(kUseBakerReadBarrier);
8361
8362 // In slow path based read barriers, the read barrier call is
8363 // inserted after the original load. However, in fast path based
8364 // Baker's read barriers, we need to perform the load of
8365 // mirror::Object::monitor_ *before* the original reference load.
8366 // This load-load ordering is required by the read barrier.
8367 // The fast path/slow path (for Baker's algorithm) should look like:
8368 //
8369 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8370 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8371 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008372 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00008373 // if (is_gray) {
8374 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
8375 // }
8376 //
8377 // Note: the original implementation in ReadBarrier::Barrier is
8378 // slightly more complex as:
8379 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00008380 // the high-bits of rb_state, which are expected to be all zeroes
8381 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
8382 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008383 // - it performs additional checks that we do not do here for
8384 // performance reasons.
8385
8386 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00008387 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
8388
Vladimir Marko953437b2016-08-24 08:30:46 +00008389 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01008390 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008391 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00008392 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
8393 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
8394 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
8395
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008396 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00008397 // ref = ReadBarrier::Mark(ref);
8398 // At this point, just do the "if" and make sure that flags are preserved until the branch.
8399 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00008400 if (needs_null_check) {
8401 MaybeRecordImplicitNullCheck(instruction);
8402 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008403
8404 // Load fence to prevent load-load reordering.
8405 // Note that this is a no-op, thanks to the x86 memory model.
8406 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
8407
8408 // The actual reference load.
8409 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00008410 __ movl(ref_reg, src); // Flags are unaffected.
8411
8412 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
8413 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008414 SlowPathCode* slow_path;
8415 if (always_update_field) {
8416 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01008417 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008418 instruction, ref, obj, src, /* unpoison_ref_before_marking= */ true, *temp);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008419 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008420 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008421 instruction, ref, /* unpoison_ref_before_marking= */ true);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008422 }
Vladimir Marko953437b2016-08-24 08:30:46 +00008423 AddSlowPath(slow_path);
8424
8425 // We have done the "if" of the gray bit check above, now branch based on the flags.
8426 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00008427
8428 // Object* ref = ref_addr->AsMirrorPtr()
8429 __ MaybeUnpoisonHeapReference(ref_reg);
8430
Roland Levillain7c1559a2015-12-15 10:55:36 +00008431 __ Bind(slow_path->GetExitLabel());
8432}
8433
8434void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
8435 Location out,
8436 Location ref,
8437 Location obj,
8438 uint32_t offset,
8439 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008440 DCHECK(kEmitCompilerReadBarrier);
8441
Roland Levillain7c1559a2015-12-15 10:55:36 +00008442 // Insert a slow path based read barrier *after* the reference load.
8443 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008444 // If heap poisoning is enabled, the unpoisoning of the loaded
8445 // reference will be carried out by the runtime within the slow
8446 // path.
8447 //
8448 // Note that `ref` currently does not get unpoisoned (when heap
8449 // poisoning is enabled), which is alright as the `ref` argument is
8450 // not used by the artReadBarrierSlow entry point.
8451 //
8452 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008453 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00008454 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
8455 AddSlowPath(slow_path);
8456
Roland Levillain0d5a2812015-11-13 10:07:31 +00008457 __ jmp(slow_path->GetEntryLabel());
8458 __ Bind(slow_path->GetExitLabel());
8459}
8460
Roland Levillain7c1559a2015-12-15 10:55:36 +00008461void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
8462 Location out,
8463 Location ref,
8464 Location obj,
8465 uint32_t offset,
8466 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008467 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008468 // Baker's read barriers shall be handled by the fast path
8469 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
8470 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008471 // If heap poisoning is enabled, unpoisoning will be taken care of
8472 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008473 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008474 } else if (kPoisonHeapReferences) {
8475 __ UnpoisonHeapReference(out.AsRegister<Register>());
8476 }
8477}
8478
Roland Levillain7c1559a2015-12-15 10:55:36 +00008479void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8480 Location out,
8481 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008482 DCHECK(kEmitCompilerReadBarrier);
8483
Roland Levillain7c1559a2015-12-15 10:55:36 +00008484 // Insert a slow path based read barrier *after* the GC root load.
8485 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008486 // Note that GC roots are not affected by heap poisoning, so we do
8487 // not need to do anything special for this here.
8488 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008489 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008490 AddSlowPath(slow_path);
8491
Roland Levillain0d5a2812015-11-13 10:07:31 +00008492 __ jmp(slow_path->GetEntryLabel());
8493 __ Bind(slow_path->GetExitLabel());
8494}
8495
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008496void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008497 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008498 LOG(FATAL) << "Unreachable";
8499}
8500
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008501void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008502 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008503 LOG(FATAL) << "Unreachable";
8504}
8505
Mark Mendellfe57faa2015-09-18 09:26:15 -04008506// Simple implementation of packed switch - generate cascaded compare/jumps.
8507void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8508 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008509 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04008510 locations->SetInAt(0, Location::RequiresRegister());
8511}
8512
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008513void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
8514 int32_t lower_bound,
8515 uint32_t num_entries,
8516 HBasicBlock* switch_block,
8517 HBasicBlock* default_block) {
8518 // Figure out the correct compare values and jump conditions.
8519 // Handle the first compare/branch as a special case because it might
8520 // jump to the default case.
8521 DCHECK_GT(num_entries, 2u);
8522 Condition first_condition;
8523 uint32_t index;
8524 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
8525 if (lower_bound != 0) {
8526 first_condition = kLess;
8527 __ cmpl(value_reg, Immediate(lower_bound));
8528 __ j(first_condition, codegen_->GetLabelOf(default_block));
8529 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008530
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008531 index = 1;
8532 } else {
8533 // Handle all the compare/jumps below.
8534 first_condition = kBelow;
8535 index = 0;
8536 }
8537
8538 // Handle the rest of the compare/jumps.
8539 for (; index + 1 < num_entries; index += 2) {
8540 int32_t compare_to_value = lower_bound + index + 1;
8541 __ cmpl(value_reg, Immediate(compare_to_value));
8542 // Jump to successors[index] if value < case_value[index].
8543 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
8544 // Jump to successors[index + 1] if value == case_value[index + 1].
8545 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
8546 }
8547
8548 if (index != num_entries) {
8549 // There are an odd number of entries. Handle the last one.
8550 DCHECK_EQ(index + 1, num_entries);
8551 __ cmpl(value_reg, Immediate(lower_bound + index));
8552 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008553 }
8554
8555 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008556 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
8557 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008558 }
8559}
8560
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008561void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8562 int32_t lower_bound = switch_instr->GetStartValue();
8563 uint32_t num_entries = switch_instr->GetNumEntries();
8564 LocationSummary* locations = switch_instr->GetLocations();
8565 Register value_reg = locations->InAt(0).AsRegister<Register>();
8566
8567 GenPackedSwitchWithCompares(value_reg,
8568 lower_bound,
8569 num_entries,
8570 switch_instr->GetBlock(),
8571 switch_instr->GetDefaultBlock());
8572}
8573
Mark Mendell805b3b52015-09-18 14:10:29 -04008574void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8575 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008576 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04008577 locations->SetInAt(0, Location::RequiresRegister());
8578
8579 // Constant area pointer.
8580 locations->SetInAt(1, Location::RequiresRegister());
8581
8582 // And the temporary we need.
8583 locations->AddTemp(Location::RequiresRegister());
8584}
8585
8586void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8587 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008588 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04008589 LocationSummary* locations = switch_instr->GetLocations();
8590 Register value_reg = locations->InAt(0).AsRegister<Register>();
8591 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8592
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008593 if (num_entries <= kPackedSwitchJumpTableThreshold) {
8594 GenPackedSwitchWithCompares(value_reg,
8595 lower_bound,
8596 num_entries,
8597 switch_instr->GetBlock(),
8598 default_block);
8599 return;
8600 }
8601
Mark Mendell805b3b52015-09-18 14:10:29 -04008602 // Optimizing has a jump area.
8603 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
8604 Register constant_area = locations->InAt(1).AsRegister<Register>();
8605
8606 // Remove the bias, if needed.
8607 if (lower_bound != 0) {
8608 __ leal(temp_reg, Address(value_reg, -lower_bound));
8609 value_reg = temp_reg;
8610 }
8611
8612 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008613 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04008614 __ cmpl(value_reg, Immediate(num_entries - 1));
8615 __ j(kAbove, codegen_->GetLabelOf(default_block));
8616
8617 // We are in the range of the table.
8618 // Load (target-constant_area) from the jump table, indexing by the value.
8619 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
8620
8621 // Compute the actual target address by adding in constant_area.
8622 __ addl(temp_reg, constant_area);
8623
8624 // And jump.
8625 __ jmp(temp_reg);
8626}
8627
Mark Mendell0616ae02015-04-17 12:49:27 -04008628void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
8629 HX86ComputeBaseMethodAddress* insn) {
8630 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008631 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008632 locations->SetOut(Location::RequiresRegister());
8633}
8634
8635void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
8636 HX86ComputeBaseMethodAddress* insn) {
8637 LocationSummary* locations = insn->GetLocations();
8638 Register reg = locations->Out().AsRegister<Register>();
8639
8640 // Generate call to next instruction.
8641 Label next_instruction;
8642 __ call(&next_instruction);
8643 __ Bind(&next_instruction);
8644
8645 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008646 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04008647
8648 // Grab the return address off the stack.
8649 __ popl(reg);
8650}
8651
8652void LocationsBuilderX86::VisitX86LoadFromConstantTable(
8653 HX86LoadFromConstantTable* insn) {
8654 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008655 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008656
8657 locations->SetInAt(0, Location::RequiresRegister());
8658 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
8659
8660 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00008661 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008662 return;
8663 }
8664
8665 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008666 case DataType::Type::kFloat32:
8667 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008668 locations->SetOut(Location::RequiresFpuRegister());
8669 break;
8670
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008671 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008672 locations->SetOut(Location::RequiresRegister());
8673 break;
8674
8675 default:
8676 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8677 }
8678}
8679
8680void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00008681 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008682 return;
8683 }
8684
8685 LocationSummary* locations = insn->GetLocations();
8686 Location out = locations->Out();
8687 Register const_area = locations->InAt(0).AsRegister<Register>();
8688 HConstant *value = insn->GetConstant();
8689
8690 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008691 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008692 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008693 codegen_->LiteralFloatAddress(
8694 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008695 break;
8696
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008697 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008698 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008699 codegen_->LiteralDoubleAddress(
8700 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008701 break;
8702
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008703 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008704 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008705 codegen_->LiteralInt32Address(
8706 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008707 break;
8708
8709 default:
8710 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8711 }
8712}
8713
Mark Mendell0616ae02015-04-17 12:49:27 -04008714/**
8715 * Class to handle late fixup of offsets into constant area.
8716 */
Vladimir Marko5233f932015-09-29 19:01:15 +01008717class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04008718 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008719 RIPFixup(CodeGeneratorX86& codegen,
8720 HX86ComputeBaseMethodAddress* base_method_address,
8721 size_t offset)
8722 : codegen_(&codegen),
8723 base_method_address_(base_method_address),
8724 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008725
8726 protected:
8727 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
8728
8729 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008730 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008731
8732 private:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008733 void Process(const MemoryRegion& region, int pos) override {
Mark Mendell0616ae02015-04-17 12:49:27 -04008734 // Patch the correct offset for the instruction. The place to patch is the
8735 // last 4 bytes of the instruction.
8736 // The value to patch is the distance from the offset in the constant area
8737 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04008738 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008739 int32_t relative_position =
8740 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04008741
8742 // Patch in the right value.
8743 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
8744 }
8745
Mark Mendell0616ae02015-04-17 12:49:27 -04008746 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04008747 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008748};
8749
Mark Mendell805b3b52015-09-18 14:10:29 -04008750/**
8751 * Class to handle late fixup of offsets to a jump table that will be created in the
8752 * constant area.
8753 */
8754class JumpTableRIPFixup : public RIPFixup {
8755 public:
8756 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008757 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
8758 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008759
8760 void CreateJumpTable() {
8761 X86Assembler* assembler = codegen_->GetAssembler();
8762
8763 // Ensure that the reference to the jump table has the correct offset.
8764 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
8765 SetOffset(offset_in_constant_table);
8766
8767 // The label values in the jump table are computed relative to the
8768 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008769 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04008770
8771 // Populate the jump table with the correct values for the jump table.
8772 int32_t num_entries = switch_instr_->GetNumEntries();
8773 HBasicBlock* block = switch_instr_->GetBlock();
8774 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
8775 // The value that we want is the target offset - the position of the table.
8776 for (int32_t i = 0; i < num_entries; i++) {
8777 HBasicBlock* b = successors[i];
8778 Label* l = codegen_->GetLabelOf(b);
8779 DCHECK(l->IsBound());
8780 int32_t offset_to_block = l->Position() - relative_offset;
8781 assembler->AppendInt32(offset_to_block);
8782 }
8783 }
8784
8785 private:
8786 const HX86PackedSwitch* switch_instr_;
8787};
8788
8789void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
8790 // Generate the constant area if needed.
8791 X86Assembler* assembler = GetAssembler();
jaishank20d1c942019-03-08 15:08:17 +05308792
Mark Mendell805b3b52015-09-18 14:10:29 -04008793 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
8794 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
8795 // byte values.
8796 assembler->Align(4, 0);
8797 constant_area_start_ = assembler->CodeSize();
8798
8799 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008800 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04008801 jump_table->CreateJumpTable();
8802 }
8803
8804 // And now add the constant area to the generated code.
8805 assembler->AddConstantArea();
8806 }
8807
8808 // And finish up.
8809 CodeGenerator::Finalize(allocator);
8810}
8811
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008812Address CodeGeneratorX86::LiteralDoubleAddress(double v,
8813 HX86ComputeBaseMethodAddress* method_base,
8814 Register reg) {
8815 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008816 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008817 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008818}
8819
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008820Address CodeGeneratorX86::LiteralFloatAddress(float v,
8821 HX86ComputeBaseMethodAddress* method_base,
8822 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008823 AssemblerFixup* fixup =
8824 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008825 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008826}
8827
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008828Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
8829 HX86ComputeBaseMethodAddress* method_base,
8830 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008831 AssemblerFixup* fixup =
8832 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008833 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008834}
8835
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008836Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
8837 HX86ComputeBaseMethodAddress* method_base,
8838 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008839 AssemblerFixup* fixup =
8840 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008841 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008842}
8843
Aart Bika19616e2016-02-01 18:57:58 -08008844void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
8845 if (value == 0) {
8846 __ xorl(dest, dest);
8847 } else {
8848 __ movl(dest, Immediate(value));
8849 }
8850}
8851
8852void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
8853 if (value == 0) {
8854 __ testl(dest, dest);
8855 } else {
8856 __ cmpl(dest, Immediate(value));
8857 }
8858}
8859
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008860void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
8861 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07008862 GenerateIntCompare(lhs_reg, rhs);
8863}
8864
8865void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008866 if (rhs.IsConstant()) {
8867 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07008868 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008869 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07008870 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008871 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07008872 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008873 }
8874}
8875
8876Address CodeGeneratorX86::ArrayAddress(Register obj,
8877 Location index,
8878 ScaleFactor scale,
8879 uint32_t data_offset) {
8880 return index.IsConstant() ?
8881 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
8882 Address(obj, index.AsRegister<Register>(), scale, data_offset);
8883}
8884
Mark Mendell805b3b52015-09-18 14:10:29 -04008885Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
8886 Register reg,
8887 Register value) {
8888 // Create a fixup to be used to create and address the jump table.
8889 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008890 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04008891
8892 // We have to populate the jump tables.
8893 fixups_to_jump_tables_.push_back(table_fixup);
8894
8895 // We want a scaled address, as we are extracting the correct offset from the table.
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008896 return Address(reg, value, TIMES_4, kPlaceholder32BitOffset, table_fixup);
Mark Mendell805b3b52015-09-18 14:10:29 -04008897}
8898
Andreas Gampe85b62f22015-09-09 13:15:38 -07008899// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008900void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07008901 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008902 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008903 return;
8904 }
8905
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008906 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008907
8908 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
8909 if (target.Equals(return_loc)) {
8910 return;
8911 }
8912
8913 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8914 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008915 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008916 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008917 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
8918 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008919 GetMoveResolver()->EmitNativeCode(&parallel_move);
8920 } else {
8921 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01008922 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07008923 parallel_move.AddMove(return_loc, target, type, nullptr);
8924 GetMoveResolver()->EmitNativeCode(&parallel_move);
8925 }
8926}
8927
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008928void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
8929 const uint8_t* roots_data,
8930 const PatchInfo<Label>& info,
8931 uint64_t index_in_table) const {
8932 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
8933 uintptr_t address =
8934 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
Andreas Gampec55bb392018-09-21 00:02:02 +00008935 using unaligned_uint32_t __attribute__((__aligned__(1))) = uint32_t;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008936 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
8937 dchecked_integral_cast<uint32_t>(address);
8938}
8939
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008940void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8941 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008942 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008943 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008944 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008945 }
8946
8947 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008948 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008949 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008950 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008951 }
8952}
8953
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008954void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8955 ATTRIBUTE_UNUSED) {
8956 LOG(FATAL) << "Unreachable";
8957}
8958
8959void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8960 ATTRIBUTE_UNUSED) {
8961 LOG(FATAL) << "Unreachable";
8962}
8963
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05308964bool LocationsBuilderX86::CpuHasAvxFeatureFlag() {
8965 return codegen_->GetInstructionSetFeatures().HasAVX();
8966}
8967bool LocationsBuilderX86::CpuHasAvx2FeatureFlag() {
8968 return codegen_->GetInstructionSetFeatures().HasAVX2();
8969}
8970bool InstructionCodeGeneratorX86::CpuHasAvxFeatureFlag() {
8971 return codegen_->GetInstructionSetFeatures().HasAVX();
8972}
8973bool InstructionCodeGeneratorX86::CpuHasAvx2FeatureFlag() {
8974 return codegen_->GetInstructionSetFeatures().HasAVX2();
8975}
8976
Roland Levillain4d027112015-07-01 15:41:14 +01008977#undef __
8978
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008979} // namespace x86
8980} // namespace art