blob: f520519edc50c2dcb1be38c979f7c177dd9ababe [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 Marko9d479252018-07-24 11:35:20 +0100294 x86_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
295 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100296 // If we also must_do_clinit, the resolved type is now in the correct register.
297 } else {
298 DCHECK(must_do_clinit);
299 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
300 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), source);
301 }
302 if (must_do_clinit) {
303 x86_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
304 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Roland Levillain888d0672015-11-23 18:53:50 +0000305 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000306
307 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000308 if (out.IsValid()) {
309 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
310 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000311 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000312 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000313 __ jmp(GetExitLabel());
314 }
315
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100316 const char* GetDescription() const override { return "LoadClassSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100317
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000318 private:
319 // The class this slow path will load.
320 HLoadClass* const cls_;
321
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000322 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
323};
324
Andreas Gampe85b62f22015-09-09 13:15:38 -0700325class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000326 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000327 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000328 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000329
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100330 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000331 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000332 DCHECK(instruction_->IsCheckCast()
333 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000334
335 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
336 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000337
Vladimir Markoe619f6c2017-12-12 16:00:01 +0000338 if (kPoisonHeapReferences &&
339 instruction_->IsCheckCast() &&
340 instruction_->AsCheckCast()->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) {
341 // First, unpoison the `cls` reference that was poisoned for direct memory comparison.
342 __ UnpoisonHeapReference(locations->InAt(1).AsRegister<Register>());
343 }
344
Vladimir Marko87584542017-12-12 17:47:52 +0000345 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000346 SaveLiveRegisters(codegen, locations);
347 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000348
349 // We're moving two locations to locations that could overlap, so we need a parallel
350 // move resolver.
351 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800352 x86_codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800353 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100354 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800355 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800356 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100357 DataType::Type::kReference);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000358 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100359 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100360 instruction_,
361 instruction_->GetDexPc(),
362 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800363 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000364 } else {
365 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800366 x86_codegen->InvokeRuntime(kQuickCheckInstanceOf,
367 instruction_,
368 instruction_->GetDexPc(),
369 this);
370 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000371 }
372
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000373 if (!is_fatal_) {
374 if (instruction_->IsInstanceOf()) {
375 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
376 }
377 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000378
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000379 __ jmp(GetExitLabel());
380 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000381 }
382
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100383 const char* GetDescription() const override { return "TypeCheckSlowPathX86"; }
384 bool IsFatal() const override { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100385
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000386 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000387 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000388
389 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
390};
391
Andreas Gampe85b62f22015-09-09 13:15:38 -0700392class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700393 public:
Aart Bik42249c32016-01-07 15:33:50 -0800394 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000395 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700396
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100397 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +0100398 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700399 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100400 LocationSummary* locations = instruction_->GetLocations();
401 SaveLiveRegisters(codegen, locations);
402 InvokeRuntimeCallingConvention calling_convention;
403 x86_codegen->Load32BitValue(
404 calling_convention.GetRegisterAt(0),
405 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100406 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100407 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700408 }
409
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100410 const char* GetDescription() const override { return "DeoptimizationSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100411
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700412 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700413 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
414};
415
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100416class ArraySetSlowPathX86 : public SlowPathCode {
417 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000418 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100419
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100420 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100421 LocationSummary* locations = instruction_->GetLocations();
422 __ Bind(GetEntryLabel());
423 SaveLiveRegisters(codegen, locations);
424
425 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100426 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100427 parallel_move.AddMove(
428 locations->InAt(0),
429 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100430 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100431 nullptr);
432 parallel_move.AddMove(
433 locations->InAt(1),
434 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100435 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100436 nullptr);
437 parallel_move.AddMove(
438 locations->InAt(2),
439 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100440 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100441 nullptr);
442 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
443
444 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100445 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000446 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100447 RestoreLiveRegisters(codegen, locations);
448 __ jmp(GetExitLabel());
449 }
450
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100451 const char* GetDescription() const override { return "ArraySetSlowPathX86"; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100452
453 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100454 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
455};
456
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100457// Slow path marking an object reference `ref` during a read
458// barrier. The field `obj.field` in the object `obj` holding this
459// reference does not get updated by this slow path after marking (see
460// ReadBarrierMarkAndUpdateFieldSlowPathX86 below for that).
461//
462// This means that after the execution of this slow path, `ref` will
463// always be up-to-date, but `obj.field` may not; i.e., after the
464// flip, `ref` will be a to-space reference, but `obj.field` will
465// probably still be a from-space reference (unless it gets updated by
466// another thread, or if another thread installed another object
467// reference (different from `ref`) in `obj.field`).
Roland Levillain7c1559a2015-12-15 10:55:36 +0000468class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
469 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100470 ReadBarrierMarkSlowPathX86(HInstruction* instruction,
471 Location ref,
472 bool unpoison_ref_before_marking)
473 : SlowPathCode(instruction),
474 ref_(ref),
475 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000476 DCHECK(kEmitCompilerReadBarrier);
477 }
478
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100479 const char* GetDescription() const override { return "ReadBarrierMarkSlowPathX86"; }
Roland Levillain7c1559a2015-12-15 10:55:36 +0000480
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100481 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000482 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100483 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000484 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100485 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000486 DCHECK(instruction_->IsInstanceFieldGet() ||
487 instruction_->IsStaticFieldGet() ||
488 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100489 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000490 instruction_->IsLoadClass() ||
491 instruction_->IsLoadString() ||
492 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100493 instruction_->IsCheckCast() ||
Andra Danciu1ca6f322020-08-12 08:58:07 +0000494 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000495 << "Unexpected instruction in read barrier marking slow path: "
496 << instruction_->DebugName();
497
498 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100499 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000500 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100501 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000502 }
Roland Levillain4359e612016-07-20 11:32:19 +0100503 // No need to save live registers; it's taken care of by the
504 // entrypoint. Also, there is no need to update the stack mask,
505 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000506 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100507 DCHECK_NE(ref_reg, ESP);
508 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100509 // "Compact" slow path, saving two moves.
510 //
511 // Instead of using the standard runtime calling convention (input
512 // and output in EAX):
513 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100514 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100515 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100516 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100517 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100518 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100519 // of a dedicated entrypoint:
520 //
521 // rX <- ReadBarrierMarkRegX(rX)
522 //
Roland Levillain97c46462017-05-11 14:04:03 +0100523 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100524 // This runtime call does not require a stack map.
525 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000526 __ jmp(GetExitLabel());
527 }
528
529 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100530 // The location (register) of the marked object reference.
531 const Location ref_;
532 // Should the reference in `ref_` be unpoisoned prior to marking it?
533 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000534
535 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
536};
537
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100538// Slow path marking an object reference `ref` during a read barrier,
539// and if needed, atomically updating the field `obj.field` in the
540// object `obj` holding this reference after marking (contrary to
541// ReadBarrierMarkSlowPathX86 above, which never tries to update
542// `obj.field`).
543//
544// This means that after the execution of this slow path, both `ref`
545// and `obj.field` will be up-to-date; i.e., after the flip, both will
546// hold the same to-space reference (unless another thread installed
547// another object reference (different from `ref`) in `obj.field`).
548class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
549 public:
550 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
551 Location ref,
552 Register obj,
553 const Address& field_addr,
554 bool unpoison_ref_before_marking,
555 Register temp)
556 : SlowPathCode(instruction),
557 ref_(ref),
558 obj_(obj),
559 field_addr_(field_addr),
560 unpoison_ref_before_marking_(unpoison_ref_before_marking),
561 temp_(temp) {
562 DCHECK(kEmitCompilerReadBarrier);
563 }
564
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100565 const char* GetDescription() const override { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100566
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100567 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100568 LocationSummary* locations = instruction_->GetLocations();
569 Register ref_reg = ref_.AsRegister<Register>();
570 DCHECK(locations->CanCall());
571 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
572 // This slow path is only used by the UnsafeCASObject intrinsic.
Andra Danciu5e13d452020-09-08 14:35:09 +0000573 DCHECK((instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100574 << "Unexpected instruction in read barrier marking and field updating slow path: "
575 << instruction_->DebugName();
576 DCHECK(instruction_->GetLocations()->Intrinsified());
Andra Danciu52d2c0c2020-09-15 14:27:21 +0000577 Intrinsics intrinsic = instruction_->AsInvoke()->GetIntrinsic();
578 static constexpr auto kVarHandleCAS = mirror::VarHandle::AccessModeTemplate::kCompareAndSet;
Andra Danciu9dfb1a92020-09-22 13:27:18 +0000579 static constexpr auto kVarHandleGetAndSet =
580 mirror::VarHandle::AccessModeTemplate::kGetAndUpdate;
Andra Danciu52d2c0c2020-09-15 14:27:21 +0000581 DCHECK(intrinsic == Intrinsics::kUnsafeCASObject ||
Andra Danciu9dfb1a92020-09-22 13:27:18 +0000582 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) == kVarHandleCAS ||
583 mirror::VarHandle::GetAccessModeTemplateByIntrinsic(intrinsic) == kVarHandleGetAndSet);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100584
585 __ Bind(GetEntryLabel());
586 if (unpoison_ref_before_marking_) {
587 // Object* ref = ref_addr->AsMirrorPtr()
588 __ MaybeUnpoisonHeapReference(ref_reg);
589 }
590
591 // Save the old (unpoisoned) reference.
592 __ movl(temp_, ref_reg);
593
594 // No need to save live registers; it's taken care of by the
595 // entrypoint. Also, there is no need to update the stack mask,
596 // as this runtime call will not trigger a garbage collection.
597 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
598 DCHECK_NE(ref_reg, ESP);
599 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
600 // "Compact" slow path, saving two moves.
601 //
602 // Instead of using the standard runtime calling convention (input
603 // and output in EAX):
604 //
605 // EAX <- ref
606 // EAX <- ReadBarrierMark(EAX)
607 // ref <- EAX
608 //
609 // we just use rX (the register containing `ref`) as input and output
610 // of a dedicated entrypoint:
611 //
612 // rX <- ReadBarrierMarkRegX(rX)
613 //
Roland Levillain97c46462017-05-11 14:04:03 +0100614 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100615 // This runtime call does not require a stack map.
616 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
617
618 // If the new reference is different from the old reference,
619 // update the field in the holder (`*field_addr`).
620 //
621 // Note that this field could also hold a different object, if
622 // another thread had concurrently changed it. In that case, the
623 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
624 // operation below would abort the CAS, leaving the field as-is.
625 NearLabel done;
626 __ cmpl(temp_, ref_reg);
627 __ j(kEqual, &done);
628
629 // Update the the holder's field atomically. This may fail if
630 // mutator updates before us, but it's OK. This is achieved
631 // using a strong compare-and-set (CAS) operation with relaxed
632 // memory synchronization ordering, where the expected value is
633 // the old reference and the desired value is the new reference.
634 // This operation is implemented with a 32-bit LOCK CMPXLCHG
635 // instruction, which requires the expected value (the old
636 // reference) to be in EAX. Save EAX beforehand, and move the
637 // expected value (stored in `temp_`) into EAX.
638 __ pushl(EAX);
639 __ movl(EAX, temp_);
640
641 // Convenience aliases.
642 Register base = obj_;
643 Register expected = EAX;
644 Register value = ref_reg;
645
646 bool base_equals_value = (base == value);
647 if (kPoisonHeapReferences) {
648 if (base_equals_value) {
649 // If `base` and `value` are the same register location, move
650 // `value` to a temporary register. This way, poisoning
651 // `value` won't invalidate `base`.
652 value = temp_;
653 __ movl(value, base);
654 }
655
656 // Check that the register allocator did not assign the location
657 // of `expected` (EAX) to `value` nor to `base`, so that heap
658 // poisoning (when enabled) works as intended below.
659 // - If `value` were equal to `expected`, both references would
660 // be poisoned twice, meaning they would not be poisoned at
661 // all, as heap poisoning uses address negation.
662 // - If `base` were equal to `expected`, poisoning `expected`
663 // would invalidate `base`.
664 DCHECK_NE(value, expected);
665 DCHECK_NE(base, expected);
666
667 __ PoisonHeapReference(expected);
668 __ PoisonHeapReference(value);
669 }
670
671 __ LockCmpxchgl(field_addr_, value);
672
673 // If heap poisoning is enabled, we need to unpoison the values
674 // that were poisoned earlier.
675 if (kPoisonHeapReferences) {
676 if (base_equals_value) {
677 // `value` has been moved to a temporary register, no need
678 // to unpoison it.
679 } else {
680 __ UnpoisonHeapReference(value);
681 }
682 // No need to unpoison `expected` (EAX), as it is be overwritten below.
683 }
684
685 // Restore EAX.
686 __ popl(EAX);
687
688 __ Bind(&done);
689 __ jmp(GetExitLabel());
690 }
691
692 private:
693 // The location (register) of the marked object reference.
694 const Location ref_;
695 // The register containing the object holding the marked object reference field.
696 const Register obj_;
697 // The address of the marked reference field. The base of this address must be `obj_`.
698 const Address field_addr_;
699
700 // Should the reference in `ref_` be unpoisoned prior to marking it?
701 const bool unpoison_ref_before_marking_;
702
703 const Register temp_;
704
705 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
706};
707
Roland Levillain0d5a2812015-11-13 10:07:31 +0000708// Slow path generating a read barrier for a heap reference.
709class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
710 public:
711 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
712 Location out,
713 Location ref,
714 Location obj,
715 uint32_t offset,
716 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000717 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000718 out_(out),
719 ref_(ref),
720 obj_(obj),
721 offset_(offset),
722 index_(index) {
723 DCHECK(kEmitCompilerReadBarrier);
724 // If `obj` is equal to `out` or `ref`, it means the initial object
725 // has been overwritten by (or after) the heap object reference load
726 // to be instrumented, e.g.:
727 //
728 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000729 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000730 //
731 // In that case, we have lost the information about the original
732 // object, and the emitted read barrier cannot work properly.
733 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
734 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
735 }
736
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100737 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000738 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
739 LocationSummary* locations = instruction_->GetLocations();
740 Register reg_out = out_.AsRegister<Register>();
741 DCHECK(locations->CanCall());
742 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100743 DCHECK(instruction_->IsInstanceFieldGet() ||
744 instruction_->IsStaticFieldGet() ||
745 instruction_->IsArrayGet() ||
746 instruction_->IsInstanceOf() ||
747 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700748 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000749 << "Unexpected instruction in read barrier for heap reference slow path: "
750 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000751
752 __ Bind(GetEntryLabel());
753 SaveLiveRegisters(codegen, locations);
754
755 // We may have to change the index's value, but as `index_` is a
756 // constant member (like other "inputs" of this slow path),
757 // introduce a copy of it, `index`.
758 Location index = index_;
759 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100760 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000761 if (instruction_->IsArrayGet()) {
762 // Compute the actual memory offset and store it in `index`.
763 Register index_reg = index_.AsRegister<Register>();
764 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
765 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
766 // We are about to change the value of `index_reg` (see the
767 // calls to art::x86::X86Assembler::shll and
768 // art::x86::X86Assembler::AddImmediate below), but it has
769 // not been saved by the previous call to
770 // art::SlowPathCode::SaveLiveRegisters, as it is a
771 // callee-save register --
772 // art::SlowPathCode::SaveLiveRegisters does not consider
773 // callee-save registers, as it has been designed with the
774 // assumption that callee-save registers are supposed to be
775 // handled by the called function. So, as a callee-save
776 // register, `index_reg` _would_ eventually be saved onto
777 // the stack, but it would be too late: we would have
778 // changed its value earlier. Therefore, we manually save
779 // it here into another freely available register,
780 // `free_reg`, chosen of course among the caller-save
781 // registers (as a callee-save `free_reg` register would
782 // exhibit the same problem).
783 //
784 // Note we could have requested a temporary register from
785 // the register allocator instead; but we prefer not to, as
786 // this is a slow path, and we know we can find a
787 // caller-save register that is available.
788 Register free_reg = FindAvailableCallerSaveRegister(codegen);
789 __ movl(free_reg, index_reg);
790 index_reg = free_reg;
791 index = Location::RegisterLocation(index_reg);
792 } else {
793 // The initial register stored in `index_` has already been
794 // saved in the call to art::SlowPathCode::SaveLiveRegisters
795 // (as it is not a callee-save register), so we can freely
796 // use it.
797 }
798 // Shifting the index value contained in `index_reg` by the scale
799 // factor (2) cannot overflow in practice, as the runtime is
800 // unable to allocate object arrays with a size larger than
801 // 2^26 - 1 (that is, 2^28 - 4 bytes).
802 __ shll(index_reg, Immediate(TIMES_4));
803 static_assert(
804 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
805 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
806 __ AddImmediate(index_reg, Immediate(offset_));
807 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100808 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
809 // intrinsics, `index_` is not shifted by a scale factor of 2
810 // (as in the case of ArrayGet), as it is actually an offset
811 // to an object field within an object.
812 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000813 DCHECK(instruction_->GetLocations()->Intrinsified());
814 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
815 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
816 << instruction_->AsInvoke()->GetIntrinsic();
817 DCHECK_EQ(offset_, 0U);
818 DCHECK(index_.IsRegisterPair());
819 // UnsafeGet's offset location is a register pair, the low
820 // part contains the correct offset.
821 index = index_.ToLow();
822 }
823 }
824
825 // We're moving two or three locations to locations that could
826 // overlap, so we need a parallel move resolver.
827 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100828 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000829 parallel_move.AddMove(ref_,
830 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100831 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000832 nullptr);
833 parallel_move.AddMove(obj_,
834 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100835 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000836 nullptr);
837 if (index.IsValid()) {
838 parallel_move.AddMove(index,
839 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100840 DataType::Type::kInt32,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000841 nullptr);
842 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
843 } else {
844 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
845 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
846 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100847 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000848 CheckEntrypointTypes<
849 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
850 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
851
852 RestoreLiveRegisters(codegen, locations);
853 __ jmp(GetExitLabel());
854 }
855
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100856 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathX86"; }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000857
858 private:
859 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
860 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
861 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
862 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
863 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
864 return static_cast<Register>(i);
865 }
866 }
867 // We shall never fail to find a free caller-save register, as
868 // there are more than two core caller-save registers on x86
869 // (meaning it is possible to find one which is different from
870 // `ref` and `obj`).
871 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
872 LOG(FATAL) << "Could not find a free caller-save register";
873 UNREACHABLE();
874 }
875
Roland Levillain0d5a2812015-11-13 10:07:31 +0000876 const Location out_;
877 const Location ref_;
878 const Location obj_;
879 const uint32_t offset_;
880 // An additional location containing an index to an array.
881 // Only used for HArrayGet and the UnsafeGetObject &
882 // UnsafeGetObjectVolatile intrinsics.
883 const Location index_;
884
885 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
886};
887
888// Slow path generating a read barrier for a GC root.
889class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
890 public:
891 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000892 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000893 DCHECK(kEmitCompilerReadBarrier);
894 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000895
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100896 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000897 LocationSummary* locations = instruction_->GetLocations();
898 Register reg_out = out_.AsRegister<Register>();
899 DCHECK(locations->CanCall());
900 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000901 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
902 << "Unexpected instruction in read barrier for GC root slow path: "
903 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000904
905 __ Bind(GetEntryLabel());
906 SaveLiveRegisters(codegen, locations);
907
908 InvokeRuntimeCallingConvention calling_convention;
909 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
910 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100911 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000912 instruction_,
913 instruction_->GetDexPc(),
914 this);
915 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
916 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
917
918 RestoreLiveRegisters(codegen, locations);
919 __ jmp(GetExitLabel());
920 }
921
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100922 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathX86"; }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000923
924 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000925 const Location out_;
926 const Location root_;
927
928 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
929};
930
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100931#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100932// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
933#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100934
Aart Bike9f37602015-10-09 11:15:55 -0700935inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700936 switch (cond) {
937 case kCondEQ: return kEqual;
938 case kCondNE: return kNotEqual;
939 case kCondLT: return kLess;
940 case kCondLE: return kLessEqual;
941 case kCondGT: return kGreater;
942 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700943 case kCondB: return kBelow;
944 case kCondBE: return kBelowEqual;
945 case kCondA: return kAbove;
946 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700947 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100948 LOG(FATAL) << "Unreachable";
949 UNREACHABLE();
950}
951
Aart Bike9f37602015-10-09 11:15:55 -0700952// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100953inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
954 switch (cond) {
955 case kCondEQ: return kEqual;
956 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700957 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100958 case kCondLT: return kBelow;
959 case kCondLE: return kBelowEqual;
960 case kCondGT: return kAbove;
961 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700962 // Unsigned remain unchanged.
963 case kCondB: return kBelow;
964 case kCondBE: return kBelowEqual;
965 case kCondA: return kAbove;
966 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100967 }
968 LOG(FATAL) << "Unreachable";
969 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700970}
971
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100972void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100973 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100974}
975
976void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100977 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100978}
979
Vladimir Markoa0431112018-06-25 09:32:54 +0100980const X86InstructionSetFeatures& CodeGeneratorX86::GetInstructionSetFeatures() const {
981 return *GetCompilerOptions().GetInstructionSetFeatures()->AsX86InstructionSetFeatures();
982}
983
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100984size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
985 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
986 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100987}
988
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100989size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
990 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
991 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100992}
993
Mark Mendell7c8d0092015-01-26 11:21:33 -0500994size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700995 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700996 __ movups(Address(ESP, stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -0700997 } else {
998 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
999 }
Artem Serov6a0b6572019-07-26 20:38:37 +01001000 return GetSlowPathFPWidth();
Mark Mendell7c8d0092015-01-26 11:21:33 -05001001}
1002
1003size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -07001004 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -07001005 __ movups(XmmRegister(reg_id), Address(ESP, stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -07001006 } else {
1007 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
1008 }
Artem Serov6a0b6572019-07-26 20:38:37 +01001009 return GetSlowPathFPWidth();
Mark Mendell7c8d0092015-01-26 11:21:33 -05001010}
1011
Calin Juravle175dc732015-08-25 15:42:32 +01001012void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
1013 HInstruction* instruction,
1014 uint32_t dex_pc,
1015 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001016 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001017 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
1018 if (EntrypointRequiresStackMap(entrypoint)) {
1019 RecordPcInfo(instruction, dex_pc, slow_path);
1020 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001021}
1022
Roland Levillaindec8f632016-07-22 17:10:06 +01001023void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1024 HInstruction* instruction,
1025 SlowPathCode* slow_path) {
1026 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001027 GenerateInvokeRuntime(entry_point_offset);
1028}
1029
1030void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001031 __ fs()->call(Address::Absolute(entry_point_offset));
1032}
1033
Mark Mendellfb8d2792015-03-31 22:16:59 -04001034CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001035 const CompilerOptions& compiler_options,
1036 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001037 : CodeGenerator(graph,
1038 kNumberOfCpuRegisters,
1039 kNumberOfXmmRegisters,
1040 kNumberOfRegisterPairs,
1041 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1042 arraysize(kCoreCalleeSaves))
1043 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001044 0,
1045 compiler_options,
1046 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001047 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001048 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001049 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001050 move_resolver_(graph->GetAllocator(), this),
1051 assembler_(graph->GetAllocator()),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001052 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1053 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1054 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1055 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001056 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001057 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +01001058 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001059 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1060 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001061 constant_area_start_(-1),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001062 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001063 method_address_offset_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001064 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001065 // Use a fake return address register to mimic Quick.
1066 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001067}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001068
David Brazdil58282f42016-01-14 12:45:10 +00001069void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001070 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001071 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001072}
1073
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001074InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001075 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001076 assembler_(codegen->GetAssembler()),
1077 codegen_(codegen) {}
1078
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001079static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001080 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001081}
1082
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001083void CodeGeneratorX86::MaybeIncrementHotness(bool is_frame_entry) {
1084 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1085 Register reg = EAX;
1086 if (is_frame_entry) {
1087 reg = kMethodRegisterArgument;
1088 } else {
1089 __ pushl(EAX);
Vladimir Markodec78172020-06-19 15:31:23 +01001090 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001091 __ movl(EAX, Address(ESP, kX86WordSize));
1092 }
1093 NearLabel overflow;
1094 __ cmpw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()),
1095 Immediate(ArtMethod::MaxCounter()));
1096 __ j(kEqual, &overflow);
1097 __ addw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()),
1098 Immediate(1));
1099 __ Bind(&overflow);
1100 if (!is_frame_entry) {
1101 __ popl(EAX);
Vladimir Markodec78172020-06-19 15:31:23 +01001102 __ cfi().AdjustCFAOffset(-4);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001103 }
1104 }
1105
1106 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffray095dc462020-08-17 16:40:28 +01001107 ScopedProfilingInfoUse spiu(
1108 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
1109 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001110 if (info != nullptr) {
1111 uint32_t address = reinterpret_cast32<uint32_t>(info);
1112 NearLabel done;
1113 if (HasEmptyFrame()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001114 CHECK(is_frame_entry);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001115 // Alignment
Vladimir Markodec78172020-06-19 15:31:23 +01001116 IncreaseFrame(8);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001117 // We need a temporary. The stub also expects the method at bottom of stack.
1118 __ pushl(EAX);
1119 __ cfi().AdjustCFAOffset(4);
1120 __ movl(EAX, Immediate(address));
1121 __ addw(Address(EAX, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()),
1122 Immediate(1));
Nicolas Geoffray8b8d93d2020-09-17 14:30:01 +01001123 __ andw(Address(EAX, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()),
1124 Immediate(interpreter::kTieredHotnessMask));
1125 __ j(kNotZero, &done);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001126 GenerateInvokeRuntime(
1127 GetThreadOffset<kX86PointerSize>(kQuickCompileOptimized).Int32Value());
1128 __ Bind(&done);
1129 // We don't strictly require to restore EAX, but this makes the generated
1130 // code easier to reason about.
1131 __ popl(EAX);
1132 __ cfi().AdjustCFAOffset(-4);
Vladimir Markodec78172020-06-19 15:31:23 +01001133 DecreaseFrame(8);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001134 } else {
1135 if (!RequiresCurrentMethod()) {
1136 CHECK(is_frame_entry);
1137 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1138 }
1139 // We need a temporary.
1140 __ pushl(EAX);
1141 __ cfi().AdjustCFAOffset(4);
1142 __ movl(EAX, Immediate(address));
1143 __ addw(Address(EAX, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()),
1144 Immediate(1));
1145 __ popl(EAX); // Put stack as expected before exiting or calling stub.
1146 __ cfi().AdjustCFAOffset(-4);
1147 __ j(kCarryClear, &done);
1148 GenerateInvokeRuntime(
1149 GetThreadOffset<kX86PointerSize>(kQuickCompileOptimized).Int32Value());
1150 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001151 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001152 }
1153 }
1154}
1155
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001156void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001157 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001158 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001159 bool skip_overflow_check =
1160 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001161 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001162
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001163 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001164 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86);
1165 __ testl(EAX, Address(ESP, -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001166 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001167 }
1168
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001169 if (!HasEmptyFrame()) {
1170 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1171 Register reg = kCoreCalleeSaves[i];
1172 if (allocated_registers_.ContainsCoreRegister(reg)) {
1173 __ pushl(reg);
1174 __ cfi().AdjustCFAOffset(kX86WordSize);
1175 __ cfi().RelOffset(DWARFReg(reg), 0);
1176 }
1177 }
Mark Mendell5f874182015-03-04 15:42:45 -05001178
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001179 int adjust = GetFrameSize() - FrameEntrySpillSize();
Vladimir Markodec78172020-06-19 15:31:23 +01001180 IncreaseFrame(adjust);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001181 // Save the current method if we need it. Note that we do not
1182 // do this in HCurrentMethod, as the instruction might have been removed
1183 // in the SSA graph.
1184 if (RequiresCurrentMethod()) {
1185 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1186 }
1187
1188 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1189 // Initialize should_deoptimize flag to 0.
1190 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
Mark Mendell5f874182015-03-04 15:42:45 -05001191 }
1192 }
1193
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001194 MaybeIncrementHotness(/* is_frame_entry= */ true);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001195}
1196
1197void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001198 __ cfi().RememberState();
1199 if (!HasEmptyFrame()) {
1200 int adjust = GetFrameSize() - FrameEntrySpillSize();
Vladimir Markodec78172020-06-19 15:31:23 +01001201 DecreaseFrame(adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001202
David Srbeckyc34dc932015-04-12 09:27:43 +01001203 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1204 Register reg = kCoreCalleeSaves[i];
1205 if (allocated_registers_.ContainsCoreRegister(reg)) {
1206 __ popl(reg);
1207 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1208 __ cfi().Restore(DWARFReg(reg));
1209 }
Mark Mendell5f874182015-03-04 15:42:45 -05001210 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001211 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001212 __ ret();
1213 __ cfi().RestoreState();
1214 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001215}
1216
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001217void CodeGeneratorX86::Bind(HBasicBlock* block) {
1218 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001219}
1220
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001221Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001222 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001223 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001224 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001225 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001226 case DataType::Type::kInt8:
1227 case DataType::Type::kUint16:
1228 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08001229 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001230 case DataType::Type::kInt32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001231 return Location::RegisterLocation(EAX);
1232
Aart Bik66c158e2018-01-31 12:55:04 -08001233 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001234 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001235 return Location::RegisterPairLocation(EAX, EDX);
1236
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001237 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001238 return Location::NoLocation();
1239
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001240 case DataType::Type::kFloat64:
1241 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001242 return Location::FpuRegisterLocation(XMM0);
1243 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001244
1245 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001246}
1247
1248Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1249 return Location::RegisterLocation(kMethodRegisterArgument);
1250}
1251
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001252Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001253 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001254 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001255 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001256 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001257 case DataType::Type::kInt8:
1258 case DataType::Type::kUint16:
1259 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001260 case DataType::Type::kInt32: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001261 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001262 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001263 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001264 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001265 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001266 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001267 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001268 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001269
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001270 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001271 uint32_t index = gp_index_;
1272 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001273 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001274 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001275 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1276 calling_convention.GetRegisterPairAt(index));
1277 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001278 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001279 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1280 }
1281 }
1282
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001283 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001284 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001285 stack_index_++;
1286 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1287 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1288 } else {
1289 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1290 }
1291 }
1292
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001293 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001294 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001295 stack_index_ += 2;
1296 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1297 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1298 } else {
1299 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001300 }
1301 }
1302
Aart Bik66c158e2018-01-31 12:55:04 -08001303 case DataType::Type::kUint32:
1304 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001305 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001306 LOG(FATAL) << "Unexpected parameter type " << type;
Elliott Hughesc1896c92018-11-29 11:33:18 -08001307 UNREACHABLE();
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001308 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001309 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001310}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001311
Vladimir Marko86c87522020-05-11 16:55:55 +01001312Location CriticalNativeCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
1313 DCHECK_NE(type, DataType::Type::kReference);
1314
1315 Location location;
1316 if (DataType::Is64BitType(type)) {
1317 location = Location::DoubleStackSlot(stack_offset_);
1318 stack_offset_ += 2 * kFramePointerSize;
1319 } else {
1320 location = Location::StackSlot(stack_offset_);
1321 stack_offset_ += kFramePointerSize;
1322 }
1323 if (for_register_allocation_) {
1324 location = Location::Any();
1325 }
1326 return location;
1327}
1328
1329Location CriticalNativeCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
1330 // We perform conversion to the managed ABI return register after the call if needed.
1331 InvokeDexCallingConventionVisitorX86 dex_calling_convention;
1332 return dex_calling_convention.GetReturnLocation(type);
1333}
1334
1335Location CriticalNativeCallingConventionVisitorX86::GetMethodLocation() const {
1336 // Pass the method in the hidden argument EAX.
1337 return Location::RegisterLocation(EAX);
1338}
1339
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001340void CodeGeneratorX86::Move32(Location destination, Location source) {
1341 if (source.Equals(destination)) {
1342 return;
1343 }
1344 if (destination.IsRegister()) {
1345 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001346 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001347 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001348 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Andra Danciu5e13d452020-09-08 14:35:09 +00001349 } else if (source.IsConstant()) {
1350 int32_t value = GetInt32ValueOf(source.GetConstant());
1351 __ movl(destination.AsRegister<Register>(), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001352 } else {
1353 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001354 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001355 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001356 } else if (destination.IsFpuRegister()) {
1357 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001358 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001359 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001360 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001361 } else {
1362 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001363 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001364 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001365 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001366 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001367 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001368 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001369 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001370 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001371 } else if (source.IsConstant()) {
1372 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001373 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001374 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001375 } else {
1376 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001377 __ pushl(Address(ESP, source.GetStackIndex()));
1378 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001379 }
1380 }
1381}
1382
1383void CodeGeneratorX86::Move64(Location destination, Location source) {
1384 if (source.Equals(destination)) {
1385 return;
1386 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001387 if (destination.IsRegisterPair()) {
1388 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001389 EmitParallelMoves(
1390 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1391 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001392 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001393 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001394 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001395 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001396 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001397 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1398 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1399 __ psrlq(src_reg, Immediate(32));
1400 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001401 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001402 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001403 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001404 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1405 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001406 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1407 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001408 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001409 if (source.IsFpuRegister()) {
1410 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1411 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001412 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001413 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001414 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Vladimir Markodec78172020-06-19 15:31:23 +01001415 // Push the 2 source registers to the stack.
1416 __ pushl(source.AsRegisterPairHigh<Register>());
1417 __ cfi().AdjustCFAOffset(elem_size);
1418 __ pushl(source.AsRegisterPairLow<Register>());
1419 __ cfi().AdjustCFAOffset(elem_size);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001420 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1421 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01001422 DecreaseFrame(2 * elem_size);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001423 } else {
1424 LOG(FATAL) << "Unimplemented";
1425 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001426 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001427 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001428 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001429 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001430 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001431 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001432 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001433 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001434 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001435 } else if (source.IsConstant()) {
1436 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001437 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1438 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001439 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001440 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1441 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001442 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001443 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001444 EmitParallelMoves(
1445 Location::StackSlot(source.GetStackIndex()),
1446 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001447 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001448 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001449 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001450 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001451 }
1452 }
1453}
1454
Andra Danciu1ca6f322020-08-12 08:58:07 +00001455static Address CreateAddress(Register base,
1456 Register index = Register::kNoRegister,
1457 ScaleFactor scale = TIMES_1,
1458 int32_t disp = 0) {
1459 if (index == Register::kNoRegister) {
1460 return Address(base, disp);
1461 }
1462
1463 return Address(base, index, scale, disp);
1464}
1465
Andra Danciud0f71f22020-09-17 09:00:15 +00001466void CodeGeneratorX86::LoadFromMemoryNoBarrier(DataType::Type dst_type,
1467 Location dst,
1468 Address src,
1469 XmmRegister temp,
1470 bool is_atomic_load) {
Andra Danciu1ca6f322020-08-12 08:58:07 +00001471 switch (dst_type) {
1472 case DataType::Type::kBool:
1473 case DataType::Type::kUint8:
1474 __ movzxb(dst.AsRegister<Register>(), src);
1475 break;
1476 case DataType::Type::kInt8:
1477 __ movsxb(dst.AsRegister<Register>(), src);
1478 break;
1479 case DataType::Type::kInt16:
1480 __ movsxw(dst.AsRegister<Register>(), src);
1481 break;
1482 case DataType::Type::kUint16:
1483 __ movzxw(dst.AsRegister<Register>(), src);
1484 break;
1485 case DataType::Type::kInt32:
Andra Danciu1ca6f322020-08-12 08:58:07 +00001486 __ movl(dst.AsRegister<Register>(), src);
1487 break;
Andra Danciud0f71f22020-09-17 09:00:15 +00001488 case DataType::Type::kInt64: {
1489 if (is_atomic_load) {
1490 __ movsd(temp, src);
1491 __ movd(dst.AsRegisterPairLow<Register>(), temp);
1492 __ psrlq(temp, Immediate(32));
1493 __ movd(dst.AsRegisterPairHigh<Register>(), temp);
1494 } else {
1495 DCHECK_NE(src.GetBaseRegister(), dst.AsRegisterPairLow<Register>());
1496 Address src_high = src.displaceBy(kX86WordSize);
1497 __ movl(dst.AsRegisterPairLow<Register>(), src);
1498 __ movl(dst.AsRegisterPairHigh<Register>(), src_high);
1499 }
Andra Danciu1ca6f322020-08-12 08:58:07 +00001500 break;
1501 }
1502 case DataType::Type::kFloat32:
1503 __ movss(dst.AsFpuRegister<XmmRegister>(), src);
1504 break;
1505 case DataType::Type::kFloat64:
1506 __ movsd(dst.AsFpuRegister<XmmRegister>(), src);
1507 break;
Andra Danciu1ca6f322020-08-12 08:58:07 +00001508 case DataType::Type::kReference:
Andra Danciud0f71f22020-09-17 09:00:15 +00001509 __ movl(dst.AsRegister<Register>(), src);
1510 __ MaybeUnpoisonHeapReference(dst.AsRegister<Register>());
1511 break;
1512 default:
Andra Danciu1ca6f322020-08-12 08:58:07 +00001513 LOG(FATAL) << "Unreachable type " << dst_type;
1514 }
1515}
1516
Andra Danciu73c31802020-09-01 13:17:05 +00001517void CodeGeneratorX86::MoveToMemory(DataType::Type src_type,
1518 Location src,
1519 Register dst_base,
1520 Register dst_index,
1521 ScaleFactor dst_scale,
1522 int32_t dst_disp) {
1523 DCHECK(dst_base != Register::kNoRegister);
1524 Address dst = CreateAddress(dst_base, dst_index, dst_scale, dst_disp);
1525
1526 switch (src_type) {
1527 case DataType::Type::kBool:
1528 case DataType::Type::kUint8:
1529 case DataType::Type::kInt8: {
1530 if (src.IsConstant()) {
1531 __ movb(dst, Immediate(CodeGenerator::GetInt8ValueOf(src.GetConstant())));
1532 } else {
1533 __ movb(dst, src.AsRegister<ByteRegister>());
1534 }
1535 break;
1536 }
1537 case DataType::Type::kUint16:
1538 case DataType::Type::kInt16: {
1539 if (src.IsConstant()) {
1540 __ movw(dst, Immediate(CodeGenerator::GetInt16ValueOf(src.GetConstant())));
1541 } else {
1542 __ movw(dst, src.AsRegister<Register>());
1543 }
1544 break;
1545 }
1546 case DataType::Type::kUint32:
1547 case DataType::Type::kInt32: {
1548 if (src.IsConstant()) {
1549 int32_t v = CodeGenerator::GetInt32ValueOf(src.GetConstant());
1550 __ movl(dst, Immediate(v));
1551 } else {
1552 __ movl(dst, src.AsRegister<Register>());
1553 }
1554 break;
1555 }
1556 case DataType::Type::kUint64:
1557 case DataType::Type::kInt64: {
1558 Address dst_next_4_bytes = CreateAddress(dst_base, dst_index, dst_scale, dst_disp + 4);
1559 if (src.IsConstant()) {
1560 int64_t v = CodeGenerator::GetInt64ValueOf(src.GetConstant());
1561 __ movl(dst, Immediate(Low32Bits(v)));
1562 __ movl(dst_next_4_bytes, Immediate(High32Bits(v)));
1563 } else {
1564 __ movl(dst, src.AsRegisterPairLow<Register>());
1565 __ movl(dst_next_4_bytes, src.AsRegisterPairHigh<Register>());
1566 }
1567 break;
1568 }
1569 case DataType::Type::kFloat32: {
1570 if (src.IsConstant()) {
1571 int32_t v = CodeGenerator::GetInt32ValueOf(src.GetConstant());
1572 __ movl(dst, Immediate(v));
1573 } else {
1574 __ movss(dst, src.AsFpuRegister<XmmRegister>());
1575 }
1576 break;
1577 }
1578 case DataType::Type::kFloat64: {
1579 Address dst_next_4_bytes = CreateAddress(dst_base, dst_index, dst_scale, dst_disp + 4);
1580 if (src.IsConstant()) {
1581 int64_t v = CodeGenerator::GetInt64ValueOf(src.GetConstant());
1582 __ movl(dst, Immediate(Low32Bits(v)));
1583 __ movl(dst_next_4_bytes, Immediate(High32Bits(v)));
1584 } else {
1585 __ movsd(dst, src.AsFpuRegister<XmmRegister>());
1586 }
1587 break;
1588 }
1589 case DataType::Type::kVoid:
1590 case DataType::Type::kReference:
1591 LOG(FATAL) << "Unreachable type " << src_type;
1592 }
1593}
1594
Calin Juravle175dc732015-08-25 15:42:32 +01001595void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1596 DCHECK(location.IsRegister());
1597 __ movl(location.AsRegister<Register>(), Immediate(value));
1598}
1599
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001600void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001601 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001602 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1603 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1604 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001605 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001606 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001607 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001608 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001609}
1610
1611void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1612 if (location.IsRegister()) {
1613 locations->AddTemp(location);
1614 } else if (location.IsRegisterPair()) {
1615 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1616 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1617 } else {
1618 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1619 }
1620}
1621
David Brazdilfc6a86a2015-06-26 10:33:45 +00001622void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001623 if (successor->IsExitBlock()) {
1624 DCHECK(got->GetPrevious()->AlwaysThrows());
1625 return; // no code needed
1626 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001627
1628 HBasicBlock* block = got->GetBlock();
1629 HInstruction* previous = got->GetPrevious();
1630
1631 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001632 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001633 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001634 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1635 return;
1636 }
1637
1638 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1639 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1640 }
1641 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001642 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001643 }
1644}
1645
David Brazdilfc6a86a2015-06-26 10:33:45 +00001646void LocationsBuilderX86::VisitGoto(HGoto* got) {
1647 got->SetLocations(nullptr);
1648}
1649
1650void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1651 HandleGoto(got, got->GetSuccessor());
1652}
1653
1654void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1655 try_boundary->SetLocations(nullptr);
1656}
1657
1658void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1659 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1660 if (!successor->IsExitBlock()) {
1661 HandleGoto(try_boundary, successor);
1662 }
1663}
1664
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001665void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001666 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001667}
1668
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001669void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001670}
1671
Mark Mendell152408f2015-12-31 12:28:50 -05001672template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001673void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001674 LabelType* true_label,
1675 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001676 if (cond->IsFPConditionTrueIfNaN()) {
1677 __ j(kUnordered, true_label);
1678 } else if (cond->IsFPConditionFalseIfNaN()) {
1679 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001680 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001681 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001682}
1683
Mark Mendell152408f2015-12-31 12:28:50 -05001684template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001685void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001686 LabelType* true_label,
1687 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001688 LocationSummary* locations = cond->GetLocations();
1689 Location left = locations->InAt(0);
1690 Location right = locations->InAt(1);
1691 IfCondition if_cond = cond->GetCondition();
1692
Mark Mendellc4701932015-04-10 13:18:51 -04001693 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001694 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001695 IfCondition true_high_cond = if_cond;
1696 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001697 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001698
1699 // Set the conditions for the test, remembering that == needs to be
1700 // decided using the low words.
1701 switch (if_cond) {
1702 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001703 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001704 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001705 break;
1706 case kCondLT:
1707 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001708 break;
1709 case kCondLE:
1710 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001711 break;
1712 case kCondGT:
1713 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001714 break;
1715 case kCondGE:
1716 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001717 break;
Aart Bike9f37602015-10-09 11:15:55 -07001718 case kCondB:
1719 false_high_cond = kCondA;
1720 break;
1721 case kCondBE:
1722 true_high_cond = kCondB;
1723 break;
1724 case kCondA:
1725 false_high_cond = kCondB;
1726 break;
1727 case kCondAE:
1728 true_high_cond = kCondA;
1729 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001730 }
1731
1732 if (right.IsConstant()) {
1733 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001734 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001735 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001736
Aart Bika19616e2016-02-01 18:57:58 -08001737 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001738 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001739 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001740 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001741 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001742 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001743 __ j(X86Condition(true_high_cond), true_label);
1744 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001745 }
1746 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001747 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001748 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001749 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001750 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001751
1752 __ cmpl(left_high, right_high);
1753 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001754 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001755 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001756 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001757 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001758 __ j(X86Condition(true_high_cond), true_label);
1759 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001760 }
1761 // Must be equal high, so compare the lows.
1762 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001763 } else {
1764 DCHECK(right.IsDoubleStackSlot());
1765 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1766 if (if_cond == kCondNE) {
1767 __ j(X86Condition(true_high_cond), true_label);
1768 } else if (if_cond == kCondEQ) {
1769 __ j(X86Condition(false_high_cond), false_label);
1770 } else {
1771 __ j(X86Condition(true_high_cond), true_label);
1772 __ j(X86Condition(false_high_cond), false_label);
1773 }
1774 // Must be equal high, so compare the lows.
1775 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001776 }
1777 // The last comparison might be unsigned.
1778 __ j(final_condition, true_label);
1779}
1780
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001781void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1782 Location rhs,
1783 HInstruction* insn,
1784 bool is_double) {
1785 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1786 if (is_double) {
1787 if (rhs.IsFpuRegister()) {
1788 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1789 } else if (const_area != nullptr) {
1790 DCHECK(const_area->IsEmittedAtUseSite());
1791 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1792 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001793 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1794 const_area->GetBaseMethodAddress(),
1795 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001796 } else {
1797 DCHECK(rhs.IsDoubleStackSlot());
1798 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1799 }
1800 } else {
1801 if (rhs.IsFpuRegister()) {
1802 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1803 } else if (const_area != nullptr) {
1804 DCHECK(const_area->IsEmittedAtUseSite());
1805 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1806 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001807 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1808 const_area->GetBaseMethodAddress(),
1809 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001810 } else {
1811 DCHECK(rhs.IsStackSlot());
1812 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1813 }
1814 }
1815}
1816
Mark Mendell152408f2015-12-31 12:28:50 -05001817template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001818void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001819 LabelType* true_target_in,
1820 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001821 // Generated branching requires both targets to be explicit. If either of the
1822 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001823 LabelType fallthrough_target;
1824 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1825 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001826
Mark Mendellc4701932015-04-10 13:18:51 -04001827 LocationSummary* locations = condition->GetLocations();
1828 Location left = locations->InAt(0);
1829 Location right = locations->InAt(1);
1830
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001831 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001832 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001833 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001834 GenerateLongComparesAndJumps(condition, true_target, false_target);
1835 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001836 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001837 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001838 GenerateFPJumps(condition, true_target, false_target);
1839 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001840 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001841 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001842 GenerateFPJumps(condition, true_target, false_target);
1843 break;
1844 default:
1845 LOG(FATAL) << "Unexpected compare type " << type;
1846 }
1847
David Brazdil0debae72015-11-12 18:37:00 +00001848 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001849 __ jmp(false_target);
1850 }
David Brazdil0debae72015-11-12 18:37:00 +00001851
1852 if (fallthrough_target.IsLinked()) {
1853 __ Bind(&fallthrough_target);
1854 }
Mark Mendellc4701932015-04-10 13:18:51 -04001855}
1856
David Brazdil0debae72015-11-12 18:37:00 +00001857static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1858 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1859 // are set only strictly before `branch`. We can't use the eflags on long/FP
1860 // conditions if they are materialized due to the complex branching.
1861 return cond->IsCondition() &&
1862 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001863 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1864 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001865}
1866
Mark Mendell152408f2015-12-31 12:28:50 -05001867template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001868void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001869 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001870 LabelType* true_target,
1871 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001872 HInstruction* cond = instruction->InputAt(condition_input_index);
1873
1874 if (true_target == nullptr && false_target == nullptr) {
1875 // Nothing to do. The code always falls through.
1876 return;
1877 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001878 // Constant condition, statically compared against "true" (integer value 1).
1879 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001880 if (true_target != nullptr) {
1881 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001882 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001883 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001884 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001885 if (false_target != nullptr) {
1886 __ jmp(false_target);
1887 }
1888 }
1889 return;
1890 }
1891
1892 // The following code generates these patterns:
1893 // (1) true_target == nullptr && false_target != nullptr
1894 // - opposite condition true => branch to false_target
1895 // (2) true_target != nullptr && false_target == nullptr
1896 // - condition true => branch to true_target
1897 // (3) true_target != nullptr && false_target != nullptr
1898 // - condition true => branch to true_target
1899 // - branch to false_target
1900 if (IsBooleanValueOrMaterializedCondition(cond)) {
1901 if (AreEflagsSetFrom(cond, instruction)) {
1902 if (true_target == nullptr) {
1903 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1904 } else {
1905 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1906 }
1907 } else {
1908 // Materialized condition, compare against 0.
1909 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1910 if (lhs.IsRegister()) {
1911 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1912 } else {
1913 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1914 }
1915 if (true_target == nullptr) {
1916 __ j(kEqual, false_target);
1917 } else {
1918 __ j(kNotEqual, true_target);
1919 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001920 }
1921 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001922 // Condition has not been materialized, use its inputs as the comparison and
1923 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001924 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001925
1926 // If this is a long or FP comparison that has been folded into
1927 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001928 DataType::Type type = condition->InputAt(0)->GetType();
1929 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001930 GenerateCompareTestAndBranch(condition, true_target, false_target);
1931 return;
1932 }
1933
1934 Location lhs = condition->GetLocations()->InAt(0);
1935 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001936 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001937 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001938 if (true_target == nullptr) {
1939 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1940 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001941 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001942 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001943 }
David Brazdil0debae72015-11-12 18:37:00 +00001944
1945 // If neither branch falls through (case 3), the conditional branch to `true_target`
1946 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1947 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001948 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001949 }
1950}
1951
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001952void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001953 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001954 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001955 locations->SetInAt(0, Location::Any());
1956 }
1957}
1958
1959void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001960 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1961 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1962 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1963 nullptr : codegen_->GetLabelOf(true_successor);
1964 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1965 nullptr : codegen_->GetLabelOf(false_successor);
Andreas Gampe3db70682018-12-26 15:12:03 -08001966 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001967}
1968
1969void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001970 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001971 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001972 InvokeRuntimeCallingConvention calling_convention;
1973 RegisterSet caller_saves = RegisterSet::Empty();
1974 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1975 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001976 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001977 locations->SetInAt(0, Location::Any());
1978 }
1979}
1980
1981void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001982 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001983 GenerateTestAndBranch<Label>(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08001984 /* condition_input_index= */ 0,
David Brazdil74eb1b22015-12-14 11:44:01 +00001985 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08001986 /* false_target= */ nullptr);
David Brazdil74eb1b22015-12-14 11:44:01 +00001987}
1988
Mingyao Yang063fc772016-08-02 11:02:54 -07001989void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001990 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001991 LocationSummary(flag, LocationSummary::kNoCall);
1992 locations->SetOut(Location::RequiresRegister());
1993}
1994
1995void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1996 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1997 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1998}
1999
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002000static bool SelectCanUseCMOV(HSelect* select) {
2001 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002002 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002003 return false;
2004 }
2005
2006 // A FP condition doesn't generate the single CC that we need.
2007 // In 32 bit mode, a long condition doesn't generate a single CC either.
2008 HInstruction* condition = select->GetCondition();
2009 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002010 DataType::Type compare_type = condition->InputAt(0)->GetType();
2011 if (compare_type == DataType::Type::kInt64 ||
2012 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002013 return false;
2014 }
2015 }
2016
2017 // We can generate a CMOV for this Select.
2018 return true;
2019}
2020
David Brazdil74eb1b22015-12-14 11:44:01 +00002021void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002022 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002023 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00002024 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002025 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00002026 } else {
2027 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002028 if (SelectCanUseCMOV(select)) {
2029 if (select->InputAt(1)->IsConstant()) {
2030 // Cmov can't handle a constant value.
2031 locations->SetInAt(1, Location::RequiresRegister());
2032 } else {
2033 locations->SetInAt(1, Location::Any());
2034 }
2035 } else {
2036 locations->SetInAt(1, Location::Any());
2037 }
David Brazdil74eb1b22015-12-14 11:44:01 +00002038 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002039 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2040 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00002041 }
2042 locations->SetOut(Location::SameAsFirstInput());
2043}
2044
2045void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
2046 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002047 DCHECK(locations->InAt(0).Equals(locations->Out()));
2048 if (SelectCanUseCMOV(select)) {
2049 // If both the condition and the source types are integer, we can generate
2050 // a CMOV to implement Select.
2051
2052 HInstruction* select_condition = select->GetCondition();
2053 Condition cond = kNotEqual;
2054
2055 // Figure out how to test the 'condition'.
2056 if (select_condition->IsCondition()) {
2057 HCondition* condition = select_condition->AsCondition();
2058 if (!condition->IsEmittedAtUseSite()) {
2059 // This was a previously materialized condition.
2060 // Can we use the existing condition code?
2061 if (AreEflagsSetFrom(condition, select)) {
2062 // Materialization was the previous instruction. Condition codes are right.
2063 cond = X86Condition(condition->GetCondition());
2064 } else {
2065 // No, we have to recreate the condition code.
2066 Register cond_reg = locations->InAt(2).AsRegister<Register>();
2067 __ testl(cond_reg, cond_reg);
2068 }
2069 } else {
2070 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002071 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
2072 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002073 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01002074 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002075 cond = X86Condition(condition->GetCondition());
2076 }
2077 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002078 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002079 Register cond_reg = locations->InAt(2).AsRegister<Register>();
2080 __ testl(cond_reg, cond_reg);
2081 }
2082
2083 // If the condition is true, overwrite the output, which already contains false.
2084 Location false_loc = locations->InAt(0);
2085 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002086 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002087 // 64 bit conditional move.
2088 Register false_high = false_loc.AsRegisterPairHigh<Register>();
2089 Register false_low = false_loc.AsRegisterPairLow<Register>();
2090 if (true_loc.IsRegisterPair()) {
2091 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
2092 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
2093 } else {
2094 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
2095 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
2096 }
2097 } else {
2098 // 32 bit conditional move.
2099 Register false_reg = false_loc.AsRegister<Register>();
2100 if (true_loc.IsRegister()) {
2101 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
2102 } else {
2103 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
2104 }
2105 }
2106 } else {
2107 NearLabel false_target;
2108 GenerateTestAndBranch<NearLabel>(
Andreas Gampe3db70682018-12-26 15:12:03 -08002109 select, /* condition_input_index= */ 2, /* true_target= */ nullptr, &false_target);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002110 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2111 __ Bind(&false_target);
2112 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002113}
2114
David Srbecky0cf44932015-12-09 14:09:59 +00002115void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002116 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00002117}
2118
David Srbeckyd28f4a02016-03-14 17:14:24 +00002119void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
2120 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002121}
2122
Vladimir Markodec78172020-06-19 15:31:23 +01002123void CodeGeneratorX86::IncreaseFrame(size_t adjustment) {
2124 __ subl(ESP, Immediate(adjustment));
2125 __ cfi().AdjustCFAOffset(adjustment);
2126}
2127
2128void CodeGeneratorX86::DecreaseFrame(size_t adjustment) {
2129 __ addl(ESP, Immediate(adjustment));
2130 __ cfi().AdjustCFAOffset(-adjustment);
2131}
2132
David Srbeckyc7098ff2016-02-09 14:30:11 +00002133void CodeGeneratorX86::GenerateNop() {
2134 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002135}
2136
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002137void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002138 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002139 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04002140 // Handle the long/FP comparisons made in instruction simplification.
2141 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002142 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04002143 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05002144 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00002145 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002146 locations->SetOut(Location::RequiresRegister());
2147 }
2148 break;
2149 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002150 case DataType::Type::kFloat32:
2151 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04002152 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002153 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
2154 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
2155 } else if (cond->InputAt(1)->IsConstant()) {
2156 locations->SetInAt(1, Location::RequiresFpuRegister());
2157 } else {
2158 locations->SetInAt(1, Location::Any());
2159 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002160 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002161 locations->SetOut(Location::RequiresRegister());
2162 }
2163 break;
2164 }
2165 default:
2166 locations->SetInAt(0, Location::RequiresRegister());
2167 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00002168 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002169 // We need a byte register.
2170 locations->SetOut(Location::RegisterLocation(ECX));
2171 }
2172 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002173 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002174}
2175
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002176void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002177 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002178 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002179 }
Mark Mendellc4701932015-04-10 13:18:51 -04002180
2181 LocationSummary* locations = cond->GetLocations();
2182 Location lhs = locations->InAt(0);
2183 Location rhs = locations->InAt(1);
2184 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05002185 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002186
2187 switch (cond->InputAt(0)->GetType()) {
2188 default: {
2189 // Integer case.
2190
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002191 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04002192 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01002193 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07002194 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04002195 return;
2196 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002197 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04002198 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2199 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002200 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002201 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04002202 GenerateFPJumps(cond, &true_label, &false_label);
2203 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002204 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002205 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04002206 GenerateFPJumps(cond, &true_label, &false_label);
2207 break;
2208 }
2209
2210 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002211 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002212
Roland Levillain4fa13f62015-07-06 18:11:54 +01002213 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04002214 __ Bind(&false_label);
2215 __ xorl(reg, reg);
2216 __ jmp(&done_label);
2217
Roland Levillain4fa13f62015-07-06 18:11:54 +01002218 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04002219 __ Bind(&true_label);
2220 __ movl(reg, Immediate(1));
2221 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002222}
2223
2224void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002225 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002226}
2227
2228void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002229 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002230}
2231
2232void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002233 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002234}
2235
2236void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002237 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002238}
2239
2240void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002241 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002242}
2243
2244void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002245 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002246}
2247
2248void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002249 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002250}
2251
2252void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002253 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002254}
2255
2256void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002257 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002258}
2259
2260void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002261 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002262}
2263
2264void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002265 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002266}
2267
2268void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002269 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002270}
2271
Aart Bike9f37602015-10-09 11:15:55 -07002272void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002273 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002274}
2275
2276void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002277 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002278}
2279
2280void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002281 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002282}
2283
2284void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002285 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002286}
2287
2288void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002289 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002290}
2291
2292void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002293 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002294}
2295
2296void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002297 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002298}
2299
2300void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002301 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002302}
2303
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002304void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002305 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002306 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002307 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002308}
2309
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002310void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002311 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002312}
2313
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002314void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2315 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002316 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002317 locations->SetOut(Location::ConstantLocation(constant));
2318}
2319
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002320void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002321 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002322}
2323
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002324void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002325 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002326 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002327 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002328}
2329
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002330void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002331 // Will be generated at use site.
2332}
2333
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002334void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2335 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002336 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002337 locations->SetOut(Location::ConstantLocation(constant));
2338}
2339
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002340void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002341 // Will be generated at use site.
2342}
2343
2344void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2345 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002346 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002347 locations->SetOut(Location::ConstantLocation(constant));
2348}
2349
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002350void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002351 // Will be generated at use site.
2352}
2353
Igor Murashkind01745e2017-04-05 16:40:31 -07002354void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2355 constructor_fence->SetLocations(nullptr);
2356}
2357
2358void InstructionCodeGeneratorX86::VisitConstructorFence(
2359 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2360 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2361}
2362
Calin Juravle27df7582015-04-17 19:12:31 +01002363void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2364 memory_barrier->SetLocations(nullptr);
2365}
2366
2367void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002368 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002369}
2370
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002371void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002372 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002373}
2374
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002375void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002376 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002377}
2378
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002379void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002380 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002381 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002382 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002383 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002384 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002385 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002386 case DataType::Type::kInt8:
2387 case DataType::Type::kUint16:
2388 case DataType::Type::kInt16:
2389 case DataType::Type::kInt32:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002390 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002391 break;
2392
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002393 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002394 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002395 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002396 break;
2397
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002398 case DataType::Type::kFloat32:
2399 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002400 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002401 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002402 break;
2403
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002404 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002405 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002406 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002407}
2408
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002409void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002410 switch (ret->InputAt(0)->GetType()) {
2411 case DataType::Type::kReference:
2412 case DataType::Type::kBool:
2413 case DataType::Type::kUint8:
2414 case DataType::Type::kInt8:
2415 case DataType::Type::kUint16:
2416 case DataType::Type::kInt16:
2417 case DataType::Type::kInt32:
2418 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
2419 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002420
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002421 case DataType::Type::kInt64:
2422 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2423 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
2424 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002425
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002426 case DataType::Type::kFloat32:
2427 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2428 if (GetGraph()->IsCompilingOsr()) {
2429 // To simplify callers of an OSR method, we put the return value in both
2430 // floating point and core registers.
2431 __ movd(EAX, XMM0);
2432 }
2433 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002434
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002435 case DataType::Type::kFloat64:
2436 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2437 if (GetGraph()->IsCompilingOsr()) {
2438 // To simplify callers of an OSR method, we put the return value in both
2439 // floating point and core registers.
2440 __ movd(EAX, XMM0);
2441 // Use XMM1 as temporary register to not clobber XMM0.
2442 __ movaps(XMM1, XMM0);
2443 __ psrlq(XMM1, Immediate(32));
2444 __ movd(EDX, XMM1);
2445 }
2446 break;
2447
2448 default:
2449 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002450 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002451 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002452}
2453
Calin Juravle175dc732015-08-25 15:42:32 +01002454void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2455 // The trampoline uses the same calling convention as dex calling conventions,
2456 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2457 // the method_idx.
2458 HandleInvoke(invoke);
2459}
2460
2461void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2462 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2463}
2464
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002465void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002466 // Explicit clinit checks triggered by static invokes must have been pruned by
2467 // art::PrepareForRegisterAllocation.
2468 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002469
Mark Mendellfb8d2792015-03-31 22:16:59 -04002470 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002471 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002472 if (invoke->GetLocations()->CanCall() &&
2473 invoke->HasPcRelativeMethodLoadKind() &&
2474 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).IsInvalid()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002475 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002476 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002477 return;
2478 }
2479
Vladimir Marko86c87522020-05-11 16:55:55 +01002480 if (invoke->GetCodePtrLocation() == HInvokeStaticOrDirect::CodePtrLocation::kCallCriticalNative) {
2481 CriticalNativeCallingConventionVisitorX86 calling_convention_visitor(
2482 /*for_register_allocation=*/ true);
2483 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2484 } else {
2485 HandleInvoke(invoke);
2486 }
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002487
Vladimir Marko86c87522020-05-11 16:55:55 +01002488 // For PC-relative load kinds the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002489 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002490 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002491 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002492}
2493
Mark Mendell09ed1a32015-03-25 08:30:06 -04002494static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2495 if (invoke->GetLocations()->Intrinsified()) {
2496 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2497 intrinsic.Dispatch(invoke);
2498 return true;
2499 }
2500 return false;
2501}
2502
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002503void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002504 // Explicit clinit checks triggered by static invokes must have been pruned by
2505 // art::PrepareForRegisterAllocation.
2506 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002507
Mark Mendell09ed1a32015-03-25 08:30:06 -04002508 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2509 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002510 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002511
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002512 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002513 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002514 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002515}
2516
2517void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002518 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2519 if (intrinsic.TryDispatch(invoke)) {
2520 return;
2521 }
2522
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002523 HandleInvoke(invoke);
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002524
2525 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002526 // Add one temporary for inline cache update.
2527 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2528 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002529}
2530
2531void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002532 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002533 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002534}
2535
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002536void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002537 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2538 return;
2539 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002540
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002541 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002542 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002543}
2544
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002545void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002546 // This call to HandleInvoke allocates a temporary (core) register
2547 // which is also used to transfer the hidden argument from FP to
2548 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002549 HandleInvoke(invoke);
2550 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002551 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002552
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002553 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002554 // Add one temporary for inline cache update.
2555 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2556 }
2557}
2558
2559void CodeGeneratorX86::MaybeGenerateInlineCacheCheck(HInstruction* instruction, Register klass) {
2560 DCHECK_EQ(EAX, klass);
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002561 // We know the destination of an intrinsic, so no need to record inline
2562 // caches (also the intrinsic location builder doesn't request an additional
2563 // temporary).
2564 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002565 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002566 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002567 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
Nicolas Geoffray095dc462020-08-17 16:40:28 +01002568 ScopedProfilingInfoUse spiu(
2569 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
2570 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002571 if (info != nullptr) {
2572 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
2573 uint32_t address = reinterpret_cast32<uint32_t>(cache);
2574 if (kIsDebugBuild) {
2575 uint32_t temp_index = instruction->GetLocations()->GetTempCount() - 1u;
2576 CHECK_EQ(EBP, instruction->GetLocations()->GetTemp(temp_index).AsRegister<Register>());
2577 }
2578 Register temp = EBP;
2579 NearLabel done;
2580 __ movl(temp, Immediate(address));
2581 // Fast path for a monomorphic cache.
2582 __ cmpl(klass, Address(temp, InlineCache::ClassesOffset().Int32Value()));
2583 __ j(kEqual, &done);
2584 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(kQuickUpdateInlineCache).Int32Value());
2585 __ Bind(&done);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002586 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002587 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002588}
2589
2590void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2591 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002592 LocationSummary* locations = invoke->GetLocations();
2593 Register temp = locations->GetTemp(0).AsRegister<Register>();
2594 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002595 Location receiver = locations->InAt(0);
2596 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2597
Roland Levillain0d5a2812015-11-13 10:07:31 +00002598 // Set the hidden argument. This is safe to do this here, as XMM7
2599 // won't be modified thereafter, before the `call` instruction.
2600 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01002601 __ movl(temp, Immediate(invoke->GetMethodReference().index));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002602 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002603
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002604 if (receiver.IsStackSlot()) {
2605 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002606 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002607 __ movl(temp, Address(temp, class_offset));
2608 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002609 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002610 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002611 }
Roland Levillain4d027112015-07-01 15:41:14 +01002612 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002613 // Instead of simply (possibly) unpoisoning `temp` here, we should
2614 // emit a read barrier for the previous class reference load.
2615 // However this is not required in practice, as this is an
2616 // intermediate/temporary reference and because the current
2617 // concurrent copying collector keeps the from-space memory
2618 // intact/accessible until the end of the marking phase (the
2619 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002620 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002621
2622 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
2623
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002624 // temp = temp->GetAddressOfIMT()
2625 __ movl(temp,
2626 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002627 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002628 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002629 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002630 __ movl(temp, Address(temp, method_offset));
2631 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002632 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002633 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002634
2635 DCHECK(!codegen_->IsLeafMethod());
2636 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2637}
2638
Orion Hodsonac141392017-01-13 11:53:47 +00002639void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00002640 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2641 if (intrinsic.TryDispatch(invoke)) {
2642 return;
2643 }
Orion Hodsonac141392017-01-13 11:53:47 +00002644 HandleInvoke(invoke);
2645}
2646
2647void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00002648 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2649 return;
2650 }
Orion Hodsonac141392017-01-13 11:53:47 +00002651 codegen_->GenerateInvokePolymorphicCall(invoke);
2652}
2653
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002654void LocationsBuilderX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2655 HandleInvoke(invoke);
2656}
2657
2658void InstructionCodeGeneratorX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2659 codegen_->GenerateInvokeCustomCall(invoke);
2660}
2661
Roland Levillain88cb1752014-10-20 16:36:47 +01002662void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2663 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002664 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002665 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002666 case DataType::Type::kInt32:
2667 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002668 locations->SetInAt(0, Location::RequiresRegister());
2669 locations->SetOut(Location::SameAsFirstInput());
2670 break;
2671
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002672 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002673 locations->SetInAt(0, Location::RequiresFpuRegister());
2674 locations->SetOut(Location::SameAsFirstInput());
2675 locations->AddTemp(Location::RequiresRegister());
2676 locations->AddTemp(Location::RequiresFpuRegister());
2677 break;
2678
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002679 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002680 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002681 locations->SetOut(Location::SameAsFirstInput());
2682 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002683 break;
2684
2685 default:
2686 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2687 }
2688}
2689
2690void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2691 LocationSummary* locations = neg->GetLocations();
2692 Location out = locations->Out();
2693 Location in = locations->InAt(0);
2694 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002695 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002696 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002697 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002698 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002699 break;
2700
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002701 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002702 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002703 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002704 __ negl(out.AsRegisterPairLow<Register>());
2705 // Negation is similar to subtraction from zero. The least
2706 // significant byte triggers a borrow when it is different from
2707 // zero; to take it into account, add 1 to the most significant
2708 // byte if the carry flag (CF) is set to 1 after the first NEGL
2709 // operation.
2710 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2711 __ negl(out.AsRegisterPairHigh<Register>());
2712 break;
2713
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002714 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002715 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002716 Register constant = locations->GetTemp(0).AsRegister<Register>();
2717 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002718 // Implement float negation with an exclusive or with value
2719 // 0x80000000 (mask for bit 31, representing the sign of a
2720 // single-precision floating-point number).
2721 __ movl(constant, Immediate(INT32_C(0x80000000)));
2722 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002723 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002724 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002725 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002726
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002727 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002728 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002729 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002730 // Implement double negation with an exclusive or with value
2731 // 0x8000000000000000 (mask for bit 63, representing the sign of
2732 // a double-precision floating-point number).
2733 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002734 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002735 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002736 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002737
2738 default:
2739 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2740 }
2741}
2742
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002743void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2744 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002745 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002746 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002747 locations->SetInAt(0, Location::RequiresFpuRegister());
2748 locations->SetInAt(1, Location::RequiresRegister());
2749 locations->SetOut(Location::SameAsFirstInput());
2750 locations->AddTemp(Location::RequiresFpuRegister());
2751}
2752
2753void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2754 LocationSummary* locations = neg->GetLocations();
2755 Location out = locations->Out();
2756 DCHECK(locations->InAt(0).Equals(out));
2757
2758 Register constant_area = locations->InAt(1).AsRegister<Register>();
2759 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002760 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002761 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2762 neg->GetBaseMethodAddress(),
2763 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002764 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2765 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002766 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2767 neg->GetBaseMethodAddress(),
2768 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002769 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2770 }
2771}
2772
Roland Levillaindff1f282014-11-05 14:15:05 +00002773void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002774 DataType::Type result_type = conversion->GetResultType();
2775 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002776 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2777 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002778
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002779 // The float-to-long and double-to-long type conversions rely on a
2780 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002781 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002782 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2783 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002784 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002785 : LocationSummary::kNoCall;
2786 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002787 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002788
Roland Levillaindff1f282014-11-05 14:15:05 +00002789 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002790 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002791 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002792 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002793 case DataType::Type::kUint8:
2794 case DataType::Type::kInt8:
2795 case DataType::Type::kUint16:
2796 case DataType::Type::kInt16:
2797 case DataType::Type::kInt32:
2798 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2799 // Make the output overlap to please the register allocator. This greatly simplifies
2800 // the validation of the linear scan implementation
2801 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2802 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002803 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002804 HInstruction* input = conversion->InputAt(0);
2805 Location input_location = input->IsConstant()
2806 ? Location::ConstantLocation(input->AsConstant())
2807 : Location::RegisterPairLocation(EAX, EDX);
2808 locations->SetInAt(0, input_location);
2809 // Make the output overlap to please the register allocator. This greatly simplifies
2810 // the validation of the linear scan implementation
2811 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2812 break;
2813 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002814
2815 default:
2816 LOG(FATAL) << "Unexpected type conversion from " << input_type
2817 << " to " << result_type;
2818 }
2819 break;
2820
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002821 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002822 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002823 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2824 locations->SetInAt(0, Location::Any());
2825 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002826 break;
2827
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002828 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002829 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002830 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002831 locations->SetInAt(0, Location::Any());
2832 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2833 break;
2834
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002835 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002836 locations->SetInAt(0, Location::RequiresFpuRegister());
2837 locations->SetOut(Location::RequiresRegister());
2838 locations->AddTemp(Location::RequiresFpuRegister());
2839 break;
2840
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002841 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002842 locations->SetInAt(0, Location::RequiresFpuRegister());
2843 locations->SetOut(Location::RequiresRegister());
2844 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002845 break;
2846
2847 default:
2848 LOG(FATAL) << "Unexpected type conversion from " << input_type
2849 << " to " << result_type;
2850 }
2851 break;
2852
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002853 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002854 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002855 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002856 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002857 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002858 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002859 case DataType::Type::kInt16:
2860 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002861 locations->SetInAt(0, Location::RegisterLocation(EAX));
2862 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2863 break;
2864
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002865 case DataType::Type::kFloat32:
2866 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002867 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002868 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2869 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2870
Vladimir Marko949c91f2015-01-27 10:48:44 +00002871 // The runtime helper puts the result in EAX, EDX.
2872 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002873 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002874 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002875
2876 default:
2877 LOG(FATAL) << "Unexpected type conversion from " << input_type
2878 << " to " << result_type;
2879 }
2880 break;
2881
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002882 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002883 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002884 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002885 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002886 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002887 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002888 case DataType::Type::kInt16:
2889 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002890 locations->SetInAt(0, Location::RequiresRegister());
2891 locations->SetOut(Location::RequiresFpuRegister());
2892 break;
2893
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002894 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002895 locations->SetInAt(0, Location::Any());
2896 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002897 break;
2898
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002899 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002900 locations->SetInAt(0, Location::RequiresFpuRegister());
2901 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002902 break;
2903
2904 default:
2905 LOG(FATAL) << "Unexpected type conversion from " << input_type
2906 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002907 }
Roland Levillaincff13742014-11-17 14:32:17 +00002908 break;
2909
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002910 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002911 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002912 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002913 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002914 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002915 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002916 case DataType::Type::kInt16:
2917 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002918 locations->SetInAt(0, Location::RequiresRegister());
2919 locations->SetOut(Location::RequiresFpuRegister());
2920 break;
2921
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002922 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002923 locations->SetInAt(0, Location::Any());
2924 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002925 break;
2926
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002927 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002928 locations->SetInAt(0, Location::RequiresFpuRegister());
2929 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002930 break;
2931
2932 default:
2933 LOG(FATAL) << "Unexpected type conversion from " << input_type
2934 << " to " << result_type;
2935 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002936 break;
2937
2938 default:
2939 LOG(FATAL) << "Unexpected type conversion from " << input_type
2940 << " to " << result_type;
2941 }
2942}
2943
2944void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2945 LocationSummary* locations = conversion->GetLocations();
2946 Location out = locations->Out();
2947 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002948 DataType::Type result_type = conversion->GetResultType();
2949 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002950 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2951 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002952 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002953 case DataType::Type::kUint8:
2954 switch (input_type) {
2955 case DataType::Type::kInt8:
2956 case DataType::Type::kUint16:
2957 case DataType::Type::kInt16:
2958 case DataType::Type::kInt32:
2959 if (in.IsRegister()) {
2960 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2961 } else {
2962 DCHECK(in.GetConstant()->IsIntConstant());
2963 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2964 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2965 }
2966 break;
2967 case DataType::Type::kInt64:
2968 if (in.IsRegisterPair()) {
2969 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2970 } else {
2971 DCHECK(in.GetConstant()->IsLongConstant());
2972 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2973 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2974 }
2975 break;
2976
2977 default:
2978 LOG(FATAL) << "Unexpected type conversion from " << input_type
2979 << " to " << result_type;
2980 }
2981 break;
2982
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002983 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002984 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002985 case DataType::Type::kUint8:
2986 case DataType::Type::kUint16:
2987 case DataType::Type::kInt16:
2988 case DataType::Type::kInt32:
2989 if (in.IsRegister()) {
2990 __ movsxb(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<int8_t>(value)));
2995 }
2996 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002997 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002998 if (in.IsRegisterPair()) {
2999 __ movsxb(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<int8_t>(value)));
3004 }
3005 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003006
3007 default:
3008 LOG(FATAL) << "Unexpected type conversion from " << input_type
3009 << " to " << result_type;
3010 }
3011 break;
3012
3013 case DataType::Type::kUint16:
3014 switch (input_type) {
3015 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003016 case DataType::Type::kInt16:
3017 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003018 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003019 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
3020 } else if (in.IsStackSlot()) {
3021 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003022 } else {
3023 DCHECK(in.GetConstant()->IsIntConstant());
3024 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003025 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
3026 }
3027 break;
3028 case DataType::Type::kInt64:
3029 if (in.IsRegisterPair()) {
3030 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
3031 } else if (in.IsDoubleStackSlot()) {
3032 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
3033 } else {
3034 DCHECK(in.GetConstant()->IsLongConstant());
3035 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3036 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003037 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00003038 break;
3039
3040 default:
3041 LOG(FATAL) << "Unexpected type conversion from " << input_type
3042 << " to " << result_type;
3043 }
3044 break;
3045
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003046 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00003047 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003048 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003049 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00003050 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003051 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00003052 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003053 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00003054 } else {
3055 DCHECK(in.GetConstant()->IsIntConstant());
3056 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003057 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00003058 }
3059 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003060 case DataType::Type::kInt64:
3061 if (in.IsRegisterPair()) {
3062 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
3063 } else if (in.IsDoubleStackSlot()) {
3064 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
3065 } else {
3066 DCHECK(in.GetConstant()->IsLongConstant());
3067 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3068 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
3069 }
3070 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00003071
3072 default:
3073 LOG(FATAL) << "Unexpected type conversion from " << input_type
3074 << " to " << result_type;
3075 }
3076 break;
3077
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003078 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00003079 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003080 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00003081 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003082 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00003083 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003084 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00003085 } else {
3086 DCHECK(in.IsConstant());
3087 DCHECK(in.GetConstant()->IsLongConstant());
3088 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003089 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00003090 }
3091 break;
3092
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003093 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00003094 XmmRegister input = in.AsFpuRegister<XmmRegister>();
3095 Register output = out.AsRegister<Register>();
3096 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04003097 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00003098
3099 __ movl(output, Immediate(kPrimIntMax));
3100 // temp = int-to-float(output)
3101 __ cvtsi2ss(temp, output);
3102 // if input >= temp goto done
3103 __ comiss(input, temp);
3104 __ j(kAboveEqual, &done);
3105 // if input == NaN goto nan
3106 __ j(kUnordered, &nan);
3107 // output = float-to-int-truncate(input)
3108 __ cvttss2si(output, input);
3109 __ jmp(&done);
3110 __ Bind(&nan);
3111 // output = 0
3112 __ xorl(output, output);
3113 __ Bind(&done);
3114 break;
3115 }
3116
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003117 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003118 XmmRegister input = in.AsFpuRegister<XmmRegister>();
3119 Register output = out.AsRegister<Register>();
3120 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04003121 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003122
3123 __ movl(output, Immediate(kPrimIntMax));
3124 // temp = int-to-double(output)
3125 __ cvtsi2sd(temp, output);
3126 // if input >= temp goto done
3127 __ comisd(input, temp);
3128 __ j(kAboveEqual, &done);
3129 // if input == NaN goto nan
3130 __ j(kUnordered, &nan);
3131 // output = double-to-int-truncate(input)
3132 __ cvttsd2si(output, input);
3133 __ jmp(&done);
3134 __ Bind(&nan);
3135 // output = 0
3136 __ xorl(output, output);
3137 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00003138 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003139 }
Roland Levillain946e1432014-11-11 17:35:19 +00003140
3141 default:
3142 LOG(FATAL) << "Unexpected type conversion from " << input_type
3143 << " to " << result_type;
3144 }
3145 break;
3146
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003147 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00003148 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003149 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003150 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003151 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003152 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003153 case DataType::Type::kInt16:
3154 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00003155 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
3156 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003157 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00003158 __ cdq();
3159 break;
3160
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003161 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01003162 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003163 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00003164 break;
3165
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003166 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01003167 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003168 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00003169 break;
3170
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::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +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 Levillain271ab9c2014-11-27 15:23:57 +00003185 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003186 break;
3187
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003188 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003189 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00003190
Roland Levillain232ade02015-04-20 15:14:36 +01003191 // Create stack space for the call to
3192 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
3193 // TODO: enhance register allocator to ask for stack temporaries.
3194 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003195 adjustment = DataType::Size(DataType::Type::kInt64);
Vladimir Markodec78172020-06-19 15:31:23 +01003196 codegen_->IncreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003197 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003198
Roland Levillain232ade02015-04-20 15:14:36 +01003199 // Load the value to the FP stack, using temporaries if needed.
3200 PushOntoFPStack(in, 0, adjustment, false, true);
3201
3202 if (out.IsStackSlot()) {
3203 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
3204 } else {
3205 __ fstps(Address(ESP, 0));
3206 Location stack_temp = Location::StackSlot(0);
3207 codegen_->Move32(out, stack_temp);
3208 }
3209
3210 // Remove the temporary stack space we allocated.
3211 if (adjustment != 0) {
Vladimir Markodec78172020-06-19 15:31:23 +01003212 codegen_->DecreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003213 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003214 break;
3215 }
3216
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003217 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003218 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003219 break;
3220
3221 default:
3222 LOG(FATAL) << "Unexpected type conversion from " << input_type
3223 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003224 }
Roland Levillaincff13742014-11-17 14:32:17 +00003225 break;
3226
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003227 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00003228 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003229 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003230 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003231 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003232 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003233 case DataType::Type::kInt16:
3234 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003235 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003236 break;
3237
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003238 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003239 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00003240
Roland Levillain232ade02015-04-20 15:14:36 +01003241 // Create stack space for the call to
3242 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
3243 // TODO: enhance register allocator to ask for stack temporaries.
3244 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003245 adjustment = DataType::Size(DataType::Type::kInt64);
Vladimir Markodec78172020-06-19 15:31:23 +01003246 codegen_->IncreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003247 }
3248
3249 // Load the value to the FP stack, using temporaries if needed.
3250 PushOntoFPStack(in, 0, adjustment, false, true);
3251
3252 if (out.IsDoubleStackSlot()) {
3253 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
3254 } else {
3255 __ fstpl(Address(ESP, 0));
3256 Location stack_temp = Location::DoubleStackSlot(0);
3257 codegen_->Move64(out, stack_temp);
3258 }
3259
3260 // Remove the temporary stack space we allocated.
3261 if (adjustment != 0) {
Vladimir Markodec78172020-06-19 15:31:23 +01003262 codegen_->DecreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003263 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003264 break;
3265 }
3266
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003267 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003268 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003269 break;
3270
3271 default:
3272 LOG(FATAL) << "Unexpected type conversion from " << input_type
3273 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003274 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003275 break;
3276
3277 default:
3278 LOG(FATAL) << "Unexpected type conversion from " << input_type
3279 << " to " << result_type;
3280 }
3281}
3282
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003283void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003284 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003285 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003286 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003287 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05003288 locations->SetInAt(0, Location::RequiresRegister());
3289 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3290 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3291 break;
3292 }
3293
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003294 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003295 locations->SetInAt(0, Location::RequiresRegister());
3296 locations->SetInAt(1, Location::Any());
3297 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003298 break;
3299 }
3300
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003301 case DataType::Type::kFloat32:
3302 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003303 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003304 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3305 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003306 } else if (add->InputAt(1)->IsConstant()) {
3307 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003308 } else {
3309 locations->SetInAt(1, Location::Any());
3310 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003311 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003312 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003313 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003314
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003315 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003316 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Elliott Hughesc1896c92018-11-29 11:33:18 -08003317 UNREACHABLE();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003318 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003319}
3320
3321void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
3322 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003323 Location first = locations->InAt(0);
3324 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05003325 Location out = locations->Out();
3326
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003327 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003328 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003329 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003330 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3331 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003332 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3333 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05003334 } else {
3335 __ leal(out.AsRegister<Register>(), Address(
3336 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3337 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003338 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003339 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3340 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3341 __ addl(out.AsRegister<Register>(), Immediate(value));
3342 } else {
3343 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3344 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003345 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003346 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003347 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003348 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003349 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003350 }
3351
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003352 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003353 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003354 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3355 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003356 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003357 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3358 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003359 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003360 } else {
3361 DCHECK(second.IsConstant()) << second;
3362 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3363 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3364 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003365 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003366 break;
3367 }
3368
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003369 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003370 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003371 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003372 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3373 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003374 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003375 __ addss(first.AsFpuRegister<XmmRegister>(),
3376 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003377 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3378 const_area->GetBaseMethodAddress(),
3379 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003380 } else {
3381 DCHECK(second.IsStackSlot());
3382 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003383 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003384 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003385 }
3386
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003387 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003388 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003389 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003390 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3391 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003392 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003393 __ addsd(first.AsFpuRegister<XmmRegister>(),
3394 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003395 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3396 const_area->GetBaseMethodAddress(),
3397 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003398 } else {
3399 DCHECK(second.IsDoubleStackSlot());
3400 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003401 }
3402 break;
3403 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003404
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003405 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003406 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003407 }
3408}
3409
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003410void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003411 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003412 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003413 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003414 case DataType::Type::kInt32:
3415 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003416 locations->SetInAt(0, Location::RequiresRegister());
3417 locations->SetInAt(1, Location::Any());
3418 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003419 break;
3420 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003421 case DataType::Type::kFloat32:
3422 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003423 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003424 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3425 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003426 } else if (sub->InputAt(1)->IsConstant()) {
3427 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003428 } else {
3429 locations->SetInAt(1, Location::Any());
3430 }
Calin Juravle11351682014-10-23 15:38:15 +01003431 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003432 break;
Calin Juravle11351682014-10-23 15:38:15 +01003433 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003434
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003435 default:
Calin Juravle11351682014-10-23 15:38:15 +01003436 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003437 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003438}
3439
3440void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3441 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003442 Location first = locations->InAt(0);
3443 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003444 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003445 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003446 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003447 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003448 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003449 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003450 __ subl(first.AsRegister<Register>(),
3451 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003452 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003453 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003454 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003455 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003456 }
3457
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003458 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003459 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003460 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3461 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003462 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003463 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003464 __ sbbl(first.AsRegisterPairHigh<Register>(),
3465 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003466 } else {
3467 DCHECK(second.IsConstant()) << second;
3468 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3469 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3470 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003471 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003472 break;
3473 }
3474
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003475 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003476 if (second.IsFpuRegister()) {
3477 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3478 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3479 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003480 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003481 __ subss(first.AsFpuRegister<XmmRegister>(),
3482 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003483 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3484 const_area->GetBaseMethodAddress(),
3485 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003486 } else {
3487 DCHECK(second.IsStackSlot());
3488 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3489 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003490 break;
Calin Juravle11351682014-10-23 15:38:15 +01003491 }
3492
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003493 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003494 if (second.IsFpuRegister()) {
3495 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3496 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3497 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003498 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003499 __ subsd(first.AsFpuRegister<XmmRegister>(),
3500 codegen_->LiteralDoubleAddress(
3501 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003502 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003503 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3504 } else {
3505 DCHECK(second.IsDoubleStackSlot());
3506 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3507 }
Calin Juravle11351682014-10-23 15:38:15 +01003508 break;
3509 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003510
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003511 default:
Calin Juravle11351682014-10-23 15:38:15 +01003512 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003513 }
3514}
3515
Calin Juravle34bacdf2014-10-07 20:23:36 +01003516void LocationsBuilderX86::VisitMul(HMul* mul) {
3517 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003518 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003519 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003520 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003521 locations->SetInAt(0, Location::RequiresRegister());
3522 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003523 if (mul->InputAt(1)->IsIntConstant()) {
3524 // Can use 3 operand multiply.
3525 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3526 } else {
3527 locations->SetOut(Location::SameAsFirstInput());
3528 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003529 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003530 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003531 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003532 locations->SetInAt(1, Location::Any());
3533 locations->SetOut(Location::SameAsFirstInput());
3534 // Needed for imul on 32bits with 64bits output.
3535 locations->AddTemp(Location::RegisterLocation(EAX));
3536 locations->AddTemp(Location::RegisterLocation(EDX));
3537 break;
3538 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003539 case DataType::Type::kFloat32:
3540 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003541 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003542 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3543 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003544 } else if (mul->InputAt(1)->IsConstant()) {
3545 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003546 } else {
3547 locations->SetInAt(1, Location::Any());
3548 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003549 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003550 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003551 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003552
3553 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003554 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003555 }
3556}
3557
3558void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3559 LocationSummary* locations = mul->GetLocations();
3560 Location first = locations->InAt(0);
3561 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003562 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003563
3564 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003565 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003566 // The constant may have ended up in a register, so test explicitly to avoid
3567 // problems where the output may not be the same as the first operand.
3568 if (mul->InputAt(1)->IsIntConstant()) {
3569 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3570 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3571 } else if (second.IsRegister()) {
3572 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003573 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003574 } else {
3575 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003576 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003577 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003578 }
3579 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003580
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003581 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003582 Register in1_hi = first.AsRegisterPairHigh<Register>();
3583 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003584 Register eax = locations->GetTemp(0).AsRegister<Register>();
3585 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003586
3587 DCHECK_EQ(EAX, eax);
3588 DCHECK_EQ(EDX, edx);
3589
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003590 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003591 // output: in1
3592 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3593 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3594 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003595 if (second.IsConstant()) {
3596 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003597
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003598 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3599 int32_t low_value = Low32Bits(value);
3600 int32_t high_value = High32Bits(value);
3601 Immediate low(low_value);
3602 Immediate high(high_value);
3603
3604 __ movl(eax, high);
3605 // eax <- in1.lo * in2.hi
3606 __ imull(eax, in1_lo);
3607 // in1.hi <- in1.hi * in2.lo
3608 __ imull(in1_hi, low);
3609 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3610 __ addl(in1_hi, eax);
3611 // move in2_lo to eax to prepare for double precision
3612 __ movl(eax, low);
3613 // edx:eax <- in1.lo * in2.lo
3614 __ mull(in1_lo);
3615 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3616 __ addl(in1_hi, edx);
3617 // in1.lo <- (in1.lo * in2.lo)[31:0];
3618 __ movl(in1_lo, eax);
3619 } else if (second.IsRegisterPair()) {
3620 Register in2_hi = second.AsRegisterPairHigh<Register>();
3621 Register in2_lo = second.AsRegisterPairLow<Register>();
3622
3623 __ movl(eax, in2_hi);
3624 // eax <- in1.lo * in2.hi
3625 __ imull(eax, in1_lo);
3626 // in1.hi <- in1.hi * in2.lo
3627 __ imull(in1_hi, in2_lo);
3628 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3629 __ addl(in1_hi, eax);
3630 // move in1_lo to eax to prepare for double precision
3631 __ movl(eax, in1_lo);
3632 // edx:eax <- in1.lo * in2.lo
3633 __ mull(in2_lo);
3634 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3635 __ addl(in1_hi, edx);
3636 // in1.lo <- (in1.lo * in2.lo)[31:0];
3637 __ movl(in1_lo, eax);
3638 } else {
3639 DCHECK(second.IsDoubleStackSlot()) << second;
3640 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3641 Address in2_lo(ESP, second.GetStackIndex());
3642
3643 __ movl(eax, in2_hi);
3644 // eax <- in1.lo * in2.hi
3645 __ imull(eax, in1_lo);
3646 // in1.hi <- in1.hi * in2.lo
3647 __ imull(in1_hi, in2_lo);
3648 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3649 __ addl(in1_hi, eax);
3650 // move in1_lo to eax to prepare for double precision
3651 __ movl(eax, in1_lo);
3652 // edx:eax <- in1.lo * in2.lo
3653 __ mull(in2_lo);
3654 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3655 __ addl(in1_hi, edx);
3656 // in1.lo <- (in1.lo * in2.lo)[31:0];
3657 __ movl(in1_lo, eax);
3658 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003659
3660 break;
3661 }
3662
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003663 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003664 DCHECK(first.Equals(locations->Out()));
3665 if (second.IsFpuRegister()) {
3666 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3667 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3668 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003669 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003670 __ mulss(first.AsFpuRegister<XmmRegister>(),
3671 codegen_->LiteralFloatAddress(
3672 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003673 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003674 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3675 } else {
3676 DCHECK(second.IsStackSlot());
3677 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3678 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003679 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003680 }
3681
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003682 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003683 DCHECK(first.Equals(locations->Out()));
3684 if (second.IsFpuRegister()) {
3685 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3686 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3687 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003688 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003689 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3690 codegen_->LiteralDoubleAddress(
3691 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003692 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003693 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3694 } else {
3695 DCHECK(second.IsDoubleStackSlot());
3696 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3697 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003698 break;
3699 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003700
3701 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003702 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003703 }
3704}
3705
Roland Levillain232ade02015-04-20 15:14:36 +01003706void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3707 uint32_t temp_offset,
3708 uint32_t stack_adjustment,
3709 bool is_fp,
3710 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003711 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003712 DCHECK(!is_wide);
3713 if (is_fp) {
3714 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3715 } else {
3716 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3717 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003718 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003719 DCHECK(is_wide);
3720 if (is_fp) {
3721 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3722 } else {
3723 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3724 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003725 } else {
3726 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003727 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003728 Location stack_temp = Location::StackSlot(temp_offset);
3729 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003730 if (is_fp) {
3731 __ flds(Address(ESP, temp_offset));
3732 } else {
3733 __ filds(Address(ESP, temp_offset));
3734 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003735 } else {
3736 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3737 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003738 if (is_fp) {
3739 __ fldl(Address(ESP, temp_offset));
3740 } else {
3741 __ fildl(Address(ESP, temp_offset));
3742 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003743 }
3744 }
3745}
3746
3747void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003748 DataType::Type type = rem->GetResultType();
3749 bool is_float = type == DataType::Type::kFloat32;
3750 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003751 LocationSummary* locations = rem->GetLocations();
3752 Location first = locations->InAt(0);
3753 Location second = locations->InAt(1);
3754 Location out = locations->Out();
3755
3756 // Create stack space for 2 elements.
3757 // TODO: enhance register allocator to ask for stack temporaries.
Vladimir Markodec78172020-06-19 15:31:23 +01003758 codegen_->IncreaseFrame(2 * elem_size);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003759
3760 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003761 const bool is_wide = !is_float;
Andreas Gampe3db70682018-12-26 15:12:03 -08003762 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp= */ true, is_wide);
3763 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp= */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003764
3765 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003766 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003767 __ Bind(&retry);
3768 __ fprem();
3769
3770 // Move FP status to AX.
3771 __ fstsw();
3772
3773 // And see if the argument reduction is complete. This is signaled by the
3774 // C2 FPU flag bit set to 0.
3775 __ andl(EAX, Immediate(kC2ConditionMask));
3776 __ j(kNotEqual, &retry);
3777
3778 // We have settled on the final value. Retrieve it into an XMM register.
3779 // Store FP top of stack to real stack.
3780 if (is_float) {
3781 __ fsts(Address(ESP, 0));
3782 } else {
3783 __ fstl(Address(ESP, 0));
3784 }
3785
3786 // Pop the 2 items from the FP stack.
3787 __ fucompp();
3788
3789 // Load the value from the stack into an XMM register.
3790 DCHECK(out.IsFpuRegister()) << out;
3791 if (is_float) {
3792 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3793 } else {
3794 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3795 }
3796
3797 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01003798 codegen_->DecreaseFrame(2 * elem_size);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003799}
3800
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003801
3802void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3803 DCHECK(instruction->IsDiv() || instruction->IsRem());
3804
3805 LocationSummary* locations = instruction->GetLocations();
3806 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003807 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003808
3809 Register out_register = locations->Out().AsRegister<Register>();
3810 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003811 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003812
3813 DCHECK(imm == 1 || imm == -1);
3814
3815 if (instruction->IsRem()) {
3816 __ xorl(out_register, out_register);
3817 } else {
3818 __ movl(out_register, input_register);
3819 if (imm == -1) {
3820 __ negl(out_register);
3821 }
3822 }
3823}
3824
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303825void InstructionCodeGeneratorX86::RemByPowerOfTwo(HRem* instruction) {
3826 LocationSummary* locations = instruction->GetLocations();
3827 Location second = locations->InAt(1);
3828
3829 Register out = locations->Out().AsRegister<Register>();
3830 Register numerator = locations->InAt(0).AsRegister<Register>();
3831
3832 int32_t imm = Int64FromConstant(second.GetConstant());
3833 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3834 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3835
3836 Register tmp = locations->GetTemp(0).AsRegister<Register>();
3837 NearLabel done;
3838 __ movl(out, numerator);
3839 __ andl(out, Immediate(abs_imm-1));
3840 __ j(Condition::kZero, &done);
3841 __ leal(tmp, Address(out, static_cast<int32_t>(~(abs_imm-1))));
3842 __ testl(numerator, numerator);
3843 __ cmovl(Condition::kLess, out, tmp);
3844 __ Bind(&done);
3845}
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003846
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003847void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003848 LocationSummary* locations = instruction->GetLocations();
3849
3850 Register out_register = locations->Out().AsRegister<Register>();
3851 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003852 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003853 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3854 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003855
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003856 Register num = locations->GetTemp(0).AsRegister<Register>();
3857
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003858 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003859 __ testl(input_register, input_register);
3860 __ cmovl(kGreaterEqual, num, input_register);
3861 int shift = CTZ(imm);
3862 __ sarl(num, Immediate(shift));
3863
3864 if (imm < 0) {
3865 __ negl(num);
3866 }
3867
3868 __ movl(out_register, num);
3869}
3870
3871void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3872 DCHECK(instruction->IsDiv() || instruction->IsRem());
3873
3874 LocationSummary* locations = instruction->GetLocations();
3875 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3876
3877 Register eax = locations->InAt(0).AsRegister<Register>();
3878 Register out = locations->Out().AsRegister<Register>();
3879 Register num;
3880 Register edx;
3881
3882 if (instruction->IsDiv()) {
3883 edx = locations->GetTemp(0).AsRegister<Register>();
3884 num = locations->GetTemp(1).AsRegister<Register>();
3885 } else {
3886 edx = locations->Out().AsRegister<Register>();
3887 num = locations->GetTemp(0).AsRegister<Register>();
3888 }
3889
3890 DCHECK_EQ(EAX, eax);
3891 DCHECK_EQ(EDX, edx);
3892 if (instruction->IsDiv()) {
3893 DCHECK_EQ(EAX, out);
3894 } else {
3895 DCHECK_EQ(EDX, out);
3896 }
3897
3898 int64_t magic;
3899 int shift;
Andreas Gampe3db70682018-12-26 15:12:03 -08003900 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003901
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003902 // Save the numerator.
3903 __ movl(num, eax);
3904
3905 // EAX = magic
3906 __ movl(eax, Immediate(magic));
3907
3908 // EDX:EAX = magic * numerator
3909 __ imull(num);
3910
3911 if (imm > 0 && magic < 0) {
3912 // EDX += num
3913 __ addl(edx, num);
3914 } else if (imm < 0 && magic > 0) {
3915 __ subl(edx, num);
3916 }
3917
3918 // Shift if needed.
3919 if (shift != 0) {
3920 __ sarl(edx, Immediate(shift));
3921 }
3922
3923 // EDX += 1 if EDX < 0
3924 __ movl(eax, edx);
3925 __ shrl(edx, Immediate(31));
3926 __ addl(edx, eax);
3927
3928 if (instruction->IsRem()) {
3929 __ movl(eax, num);
3930 __ imull(edx, Immediate(imm));
3931 __ subl(eax, edx);
3932 __ movl(edx, eax);
3933 } else {
3934 __ movl(eax, edx);
3935 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003936}
3937
Calin Juravlebacfec32014-11-14 15:54:36 +00003938void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3939 DCHECK(instruction->IsDiv() || instruction->IsRem());
3940
3941 LocationSummary* locations = instruction->GetLocations();
3942 Location out = locations->Out();
3943 Location first = locations->InAt(0);
3944 Location second = locations->InAt(1);
3945 bool is_div = instruction->IsDiv();
3946
3947 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003948 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003949 DCHECK_EQ(EAX, first.AsRegister<Register>());
3950 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003951
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003952 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003953 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003954
3955 if (imm == 0) {
3956 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3957 } else if (imm == 1 || imm == -1) {
3958 DivRemOneOrMinusOne(instruction);
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303959 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
3960 if (is_div) {
3961 DivByPowerOfTwo(instruction->AsDiv());
3962 } else {
3963 RemByPowerOfTwo(instruction->AsRem());
3964 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003965 } else {
3966 DCHECK(imm <= -2 || imm >= 2);
3967 GenerateDivRemWithAnyConstant(instruction);
3968 }
3969 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003970 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00003971 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003972 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003973
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003974 Register second_reg = second.AsRegister<Register>();
3975 // 0x80000000/-1 triggers an arithmetic exception!
3976 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3977 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003978
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003979 __ cmpl(second_reg, Immediate(-1));
3980 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003981
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003982 // edx:eax <- sign-extended of eax
3983 __ cdq();
3984 // eax = quotient, edx = remainder
3985 __ idivl(second_reg);
3986 __ Bind(slow_path->GetExitLabel());
3987 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003988 break;
3989 }
3990
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003991 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003992 InvokeRuntimeCallingConvention calling_convention;
3993 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3994 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3995 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3996 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3997 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3998 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3999
4000 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004001 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004002 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004003 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004004 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004005 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004006 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004007 break;
4008 }
4009
4010 default:
4011 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
4012 }
4013}
4014
Calin Juravle7c4954d2014-10-28 16:57:40 +00004015void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004016 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004017 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004018 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01004019 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004020
Calin Juravle7c4954d2014-10-28 16:57:40 +00004021 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004022 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00004023 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004024 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00004025 locations->SetOut(Location::SameAsFirstInput());
4026 // Intel uses edx:eax as the dividend.
4027 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004028 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
4029 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
4030 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01004031 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004032 locations->AddTemp(Location::RequiresRegister());
4033 }
Calin Juravled0d48522014-11-04 16:40:20 +00004034 break;
4035 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004036 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004037 InvokeRuntimeCallingConvention calling_convention;
4038 locations->SetInAt(0, Location::RegisterPairLocation(
4039 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4040 locations->SetInAt(1, Location::RegisterPairLocation(
4041 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4042 // Runtime helper puts the result in EAX, EDX.
4043 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00004044 break;
4045 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004046 case DataType::Type::kFloat32:
4047 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004048 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00004049 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
4050 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00004051 } else if (div->InputAt(1)->IsConstant()) {
4052 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00004053 } else {
4054 locations->SetInAt(1, Location::Any());
4055 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004056 locations->SetOut(Location::SameAsFirstInput());
4057 break;
4058 }
4059
4060 default:
4061 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4062 }
4063}
4064
4065void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
4066 LocationSummary* locations = div->GetLocations();
4067 Location first = locations->InAt(0);
4068 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004069
4070 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004071 case DataType::Type::kInt32:
4072 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004073 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004074 break;
4075 }
4076
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004077 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04004078 if (second.IsFpuRegister()) {
4079 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
4080 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
4081 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00004082 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04004083 __ divss(first.AsFpuRegister<XmmRegister>(),
4084 codegen_->LiteralFloatAddress(
4085 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004086 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04004087 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
4088 } else {
4089 DCHECK(second.IsStackSlot());
4090 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
4091 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004092 break;
4093 }
4094
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004095 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04004096 if (second.IsFpuRegister()) {
4097 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
4098 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
4099 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00004100 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04004101 __ divsd(first.AsFpuRegister<XmmRegister>(),
4102 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004103 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
4104 const_area->GetBaseMethodAddress(),
4105 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04004106 } else {
4107 DCHECK(second.IsDoubleStackSlot());
4108 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
4109 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004110 break;
4111 }
4112
4113 default:
4114 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4115 }
4116}
4117
Calin Juravlebacfec32014-11-14 15:54:36 +00004118void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004119 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004120
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004121 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004122 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004123 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01004124 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00004125
Calin Juravled2ec87d2014-12-08 14:24:46 +00004126 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004127 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004128 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004129 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00004130 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004131 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
4132 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
4133 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01004134 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004135 locations->AddTemp(Location::RequiresRegister());
4136 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004137 break;
4138 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004139 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004140 InvokeRuntimeCallingConvention calling_convention;
4141 locations->SetInAt(0, Location::RegisterPairLocation(
4142 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4143 locations->SetInAt(1, Location::RegisterPairLocation(
4144 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4145 // Runtime helper puts the result in EAX, EDX.
4146 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
4147 break;
4148 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004149 case DataType::Type::kFloat64:
4150 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05004151 locations->SetInAt(0, Location::Any());
4152 locations->SetInAt(1, Location::Any());
4153 locations->SetOut(Location::RequiresFpuRegister());
4154 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00004155 break;
4156 }
4157
4158 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004159 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004160 }
4161}
4162
4163void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004164 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00004165 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004166 case DataType::Type::kInt32:
4167 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004168 GenerateDivRemIntegral(rem);
4169 break;
4170 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004171 case DataType::Type::kFloat32:
4172 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05004173 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00004174 break;
4175 }
4176 default:
4177 LOG(FATAL) << "Unexpected rem type " << type;
4178 }
4179}
4180
Aart Bik1f8d51b2018-02-15 10:42:37 -08004181static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
4182 LocationSummary* locations = new (allocator) LocationSummary(minmax);
4183 switch (minmax->GetResultType()) {
4184 case DataType::Type::kInt32:
4185 locations->SetInAt(0, Location::RequiresRegister());
4186 locations->SetInAt(1, Location::RequiresRegister());
4187 locations->SetOut(Location::SameAsFirstInput());
4188 break;
4189 case DataType::Type::kInt64:
4190 locations->SetInAt(0, Location::RequiresRegister());
4191 locations->SetInAt(1, Location::RequiresRegister());
4192 locations->SetOut(Location::SameAsFirstInput());
4193 // Register to use to perform a long subtract to set cc.
4194 locations->AddTemp(Location::RequiresRegister());
4195 break;
4196 case DataType::Type::kFloat32:
4197 locations->SetInAt(0, Location::RequiresFpuRegister());
4198 locations->SetInAt(1, Location::RequiresFpuRegister());
4199 locations->SetOut(Location::SameAsFirstInput());
4200 locations->AddTemp(Location::RequiresRegister());
4201 break;
4202 case DataType::Type::kFloat64:
4203 locations->SetInAt(0, Location::RequiresFpuRegister());
4204 locations->SetInAt(1, Location::RequiresFpuRegister());
4205 locations->SetOut(Location::SameAsFirstInput());
4206 break;
4207 default:
4208 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
4209 }
4210}
4211
Aart Bik351df3e2018-03-07 11:54:57 -08004212void InstructionCodeGeneratorX86::GenerateMinMaxInt(LocationSummary* locations,
4213 bool is_min,
4214 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08004215 Location op1_loc = locations->InAt(0);
4216 Location op2_loc = locations->InAt(1);
4217
4218 // Shortcut for same input locations.
4219 if (op1_loc.Equals(op2_loc)) {
4220 // Can return immediately, as op1_loc == out_loc.
4221 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
4222 // a copy here.
4223 DCHECK(locations->Out().Equals(op1_loc));
4224 return;
4225 }
4226
4227 if (type == DataType::Type::kInt64) {
4228 // Need to perform a subtract to get the sign right.
4229 // op1 is already in the same location as the output.
4230 Location output = locations->Out();
4231 Register output_lo = output.AsRegisterPairLow<Register>();
4232 Register output_hi = output.AsRegisterPairHigh<Register>();
4233
4234 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
4235 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
4236
4237 // The comparison is performed by subtracting the second operand from
4238 // the first operand and then setting the status flags in the same
4239 // manner as the SUB instruction."
4240 __ cmpl(output_lo, op2_lo);
4241
4242 // Now use a temp and the borrow to finish the subtraction of op2_hi.
4243 Register temp = locations->GetTemp(0).AsRegister<Register>();
4244 __ movl(temp, output_hi);
4245 __ sbbl(temp, op2_hi);
4246
4247 // Now the condition code is correct.
4248 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
4249 __ cmovl(cond, output_lo, op2_lo);
4250 __ cmovl(cond, output_hi, op2_hi);
4251 } else {
4252 DCHECK_EQ(type, DataType::Type::kInt32);
4253 Register out = locations->Out().AsRegister<Register>();
4254 Register op2 = op2_loc.AsRegister<Register>();
4255
4256 // (out := op1)
4257 // out <=? op2
4258 // if out is min jmp done
4259 // out := op2
4260 // done:
4261
4262 __ cmpl(out, op2);
4263 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
4264 __ cmovl(cond, out, op2);
4265 }
4266}
4267
4268void InstructionCodeGeneratorX86::GenerateMinMaxFP(LocationSummary* locations,
4269 bool is_min,
4270 DataType::Type type) {
4271 Location op1_loc = locations->InAt(0);
4272 Location op2_loc = locations->InAt(1);
4273 Location out_loc = locations->Out();
4274 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
4275
4276 // Shortcut for same input locations.
4277 if (op1_loc.Equals(op2_loc)) {
4278 DCHECK(out_loc.Equals(op1_loc));
4279 return;
4280 }
4281
4282 // (out := op1)
4283 // out <=? op2
4284 // if Nan jmp Nan_label
4285 // if out is min jmp done
4286 // if op2 is min jmp op2_label
4287 // handle -0/+0
4288 // jmp done
4289 // Nan_label:
4290 // out := NaN
4291 // op2_label:
4292 // out := op2
4293 // done:
4294 //
4295 // This removes one jmp, but needs to copy one input (op1) to out.
4296 //
4297 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
4298
4299 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
4300
4301 NearLabel nan, done, op2_label;
4302 if (type == DataType::Type::kFloat64) {
4303 __ ucomisd(out, op2);
4304 } else {
4305 DCHECK_EQ(type, DataType::Type::kFloat32);
4306 __ ucomiss(out, op2);
4307 }
4308
4309 __ j(Condition::kParityEven, &nan);
4310
4311 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
4312 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
4313
4314 // Handle 0.0/-0.0.
4315 if (is_min) {
4316 if (type == DataType::Type::kFloat64) {
4317 __ orpd(out, op2);
4318 } else {
4319 __ orps(out, op2);
4320 }
4321 } else {
4322 if (type == DataType::Type::kFloat64) {
4323 __ andpd(out, op2);
4324 } else {
4325 __ andps(out, op2);
4326 }
4327 }
4328 __ jmp(&done);
4329
4330 // NaN handling.
4331 __ Bind(&nan);
4332 if (type == DataType::Type::kFloat64) {
4333 // TODO: Use a constant from the constant table (requires extra input).
4334 __ LoadLongConstant(out, kDoubleNaN);
4335 } else {
4336 Register constant = locations->GetTemp(0).AsRegister<Register>();
4337 __ movl(constant, Immediate(kFloatNaN));
4338 __ movd(out, constant);
4339 }
4340 __ jmp(&done);
4341
4342 // out := op2;
4343 __ Bind(&op2_label);
4344 if (type == DataType::Type::kFloat64) {
4345 __ movsd(out, op2);
4346 } else {
4347 __ movss(out, op2);
4348 }
4349
4350 // Done.
4351 __ Bind(&done);
4352}
4353
Aart Bik351df3e2018-03-07 11:54:57 -08004354void InstructionCodeGeneratorX86::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4355 DataType::Type type = minmax->GetResultType();
4356 switch (type) {
4357 case DataType::Type::kInt32:
4358 case DataType::Type::kInt64:
4359 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
4360 break;
4361 case DataType::Type::kFloat32:
4362 case DataType::Type::kFloat64:
4363 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
4364 break;
4365 default:
4366 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4367 }
4368}
4369
Aart Bik1f8d51b2018-02-15 10:42:37 -08004370void LocationsBuilderX86::VisitMin(HMin* min) {
4371 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4372}
4373
4374void InstructionCodeGeneratorX86::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004375 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004376}
4377
4378void LocationsBuilderX86::VisitMax(HMax* max) {
4379 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4380}
4381
4382void InstructionCodeGeneratorX86::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004383 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004384}
4385
Aart Bik3dad3412018-02-28 12:01:46 -08004386void LocationsBuilderX86::VisitAbs(HAbs* abs) {
4387 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4388 switch (abs->GetResultType()) {
4389 case DataType::Type::kInt32:
4390 locations->SetInAt(0, Location::RegisterLocation(EAX));
4391 locations->SetOut(Location::SameAsFirstInput());
4392 locations->AddTemp(Location::RegisterLocation(EDX));
4393 break;
4394 case DataType::Type::kInt64:
4395 locations->SetInAt(0, Location::RequiresRegister());
4396 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4397 locations->AddTemp(Location::RequiresRegister());
4398 break;
4399 case DataType::Type::kFloat32:
4400 locations->SetInAt(0, Location::RequiresFpuRegister());
4401 locations->SetOut(Location::SameAsFirstInput());
4402 locations->AddTemp(Location::RequiresFpuRegister());
4403 locations->AddTemp(Location::RequiresRegister());
4404 break;
4405 case DataType::Type::kFloat64:
4406 locations->SetInAt(0, Location::RequiresFpuRegister());
4407 locations->SetOut(Location::SameAsFirstInput());
4408 locations->AddTemp(Location::RequiresFpuRegister());
4409 break;
4410 default:
4411 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4412 }
4413}
4414
4415void InstructionCodeGeneratorX86::VisitAbs(HAbs* abs) {
4416 LocationSummary* locations = abs->GetLocations();
4417 switch (abs->GetResultType()) {
4418 case DataType::Type::kInt32: {
4419 Register out = locations->Out().AsRegister<Register>();
4420 DCHECK_EQ(out, EAX);
4421 Register temp = locations->GetTemp(0).AsRegister<Register>();
4422 DCHECK_EQ(temp, EDX);
4423 // Sign extend EAX into EDX.
4424 __ cdq();
4425 // XOR EAX with sign.
4426 __ xorl(EAX, EDX);
4427 // Subtract out sign to correct.
4428 __ subl(EAX, EDX);
4429 // The result is in EAX.
4430 break;
4431 }
4432 case DataType::Type::kInt64: {
4433 Location input = locations->InAt(0);
4434 Register input_lo = input.AsRegisterPairLow<Register>();
4435 Register input_hi = input.AsRegisterPairHigh<Register>();
4436 Location output = locations->Out();
4437 Register output_lo = output.AsRegisterPairLow<Register>();
4438 Register output_hi = output.AsRegisterPairHigh<Register>();
4439 Register temp = locations->GetTemp(0).AsRegister<Register>();
4440 // Compute the sign into the temporary.
4441 __ movl(temp, input_hi);
4442 __ sarl(temp, Immediate(31));
4443 // Store the sign into the output.
4444 __ movl(output_lo, temp);
4445 __ movl(output_hi, temp);
4446 // XOR the input to the output.
4447 __ xorl(output_lo, input_lo);
4448 __ xorl(output_hi, input_hi);
4449 // Subtract the sign.
4450 __ subl(output_lo, temp);
4451 __ sbbl(output_hi, temp);
4452 break;
4453 }
4454 case DataType::Type::kFloat32: {
4455 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4456 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4457 Register constant = locations->GetTemp(1).AsRegister<Register>();
4458 __ movl(constant, Immediate(INT32_C(0x7FFFFFFF)));
4459 __ movd(temp, constant);
4460 __ andps(out, temp);
4461 break;
4462 }
4463 case DataType::Type::kFloat64: {
4464 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4465 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4466 // TODO: Use a constant from the constant table (requires extra input).
4467 __ LoadLongConstant(temp, INT64_C(0x7FFFFFFFFFFFFFFF));
4468 __ andpd(out, temp);
4469 break;
4470 }
4471 default:
4472 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4473 }
4474}
4475
Calin Juravled0d48522014-11-04 16:40:20 +00004476void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004477 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004478 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004479 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004480 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004481 case DataType::Type::kInt8:
4482 case DataType::Type::kUint16:
4483 case DataType::Type::kInt16:
4484 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004485 locations->SetInAt(0, Location::Any());
4486 break;
4487 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004488 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004489 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
4490 if (!instruction->IsConstant()) {
4491 locations->AddTemp(Location::RequiresRegister());
4492 }
4493 break;
4494 }
4495 default:
4496 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4497 }
Calin Juravled0d48522014-11-04 16:40:20 +00004498}
4499
4500void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004501 SlowPathCode* slow_path =
4502 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004503 codegen_->AddSlowPath(slow_path);
4504
4505 LocationSummary* locations = instruction->GetLocations();
4506 Location value = locations->InAt(0);
4507
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 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004516 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004517 __ j(kEqual, slow_path->GetEntryLabel());
4518 } else if (value.IsStackSlot()) {
4519 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
4520 __ j(kEqual, slow_path->GetEntryLabel());
4521 } else {
4522 DCHECK(value.IsConstant()) << value;
4523 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004524 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004525 }
4526 }
4527 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004528 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004529 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004530 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004531 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004532 __ movl(temp, value.AsRegisterPairLow<Register>());
4533 __ orl(temp, value.AsRegisterPairHigh<Register>());
4534 __ j(kEqual, slow_path->GetEntryLabel());
4535 } else {
4536 DCHECK(value.IsConstant()) << value;
4537 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4538 __ jmp(slow_path->GetEntryLabel());
4539 }
4540 }
4541 break;
4542 }
4543 default:
4544 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004545 }
Calin Juravled0d48522014-11-04 16:40:20 +00004546}
4547
Calin Juravle9aec02f2014-11-18 23:06:35 +00004548void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
4549 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4550
4551 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004552 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004553
4554 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004555 case DataType::Type::kInt32:
4556 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004557 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00004558 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00004559 // The shift count needs to be in CL or a constant.
4560 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004561 locations->SetOut(Location::SameAsFirstInput());
4562 break;
4563 }
4564 default:
4565 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4566 }
4567}
4568
4569void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
4570 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4571
4572 LocationSummary* locations = op->GetLocations();
4573 Location first = locations->InAt(0);
4574 Location second = locations->InAt(1);
4575 DCHECK(first.Equals(locations->Out()));
4576
4577 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004578 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00004579 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004580 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004581 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004582 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004583 DCHECK_EQ(ECX, second_reg);
4584 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004585 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004586 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004587 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004588 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004589 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004590 }
4591 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004592 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004593 if (shift == 0) {
4594 return;
4595 }
4596 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004597 if (op->IsShl()) {
4598 __ shll(first_reg, imm);
4599 } else if (op->IsShr()) {
4600 __ sarl(first_reg, imm);
4601 } else {
4602 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004603 }
4604 }
4605 break;
4606 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004607 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004608 if (second.IsRegister()) {
4609 Register second_reg = second.AsRegister<Register>();
4610 DCHECK_EQ(ECX, second_reg);
4611 if (op->IsShl()) {
4612 GenerateShlLong(first, second_reg);
4613 } else if (op->IsShr()) {
4614 GenerateShrLong(first, second_reg);
4615 } else {
4616 GenerateUShrLong(first, second_reg);
4617 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004618 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00004619 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004620 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004621 // Nothing to do if the shift is 0, as the input is already the output.
4622 if (shift != 0) {
4623 if (op->IsShl()) {
4624 GenerateShlLong(first, shift);
4625 } else if (op->IsShr()) {
4626 GenerateShrLong(first, shift);
4627 } else {
4628 GenerateUShrLong(first, shift);
4629 }
4630 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004631 }
4632 break;
4633 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004634 default:
4635 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4636 }
4637}
4638
Mark P Mendell73945692015-04-29 14:56:17 +00004639void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
4640 Register low = loc.AsRegisterPairLow<Register>();
4641 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04004642 if (shift == 1) {
4643 // This is just an addition.
4644 __ addl(low, low);
4645 __ adcl(high, high);
4646 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00004647 // Shift by 32 is easy. High gets low, and low gets 0.
4648 codegen_->EmitParallelMoves(
4649 loc.ToLow(),
4650 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004651 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004652 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4653 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004654 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004655 } else if (shift > 32) {
4656 // Low part becomes 0. High part is low part << (shift-32).
4657 __ movl(high, low);
4658 __ shll(high, Immediate(shift - 32));
4659 __ xorl(low, low);
4660 } else {
4661 // Between 1 and 31.
4662 __ shld(high, low, Immediate(shift));
4663 __ shll(low, Immediate(shift));
4664 }
4665}
4666
Calin Juravle9aec02f2014-11-18 23:06:35 +00004667void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004668 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004669 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4670 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4671 __ testl(shifter, Immediate(32));
4672 __ j(kEqual, &done);
4673 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4674 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4675 __ Bind(&done);
4676}
4677
Mark P Mendell73945692015-04-29 14:56:17 +00004678void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4679 Register low = loc.AsRegisterPairLow<Register>();
4680 Register high = loc.AsRegisterPairHigh<Register>();
4681 if (shift == 32) {
4682 // Need to copy the sign.
4683 DCHECK_NE(low, high);
4684 __ movl(low, high);
4685 __ sarl(high, Immediate(31));
4686 } else if (shift > 32) {
4687 DCHECK_NE(low, high);
4688 // High part becomes sign. Low part is shifted by shift - 32.
4689 __ movl(low, high);
4690 __ sarl(high, Immediate(31));
4691 __ sarl(low, Immediate(shift - 32));
4692 } else {
4693 // Between 1 and 31.
4694 __ shrd(low, high, Immediate(shift));
4695 __ sarl(high, Immediate(shift));
4696 }
4697}
4698
Calin Juravle9aec02f2014-11-18 23:06:35 +00004699void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004700 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004701 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4702 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4703 __ testl(shifter, Immediate(32));
4704 __ j(kEqual, &done);
4705 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4706 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4707 __ Bind(&done);
4708}
4709
Mark P Mendell73945692015-04-29 14:56:17 +00004710void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4711 Register low = loc.AsRegisterPairLow<Register>();
4712 Register high = loc.AsRegisterPairHigh<Register>();
4713 if (shift == 32) {
4714 // Shift by 32 is easy. Low gets high, and high gets 0.
4715 codegen_->EmitParallelMoves(
4716 loc.ToHigh(),
4717 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004718 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004719 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4720 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004721 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004722 } else if (shift > 32) {
4723 // Low part is high >> (shift - 32). High part becomes 0.
4724 __ movl(low, high);
4725 __ shrl(low, Immediate(shift - 32));
4726 __ xorl(high, high);
4727 } else {
4728 // Between 1 and 31.
4729 __ shrd(low, high, Immediate(shift));
4730 __ shrl(high, Immediate(shift));
4731 }
4732}
4733
Calin Juravle9aec02f2014-11-18 23:06:35 +00004734void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004735 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004736 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4737 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4738 __ testl(shifter, Immediate(32));
4739 __ j(kEqual, &done);
4740 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4741 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4742 __ Bind(&done);
4743}
4744
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004745void LocationsBuilderX86::VisitRor(HRor* ror) {
4746 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004747 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004748
4749 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004750 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004751 // Add the temporary needed.
4752 locations->AddTemp(Location::RequiresRegister());
4753 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004754 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004755 locations->SetInAt(0, Location::RequiresRegister());
4756 // The shift count needs to be in CL (unless it is a constant).
4757 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4758 locations->SetOut(Location::SameAsFirstInput());
4759 break;
4760 default:
4761 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4762 UNREACHABLE();
4763 }
4764}
4765
4766void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4767 LocationSummary* locations = ror->GetLocations();
4768 Location first = locations->InAt(0);
4769 Location second = locations->InAt(1);
4770
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004771 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004772 Register first_reg = first.AsRegister<Register>();
4773 if (second.IsRegister()) {
4774 Register second_reg = second.AsRegister<Register>();
4775 __ rorl(first_reg, second_reg);
4776 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004777 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004778 __ rorl(first_reg, imm);
4779 }
4780 return;
4781 }
4782
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004783 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004784 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4785 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4786 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4787 if (second.IsRegister()) {
4788 Register second_reg = second.AsRegister<Register>();
4789 DCHECK_EQ(second_reg, ECX);
4790 __ movl(temp_reg, first_reg_hi);
4791 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4792 __ shrd(first_reg_lo, temp_reg, second_reg);
4793 __ movl(temp_reg, first_reg_hi);
4794 __ testl(second_reg, Immediate(32));
4795 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4796 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4797 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004798 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004799 if (shift_amt == 0) {
4800 // Already fine.
4801 return;
4802 }
4803 if (shift_amt == 32) {
4804 // Just swap.
4805 __ movl(temp_reg, first_reg_lo);
4806 __ movl(first_reg_lo, first_reg_hi);
4807 __ movl(first_reg_hi, temp_reg);
4808 return;
4809 }
4810
4811 Immediate imm(shift_amt);
4812 // Save the constents of the low value.
4813 __ movl(temp_reg, first_reg_lo);
4814
4815 // Shift right into low, feeding bits from high.
4816 __ shrd(first_reg_lo, first_reg_hi, imm);
4817
4818 // Shift right into high, feeding bits from the original low.
4819 __ shrd(first_reg_hi, temp_reg, imm);
4820
4821 // Swap if needed.
4822 if (shift_amt > 32) {
4823 __ movl(temp_reg, first_reg_lo);
4824 __ movl(first_reg_lo, first_reg_hi);
4825 __ movl(first_reg_hi, temp_reg);
4826 }
4827 }
4828}
4829
Calin Juravle9aec02f2014-11-18 23:06:35 +00004830void LocationsBuilderX86::VisitShl(HShl* shl) {
4831 HandleShift(shl);
4832}
4833
4834void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4835 HandleShift(shl);
4836}
4837
4838void LocationsBuilderX86::VisitShr(HShr* shr) {
4839 HandleShift(shr);
4840}
4841
4842void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4843 HandleShift(shr);
4844}
4845
4846void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4847 HandleShift(ushr);
4848}
4849
4850void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4851 HandleShift(ushr);
4852}
4853
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004854void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004855 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4856 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004857 locations->SetOut(Location::RegisterLocation(EAX));
Alex Lightd109e302018-06-27 10:25:41 -07004858 InvokeRuntimeCallingConvention calling_convention;
4859 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004860}
4861
4862void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07004863 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
4864 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4865 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004866}
4867
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004868void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004869 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4870 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004871 locations->SetOut(Location::RegisterLocation(EAX));
4872 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004873 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4874 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004875}
4876
4877void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01004878 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
4879 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004880 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004881 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004882 DCHECK(!codegen_->IsLeafMethod());
4883}
4884
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004885void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004886 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004887 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004888 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4889 if (location.IsStackSlot()) {
4890 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4891 } else if (location.IsDoubleStackSlot()) {
4892 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004893 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004894 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004895}
4896
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004897void InstructionCodeGeneratorX86::VisitParameterValue(
4898 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4899}
4900
4901void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4902 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004903 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004904 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4905}
4906
4907void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004908}
4909
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004910void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4911 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004912 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004913 locations->SetInAt(0, Location::RequiresRegister());
4914 locations->SetOut(Location::RequiresRegister());
4915}
4916
4917void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4918 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004919 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004920 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004921 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004922 __ movl(locations->Out().AsRegister<Register>(),
4923 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004924 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004925 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004926 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004927 __ movl(locations->Out().AsRegister<Register>(),
4928 Address(locations->InAt(0).AsRegister<Register>(),
4929 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4930 // temp = temp->GetImtEntryAt(method_offset);
4931 __ movl(locations->Out().AsRegister<Register>(),
4932 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004933 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004934}
4935
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004936void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004937 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004938 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004939 locations->SetInAt(0, Location::RequiresRegister());
4940 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004941}
4942
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004943void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4944 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004945 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004946 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004947 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004948 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004949 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004950 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004951 break;
4952
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004953 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004954 __ notl(out.AsRegisterPairLow<Register>());
4955 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004956 break;
4957
4958 default:
4959 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4960 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004961}
4962
David Brazdil66d126e2015-04-03 16:02:44 +01004963void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4964 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004965 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004966 locations->SetInAt(0, Location::RequiresRegister());
4967 locations->SetOut(Location::SameAsFirstInput());
4968}
4969
4970void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004971 LocationSummary* locations = bool_not->GetLocations();
4972 Location in = locations->InAt(0);
4973 Location out = locations->Out();
4974 DCHECK(in.Equals(out));
4975 __ xorl(out.AsRegister<Register>(), Immediate(1));
4976}
4977
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004978void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004979 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004980 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004981 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004982 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004983 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004984 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004985 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004986 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004987 case DataType::Type::kInt32:
4988 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004989 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004990 locations->SetInAt(1, Location::Any());
4991 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4992 break;
4993 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004994 case DataType::Type::kFloat32:
4995 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004996 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004997 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4998 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4999 } else if (compare->InputAt(1)->IsConstant()) {
5000 locations->SetInAt(1, Location::RequiresFpuRegister());
5001 } else {
5002 locations->SetInAt(1, Location::Any());
5003 }
Calin Juravleddb7df22014-11-25 20:56:51 +00005004 locations->SetOut(Location::RequiresRegister());
5005 break;
5006 }
5007 default:
5008 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5009 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005010}
5011
5012void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005013 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005014 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00005015 Location left = locations->InAt(0);
5016 Location right = locations->InAt(1);
5017
Mark Mendell0c9497d2015-08-21 09:30:05 -04005018 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08005019 Condition less_cond = kLess;
5020
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005021 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005022 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005023 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005024 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005025 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005026 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005027 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01005028 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08005029 break;
5030 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005031 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005032 Register left_low = left.AsRegisterPairLow<Register>();
5033 Register left_high = left.AsRegisterPairHigh<Register>();
5034 int32_t val_low = 0;
5035 int32_t val_high = 0;
5036 bool right_is_const = false;
5037
5038 if (right.IsConstant()) {
5039 DCHECK(right.GetConstant()->IsLongConstant());
5040 right_is_const = true;
5041 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
5042 val_low = Low32Bits(val);
5043 val_high = High32Bits(val);
5044 }
5045
Calin Juravleddb7df22014-11-25 20:56:51 +00005046 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005047 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005048 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005049 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005050 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005051 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08005052 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005053 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005054 __ j(kLess, &less); // Signed compare.
5055 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005056 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005057 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005058 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005059 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005060 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005061 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08005062 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005063 }
Aart Bika19616e2016-02-01 18:57:58 -08005064 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00005065 break;
5066 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005067 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00005068 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00005069 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08005070 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00005071 break;
5072 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005073 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00005074 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00005075 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08005076 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005077 break;
5078 }
5079 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00005080 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005081 }
Aart Bika19616e2016-02-01 18:57:58 -08005082
Calin Juravleddb7df22014-11-25 20:56:51 +00005083 __ movl(out, Immediate(0));
5084 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08005085 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00005086
5087 __ Bind(&greater);
5088 __ movl(out, Immediate(1));
5089 __ jmp(&done);
5090
5091 __ Bind(&less);
5092 __ movl(out, Immediate(-1));
5093
5094 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005095}
5096
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005097void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005098 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005099 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005100 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01005101 locations->SetInAt(i, Location::Any());
5102 }
5103 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005104}
5105
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005106void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005107 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005108}
5109
Roland Levillain7c1559a2015-12-15 10:55:36 +00005110void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00005111 /*
5112 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
5113 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
5114 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
5115 */
5116 switch (kind) {
5117 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00005118 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00005119 break;
5120 }
5121 case MemBarrierKind::kAnyStore:
5122 case MemBarrierKind::kLoadAny:
5123 case MemBarrierKind::kStoreStore: {
5124 // nop
5125 break;
5126 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05005127 case MemBarrierKind::kNTStoreStore:
5128 // Non-Temporal Store/Store needs an explicit fence.
Andreas Gampe3db70682018-12-26 15:12:03 -08005129 MemoryFence(/* non-temporal= */ true);
Mark Mendell7aa04a12016-01-27 22:39:07 -05005130 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005131 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005132}
5133
Vladimir Markodc151b22015-10-15 18:02:30 +01005134HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
5135 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01005136 ArtMethod* method ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005137 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005138}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005139
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005140Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
5141 Register temp) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00005142 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005143 if (!invoke->GetLocations()->Intrinsified()) {
5144 return location.AsRegister<Register>();
5145 }
5146 // For intrinsics we allow any location, so it may be on the stack.
5147 if (!location.IsRegister()) {
5148 __ movl(temp, Address(ESP, location.GetStackIndex()));
5149 return temp;
5150 }
5151 // For register locations, check if the register was saved. If so, get it from the stack.
5152 // Note: There is a chance that the register was saved but not overwritten, so we could
5153 // save one load. However, since this is just an intrinsic slow path we prefer this
5154 // simple and more robust approach rather that trying to determine if that's the case.
5155 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00005156 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
5157 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
5158 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
5159 __ movl(temp, Address(ESP, stack_offset));
5160 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005161 }
5162 return location.AsRegister<Register>();
5163}
5164
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005165void CodeGeneratorX86::GenerateStaticOrDirectCall(
5166 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00005167 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
5168 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005169 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005170 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005171 uint32_t offset =
5172 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
5173 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00005174 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005175 }
Vladimir Marko58155012015-08-19 12:49:41 +00005176 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Marko86c87522020-05-11 16:55:55 +01005177 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005178 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005179 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005180 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko65979462017-05-19 17:25:12 +01005181 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
5182 temp.AsRegister<Register>());
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005183 __ leal(temp.AsRegister<Register>(),
5184 Address(base_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005185 RecordBootImageMethodPatch(invoke);
Vladimir Marko65979462017-05-19 17:25:12 +01005186 break;
5187 }
Vladimir Markob066d432018-01-03 13:14:37 +00005188 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
5189 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
5190 temp.AsRegister<Register>());
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005191 __ movl(temp.AsRegister<Register>(), Address(base_reg, kPlaceholder32BitOffset));
Vladimir Markob066d432018-01-03 13:14:37 +00005192 RecordBootImageRelRoPatch(
5193 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005194 GetBootImageOffset(invoke));
Vladimir Markob066d432018-01-03 13:14:37 +00005195 break;
5196 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005197 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005198 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
5199 temp.AsRegister<Register>());
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005200 __ movl(temp.AsRegister<Register>(), Address(base_reg, kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005201 RecordMethodBssEntryPatch(invoke);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005202 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005203 break;
5204 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005205 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
5206 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
5207 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005208 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
5209 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5210 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01005211 }
Vladimir Marko58155012015-08-19 12:49:41 +00005212 }
5213
5214 switch (invoke->GetCodePtrLocation()) {
5215 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
5216 __ call(GetFrameEntryLabel());
Vladimir Marko86c87522020-05-11 16:55:55 +01005217 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko58155012015-08-19 12:49:41 +00005218 break;
Vladimir Marko86c87522020-05-11 16:55:55 +01005219 case HInvokeStaticOrDirect::CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01005220 size_t out_frame_size =
5221 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorX86,
5222 kNativeStackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01005223 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Marko86c87522020-05-11 16:55:55 +01005224 // (callee_method + offset_of_jni_entry_point)()
5225 __ call(Address(callee_method.AsRegister<Register>(),
5226 ArtMethod::EntryPointFromJniOffset(kX86PointerSize).Int32Value()));
5227 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5228 if (out_frame_size == 0u && DataType::IsFloatingPointType(invoke->GetType())) {
5229 // Create space for conversion.
5230 out_frame_size = 8u;
Vladimir Markodec78172020-06-19 15:31:23 +01005231 IncreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01005232 }
5233 // Zero-/sign-extend or move the result when needed due to native and managed ABI mismatch.
5234 switch (invoke->GetType()) {
5235 case DataType::Type::kBool:
5236 __ movzxb(EAX, AL);
5237 break;
5238 case DataType::Type::kInt8:
5239 __ movsxb(EAX, AL);
5240 break;
5241 case DataType::Type::kUint16:
5242 __ movzxw(EAX, EAX);
5243 break;
5244 case DataType::Type::kInt16:
5245 __ movsxw(EAX, EAX);
5246 break;
5247 case DataType::Type::kFloat32:
5248 __ fstps(Address(ESP, 0));
5249 __ movss(XMM0, Address(ESP, 0));
5250 break;
5251 case DataType::Type::kFloat64:
5252 __ fstpl(Address(ESP, 0));
5253 __ movsd(XMM0, Address(ESP, 0));
5254 break;
5255 case DataType::Type::kInt32:
5256 case DataType::Type::kInt64:
5257 case DataType::Type::kVoid:
5258 break;
5259 default:
5260 DCHECK(false) << invoke->GetType();
5261 break;
5262 }
5263 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01005264 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01005265 }
5266 break;
5267 }
Vladimir Marko58155012015-08-19 12:49:41 +00005268 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5269 // (callee_method + offset_of_quick_compiled_code)()
5270 __ call(Address(callee_method.AsRegister<Register>(),
5271 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005272 kX86PointerSize).Int32Value()));
Vladimir Marko86c87522020-05-11 16:55:55 +01005273 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko58155012015-08-19 12:49:41 +00005274 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04005275 }
5276
5277 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04005278}
5279
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005280void CodeGeneratorX86::GenerateVirtualCall(
5281 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005282 Register temp = temp_in.AsRegister<Register>();
5283 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5284 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005285
5286 // Use the calling convention instead of the location of the receiver, as
5287 // intrinsics may have put the receiver in a different register. In the intrinsics
5288 // slow path, the arguments have been moved to the right place, so here we are
5289 // guaranteed that the receiver is the first register of the calling convention.
5290 InvokeDexCallingConvention calling_convention;
5291 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005292 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005293 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005294 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005295 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005296 // Instead of simply (possibly) unpoisoning `temp` here, we should
5297 // emit a read barrier for the previous class reference load.
5298 // However this is not required in practice, as this is an
5299 // intermediate/temporary reference and because the current
5300 // concurrent copying collector keeps the from-space memory
5301 // intact/accessible until the end of the marking phase (the
5302 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005303 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00005304
5305 MaybeGenerateInlineCacheCheck(invoke, temp);
5306
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005307 // temp = temp->GetMethodAt(method_offset);
5308 __ movl(temp, Address(temp, method_offset));
5309 // call temp->GetEntryPoint();
5310 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07005311 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005312 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005313}
5314
Vladimir Marko6fd16062018-06-26 11:02:04 +01005315void CodeGeneratorX86::RecordBootImageIntrinsicPatch(HX86ComputeBaseMethodAddress* method_address,
5316 uint32_t intrinsic_data) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005317 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005318 method_address, /* target_dex_file= */ nullptr, intrinsic_data);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005319 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005320}
5321
Vladimir Markob066d432018-01-03 13:14:37 +00005322void CodeGeneratorX86::RecordBootImageRelRoPatch(HX86ComputeBaseMethodAddress* method_address,
5323 uint32_t boot_image_offset) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005324 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005325 method_address, /* target_dex_file= */ nullptr, boot_image_offset);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005326 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Markob066d432018-01-03 13:14:37 +00005327}
5328
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005329void CodeGeneratorX86::RecordBootImageMethodPatch(HInvokeStaticOrDirect* invoke) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005330 HX86ComputeBaseMethodAddress* method_address =
Vladimir Marko65979462017-05-19 17:25:12 +01005331 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005332 boot_image_method_patches_.emplace_back(
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005333 method_address,
5334 invoke->GetResolvedMethodReference().dex_file,
5335 invoke->GetResolvedMethodReference().index);
Vladimir Marko65979462017-05-19 17:25:12 +01005336 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005337}
5338
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005339void CodeGeneratorX86::RecordMethodBssEntryPatch(HInvokeStaticOrDirect* invoke) {
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005340 DCHECK(IsSameDexFile(GetGraph()->GetDexFile(), *invoke->GetMethodReference().dex_file));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005341 HX86ComputeBaseMethodAddress* method_address =
5342 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005343 // Add the patch entry and bind its label at the end of the instruction.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005344 method_bss_entry_patches_.emplace_back(
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005345 method_address,
5346 invoke->GetMethodReference().dex_file,
5347 invoke->GetMethodReference().index);
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005348 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005349}
5350
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005351void CodeGeneratorX86::RecordBootImageTypePatch(HLoadClass* load_class) {
5352 HX86ComputeBaseMethodAddress* method_address =
5353 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5354 boot_image_type_patches_.emplace_back(
5355 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005356 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005357}
5358
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005359Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005360 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005361 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5362 type_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005363 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005364 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005365}
5366
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005367void CodeGeneratorX86::RecordBootImageStringPatch(HLoadString* load_string) {
5368 HX86ComputeBaseMethodAddress* method_address =
5369 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
5370 boot_image_string_patches_.emplace_back(
5371 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
5372 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01005373}
5374
Vladimir Markoaad75c62016-10-03 08:46:48 +00005375Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005376 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005377 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005378 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005379 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005380 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005381}
5382
Vladimir Markoeebb8212018-06-05 14:57:24 +01005383void CodeGeneratorX86::LoadBootImageAddress(Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01005384 uint32_t boot_image_reference,
Vladimir Markoeebb8212018-06-05 14:57:24 +01005385 HInvokeStaticOrDirect* invoke) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005386 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005387 HX86ComputeBaseMethodAddress* method_address =
5388 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5389 DCHECK(method_address != nullptr);
5390 Register method_address_reg =
5391 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005392 __ leal(reg, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005393 RecordBootImageIntrinsicPatch(method_address, boot_image_reference);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01005394 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01005395 HX86ComputeBaseMethodAddress* method_address =
5396 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5397 DCHECK(method_address != nullptr);
5398 Register method_address_reg =
5399 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005400 __ movl(reg, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005401 RecordBootImageRelRoPatch(method_address, boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01005402 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01005403 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01005404 gc::Heap* heap = Runtime::Current()->GetHeap();
5405 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01005406 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01005407 __ movl(reg, Immediate(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address))));
5408 }
5409}
5410
Vladimir Marko6fd16062018-06-26 11:02:04 +01005411void CodeGeneratorX86::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
5412 uint32_t boot_image_offset) {
5413 DCHECK(invoke->IsStatic());
5414 InvokeRuntimeCallingConvention calling_convention;
5415 Register argument = calling_convention.GetRegisterAt(0);
5416 if (GetCompilerOptions().IsBootImage()) {
5417 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
5418 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
Vladimir Marko6fd16062018-06-26 11:02:04 +01005419 HX86ComputeBaseMethodAddress* method_address =
5420 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5421 DCHECK(method_address != nullptr);
5422 Register method_address_reg =
5423 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005424 __ leal(argument, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005425 MethodReference target_method = invoke->GetResolvedMethodReference();
Vladimir Marko6fd16062018-06-26 11:02:04 +01005426 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
5427 boot_image_type_patches_.emplace_back(method_address, target_method.dex_file, type_idx.index_);
5428 __ Bind(&boot_image_type_patches_.back().label);
5429 } else {
5430 LoadBootImageAddress(argument, boot_image_offset, invoke);
5431 }
5432 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
5433 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
5434}
5435
Vladimir Markoaad75c62016-10-03 08:46:48 +00005436// The label points to the end of the "movl" or another instruction but the literal offset
5437// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
5438constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
5439
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005440template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005441inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005442 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005443 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005444 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005445 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005446 linker_patches->push_back(Factory(literal_offset,
5447 info.target_dex_file,
5448 GetMethodAddressOffset(info.method_address),
5449 info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005450 }
5451}
5452
Vladimir Marko6fd16062018-06-26 11:02:04 +01005453template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
5454linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
5455 const DexFile* target_dex_file,
5456 uint32_t pc_insn_offset,
5457 uint32_t boot_image_offset) {
5458 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5459 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005460}
5461
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005462void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005463 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005464 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01005465 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005466 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00005467 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01005468 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005469 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01005470 string_bss_entry_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01005471 boot_image_other_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005472 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01005473 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005474 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
5475 boot_image_method_patches_, linker_patches);
5476 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
5477 boot_image_type_patches_, linker_patches);
5478 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005479 boot_image_string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005480 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005481 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005482 DCHECK(boot_image_type_patches_.empty());
5483 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01005484 }
5485 if (GetCompilerOptions().IsBootImage()) {
5486 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5487 boot_image_other_patches_, linker_patches);
5488 } else {
5489 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
5490 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005491 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005492 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5493 method_bss_entry_patches_, linker_patches);
5494 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5495 type_bss_entry_patches_, linker_patches);
5496 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5497 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005498 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005499}
5500
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005501void CodeGeneratorX86::MarkGCCard(Register temp,
5502 Register card,
5503 Register object,
5504 Register value,
5505 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005506 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005507 if (value_can_be_null) {
5508 __ testl(value, value);
5509 __ j(kEqual, &is_null);
5510 }
Roland Levillainc73f0522018-08-14 15:16:50 +01005511 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07005512 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01005513 // Calculate the offset (in the card table) of the card corresponding to
5514 // `object`.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005515 __ movl(temp, object);
5516 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01005517 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
5518 // `object`'s card.
5519 //
5520 // Register `card` contains the address of the card table. Note that the card
5521 // table's base is biased during its creation so that it always starts at an
5522 // address whose least-significant byte is equal to `kCardDirty` (see
5523 // art::gc::accounting::CardTable::Create). Therefore the MOVB instruction
5524 // below writes the `kCardDirty` (byte) value into the `object`'s card
5525 // (located at `card + object >> kCardShift`).
5526 //
5527 // This dual use of the value in register `card` (1. to calculate the location
5528 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
5529 // (no need to explicitly load `kCardDirty` as an immediate value).
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00005530 __ movb(Address(temp, card, TIMES_1, 0),
5531 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005532 if (value_can_be_null) {
5533 __ Bind(&is_null);
5534 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005535}
5536
Calin Juravle52c48962014-12-16 17:02:57 +00005537void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5538 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005539
5540 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005541 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005542 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005543 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5544 kEmitCompilerReadBarrier
5545 ? LocationSummary::kCallOnSlowPath
5546 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005547 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005548 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005549 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005550 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005551
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005552 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005553 locations->SetOut(Location::RequiresFpuRegister());
5554 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005555 // The output overlaps in case of long: we don't want the low move
5556 // to overwrite the object's location. Likewise, in the case of
5557 // an object field get with read barriers enabled, we do not want
5558 // the move to overwrite the object's location, as we need it to emit
5559 // the read barrier.
5560 locations->SetOut(
5561 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005562 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005563 Location::kOutputOverlap :
5564 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005565 }
Calin Juravle52c48962014-12-16 17:02:57 +00005566
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005567 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00005568 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005569 // So we use an XMM register as a temp to achieve atomicity (first
5570 // load the temp into the XMM and then copy the XMM into the
5571 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00005572 locations->AddTemp(Location::RequiresFpuRegister());
5573 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005574}
5575
Calin Juravle52c48962014-12-16 17:02:57 +00005576void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
5577 const FieldInfo& field_info) {
5578 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005579
Calin Juravle52c48962014-12-16 17:02:57 +00005580 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005581 Location base_loc = locations->InAt(0);
5582 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005583 Location out = locations->Out();
5584 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01005585 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5586 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00005587 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5588
Vladimir Marko61b92282017-10-11 13:23:17 +01005589 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005590 case DataType::Type::kBool:
5591 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005592 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005593 break;
5594 }
5595
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005596 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005597 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005598 break;
5599 }
5600
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005601 case DataType::Type::kUint16: {
5602 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005603 break;
5604 }
5605
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005606 case DataType::Type::kInt16: {
5607 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005608 break;
5609 }
5610
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005611 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00005612 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005613 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005614
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005615 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005616 // /* HeapReference<Object> */ out = *(base + offset)
5617 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005618 // Note that a potential implicit null check is handled in this
5619 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
5620 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005621 instruction, out, base, offset, /* needs_null_check= */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005622 if (is_volatile) {
5623 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5624 }
5625 } else {
5626 __ movl(out.AsRegister<Register>(), Address(base, offset));
5627 codegen_->MaybeRecordImplicitNullCheck(instruction);
5628 if (is_volatile) {
5629 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5630 }
5631 // If read barriers are enabled, emit read barriers other than
5632 // Baker's using a slow path (and also unpoison the loaded
5633 // reference, if heap poisoning is enabled).
5634 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5635 }
5636 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005637 }
5638
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005639 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005640 if (is_volatile) {
5641 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5642 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005643 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005644 __ movd(out.AsRegisterPairLow<Register>(), temp);
5645 __ psrlq(temp, Immediate(32));
5646 __ movd(out.AsRegisterPairHigh<Register>(), temp);
5647 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005648 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00005649 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005650 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005651 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
5652 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005653 break;
5654 }
5655
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005656 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00005657 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005658 break;
5659 }
5660
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005661 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005662 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005663 break;
5664 }
5665
Aart Bik66c158e2018-01-31 12:55:04 -08005666 case DataType::Type::kUint32:
5667 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005668 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005669 LOG(FATAL) << "Unreachable type " << load_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005670 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005671 }
Calin Juravle52c48962014-12-16 17:02:57 +00005672
Vladimir Marko61b92282017-10-11 13:23:17 +01005673 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005674 // Potential implicit null checks, in the case of reference or
5675 // long fields, are handled in the previous switch statement.
5676 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005677 codegen_->MaybeRecordImplicitNullCheck(instruction);
5678 }
5679
Calin Juravle52c48962014-12-16 17:02:57 +00005680 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005681 if (load_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005682 // Memory barriers, in the case of references, are also handled
5683 // in the previous switch statement.
5684 } else {
5685 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5686 }
Roland Levillain4d027112015-07-01 15:41:14 +01005687 }
Calin Juravle52c48962014-12-16 17:02:57 +00005688}
5689
5690void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5691 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5692
5693 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005694 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00005695 locations->SetInAt(0, Location::RequiresRegister());
5696 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005697 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005698 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00005699
5700 // The register allocator does not support multiple
5701 // inputs that die at entry with one in a specific register.
5702 if (is_byte_type) {
5703 // Ensure the value is in a byte register.
5704 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005705 } else if (DataType::IsFloatingPointType(field_type)) {
5706 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05005707 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5708 locations->SetInAt(1, Location::RequiresFpuRegister());
5709 } else {
5710 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
5711 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005712 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05005713 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00005714 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005715
Calin Juravle52c48962014-12-16 17:02:57 +00005716 // 64bits value can be atomically written to an address with movsd and an XMM register.
5717 // We need two XMM registers because there's no easier way to (bit) copy a register pair
5718 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
5719 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
5720 // isolated cases when we need this it isn't worth adding the extra complexity.
5721 locations->AddTemp(Location::RequiresFpuRegister());
5722 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005723 } else {
5724 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5725
5726 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5727 // Temporary registers for the write barrier.
5728 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
5729 // Ensure the card is in a byte register.
5730 locations->AddTemp(Location::RegisterLocation(ECX));
5731 }
Calin Juravle52c48962014-12-16 17:02:57 +00005732 }
5733}
5734
5735void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Andra Danciucde98192020-09-13 12:32:09 +00005736 uint32_t value_index,
5737 DataType::Type field_type,
5738 Address field_addr,
5739 Register base,
5740 bool is_volatile,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005741 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005742 LocationSummary* locations = instruction->GetLocations();
Andra Danciucde98192020-09-13 12:32:09 +00005743 Location value = locations->InAt(value_index);
Roland Levillain4d027112015-07-01 15:41:14 +01005744 bool needs_write_barrier =
Andra Danciucde98192020-09-13 12:32:09 +00005745 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(value_index));
Calin Juravle52c48962014-12-16 17:02:57 +00005746
5747 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005748 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005749 }
5750
Mark Mendell81489372015-11-04 11:30:41 -05005751 bool maybe_record_implicit_null_check_done = false;
5752
Calin Juravle52c48962014-12-16 17:02:57 +00005753 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005754 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005755 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005756 case DataType::Type::kInt8: {
Andra Danciucde98192020-09-13 12:32:09 +00005757 if (value.IsConstant()) {
5758 __ movb(field_addr, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
5759 } else {
5760 __ movb(field_addr, value.AsRegister<ByteRegister>());
5761 }
Calin Juravle52c48962014-12-16 17:02:57 +00005762 break;
5763 }
5764
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005765 case DataType::Type::kUint16:
5766 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05005767 if (value.IsConstant()) {
Andra Danciucde98192020-09-13 12:32:09 +00005768 __ movw(field_addr, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05005769 } else {
Andra Danciucde98192020-09-13 12:32:09 +00005770 __ movw(field_addr, value.AsRegister<Register>());
Mark Mendell81489372015-11-04 11:30:41 -05005771 }
Calin Juravle52c48962014-12-16 17:02:57 +00005772 break;
5773 }
5774
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005775 case DataType::Type::kInt32:
5776 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01005777 if (kPoisonHeapReferences && needs_write_barrier) {
5778 // Note that in the case where `value` is a null reference,
5779 // we do not enter this block, as the reference does not
5780 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005781 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01005782 Register temp = locations->GetTemp(0).AsRegister<Register>();
5783 __ movl(temp, value.AsRegister<Register>());
5784 __ PoisonHeapReference(temp);
Andra Danciucde98192020-09-13 12:32:09 +00005785 __ movl(field_addr, temp);
Mark Mendell81489372015-11-04 11:30:41 -05005786 } else if (value.IsConstant()) {
5787 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Andra Danciucde98192020-09-13 12:32:09 +00005788 __ movl(field_addr, Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005789 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005790 DCHECK(value.IsRegister()) << value;
Andra Danciucde98192020-09-13 12:32:09 +00005791 __ movl(field_addr, value.AsRegister<Register>());
Roland Levillain4d027112015-07-01 15:41:14 +01005792 }
Calin Juravle52c48962014-12-16 17:02:57 +00005793 break;
5794 }
5795
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005796 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005797 if (is_volatile) {
5798 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5799 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5800 __ movd(temp1, value.AsRegisterPairLow<Register>());
5801 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5802 __ punpckldq(temp1, temp2);
Andra Danciucde98192020-09-13 12:32:09 +00005803 __ movsd(field_addr, temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005804 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005805 } else if (value.IsConstant()) {
5806 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
Andra Danciucde98192020-09-13 12:32:09 +00005807 __ movl(field_addr, Immediate(Low32Bits(v)));
Mark Mendell81489372015-11-04 11:30:41 -05005808 codegen_->MaybeRecordImplicitNullCheck(instruction);
Andra Danciucde98192020-09-13 12:32:09 +00005809 __ movl(field_addr.displaceBy(kX86WordSize), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005810 } else {
Andra Danciucde98192020-09-13 12:32:09 +00005811 __ movl(field_addr, value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005812 codegen_->MaybeRecordImplicitNullCheck(instruction);
Andra Danciucde98192020-09-13 12:32:09 +00005813 __ movl(field_addr.displaceBy(kX86WordSize), value.AsRegisterPairHigh<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00005814 }
Mark Mendell81489372015-11-04 11:30:41 -05005815 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005816 break;
5817 }
5818
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005819 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05005820 if (value.IsConstant()) {
5821 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
Andra Danciucde98192020-09-13 12:32:09 +00005822 __ movl(field_addr, Immediate(v));
Mark Mendell81489372015-11-04 11:30:41 -05005823 } else {
Andra Danciucde98192020-09-13 12:32:09 +00005824 __ movss(field_addr, value.AsFpuRegister<XmmRegister>());
Mark Mendell81489372015-11-04 11:30:41 -05005825 }
Calin Juravle52c48962014-12-16 17:02:57 +00005826 break;
5827 }
5828
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005829 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005830 if (value.IsConstant()) {
Andra Danciuc992e422020-09-16 08:12:02 +00005831 DCHECK(!is_volatile);
Mark Mendell81489372015-11-04 11:30:41 -05005832 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
Andra Danciucde98192020-09-13 12:32:09 +00005833 __ movl(field_addr, Immediate(Low32Bits(v)));
Mark Mendell81489372015-11-04 11:30:41 -05005834 codegen_->MaybeRecordImplicitNullCheck(instruction);
Andra Danciucde98192020-09-13 12:32:09 +00005835 __ movl(field_addr.displaceBy(kX86WordSize), Immediate(High32Bits(v)));
Mark Mendell81489372015-11-04 11:30:41 -05005836 maybe_record_implicit_null_check_done = true;
5837 } else {
Andra Danciucde98192020-09-13 12:32:09 +00005838 __ movsd(field_addr, value.AsFpuRegister<XmmRegister>());
Mark Mendell81489372015-11-04 11:30:41 -05005839 }
Calin Juravle52c48962014-12-16 17:02:57 +00005840 break;
5841 }
5842
Aart Bik66c158e2018-01-31 12:55:04 -08005843 case DataType::Type::kUint32:
5844 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005845 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005846 LOG(FATAL) << "Unreachable type " << field_type;
5847 UNREACHABLE();
5848 }
5849
Mark Mendell81489372015-11-04 11:30:41 -05005850 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005851 codegen_->MaybeRecordImplicitNullCheck(instruction);
5852 }
5853
Roland Levillain4d027112015-07-01 15:41:14 +01005854 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005855 Register temp = locations->GetTemp(0).AsRegister<Register>();
5856 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005857 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005858 }
5859
Calin Juravle52c48962014-12-16 17:02:57 +00005860 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005861 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005862 }
5863}
5864
Andra Danciucde98192020-09-13 12:32:09 +00005865void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
5866 const FieldInfo& field_info,
5867 bool value_can_be_null) {
5868 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5869
5870 LocationSummary* locations = instruction->GetLocations();
5871 Register base = locations->InAt(0).AsRegister<Register>();
5872 bool is_volatile = field_info.IsVolatile();
5873 DataType::Type field_type = field_info.GetFieldType();
5874 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5875
5876 Address field_addr(base, offset);
5877
5878 HandleFieldSet(instruction,
5879 /* value_index= */ 1,
5880 field_type,
5881 field_addr,
5882 base,
5883 is_volatile,
5884 value_can_be_null);
5885}
5886
Calin Juravle52c48962014-12-16 17:02:57 +00005887void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5888 HandleFieldGet(instruction, instruction->GetFieldInfo());
5889}
5890
5891void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5892 HandleFieldGet(instruction, instruction->GetFieldInfo());
5893}
5894
5895void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5896 HandleFieldSet(instruction, instruction->GetFieldInfo());
5897}
5898
5899void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005900 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005901}
5902
5903void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5904 HandleFieldSet(instruction, instruction->GetFieldInfo());
5905}
5906
5907void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005908 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005909}
5910
5911void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5912 HandleFieldGet(instruction, instruction->GetFieldInfo());
5913}
5914
5915void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5916 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005917}
5918
Vladimir Marko552a1342017-10-31 10:56:47 +00005919void LocationsBuilderX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5920 codegen_->CreateStringBuilderAppendLocations(instruction, Location::RegisterLocation(EAX));
5921}
5922
5923void InstructionCodeGeneratorX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5924 __ movl(EAX, Immediate(instruction->GetFormat()->GetValue()));
5925 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
5926}
5927
Calin Juravlee460d1d2015-09-29 04:52:17 +01005928void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5929 HUnresolvedInstanceFieldGet* instruction) {
5930 FieldAccessCallingConventionX86 calling_convention;
5931 codegen_->CreateUnresolvedFieldLocationSummary(
5932 instruction, instruction->GetFieldType(), calling_convention);
5933}
5934
5935void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5936 HUnresolvedInstanceFieldGet* instruction) {
5937 FieldAccessCallingConventionX86 calling_convention;
5938 codegen_->GenerateUnresolvedFieldAccess(instruction,
5939 instruction->GetFieldType(),
5940 instruction->GetFieldIndex(),
5941 instruction->GetDexPc(),
5942 calling_convention);
5943}
5944
5945void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5946 HUnresolvedInstanceFieldSet* instruction) {
5947 FieldAccessCallingConventionX86 calling_convention;
5948 codegen_->CreateUnresolvedFieldLocationSummary(
5949 instruction, instruction->GetFieldType(), calling_convention);
5950}
5951
5952void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5953 HUnresolvedInstanceFieldSet* instruction) {
5954 FieldAccessCallingConventionX86 calling_convention;
5955 codegen_->GenerateUnresolvedFieldAccess(instruction,
5956 instruction->GetFieldType(),
5957 instruction->GetFieldIndex(),
5958 instruction->GetDexPc(),
5959 calling_convention);
5960}
5961
5962void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5963 HUnresolvedStaticFieldGet* instruction) {
5964 FieldAccessCallingConventionX86 calling_convention;
5965 codegen_->CreateUnresolvedFieldLocationSummary(
5966 instruction, instruction->GetFieldType(), calling_convention);
5967}
5968
5969void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5970 HUnresolvedStaticFieldGet* instruction) {
5971 FieldAccessCallingConventionX86 calling_convention;
5972 codegen_->GenerateUnresolvedFieldAccess(instruction,
5973 instruction->GetFieldType(),
5974 instruction->GetFieldIndex(),
5975 instruction->GetDexPc(),
5976 calling_convention);
5977}
5978
5979void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5980 HUnresolvedStaticFieldSet* instruction) {
5981 FieldAccessCallingConventionX86 calling_convention;
5982 codegen_->CreateUnresolvedFieldLocationSummary(
5983 instruction, instruction->GetFieldType(), calling_convention);
5984}
5985
5986void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5987 HUnresolvedStaticFieldSet* instruction) {
5988 FieldAccessCallingConventionX86 calling_convention;
5989 codegen_->GenerateUnresolvedFieldAccess(instruction,
5990 instruction->GetFieldType(),
5991 instruction->GetFieldIndex(),
5992 instruction->GetDexPc(),
5993 calling_convention);
5994}
5995
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005996void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005997 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5998 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5999 ? Location::RequiresRegister()
6000 : Location::Any();
6001 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006002}
6003
Calin Juravle2ae48182016-03-16 14:05:09 +00006004void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
6005 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00006006 return;
6007 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006008 LocationSummary* locations = instruction->GetLocations();
6009 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00006010
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006011 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00006012 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006013}
6014
Calin Juravle2ae48182016-03-16 14:05:09 +00006015void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006016 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006017 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006018
6019 LocationSummary* locations = instruction->GetLocations();
6020 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006021
6022 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04006023 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006024 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006025 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006026 } else {
6027 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00006028 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006029 __ jmp(slow_path->GetEntryLabel());
6030 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006031 }
6032 __ j(kEqual, slow_path->GetEntryLabel());
6033}
6034
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006035void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006036 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006037}
6038
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006039void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006040 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006041 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006042 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006043 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
6044 object_array_get_with_read_barrier
6045 ? LocationSummary::kCallOnSlowPath
6046 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01006047 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006048 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006049 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006050 locations->SetInAt(0, Location::RequiresRegister());
6051 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006052 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01006053 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6054 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006055 // The output overlaps in case of long: we don't want the low move
6056 // to overwrite the array's location. Likewise, in the case of an
6057 // object array get with read barriers enabled, we do not want the
6058 // move to overwrite the array's location, as we need it to emit
6059 // the read barrier.
6060 locations->SetOut(
6061 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006062 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
6063 ? Location::kOutputOverlap
6064 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01006065 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006066}
6067
6068void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
6069 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006070 Location obj_loc = locations->InAt(0);
6071 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006072 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006073 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006074 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006075
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006076 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00006077 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006078 case DataType::Type::kBool:
6079 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006080 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006081 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006082 break;
6083 }
6084
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006085 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006086 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006087 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006088 break;
6089 }
6090
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006091 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006092 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07006093 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6094 // Branch cases into compressed and uncompressed for each index's type.
6095 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
6096 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00006097 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006098 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006099 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6100 "Expecting 0=compressed, 1=uncompressed");
6101 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07006102 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
6103 __ jmp(&done);
6104 __ Bind(&not_compressed);
6105 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
6106 __ Bind(&done);
6107 } else {
6108 // Common case for charAt of array of char or when string compression's
6109 // feature is turned off.
6110 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
6111 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006112 break;
6113 }
6114
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006115 case DataType::Type::kInt16: {
6116 Register out = out_loc.AsRegister<Register>();
6117 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
6118 break;
6119 }
6120
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006121 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006122 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006123 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006124 break;
6125 }
6126
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006127 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006128 static_assert(
6129 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6130 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00006131 // /* HeapReference<Object> */ out =
6132 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6133 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006134 // Note that a potential implicit null check is handled in this
6135 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
6136 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08006137 instruction, out_loc, obj, data_offset, index, /* needs_null_check= */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006138 } else {
6139 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006140 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
6141 codegen_->MaybeRecordImplicitNullCheck(instruction);
6142 // If read barriers are enabled, emit read barriers other than
6143 // Baker's using a slow path (and also unpoison the loaded
6144 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00006145 if (index.IsConstant()) {
6146 uint32_t offset =
6147 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006148 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6149 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006150 codegen_->MaybeGenerateReadBarrierSlow(
6151 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6152 }
6153 }
6154 break;
6155 }
6156
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006157 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006158 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006159 __ movl(out_loc.AsRegisterPairLow<Register>(),
6160 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
6161 codegen_->MaybeRecordImplicitNullCheck(instruction);
6162 __ movl(out_loc.AsRegisterPairHigh<Register>(),
6163 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006164 break;
6165 }
6166
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006167 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006168 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006169 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006170 break;
6171 }
6172
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006173 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006174 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006175 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006176 break;
6177 }
6178
Aart Bik66c158e2018-01-31 12:55:04 -08006179 case DataType::Type::kUint32:
6180 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006181 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00006182 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07006183 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006184 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006185
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006186 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006187 // Potential implicit null checks, in the case of reference or
6188 // long arrays, are handled in the previous switch statement.
6189 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00006190 codegen_->MaybeRecordImplicitNullCheck(instruction);
6191 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006192}
6193
6194void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006195 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006196
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006197 bool needs_write_barrier =
6198 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006199 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006200
Vladimir Markoca6fff82017-10-03 14:49:14 +01006201 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01006202 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006203 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006204
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006205 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006206 // We need the inputs to be different than the output in case of long operation.
6207 // In case of a byte operation, the register allocator does not support multiple
6208 // inputs that die at entry with one in a specific register.
6209 locations->SetInAt(0, Location::RequiresRegister());
6210 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6211 if (is_byte_type) {
6212 // Ensure the value is in a byte register.
6213 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006214 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05006215 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006216 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006217 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
6218 }
6219 if (needs_write_barrier) {
6220 // Temporary registers for the write barrier.
6221 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6222 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00006223 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006224 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006225}
6226
6227void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
6228 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006229 Location array_loc = locations->InAt(0);
6230 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006231 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006232 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006233 DataType::Type value_type = instruction->GetComponentType();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006234 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006235 bool needs_write_barrier =
6236 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006237
6238 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006239 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006240 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006241 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006242 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006243 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006244 if (value.IsRegister()) {
6245 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006246 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01006247 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006248 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006249 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006250 break;
6251 }
6252
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006253 case DataType::Type::kUint16:
6254 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006255 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006256 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006257 if (value.IsRegister()) {
6258 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006259 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01006260 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006261 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006262 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006263 break;
6264 }
6265
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006266 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006267 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006268 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006269
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006270 if (!value.IsRegister()) {
6271 // Just setting null.
6272 DCHECK(instruction->InputAt(2)->IsNullConstant());
6273 DCHECK(value.IsConstant()) << value;
6274 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00006275 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006276 DCHECK(!needs_write_barrier);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006277 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006278 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006279 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006280
6281 DCHECK(needs_write_barrier);
6282 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01006283 Location temp_loc = locations->GetTemp(0);
6284 Register temp = temp_loc.AsRegister<Register>();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006285
6286 bool can_value_be_null = instruction->GetValueCanBeNull();
6287 NearLabel do_store;
6288 if (can_value_be_null) {
6289 __ testl(register_value, register_value);
6290 __ j(kEqual, &do_store);
6291 }
6292
6293 SlowPathCode* slow_path = nullptr;
6294 if (needs_type_check) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006295 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006296 codegen_->AddSlowPath(slow_path);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006297
6298 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6299 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6300 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006301
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006302 // Note that when Baker read barriers are enabled, the type
6303 // checks are performed without read barriers. This is fine,
6304 // even in the case where a class object is in the from-space
6305 // after the flip, as a comparison involving such a type would
6306 // not produce a false positive; it may of course produce a
6307 // false negative, in which case we would take the ArraySet
6308 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01006309
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006310 // /* HeapReference<Class> */ temp = array->klass_
6311 __ movl(temp, Address(array, class_offset));
6312 codegen_->MaybeRecordImplicitNullCheck(instruction);
6313 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01006314
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006315 // /* HeapReference<Class> */ temp = temp->component_type_
6316 __ movl(temp, Address(temp, component_offset));
6317 // If heap poisoning is enabled, no need to unpoison `temp`
6318 // nor the object reference in `register_value->klass`, as
6319 // we are comparing two poisoned references.
6320 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01006321
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006322 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006323 NearLabel do_put;
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006324 __ j(kEqual, &do_put);
6325 // If heap poisoning is enabled, the `temp` reference has
6326 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006327 __ MaybeUnpoisonHeapReference(temp);
6328
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006329 // If heap poisoning is enabled, no need to unpoison the
6330 // heap reference loaded below, as it is only used for a
6331 // comparison with null.
6332 __ cmpl(Address(temp, super_offset), Immediate(0));
6333 __ j(kNotEqual, slow_path->GetEntryLabel());
6334 __ Bind(&do_put);
6335 } else {
6336 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006337 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006338 }
6339
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006340 Register card = locations->GetTemp(1).AsRegister<Register>();
6341 codegen_->MarkGCCard(
6342 temp, card, array, value.AsRegister<Register>(), /* value_can_be_null= */ false);
6343
6344 if (can_value_be_null) {
6345 DCHECK(do_store.IsLinked());
6346 __ Bind(&do_store);
6347 }
6348
6349 Register source = register_value;
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006350 if (kPoisonHeapReferences) {
6351 __ movl(temp, register_value);
6352 __ PoisonHeapReference(temp);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006353 source = temp;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006354 }
6355
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006356 __ movl(address, source);
6357
6358 if (can_value_be_null || !needs_type_check) {
6359 codegen_->MaybeRecordImplicitNullCheck(instruction);
6360 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006361
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006362 if (slow_path != nullptr) {
6363 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006364 }
6365
6366 break;
6367 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006368
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006369 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006370 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006371 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006372 if (value.IsRegister()) {
6373 __ movl(address, value.AsRegister<Register>());
6374 } else {
6375 DCHECK(value.IsConstant()) << value;
6376 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
6377 __ movl(address, Immediate(v));
6378 }
6379 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006380 break;
6381 }
6382
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006383 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006384 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006385 if (value.IsRegisterPair()) {
6386 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6387 value.AsRegisterPairLow<Register>());
6388 codegen_->MaybeRecordImplicitNullCheck(instruction);
6389 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6390 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006391 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006392 DCHECK(value.IsConstant());
6393 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
6394 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6395 Immediate(Low32Bits(val)));
6396 codegen_->MaybeRecordImplicitNullCheck(instruction);
6397 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6398 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006399 }
6400 break;
6401 }
6402
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006403 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006404 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006405 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006406 if (value.IsFpuRegister()) {
6407 __ movss(address, value.AsFpuRegister<XmmRegister>());
6408 } else {
6409 DCHECK(value.IsConstant());
6410 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
6411 __ movl(address, Immediate(v));
6412 }
6413 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006414 break;
6415 }
6416
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006417 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006418 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006419 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006420 if (value.IsFpuRegister()) {
6421 __ movsd(address, value.AsFpuRegister<XmmRegister>());
6422 } else {
6423 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006424 Address address_hi =
6425 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05006426 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
6427 __ movl(address, Immediate(Low32Bits(v)));
6428 codegen_->MaybeRecordImplicitNullCheck(instruction);
6429 __ movl(address_hi, Immediate(High32Bits(v)));
6430 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006431 break;
6432 }
6433
Aart Bik66c158e2018-01-31 12:55:04 -08006434 case DataType::Type::kUint32:
6435 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006436 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006437 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07006438 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006439 }
6440}
6441
6442void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006443 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006444 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04006445 if (!instruction->IsEmittedAtUseSite()) {
6446 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6447 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006448}
6449
6450void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04006451 if (instruction->IsEmittedAtUseSite()) {
6452 return;
6453 }
6454
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006455 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006456 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006457 Register obj = locations->InAt(0).AsRegister<Register>();
6458 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006459 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00006460 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07006461 // Mask out most significant bit in case the array is String's array of char.
6462 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006463 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006464 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006465}
6466
6467void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006468 RegisterSet caller_saves = RegisterSet::Empty();
6469 InvokeRuntimeCallingConvention calling_convention;
6470 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6471 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6472 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05006473 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04006474 HInstruction* length = instruction->InputAt(1);
6475 if (!length->IsEmittedAtUseSite()) {
6476 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6477 }
jessicahandojo4877b792016-09-08 19:49:13 -07006478 // Need register to see array's length.
6479 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6480 locations->AddTemp(Location::RequiresRegister());
6481 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006482}
6483
6484void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07006485 const bool is_string_compressed_char_at =
6486 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006487 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05006488 Location index_loc = locations->InAt(0);
6489 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006490 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006491 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006492
Mark Mendell99dbd682015-04-22 16:18:52 -04006493 if (length_loc.IsConstant()) {
6494 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
6495 if (index_loc.IsConstant()) {
6496 // BCE will remove the bounds check if we are guarenteed to pass.
6497 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6498 if (index < 0 || index >= length) {
6499 codegen_->AddSlowPath(slow_path);
6500 __ jmp(slow_path->GetEntryLabel());
6501 } else {
6502 // Some optimization after BCE may have generated this, and we should not
6503 // generate a bounds check if it is a valid range.
6504 }
6505 return;
6506 }
6507
6508 // We have to reverse the jump condition because the length is the constant.
6509 Register index_reg = index_loc.AsRegister<Register>();
6510 __ cmpl(index_reg, Immediate(length));
6511 codegen_->AddSlowPath(slow_path);
6512 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006513 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04006514 HInstruction* array_length = instruction->InputAt(1);
6515 if (array_length->IsEmittedAtUseSite()) {
6516 // Address the length field in the array.
6517 DCHECK(array_length->IsArrayLength());
6518 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
6519 Location array_loc = array_length->GetLocations()->InAt(0);
6520 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07006521 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006522 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
6523 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07006524 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
6525 __ movl(length_reg, array_len);
6526 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006527 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006528 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04006529 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006530 // Checking bounds for general case:
6531 // Array of char or string's array with feature compression off.
6532 if (index_loc.IsConstant()) {
6533 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6534 __ cmpl(array_len, Immediate(value));
6535 } else {
6536 __ cmpl(array_len, index_loc.AsRegister<Register>());
6537 }
6538 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04006539 }
Mark Mendell99dbd682015-04-22 16:18:52 -04006540 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006541 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04006542 }
6543 codegen_->AddSlowPath(slow_path);
6544 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006545 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006546}
6547
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006548void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006549 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006550}
6551
6552void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006553 if (instruction->GetNext()->IsSuspendCheck() &&
6554 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6555 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6556 // The back edge will generate the suspend check.
6557 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6558 }
6559
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006560 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6561}
6562
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006563void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006564 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6565 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07006566 // In suspend check slow path, usually there are no caller-save registers at all.
6567 // If SIMD instructions are present, however, we force spilling all live SIMD
6568 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07006569 locations->SetCustomSlowPathCallerSaves(
6570 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006571}
6572
6573void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006574 HBasicBlock* block = instruction->GetBlock();
6575 if (block->GetLoopInformation() != nullptr) {
6576 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6577 // The back edge will generate the suspend check.
6578 return;
6579 }
6580 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6581 // The goto will generate the suspend check.
6582 return;
6583 }
6584 GenerateSuspendCheck(instruction, nullptr);
6585}
6586
6587void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
6588 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006589 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006590 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
6591 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006592 slow_path =
6593 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006594 instruction->SetSlowPath(slow_path);
6595 codegen_->AddSlowPath(slow_path);
6596 if (successor != nullptr) {
6597 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006598 }
6599 } else {
6600 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6601 }
6602
Andreas Gampe542451c2016-07-26 09:02:02 -07006603 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006604 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006605 if (successor == nullptr) {
6606 __ j(kNotEqual, slow_path->GetEntryLabel());
6607 __ Bind(slow_path->GetReturnLabel());
6608 } else {
6609 __ j(kEqual, codegen_->GetLabelOf(successor));
6610 __ jmp(slow_path->GetEntryLabel());
6611 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006612}
6613
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006614X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
6615 return codegen_->GetAssembler();
6616}
6617
Aart Bikcfe50bb2017-12-12 14:54:12 -08006618void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006619 ScratchRegisterScope ensure_scratch(
6620 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6621 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6622 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05006623
Aart Bikcfe50bb2017-12-12 14:54:12 -08006624 // Now that temp register is available (possibly spilled), move blocks of memory.
6625 for (int i = 0; i < number_of_words; i++) {
6626 __ movl(temp_reg, Address(ESP, src + stack_offset));
6627 __ movl(Address(ESP, dst + stack_offset), temp_reg);
6628 stack_offset += kX86WordSize;
6629 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006630}
6631
6632void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006633 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006634 Location source = move->GetSource();
6635 Location destination = move->GetDestination();
6636
6637 if (source.IsRegister()) {
6638 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006639 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006640 } else if (destination.IsFpuRegister()) {
6641 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006642 } else {
6643 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006644 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006645 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006646 } else if (source.IsRegisterPair()) {
Vladimir Marko86c87522020-05-11 16:55:55 +01006647 if (destination.IsRegisterPair()) {
6648 __ movl(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
6649 DCHECK_NE(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
6650 __ movl(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
6651 } else if (destination.IsFpuRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006652 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Vladimir Markodec78172020-06-19 15:31:23 +01006653 // Push the 2 source registers to the stack.
Vladimir Marko86c87522020-05-11 16:55:55 +01006654 __ pushl(source.AsRegisterPairHigh<Register>());
6655 __ cfi().AdjustCFAOffset(elem_size);
6656 __ pushl(source.AsRegisterPairLow<Register>());
6657 __ cfi().AdjustCFAOffset(elem_size);
6658 // Load the destination register.
David Brazdil74eb1b22015-12-14 11:44:01 +00006659 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
6660 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01006661 codegen_->DecreaseFrame(2 * elem_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01006662 } else {
6663 DCHECK(destination.IsDoubleStackSlot());
6664 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
6665 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
6666 source.AsRegisterPairHigh<Register>());
6667 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006668 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006669 if (destination.IsRegister()) {
6670 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
6671 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006672 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006673 } else if (destination.IsRegisterPair()) {
Vladimir Marko86c87522020-05-11 16:55:55 +01006674 size_t elem_size = DataType::Size(DataType::Type::kInt32);
6675 // Create stack space for 2 elements.
Vladimir Markodec78172020-06-19 15:31:23 +01006676 codegen_->IncreaseFrame(2 * elem_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01006677 // Store the source register.
6678 __ movsd(Address(ESP, 0), source.AsFpuRegister<XmmRegister>());
6679 // And pop the values into destination registers.
6680 __ popl(destination.AsRegisterPairLow<Register>());
6681 __ cfi().AdjustCFAOffset(-elem_size);
6682 __ popl(destination.AsRegisterPairHigh<Register>());
6683 __ cfi().AdjustCFAOffset(-elem_size);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006684 } else if (destination.IsStackSlot()) {
6685 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006686 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006687 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006688 } else {
6689 DCHECK(destination.IsSIMDStackSlot());
6690 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05006691 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006692 } else if (source.IsStackSlot()) {
6693 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006694 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006695 } else if (destination.IsFpuRegister()) {
6696 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006697 } else {
6698 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08006699 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006700 }
6701 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006702 if (destination.IsRegisterPair()) {
6703 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
6704 __ movl(destination.AsRegisterPairHigh<Register>(),
6705 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
6706 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006707 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6708 } else {
6709 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006710 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006711 }
Aart Bik5576f372017-03-23 16:17:37 -07006712 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006713 if (destination.IsFpuRegister()) {
6714 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6715 } else {
6716 DCHECK(destination.IsSIMDStackSlot());
6717 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6718 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006719 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006720 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006721 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006722 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006723 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006724 if (value == 0) {
6725 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
6726 } else {
6727 __ movl(destination.AsRegister<Register>(), Immediate(value));
6728 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006729 } else {
6730 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05006731 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006732 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006733 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006734 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006735 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006736 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006737 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006738 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6739 if (value == 0) {
6740 // Easy handling of 0.0.
6741 __ xorps(dest, dest);
6742 } else {
6743 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006744 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6745 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
6746 __ movl(temp, Immediate(value));
6747 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006748 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006749 } else {
6750 DCHECK(destination.IsStackSlot()) << destination;
6751 __ movl(Address(ESP, destination.GetStackIndex()), imm);
6752 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006753 } else if (constant->IsLongConstant()) {
6754 int64_t value = constant->AsLongConstant()->GetValue();
6755 int32_t low_value = Low32Bits(value);
6756 int32_t high_value = High32Bits(value);
6757 Immediate low(low_value);
6758 Immediate high(high_value);
6759 if (destination.IsDoubleStackSlot()) {
6760 __ movl(Address(ESP, destination.GetStackIndex()), low);
6761 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6762 } else {
6763 __ movl(destination.AsRegisterPairLow<Register>(), low);
6764 __ movl(destination.AsRegisterPairHigh<Register>(), high);
6765 }
6766 } else {
6767 DCHECK(constant->IsDoubleConstant());
6768 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006769 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006770 int32_t low_value = Low32Bits(value);
6771 int32_t high_value = High32Bits(value);
6772 Immediate low(low_value);
6773 Immediate high(high_value);
6774 if (destination.IsFpuRegister()) {
6775 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6776 if (value == 0) {
6777 // Easy handling of 0.0.
6778 __ xorpd(dest, dest);
6779 } else {
6780 __ pushl(high);
Vladimir Marko86c87522020-05-11 16:55:55 +01006781 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006782 __ pushl(low);
Vladimir Marko86c87522020-05-11 16:55:55 +01006783 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006784 __ movsd(dest, Address(ESP, 0));
Vladimir Markodec78172020-06-19 15:31:23 +01006785 codegen_->DecreaseFrame(8);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006786 }
6787 } else {
6788 DCHECK(destination.IsDoubleStackSlot()) << destination;
6789 __ movl(Address(ESP, destination.GetStackIndex()), low);
6790 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6791 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006792 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006793 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006794 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006795 }
6796}
6797
Mark Mendella5c19ce2015-04-01 12:51:05 -04006798void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006799 Register suggested_scratch = reg == EAX ? EBX : EAX;
6800 ScratchRegisterScope ensure_scratch(
6801 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
6802
6803 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6804 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
6805 __ movl(Address(ESP, mem + stack_offset), reg);
6806 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006807}
6808
Mark Mendell7c8d0092015-01-26 11:21:33 -05006809void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006810 ScratchRegisterScope ensure_scratch(
6811 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6812
6813 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6814 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6815 __ movl(temp_reg, Address(ESP, mem + stack_offset));
6816 __ movss(Address(ESP, mem + stack_offset), reg);
6817 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006818}
6819
Aart Bikcfe50bb2017-12-12 14:54:12 -08006820void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
6821 size_t extra_slot = 4 * kX86WordSize;
Vladimir Markodec78172020-06-19 15:31:23 +01006822 codegen_->IncreaseFrame(extra_slot);
Aart Bikcfe50bb2017-12-12 14:54:12 -08006823 __ movups(Address(ESP, 0), XmmRegister(reg));
6824 ExchangeMemory(0, mem + extra_slot, 4);
6825 __ movups(XmmRegister(reg), Address(ESP, 0));
Vladimir Markodec78172020-06-19 15:31:23 +01006826 codegen_->DecreaseFrame(extra_slot);
Aart Bikcfe50bb2017-12-12 14:54:12 -08006827}
6828
6829void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006830 ScratchRegisterScope ensure_scratch1(
6831 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006832
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006833 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
6834 ScratchRegisterScope ensure_scratch2(
6835 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006836
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006837 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
6838 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006839
6840 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
6841 for (int i = 0; i < number_of_words; i++) {
6842 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
6843 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
6844 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
6845 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
6846 stack_offset += kX86WordSize;
6847 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006848}
6849
6850void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006851 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006852 Location source = move->GetSource();
6853 Location destination = move->GetDestination();
6854
6855 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04006856 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
6857 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
6858 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
6859 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
6860 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006861 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006862 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006863 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006864 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006865 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006866 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006867 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
6868 // Use XOR Swap algorithm to avoid a temporary.
6869 DCHECK_NE(source.reg(), destination.reg());
6870 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6871 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
6872 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6873 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
6874 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6875 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
6876 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006877 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
6878 // Take advantage of the 16 bytes in the XMM register.
6879 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
6880 Address stack(ESP, destination.GetStackIndex());
6881 // Load the double into the high doubleword.
6882 __ movhpd(reg, stack);
6883
6884 // Store the low double into the destination.
6885 __ movsd(stack, reg);
6886
6887 // Move the high double to the low double.
6888 __ psrldq(reg, Immediate(8));
6889 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6890 // Take advantage of the 16 bytes in the XMM register.
6891 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6892 Address stack(ESP, source.GetStackIndex());
6893 // Load the double into the high doubleword.
6894 __ movhpd(reg, stack);
6895
6896 // Store the low double into the destination.
6897 __ movsd(stack, reg);
6898
6899 // Move the high double to the low double.
6900 __ psrldq(reg, Immediate(8));
6901 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006902 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
6903 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
6904 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6905 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
6906 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6907 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
6908 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006909 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006910 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006911 }
6912}
6913
6914void ParallelMoveResolverX86::SpillScratch(int reg) {
6915 __ pushl(static_cast<Register>(reg));
6916}
6917
6918void ParallelMoveResolverX86::RestoreScratch(int reg) {
6919 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006920}
6921
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006922HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6923 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006924 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006925 case HLoadClass::LoadKind::kInvalid:
6926 LOG(FATAL) << "UNREACHABLE";
6927 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006928 case HLoadClass::LoadKind::kReferrersClass:
6929 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006930 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006931 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006932 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01006933 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006934 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006935 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006936 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01006937 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006938 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006939 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006940 break;
6941 }
6942 return desired_class_load_kind;
6943}
6944
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006945void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006946 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006947 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006948 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006949 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006950 cls,
6951 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006952 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006953 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006954 return;
6955 }
Vladimir Marko41559982017-01-06 14:04:23 +00006956 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006957
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006958 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6959 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006960 ? LocationSummary::kCallOnSlowPath
6961 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006962 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006963 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006964 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006965 }
6966
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006967 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006968 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006969 load_kind == HLoadClass::LoadKind::kBootImageRelRo ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006970 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006971 locations->SetInAt(0, Location::RequiresRegister());
6972 }
6973 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006974 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6975 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6976 // Rely on the type resolution and/or initialization to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006977 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006978 } else {
6979 // For non-Baker read barrier we have a temp-clobbering call.
6980 }
6981 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006982}
6983
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006984Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006985 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006986 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006987 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006988 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006989 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006990 PatchInfo<Label>* info = &jit_class_patches_.back();
6991 return &info->label;
6992}
6993
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006994// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6995// move.
6996void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006997 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006998 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006999 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01007000 return;
7001 }
Vladimir Marko41559982017-01-06 14:04:23 +00007002 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01007003
Vladimir Marko41559982017-01-06 14:04:23 +00007004 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007005 Location out_loc = locations->Out();
7006 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007007
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007008 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007009 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7010 ? kWithoutReadBarrier
7011 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00007012 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007013 case HLoadClass::LoadKind::kReferrersClass: {
7014 DCHECK(!cls->CanCallRuntime());
7015 DCHECK(!cls->MustGenerateClinitCheck());
7016 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7017 Register current_method = locations->InAt(0).AsRegister<Register>();
7018 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007019 cls,
7020 out_loc,
7021 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Andreas Gampe3db70682018-12-26 15:12:03 -08007022 /* fixup_label= */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007023 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007024 break;
7025 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007026 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007027 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7028 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007029 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007030 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007031 __ leal(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007032 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007033 break;
7034 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007035 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007036 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7037 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007038 __ movl(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007039 codegen_->RecordBootImageRelRoPatch(cls->InputAt(0)->AsX86ComputeBaseMethodAddress(),
7040 codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007041 break;
7042 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007043 case HLoadClass::LoadKind::kBssEntry: {
7044 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007045 Address address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007046 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
7047 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007048 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007049 generate_null_check = true;
7050 break;
7051 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007052 case HLoadClass::LoadKind::kJitBootImageAddress: {
7053 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7054 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
7055 DCHECK_NE(address, 0u);
7056 __ movl(out, Immediate(address));
7057 break;
7058 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007059 case HLoadClass::LoadKind::kJitTableAddress: {
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007060 Address address = Address::Absolute(CodeGeneratorX86::kPlaceholder32BitOffset);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007061 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007062 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007063 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00007064 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007065 break;
7066 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007067 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007068 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007069 LOG(FATAL) << "UNREACHABLE";
7070 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007071 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007072
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007073 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7074 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007075 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007076 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007077
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007078 if (generate_null_check) {
7079 __ testl(out, out);
7080 __ j(kEqual, slow_path->GetEntryLabel());
7081 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007082
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007083 if (cls->MustGenerateClinitCheck()) {
7084 GenerateClassInitializationCheck(slow_path, out);
7085 } else {
7086 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007087 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007088 }
7089}
7090
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007091void LocationsBuilderX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7092 InvokeRuntimeCallingConvention calling_convention;
7093 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7094 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
7095}
7096
7097void InstructionCodeGeneratorX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7098 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
7099}
7100
Orion Hodson18259d72018-04-12 11:18:23 +01007101void LocationsBuilderX86::VisitLoadMethodType(HLoadMethodType* load) {
7102 InvokeRuntimeCallingConvention calling_convention;
7103 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7104 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
7105}
7106
7107void InstructionCodeGeneratorX86::VisitLoadMethodType(HLoadMethodType* load) {
7108 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
7109}
7110
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007111void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
7112 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007113 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007114 locations->SetInAt(0, Location::RequiresRegister());
7115 if (check->HasUses()) {
7116 locations->SetOut(Location::SameAsFirstInput());
7117 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007118 // Rely on the type initialization to save everything we need.
7119 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007120}
7121
7122void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007123 // We assume the class to not be null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007124 SlowPathCode* slow_path =
7125 new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(check->GetLoadClass(), check);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007126 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00007127 GenerateClassInitializationCheck(slow_path,
7128 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007129}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007130
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007131void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07007132 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00007133 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
7134 const size_t status_byte_offset =
7135 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
Vladimir Markobf121912019-06-04 13:49:05 +01007136 constexpr uint32_t shifted_visibly_initialized_value =
7137 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << (status_lsb_position % kBitsPerByte);
Vladimir Markodc682aa2018-01-04 18:42:57 +00007138
Vladimir Markobf121912019-06-04 13:49:05 +01007139 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_visibly_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00007140 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007141 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007142}
7143
Vladimir Marko175e7862018-03-27 09:03:13 +00007144void InstructionCodeGeneratorX86::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
7145 Register temp) {
7146 uint32_t path_to_root = check->GetBitstringPathToRoot();
7147 uint32_t mask = check->GetBitstringMask();
7148 DCHECK(IsPowerOfTwo(mask + 1));
7149 size_t mask_bits = WhichPowerOf2(mask + 1);
7150
7151 if (mask_bits == 16u) {
7152 // Compare the bitstring in memory.
7153 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
7154 } else {
7155 // /* uint32_t */ temp = temp->status_
7156 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
7157 // Compare the bitstring bits using SUB.
7158 __ subl(temp, Immediate(path_to_root));
7159 // Shift out bits that do not contribute to the comparison.
7160 __ shll(temp, Immediate(32u - mask_bits));
7161 }
7162}
7163
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007164HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
7165 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007166 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007167 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007168 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007169 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01007170 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007171 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007172 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007173 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01007174 DCHECK(GetCompilerOptions().IsJitCompiler());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007175 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007176 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007177 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007178 }
7179 return desired_string_load_kind;
7180}
7181
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007182void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007183 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007184 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007185 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007186 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007187 load_kind == HLoadString::LoadKind::kBootImageRelRo ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00007188 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007189 locations->SetInAt(0, Location::RequiresRegister());
7190 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007191 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007192 locations->SetOut(Location::RegisterLocation(EAX));
7193 } else {
7194 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007195 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7196 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007197 // Rely on the pResolveString to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007198 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007199 } else {
7200 // For non-Baker read barrier we have a temp-clobbering call.
7201 }
7202 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007203 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007204}
7205
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007206Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01007207 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007208 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007209 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007210 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007211 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007212 PatchInfo<Label>* info = &jit_string_patches_.back();
7213 return &info->label;
7214}
7215
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007216// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7217// move.
7218void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01007219 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007220 Location out_loc = locations->Out();
7221 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007222
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007223 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007224 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007225 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7226 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007227 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007228 __ leal(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007229 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007230 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007231 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007232 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007233 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7234 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007235 __ movl(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007236 codegen_->RecordBootImageRelRoPatch(load->InputAt(0)->AsX86ComputeBaseMethodAddress(),
7237 codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007238 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007239 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007240 case HLoadString::LoadKind::kBssEntry: {
7241 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007242 Address address = Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007243 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007244 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007245 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007246 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007247 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007248 codegen_->AddSlowPath(slow_path);
7249 __ testl(out, out);
7250 __ j(kEqual, slow_path->GetEntryLabel());
7251 __ Bind(slow_path->GetExitLabel());
7252 return;
7253 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007254 case HLoadString::LoadKind::kJitBootImageAddress: {
7255 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
7256 DCHECK_NE(address, 0u);
7257 __ movl(out, Immediate(address));
7258 return;
7259 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007260 case HLoadString::LoadKind::kJitTableAddress: {
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007261 Address address = Address::Absolute(CodeGeneratorX86::kPlaceholder32BitOffset);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007262 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007263 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007264 // /* GcRoot<mirror::String> */ out = *address
7265 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
7266 return;
7267 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007268 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007269 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007270 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007271
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007272 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007273 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007274 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007275 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007276 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7277 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007278}
7279
David Brazdilcb1c0552015-08-04 16:22:25 +01007280static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007281 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01007282}
7283
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007284void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
7285 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007286 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007287 locations->SetOut(Location::RequiresRegister());
7288}
7289
7290void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01007291 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
7292}
7293
7294void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007295 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01007296}
7297
7298void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7299 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007300}
7301
7302void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007303 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7304 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007305 InvokeRuntimeCallingConvention calling_convention;
7306 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7307}
7308
7309void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007310 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007311 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007312}
7313
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007314// Temp is used for read barrier.
7315static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7316 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00007317 !kUseBakerReadBarrier &&
7318 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00007319 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007320 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7321 return 1;
7322 }
7323 return 0;
7324}
7325
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007326// Interface case has 2 temps, one for holding the number of interfaces, one for the current
7327// interface pointer, the current interface is compared in memory.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007328// The other checks have one temp for loading the object's class.
7329static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007330 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007331 return 2;
7332 }
7333 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007334}
7335
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007336void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007337 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007338 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01007339 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007340 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007341 case TypeCheckKind::kExactCheck:
7342 case TypeCheckKind::kAbstractClassCheck:
7343 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007344 case TypeCheckKind::kArrayObjectCheck: {
7345 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7346 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7347 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007348 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007349 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007350 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007351 case TypeCheckKind::kUnresolvedCheck:
7352 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007353 call_kind = LocationSummary::kCallOnSlowPath;
7354 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007355 case TypeCheckKind::kBitstringCheck:
7356 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007357 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007358
Vladimir Markoca6fff82017-10-03 14:49:14 +01007359 LocationSummary* locations =
7360 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01007361 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007362 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007363 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007364 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007365 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7366 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7367 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7368 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7369 } else {
7370 locations->SetInAt(1, Location::Any());
7371 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007372 // Note that TypeCheckSlowPathX86 uses this "out" register too.
7373 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007374 // When read barriers are enabled, we need a temporary register for some cases.
7375 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007376}
7377
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007378void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007379 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007380 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007381 Location obj_loc = locations->InAt(0);
7382 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007383 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007384 Location out_loc = locations->Out();
7385 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007386 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7387 DCHECK_LE(num_temps, 1u);
7388 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007389 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007390 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7391 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7392 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07007393 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007394 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007395
7396 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007397 // Avoid null check if we know obj is not null.
7398 if (instruction->MustDoNullCheck()) {
7399 __ testl(obj, obj);
7400 __ j(kEqual, &zero);
7401 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007402
Roland Levillain7c1559a2015-12-15 10:55:36 +00007403 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007404 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007405 ReadBarrierOption read_barrier_option =
7406 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007407 // /* HeapReference<Class> */ out = obj->klass_
7408 GenerateReferenceLoadTwoRegisters(instruction,
7409 out_loc,
7410 obj_loc,
7411 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007412 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007413 if (cls.IsRegister()) {
7414 __ cmpl(out, cls.AsRegister<Register>());
7415 } else {
7416 DCHECK(cls.IsStackSlot()) << cls;
7417 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7418 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007419
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007420 // Classes must be equal for the instanceof to succeed.
7421 __ j(kNotEqual, &zero);
7422 __ movl(out, Immediate(1));
7423 __ jmp(&done);
7424 break;
7425 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007426
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007427 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007428 ReadBarrierOption read_barrier_option =
7429 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007430 // /* HeapReference<Class> */ out = obj->klass_
7431 GenerateReferenceLoadTwoRegisters(instruction,
7432 out_loc,
7433 obj_loc,
7434 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007435 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007436 // If the class is abstract, we eagerly fetch the super class of the
7437 // object to avoid doing a comparison we know will fail.
7438 NearLabel loop;
7439 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007440 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007441 GenerateReferenceLoadOneRegister(instruction,
7442 out_loc,
7443 super_offset,
7444 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007445 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007446 __ testl(out, out);
7447 // If `out` is null, we use it for the result, and jump to `done`.
7448 __ j(kEqual, &done);
7449 if (cls.IsRegister()) {
7450 __ cmpl(out, cls.AsRegister<Register>());
7451 } else {
7452 DCHECK(cls.IsStackSlot()) << cls;
7453 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7454 }
7455 __ j(kNotEqual, &loop);
7456 __ movl(out, Immediate(1));
7457 if (zero.IsLinked()) {
7458 __ jmp(&done);
7459 }
7460 break;
7461 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007462
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007463 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007464 ReadBarrierOption read_barrier_option =
7465 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007466 // /* HeapReference<Class> */ out = obj->klass_
7467 GenerateReferenceLoadTwoRegisters(instruction,
7468 out_loc,
7469 obj_loc,
7470 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007471 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007472 // Walk over the class hierarchy to find a match.
7473 NearLabel loop, success;
7474 __ Bind(&loop);
7475 if (cls.IsRegister()) {
7476 __ cmpl(out, cls.AsRegister<Register>());
7477 } else {
7478 DCHECK(cls.IsStackSlot()) << cls;
7479 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7480 }
7481 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007482 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007483 GenerateReferenceLoadOneRegister(instruction,
7484 out_loc,
7485 super_offset,
7486 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007487 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007488 __ testl(out, out);
7489 __ j(kNotEqual, &loop);
7490 // If `out` is null, we use it for the result, and jump to `done`.
7491 __ jmp(&done);
7492 __ Bind(&success);
7493 __ movl(out, Immediate(1));
7494 if (zero.IsLinked()) {
7495 __ jmp(&done);
7496 }
7497 break;
7498 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007499
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007500 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007501 ReadBarrierOption read_barrier_option =
7502 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007503 // /* HeapReference<Class> */ out = obj->klass_
7504 GenerateReferenceLoadTwoRegisters(instruction,
7505 out_loc,
7506 obj_loc,
7507 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007508 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007509 // Do an exact check.
7510 NearLabel exact_check;
7511 if (cls.IsRegister()) {
7512 __ cmpl(out, cls.AsRegister<Register>());
7513 } else {
7514 DCHECK(cls.IsStackSlot()) << cls;
7515 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7516 }
7517 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007518 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007519 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007520 GenerateReferenceLoadOneRegister(instruction,
7521 out_loc,
7522 component_offset,
7523 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007524 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007525 __ testl(out, out);
7526 // If `out` is null, we use it for the result, and jump to `done`.
7527 __ j(kEqual, &done);
7528 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
7529 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007530 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007531 __ movl(out, Immediate(1));
7532 __ jmp(&done);
7533 break;
7534 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007535
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007536 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007537 // No read barrier since the slow path will retry upon failure.
7538 // /* HeapReference<Class> */ out = obj->klass_
7539 GenerateReferenceLoadTwoRegisters(instruction,
7540 out_loc,
7541 obj_loc,
7542 class_offset,
7543 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007544 if (cls.IsRegister()) {
7545 __ cmpl(out, cls.AsRegister<Register>());
7546 } else {
7547 DCHECK(cls.IsStackSlot()) << cls;
7548 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7549 }
7550 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007551 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007552 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007553 codegen_->AddSlowPath(slow_path);
7554 __ j(kNotEqual, slow_path->GetEntryLabel());
7555 __ movl(out, Immediate(1));
7556 if (zero.IsLinked()) {
7557 __ jmp(&done);
7558 }
7559 break;
7560 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007561
Calin Juravle98893e12015-10-02 21:05:03 +01007562 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007563 case TypeCheckKind::kInterfaceCheck: {
7564 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007565 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00007566 // cases.
7567 //
7568 // We cannot directly call the InstanceofNonTrivial runtime
7569 // entry point without resorting to a type checking slow path
7570 // here (i.e. by calling InvokeRuntime directly), as it would
7571 // require to assign fixed registers for the inputs of this
7572 // HInstanceOf instruction (following the runtime calling
7573 // convention), which might be cluttered by the potential first
7574 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007575 //
7576 // TODO: Introduce a new runtime entry point taking the object
7577 // to test (instead of its class) as argument, and let it deal
7578 // with the read barrier issues. This will let us refactor this
7579 // case of the `switch` code as it was previously (with a direct
7580 // call to the runtime not using a type checking slow path).
7581 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007582 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007583 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007584 instruction, /* is_fatal= */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007585 codegen_->AddSlowPath(slow_path);
7586 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007587 if (zero.IsLinked()) {
7588 __ jmp(&done);
7589 }
7590 break;
7591 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007592
7593 case TypeCheckKind::kBitstringCheck: {
7594 // /* HeapReference<Class> */ temp = obj->klass_
7595 GenerateReferenceLoadTwoRegisters(instruction,
7596 out_loc,
7597 obj_loc,
7598 class_offset,
7599 kWithoutReadBarrier);
7600
7601 GenerateBitstringTypeCheckCompare(instruction, out);
7602 __ j(kNotEqual, &zero);
7603 __ movl(out, Immediate(1));
7604 __ jmp(&done);
7605 break;
7606 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007607 }
7608
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007609 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007610 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007611 __ xorl(out, out);
7612 }
7613
7614 if (done.IsLinked()) {
7615 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007616 }
7617
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007618 if (slow_path != nullptr) {
7619 __ Bind(slow_path->GetExitLabel());
7620 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007621}
7622
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007623void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007624 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007625 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007626 LocationSummary* locations =
7627 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007628 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007629 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7630 // Require a register for the interface check since there is a loop that compares the class to
7631 // a memory address.
7632 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007633 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7634 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7635 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7636 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007637 } else {
7638 locations->SetInAt(1, Location::Any());
7639 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007640 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007641 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
7642}
7643
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007644void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007645 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007646 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007647 Location obj_loc = locations->InAt(0);
7648 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007649 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007650 Location temp_loc = locations->GetTemp(0);
7651 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007652 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7653 DCHECK_GE(num_temps, 1u);
7654 DCHECK_LE(num_temps, 2u);
7655 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7656 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7657 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7658 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7659 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7660 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7661 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7662 const uint32_t object_array_data_offset =
7663 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007664
Vladimir Marko87584542017-12-12 17:47:52 +00007665 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007666 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007667 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7668 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007669 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007670
Roland Levillain0d5a2812015-11-13 10:07:31 +00007671 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007672 // Avoid null check if we know obj is not null.
7673 if (instruction->MustDoNullCheck()) {
7674 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007675 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007676 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007677
Roland Levillain0d5a2812015-11-13 10:07:31 +00007678 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007679 case TypeCheckKind::kExactCheck:
7680 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007681 // /* HeapReference<Class> */ temp = obj->klass_
7682 GenerateReferenceLoadTwoRegisters(instruction,
7683 temp_loc,
7684 obj_loc,
7685 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007686 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007687
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007688 if (cls.IsRegister()) {
7689 __ cmpl(temp, cls.AsRegister<Register>());
7690 } else {
7691 DCHECK(cls.IsStackSlot()) << cls;
7692 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7693 }
7694 // Jump to slow path for throwing the exception or doing a
7695 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007696 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007697 break;
7698 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007699
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007700 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007701 // /* HeapReference<Class> */ temp = obj->klass_
7702 GenerateReferenceLoadTwoRegisters(instruction,
7703 temp_loc,
7704 obj_loc,
7705 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007706 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007707
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007708 // If the class is abstract, we eagerly fetch the super class of the
7709 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007710 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007711 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007712 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007713 GenerateReferenceLoadOneRegister(instruction,
7714 temp_loc,
7715 super_offset,
7716 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007717 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007718
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007719 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7720 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007721 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007722 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007723
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007724 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007725 if (cls.IsRegister()) {
7726 __ cmpl(temp, cls.AsRegister<Register>());
7727 } else {
7728 DCHECK(cls.IsStackSlot()) << cls;
7729 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7730 }
7731 __ j(kNotEqual, &loop);
7732 break;
7733 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007734
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007735 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007736 // /* HeapReference<Class> */ temp = obj->klass_
7737 GenerateReferenceLoadTwoRegisters(instruction,
7738 temp_loc,
7739 obj_loc,
7740 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007741 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007742
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007743 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007744 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007745 __ Bind(&loop);
7746 if (cls.IsRegister()) {
7747 __ cmpl(temp, cls.AsRegister<Register>());
7748 } else {
7749 DCHECK(cls.IsStackSlot()) << cls;
7750 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7751 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007752 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007753
Roland Levillain0d5a2812015-11-13 10:07:31 +00007754 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007755 GenerateReferenceLoadOneRegister(instruction,
7756 temp_loc,
7757 super_offset,
7758 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007759 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007760
7761 // If the class reference currently in `temp` is not null, jump
7762 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007763 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007764 __ j(kNotZero, &loop);
7765 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007766 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007767 break;
7768 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007769
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007770 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007771 // /* HeapReference<Class> */ temp = obj->klass_
7772 GenerateReferenceLoadTwoRegisters(instruction,
7773 temp_loc,
7774 obj_loc,
7775 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007776 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007777
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007778 // Do an exact check.
7779 if (cls.IsRegister()) {
7780 __ cmpl(temp, cls.AsRegister<Register>());
7781 } else {
7782 DCHECK(cls.IsStackSlot()) << cls;
7783 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7784 }
7785 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007786
7787 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007788 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007789 GenerateReferenceLoadOneRegister(instruction,
7790 temp_loc,
7791 component_offset,
7792 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007793 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007794
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007795 // If the component type is null (i.e. the object not an array), jump to the slow path to
7796 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007797 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007798 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007799
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007800 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007801 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007802 break;
7803 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007804
Calin Juravle98893e12015-10-02 21:05:03 +01007805 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007806 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007807 // We cannot directly call the CheckCast runtime entry point
7808 // without resorting to a type checking slow path here (i.e. by
7809 // calling InvokeRuntime directly), as it would require to
7810 // assign fixed registers for the inputs of this HInstanceOf
7811 // instruction (following the runtime calling convention), which
7812 // might be cluttered by the potential first read barrier
7813 // emission at the beginning of this method.
7814 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007815 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007816
7817 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007818 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
7819 // We can not get false positives by doing this.
7820 // /* HeapReference<Class> */ temp = obj->klass_
7821 GenerateReferenceLoadTwoRegisters(instruction,
7822 temp_loc,
7823 obj_loc,
7824 class_offset,
7825 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007826
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007827 // /* HeapReference<Class> */ temp = temp->iftable_
7828 GenerateReferenceLoadTwoRegisters(instruction,
7829 temp_loc,
7830 temp_loc,
7831 iftable_offset,
7832 kWithoutReadBarrier);
7833 // Iftable is never null.
7834 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
7835 // Maybe poison the `cls` for direct comparison with memory.
7836 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
7837 // Loop through the iftable and check if any class matches.
7838 NearLabel start_loop;
7839 __ Bind(&start_loop);
7840 // Need to subtract first to handle the empty array case.
7841 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
7842 __ j(kNegative, type_check_slow_path->GetEntryLabel());
7843 // Go to next interface if the classes do not match.
7844 __ cmpl(cls.AsRegister<Register>(),
7845 CodeGeneratorX86::ArrayAddress(temp,
7846 maybe_temp2_loc,
7847 TIMES_4,
7848 object_array_data_offset));
7849 __ j(kNotEqual, &start_loop);
7850 // If `cls` was poisoned above, unpoison it.
7851 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007852 break;
7853 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007854
7855 case TypeCheckKind::kBitstringCheck: {
7856 // /* HeapReference<Class> */ temp = obj->klass_
7857 GenerateReferenceLoadTwoRegisters(instruction,
7858 temp_loc,
7859 obj_loc,
7860 class_offset,
7861 kWithoutReadBarrier);
7862
7863 GenerateBitstringTypeCheckCompare(instruction, temp);
7864 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
7865 break;
7866 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007867 }
7868 __ Bind(&done);
7869
Roland Levillain0d5a2812015-11-13 10:07:31 +00007870 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007871}
7872
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007873void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007874 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7875 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007876 InvokeRuntimeCallingConvention calling_convention;
7877 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7878}
7879
7880void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007881 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
7882 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01007883 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01007884 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007885 if (instruction->IsEnter()) {
7886 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7887 } else {
7888 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7889 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007890}
7891
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307892void LocationsBuilderX86::VisitX86AndNot(HX86AndNot* instruction) {
7893 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7894 DCHECK(DataType::IsIntOrLongType(instruction->GetType())) << instruction->GetType();
7895 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7896 locations->SetInAt(0, Location::RequiresRegister());
7897 locations->SetInAt(1, Location::RequiresRegister());
7898 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7899}
7900
7901void InstructionCodeGeneratorX86::VisitX86AndNot(HX86AndNot* instruction) {
7902 LocationSummary* locations = instruction->GetLocations();
7903 Location first = locations->InAt(0);
7904 Location second = locations->InAt(1);
7905 Location dest = locations->Out();
7906 if (instruction->GetResultType() == DataType::Type::kInt32) {
7907 __ andn(dest.AsRegister<Register>(),
7908 first.AsRegister<Register>(),
7909 second.AsRegister<Register>());
7910 } else {
7911 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
7912 __ andn(dest.AsRegisterPairLow<Register>(),
7913 first.AsRegisterPairLow<Register>(),
7914 second.AsRegisterPairLow<Register>());
7915 __ andn(dest.AsRegisterPairHigh<Register>(),
7916 first.AsRegisterPairHigh<Register>(),
7917 second.AsRegisterPairHigh<Register>());
7918 }
7919}
7920
7921void LocationsBuilderX86::VisitX86MaskOrResetLeastSetBit(HX86MaskOrResetLeastSetBit* instruction) {
7922 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7923 DCHECK(instruction->GetType() == DataType::Type::kInt32) << instruction->GetType();
7924 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7925 locations->SetInAt(0, Location::RequiresRegister());
7926 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7927}
7928
7929void InstructionCodeGeneratorX86::VisitX86MaskOrResetLeastSetBit(
7930 HX86MaskOrResetLeastSetBit* instruction) {
7931 LocationSummary* locations = instruction->GetLocations();
7932 Location src = locations->InAt(0);
7933 Location dest = locations->Out();
7934 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
7935 switch (instruction->GetOpKind()) {
7936 case HInstruction::kAnd:
7937 __ blsr(dest.AsRegister<Register>(), src.AsRegister<Register>());
7938 break;
7939 case HInstruction::kXor:
7940 __ blsmsk(dest.AsRegister<Register>(), src.AsRegister<Register>());
7941 break;
7942 default:
7943 LOG(FATAL) << "Unreachable";
7944 }
7945}
7946
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007947void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
7948void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
7949void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
7950
7951void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7952 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007953 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007954 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7955 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007956 locations->SetInAt(0, Location::RequiresRegister());
7957 locations->SetInAt(1, Location::Any());
7958 locations->SetOut(Location::SameAsFirstInput());
7959}
7960
7961void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
7962 HandleBitwiseOperation(instruction);
7963}
7964
7965void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
7966 HandleBitwiseOperation(instruction);
7967}
7968
7969void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
7970 HandleBitwiseOperation(instruction);
7971}
7972
7973void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7974 LocationSummary* locations = instruction->GetLocations();
7975 Location first = locations->InAt(0);
7976 Location second = locations->InAt(1);
7977 DCHECK(first.Equals(locations->Out()));
7978
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007979 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007980 if (second.IsRegister()) {
7981 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007982 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007983 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007984 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007985 } else {
7986 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007987 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007988 }
7989 } else if (second.IsConstant()) {
7990 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007991 __ andl(first.AsRegister<Register>(),
7992 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007993 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007994 __ orl(first.AsRegister<Register>(),
7995 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007996 } else {
7997 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00007998 __ xorl(first.AsRegister<Register>(),
7999 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008000 }
8001 } else {
8002 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00008003 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008004 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00008005 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008006 } else {
8007 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00008008 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008009 }
8010 }
8011 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008012 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008013 if (second.IsRegisterPair()) {
8014 if (instruction->IsAnd()) {
8015 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
8016 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
8017 } else if (instruction->IsOr()) {
8018 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
8019 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
8020 } else {
8021 DCHECK(instruction->IsXor());
8022 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
8023 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
8024 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008025 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008026 if (instruction->IsAnd()) {
8027 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
8028 __ andl(first.AsRegisterPairHigh<Register>(),
8029 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
8030 } else if (instruction->IsOr()) {
8031 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
8032 __ orl(first.AsRegisterPairHigh<Register>(),
8033 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
8034 } else {
8035 DCHECK(instruction->IsXor());
8036 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
8037 __ xorl(first.AsRegisterPairHigh<Register>(),
8038 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
8039 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008040 } else {
8041 DCHECK(second.IsConstant()) << second;
8042 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008043 int32_t low_value = Low32Bits(value);
8044 int32_t high_value = High32Bits(value);
8045 Immediate low(low_value);
8046 Immediate high(high_value);
8047 Register first_low = first.AsRegisterPairLow<Register>();
8048 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008049 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008050 if (low_value == 0) {
8051 __ xorl(first_low, first_low);
8052 } else if (low_value != -1) {
8053 __ andl(first_low, low);
8054 }
8055 if (high_value == 0) {
8056 __ xorl(first_high, first_high);
8057 } else if (high_value != -1) {
8058 __ andl(first_high, high);
8059 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008060 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008061 if (low_value != 0) {
8062 __ orl(first_low, low);
8063 }
8064 if (high_value != 0) {
8065 __ orl(first_high, high);
8066 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008067 } else {
8068 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008069 if (low_value != 0) {
8070 __ xorl(first_low, low);
8071 }
8072 if (high_value != 0) {
8073 __ xorl(first_high, high);
8074 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008075 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008076 }
8077 }
8078}
8079
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008080void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
8081 HInstruction* instruction,
8082 Location out,
8083 uint32_t offset,
8084 Location maybe_temp,
8085 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008086 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008087 if (read_barrier_option == kWithReadBarrier) {
8088 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008089 if (kUseBakerReadBarrier) {
8090 // Load with fast path based Baker's read barrier.
8091 // /* HeapReference<Object> */ out = *(out + offset)
8092 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008093 instruction, out, out_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008094 } else {
8095 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008096 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00008097 // in the following move operation, as we will need it for the
8098 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00008099 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008100 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008101 // /* HeapReference<Object> */ out = *(out + offset)
8102 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008103 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008104 }
8105 } else {
8106 // Plain load with no read barrier.
8107 // /* HeapReference<Object> */ out = *(out + offset)
8108 __ movl(out_reg, Address(out_reg, offset));
8109 __ MaybeUnpoisonHeapReference(out_reg);
8110 }
8111}
8112
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008113void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
8114 HInstruction* instruction,
8115 Location out,
8116 Location obj,
8117 uint32_t offset,
8118 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008119 Register out_reg = out.AsRegister<Register>();
8120 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008121 if (read_barrier_option == kWithReadBarrier) {
8122 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008123 if (kUseBakerReadBarrier) {
8124 // Load with fast path based Baker's read barrier.
8125 // /* HeapReference<Object> */ out = *(obj + offset)
8126 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008127 instruction, out, obj_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008128 } else {
8129 // Load with slow path based read barrier.
8130 // /* HeapReference<Object> */ out = *(obj + offset)
8131 __ movl(out_reg, Address(obj_reg, offset));
8132 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8133 }
8134 } else {
8135 // Plain load with no read barrier.
8136 // /* HeapReference<Object> */ out = *(obj + offset)
8137 __ movl(out_reg, Address(obj_reg, offset));
8138 __ MaybeUnpoisonHeapReference(out_reg);
8139 }
8140}
8141
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008142void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
8143 HInstruction* instruction,
8144 Location root,
8145 const Address& address,
8146 Label* fixup_label,
8147 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008148 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008149 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07008150 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008151 if (kUseBakerReadBarrier) {
8152 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
8153 // Baker's read barrier are used:
8154 //
Roland Levillaind966ce72017-02-09 16:20:14 +00008155 // root = obj.field;
8156 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8157 // if (temp != null) {
8158 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00008159 // }
8160
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008161 // /* GcRoot<mirror::Object> */ root = *address
8162 __ movl(root_reg, address);
8163 if (fixup_label != nullptr) {
8164 __ Bind(fixup_label);
8165 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008166 static_assert(
8167 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8168 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8169 "have different sizes.");
8170 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8171 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8172 "have different sizes.");
8173
Vladimir Marko953437b2016-08-24 08:30:46 +00008174 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008175 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008176 instruction, root, /* unpoison_ref_before_marking= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008177 codegen_->AddSlowPath(slow_path);
8178
Roland Levillaind966ce72017-02-09 16:20:14 +00008179 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
8180 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01008181 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00008182 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
8183 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008184 __ j(kNotEqual, slow_path->GetEntryLabel());
8185 __ Bind(slow_path->GetExitLabel());
8186 } else {
8187 // GC root loaded through a slow path for read barriers other
8188 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008189 // /* GcRoot<mirror::Object>* */ root = address
8190 __ leal(root_reg, address);
8191 if (fixup_label != nullptr) {
8192 __ Bind(fixup_label);
8193 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008194 // /* mirror::Object* */ root = root->Read()
8195 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8196 }
8197 } else {
8198 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008199 // /* GcRoot<mirror::Object> */ root = *address
8200 __ movl(root_reg, address);
8201 if (fixup_label != nullptr) {
8202 __ Bind(fixup_label);
8203 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00008204 // Note that GC roots are not affected by heap poisoning, thus we
8205 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008206 }
8207}
8208
8209void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8210 Location ref,
8211 Register obj,
8212 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00008213 bool needs_null_check) {
8214 DCHECK(kEmitCompilerReadBarrier);
8215 DCHECK(kUseBakerReadBarrier);
8216
8217 // /* HeapReference<Object> */ ref = *(obj + offset)
8218 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00008219 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008220}
8221
8222void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8223 Location ref,
8224 Register obj,
8225 uint32_t data_offset,
8226 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00008227 bool needs_null_check) {
8228 DCHECK(kEmitCompilerReadBarrier);
8229 DCHECK(kUseBakerReadBarrier);
8230
Roland Levillain3d312422016-06-23 13:53:42 +01008231 static_assert(
8232 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8233 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00008234 // /* HeapReference<Object> */ ref =
8235 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008236 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00008237 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008238}
8239
8240void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8241 Location ref,
8242 Register obj,
8243 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008244 bool needs_null_check,
8245 bool always_update_field,
8246 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008247 DCHECK(kEmitCompilerReadBarrier);
8248 DCHECK(kUseBakerReadBarrier);
8249
8250 // In slow path based read barriers, the read barrier call is
8251 // inserted after the original load. However, in fast path based
8252 // Baker's read barriers, we need to perform the load of
8253 // mirror::Object::monitor_ *before* the original reference load.
8254 // This load-load ordering is required by the read barrier.
8255 // The fast path/slow path (for Baker's algorithm) should look like:
8256 //
8257 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8258 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8259 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008260 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00008261 // if (is_gray) {
8262 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
8263 // }
8264 //
8265 // Note: the original implementation in ReadBarrier::Barrier is
8266 // slightly more complex as:
8267 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00008268 // the high-bits of rb_state, which are expected to be all zeroes
8269 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
8270 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008271 // - it performs additional checks that we do not do here for
8272 // performance reasons.
8273
8274 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00008275 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
8276
Vladimir Marko953437b2016-08-24 08:30:46 +00008277 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01008278 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008279 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00008280 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
8281 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
8282 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
8283
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008284 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00008285 // ref = ReadBarrier::Mark(ref);
8286 // At this point, just do the "if" and make sure that flags are preserved until the branch.
8287 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00008288 if (needs_null_check) {
8289 MaybeRecordImplicitNullCheck(instruction);
8290 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008291
8292 // Load fence to prevent load-load reordering.
8293 // Note that this is a no-op, thanks to the x86 memory model.
8294 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
8295
8296 // The actual reference load.
8297 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00008298 __ movl(ref_reg, src); // Flags are unaffected.
8299
8300 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
8301 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008302 SlowPathCode* slow_path;
8303 if (always_update_field) {
8304 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01008305 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008306 instruction, ref, obj, src, /* unpoison_ref_before_marking= */ true, *temp);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008307 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008308 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008309 instruction, ref, /* unpoison_ref_before_marking= */ true);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008310 }
Vladimir Marko953437b2016-08-24 08:30:46 +00008311 AddSlowPath(slow_path);
8312
8313 // We have done the "if" of the gray bit check above, now branch based on the flags.
8314 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00008315
8316 // Object* ref = ref_addr->AsMirrorPtr()
8317 __ MaybeUnpoisonHeapReference(ref_reg);
8318
Roland Levillain7c1559a2015-12-15 10:55:36 +00008319 __ Bind(slow_path->GetExitLabel());
8320}
8321
8322void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
8323 Location out,
8324 Location ref,
8325 Location obj,
8326 uint32_t offset,
8327 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008328 DCHECK(kEmitCompilerReadBarrier);
8329
Roland Levillain7c1559a2015-12-15 10:55:36 +00008330 // Insert a slow path based read barrier *after* the reference load.
8331 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008332 // If heap poisoning is enabled, the unpoisoning of the loaded
8333 // reference will be carried out by the runtime within the slow
8334 // path.
8335 //
8336 // Note that `ref` currently does not get unpoisoned (when heap
8337 // poisoning is enabled), which is alright as the `ref` argument is
8338 // not used by the artReadBarrierSlow entry point.
8339 //
8340 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008341 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00008342 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
8343 AddSlowPath(slow_path);
8344
Roland Levillain0d5a2812015-11-13 10:07:31 +00008345 __ jmp(slow_path->GetEntryLabel());
8346 __ Bind(slow_path->GetExitLabel());
8347}
8348
Roland Levillain7c1559a2015-12-15 10:55:36 +00008349void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
8350 Location out,
8351 Location ref,
8352 Location obj,
8353 uint32_t offset,
8354 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008355 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008356 // Baker's read barriers shall be handled by the fast path
8357 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
8358 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008359 // If heap poisoning is enabled, unpoisoning will be taken care of
8360 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008361 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008362 } else if (kPoisonHeapReferences) {
8363 __ UnpoisonHeapReference(out.AsRegister<Register>());
8364 }
8365}
8366
Roland Levillain7c1559a2015-12-15 10:55:36 +00008367void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8368 Location out,
8369 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008370 DCHECK(kEmitCompilerReadBarrier);
8371
Roland Levillain7c1559a2015-12-15 10:55:36 +00008372 // Insert a slow path based read barrier *after* the GC root load.
8373 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008374 // Note that GC roots are not affected by heap poisoning, so we do
8375 // not need to do anything special for this here.
8376 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008377 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008378 AddSlowPath(slow_path);
8379
Roland Levillain0d5a2812015-11-13 10:07:31 +00008380 __ jmp(slow_path->GetEntryLabel());
8381 __ Bind(slow_path->GetExitLabel());
8382}
8383
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008384void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008385 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008386 LOG(FATAL) << "Unreachable";
8387}
8388
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008389void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008390 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008391 LOG(FATAL) << "Unreachable";
8392}
8393
Mark Mendellfe57faa2015-09-18 09:26:15 -04008394// Simple implementation of packed switch - generate cascaded compare/jumps.
8395void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8396 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008397 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04008398 locations->SetInAt(0, Location::RequiresRegister());
8399}
8400
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008401void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
8402 int32_t lower_bound,
8403 uint32_t num_entries,
8404 HBasicBlock* switch_block,
8405 HBasicBlock* default_block) {
8406 // Figure out the correct compare values and jump conditions.
8407 // Handle the first compare/branch as a special case because it might
8408 // jump to the default case.
8409 DCHECK_GT(num_entries, 2u);
8410 Condition first_condition;
8411 uint32_t index;
8412 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
8413 if (lower_bound != 0) {
8414 first_condition = kLess;
8415 __ cmpl(value_reg, Immediate(lower_bound));
8416 __ j(first_condition, codegen_->GetLabelOf(default_block));
8417 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008418
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008419 index = 1;
8420 } else {
8421 // Handle all the compare/jumps below.
8422 first_condition = kBelow;
8423 index = 0;
8424 }
8425
8426 // Handle the rest of the compare/jumps.
8427 for (; index + 1 < num_entries; index += 2) {
8428 int32_t compare_to_value = lower_bound + index + 1;
8429 __ cmpl(value_reg, Immediate(compare_to_value));
8430 // Jump to successors[index] if value < case_value[index].
8431 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
8432 // Jump to successors[index + 1] if value == case_value[index + 1].
8433 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
8434 }
8435
8436 if (index != num_entries) {
8437 // There are an odd number of entries. Handle the last one.
8438 DCHECK_EQ(index + 1, num_entries);
8439 __ cmpl(value_reg, Immediate(lower_bound + index));
8440 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008441 }
8442
8443 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008444 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
8445 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008446 }
8447}
8448
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008449void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8450 int32_t lower_bound = switch_instr->GetStartValue();
8451 uint32_t num_entries = switch_instr->GetNumEntries();
8452 LocationSummary* locations = switch_instr->GetLocations();
8453 Register value_reg = locations->InAt(0).AsRegister<Register>();
8454
8455 GenPackedSwitchWithCompares(value_reg,
8456 lower_bound,
8457 num_entries,
8458 switch_instr->GetBlock(),
8459 switch_instr->GetDefaultBlock());
8460}
8461
Mark Mendell805b3b52015-09-18 14:10:29 -04008462void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8463 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008464 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04008465 locations->SetInAt(0, Location::RequiresRegister());
8466
8467 // Constant area pointer.
8468 locations->SetInAt(1, Location::RequiresRegister());
8469
8470 // And the temporary we need.
8471 locations->AddTemp(Location::RequiresRegister());
8472}
8473
8474void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8475 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008476 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04008477 LocationSummary* locations = switch_instr->GetLocations();
8478 Register value_reg = locations->InAt(0).AsRegister<Register>();
8479 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8480
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008481 if (num_entries <= kPackedSwitchJumpTableThreshold) {
8482 GenPackedSwitchWithCompares(value_reg,
8483 lower_bound,
8484 num_entries,
8485 switch_instr->GetBlock(),
8486 default_block);
8487 return;
8488 }
8489
Mark Mendell805b3b52015-09-18 14:10:29 -04008490 // Optimizing has a jump area.
8491 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
8492 Register constant_area = locations->InAt(1).AsRegister<Register>();
8493
8494 // Remove the bias, if needed.
8495 if (lower_bound != 0) {
8496 __ leal(temp_reg, Address(value_reg, -lower_bound));
8497 value_reg = temp_reg;
8498 }
8499
8500 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008501 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04008502 __ cmpl(value_reg, Immediate(num_entries - 1));
8503 __ j(kAbove, codegen_->GetLabelOf(default_block));
8504
8505 // We are in the range of the table.
8506 // Load (target-constant_area) from the jump table, indexing by the value.
8507 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
8508
8509 // Compute the actual target address by adding in constant_area.
8510 __ addl(temp_reg, constant_area);
8511
8512 // And jump.
8513 __ jmp(temp_reg);
8514}
8515
Mark Mendell0616ae02015-04-17 12:49:27 -04008516void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
8517 HX86ComputeBaseMethodAddress* insn) {
8518 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008519 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008520 locations->SetOut(Location::RequiresRegister());
8521}
8522
8523void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
8524 HX86ComputeBaseMethodAddress* insn) {
8525 LocationSummary* locations = insn->GetLocations();
8526 Register reg = locations->Out().AsRegister<Register>();
8527
8528 // Generate call to next instruction.
8529 Label next_instruction;
8530 __ call(&next_instruction);
8531 __ Bind(&next_instruction);
8532
8533 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008534 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04008535
8536 // Grab the return address off the stack.
8537 __ popl(reg);
8538}
8539
8540void LocationsBuilderX86::VisitX86LoadFromConstantTable(
8541 HX86LoadFromConstantTable* insn) {
8542 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008543 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008544
8545 locations->SetInAt(0, Location::RequiresRegister());
8546 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
8547
8548 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00008549 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008550 return;
8551 }
8552
8553 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008554 case DataType::Type::kFloat32:
8555 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008556 locations->SetOut(Location::RequiresFpuRegister());
8557 break;
8558
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008559 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008560 locations->SetOut(Location::RequiresRegister());
8561 break;
8562
8563 default:
8564 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8565 }
8566}
8567
8568void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00008569 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008570 return;
8571 }
8572
8573 LocationSummary* locations = insn->GetLocations();
8574 Location out = locations->Out();
8575 Register const_area = locations->InAt(0).AsRegister<Register>();
8576 HConstant *value = insn->GetConstant();
8577
8578 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008579 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008580 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008581 codegen_->LiteralFloatAddress(
8582 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008583 break;
8584
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008585 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008586 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008587 codegen_->LiteralDoubleAddress(
8588 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008589 break;
8590
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008591 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008592 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008593 codegen_->LiteralInt32Address(
8594 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008595 break;
8596
8597 default:
8598 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8599 }
8600}
8601
Mark Mendell0616ae02015-04-17 12:49:27 -04008602/**
8603 * Class to handle late fixup of offsets into constant area.
8604 */
Vladimir Marko5233f932015-09-29 19:01:15 +01008605class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04008606 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008607 RIPFixup(CodeGeneratorX86& codegen,
8608 HX86ComputeBaseMethodAddress* base_method_address,
8609 size_t offset)
8610 : codegen_(&codegen),
8611 base_method_address_(base_method_address),
8612 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008613
8614 protected:
8615 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
8616
8617 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008618 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008619
8620 private:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008621 void Process(const MemoryRegion& region, int pos) override {
Mark Mendell0616ae02015-04-17 12:49:27 -04008622 // Patch the correct offset for the instruction. The place to patch is the
8623 // last 4 bytes of the instruction.
8624 // The value to patch is the distance from the offset in the constant area
8625 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04008626 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008627 int32_t relative_position =
8628 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04008629
8630 // Patch in the right value.
8631 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
8632 }
8633
Mark Mendell0616ae02015-04-17 12:49:27 -04008634 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04008635 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008636};
8637
Mark Mendell805b3b52015-09-18 14:10:29 -04008638/**
8639 * Class to handle late fixup of offsets to a jump table that will be created in the
8640 * constant area.
8641 */
8642class JumpTableRIPFixup : public RIPFixup {
8643 public:
8644 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008645 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
8646 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008647
8648 void CreateJumpTable() {
8649 X86Assembler* assembler = codegen_->GetAssembler();
8650
8651 // Ensure that the reference to the jump table has the correct offset.
8652 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
8653 SetOffset(offset_in_constant_table);
8654
8655 // The label values in the jump table are computed relative to the
8656 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008657 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04008658
8659 // Populate the jump table with the correct values for the jump table.
8660 int32_t num_entries = switch_instr_->GetNumEntries();
8661 HBasicBlock* block = switch_instr_->GetBlock();
8662 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
8663 // The value that we want is the target offset - the position of the table.
8664 for (int32_t i = 0; i < num_entries; i++) {
8665 HBasicBlock* b = successors[i];
8666 Label* l = codegen_->GetLabelOf(b);
8667 DCHECK(l->IsBound());
8668 int32_t offset_to_block = l->Position() - relative_offset;
8669 assembler->AppendInt32(offset_to_block);
8670 }
8671 }
8672
8673 private:
8674 const HX86PackedSwitch* switch_instr_;
8675};
8676
8677void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
8678 // Generate the constant area if needed.
8679 X86Assembler* assembler = GetAssembler();
jaishank20d1c942019-03-08 15:08:17 +05308680
Mark Mendell805b3b52015-09-18 14:10:29 -04008681 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
8682 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
8683 // byte values.
8684 assembler->Align(4, 0);
8685 constant_area_start_ = assembler->CodeSize();
8686
8687 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008688 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04008689 jump_table->CreateJumpTable();
8690 }
8691
8692 // And now add the constant area to the generated code.
8693 assembler->AddConstantArea();
8694 }
8695
8696 // And finish up.
8697 CodeGenerator::Finalize(allocator);
8698}
8699
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008700Address CodeGeneratorX86::LiteralDoubleAddress(double v,
8701 HX86ComputeBaseMethodAddress* method_base,
8702 Register reg) {
8703 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008704 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008705 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008706}
8707
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008708Address CodeGeneratorX86::LiteralFloatAddress(float v,
8709 HX86ComputeBaseMethodAddress* method_base,
8710 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008711 AssemblerFixup* fixup =
8712 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008713 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008714}
8715
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008716Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
8717 HX86ComputeBaseMethodAddress* method_base,
8718 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008719 AssemblerFixup* fixup =
8720 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008721 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008722}
8723
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008724Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
8725 HX86ComputeBaseMethodAddress* method_base,
8726 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008727 AssemblerFixup* fixup =
8728 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008729 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008730}
8731
Aart Bika19616e2016-02-01 18:57:58 -08008732void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
8733 if (value == 0) {
8734 __ xorl(dest, dest);
8735 } else {
8736 __ movl(dest, Immediate(value));
8737 }
8738}
8739
8740void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
8741 if (value == 0) {
8742 __ testl(dest, dest);
8743 } else {
8744 __ cmpl(dest, Immediate(value));
8745 }
8746}
8747
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008748void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
8749 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07008750 GenerateIntCompare(lhs_reg, rhs);
8751}
8752
8753void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008754 if (rhs.IsConstant()) {
8755 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07008756 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008757 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07008758 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008759 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07008760 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008761 }
8762}
8763
8764Address CodeGeneratorX86::ArrayAddress(Register obj,
8765 Location index,
8766 ScaleFactor scale,
8767 uint32_t data_offset) {
8768 return index.IsConstant() ?
8769 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
8770 Address(obj, index.AsRegister<Register>(), scale, data_offset);
8771}
8772
Mark Mendell805b3b52015-09-18 14:10:29 -04008773Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
8774 Register reg,
8775 Register value) {
8776 // Create a fixup to be used to create and address the jump table.
8777 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008778 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04008779
8780 // We have to populate the jump tables.
8781 fixups_to_jump_tables_.push_back(table_fixup);
8782
8783 // We want a scaled address, as we are extracting the correct offset from the table.
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008784 return Address(reg, value, TIMES_4, kPlaceholder32BitOffset, table_fixup);
Mark Mendell805b3b52015-09-18 14:10:29 -04008785}
8786
Andreas Gampe85b62f22015-09-09 13:15:38 -07008787// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008788void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07008789 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008790 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008791 return;
8792 }
8793
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008794 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008795
8796 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
8797 if (target.Equals(return_loc)) {
8798 return;
8799 }
8800
8801 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8802 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008803 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008804 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008805 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
8806 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008807 GetMoveResolver()->EmitNativeCode(&parallel_move);
8808 } else {
8809 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01008810 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07008811 parallel_move.AddMove(return_loc, target, type, nullptr);
8812 GetMoveResolver()->EmitNativeCode(&parallel_move);
8813 }
8814}
8815
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008816void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
8817 const uint8_t* roots_data,
8818 const PatchInfo<Label>& info,
8819 uint64_t index_in_table) const {
8820 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
8821 uintptr_t address =
8822 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
Andreas Gampec55bb392018-09-21 00:02:02 +00008823 using unaligned_uint32_t __attribute__((__aligned__(1))) = uint32_t;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008824 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
8825 dchecked_integral_cast<uint32_t>(address);
8826}
8827
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008828void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8829 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008830 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008831 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008832 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008833 }
8834
8835 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008836 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008837 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008838 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008839 }
8840}
8841
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008842void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8843 ATTRIBUTE_UNUSED) {
8844 LOG(FATAL) << "Unreachable";
8845}
8846
8847void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8848 ATTRIBUTE_UNUSED) {
8849 LOG(FATAL) << "Unreachable";
8850}
8851
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05308852bool LocationsBuilderX86::CpuHasAvxFeatureFlag() {
8853 return codegen_->GetInstructionSetFeatures().HasAVX();
8854}
8855bool LocationsBuilderX86::CpuHasAvx2FeatureFlag() {
8856 return codegen_->GetInstructionSetFeatures().HasAVX2();
8857}
8858bool InstructionCodeGeneratorX86::CpuHasAvxFeatureFlag() {
8859 return codegen_->GetInstructionSetFeatures().HasAVX();
8860}
8861bool InstructionCodeGeneratorX86::CpuHasAvx2FeatureFlag() {
8862 return codegen_->GetInstructionSetFeatures().HasAVX2();
8863}
8864
Roland Levillain4d027112015-07-01 15:41:14 +01008865#undef __
8866
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008867} // namespace x86
8868} // namespace art