blob: 4e1994181eadf6480c092bbb2f875c2152889c55 [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"
Mark Mendell09ed1a32015-03-25 08:30:06 -040029#include "intrinsics.h"
30#include "intrinsics_x86.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000031#include "jit/profiling_info.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010032#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070033#include "lock_word.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070034#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070035#include "mirror/class-inl.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000036#include "scoped_thread_state_change-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010037#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000038#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010039#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010041#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000042
Vladimir Marko0a516052019-10-14 13:00:44 +000043namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010044
Roland Levillain0d5a2812015-11-13 10:07:31 +000045template<class MirrorType>
46class GcRoot;
47
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000048namespace x86 {
49
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010050static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010051static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050052static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010053
Mark Mendell24f2dfa2015-01-14 19:51:45 -050054static constexpr int kC2ConditionMask = 0x400;
55
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000056static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000057
Aart Bik1f8d51b2018-02-15 10:42:37 -080058static constexpr int64_t kDoubleNaN = INT64_C(0x7FF8000000000000);
59static constexpr int32_t kFloatNaN = INT32_C(0x7FC00000);
60
Vladimir Marko3232dbb2018-07-25 15:42:46 +010061static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
62 InvokeRuntimeCallingConvention calling_convention;
63 RegisterSet caller_saves = RegisterSet::Empty();
64 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
65 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
66 // that the the kPrimNot result register is the same as the first argument register.
67 return caller_saves;
68}
69
Roland Levillain7cbd27f2016-08-11 23:53:33 +010070// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
71#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070072#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010073
Andreas Gampe85b62f22015-09-09 13:15:38 -070074class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010075 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000076 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010078 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +010079 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000081 if (instruction_->CanThrowIntoCatchBlock()) {
82 // Live registers will be restored in the catch block if caught.
83 SaveLiveRegisters(codegen, instruction_->GetLocations());
84 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010085 x86_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexandre Rames8158f282015-08-07 10:26:17 +010086 instruction_,
87 instruction_->GetDexPc(),
88 this);
Roland Levillain888d0672015-11-23 18:53:50 +000089 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010090 }
91
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010092 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +010093
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010094 const char* GetDescription() const override { return "NullCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +010095
Nicolas Geoffraye5038322014-07-04 09:41:32 +010096 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010097 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
98};
99
Andreas Gampe85b62f22015-09-09 13:15:38 -0700100class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000101 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000102 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000103
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100104 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +0100105 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +0000106 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100107 x86_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000108 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000109 }
110
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100111 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100112
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100113 const char* GetDescription() const override { return "DivZeroCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100114
Calin Juravled0d48522014-11-04 16:40:20 +0000115 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000116 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
117};
118
Andreas Gampe85b62f22015-09-09 13:15:38 -0700119class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000120 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000121 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
122 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000123
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100124 void EmitNativeCode(CodeGenerator* codegen) override {
Calin Juravled0d48522014-11-04 16:40:20 +0000125 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000126 if (is_div_) {
127 __ negl(reg_);
128 } else {
129 __ movl(reg_, Immediate(0));
130 }
Calin Juravled0d48522014-11-04 16:40:20 +0000131 __ jmp(GetExitLabel());
132 }
133
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100134 const char* GetDescription() const override { return "DivRemMinusOneSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100135
Calin Juravled0d48522014-11-04 16:40:20 +0000136 private:
137 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000138 bool is_div_;
139 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000140};
141
Andreas Gampe85b62f22015-09-09 13:15:38 -0700142class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100143 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000144 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100145
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100146 void EmitNativeCode(CodeGenerator* codegen) override {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100147 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100148 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100149 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000150 // We're moving two locations to locations that could overlap, so we need a parallel
151 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000152 if (instruction_->CanThrowIntoCatchBlock()) {
153 // Live registers will be restored in the catch block if caught.
154 SaveLiveRegisters(codegen, instruction_->GetLocations());
155 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400156
157 // Are we using an array length from memory?
158 HInstruction* array_length = instruction_->InputAt(1);
159 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100160 InvokeRuntimeCallingConvention calling_convention;
Mark Mendellee8d9712016-07-12 11:13:15 -0400161 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
162 // Load the array length into our temporary.
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100163 HArrayLength* length = array_length->AsArrayLength();
164 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(length);
Mark Mendellee8d9712016-07-12 11:13:15 -0400165 Location array_loc = array_length->GetLocations()->InAt(0);
166 Address array_len(array_loc.AsRegister<Register>(), len_offset);
167 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
168 // Check for conflicts with index.
169 if (length_loc.Equals(locations->InAt(0))) {
170 // We know we aren't using parameter 2.
171 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
172 }
173 __ movl(length_loc.AsRegister<Register>(), array_len);
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100174 if (mirror::kUseStringCompression && length->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100175 __ shrl(length_loc.AsRegister<Register>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700176 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400177 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000178 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100179 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000180 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100181 DataType::Type::kInt32,
Mark Mendellee8d9712016-07-12 11:13:15 -0400182 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100183 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100184 DataType::Type::kInt32);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100185 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
186 ? kQuickThrowStringBounds
187 : kQuickThrowArrayBounds;
188 x86_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100189 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000190 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100191 }
192
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100193 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100194
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100195 const char* GetDescription() const override { return "BoundsCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100196
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100197 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100198 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
199};
200
Andreas Gampe85b62f22015-09-09 13:15:38 -0700201class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000202 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000203 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000204 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000205
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100206 void EmitNativeCode(CodeGenerator* codegen) override {
Aart Bikb13c65b2017-03-21 20:14:07 -0700207 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100208 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000209 __ Bind(GetEntryLabel());
Aart Bik24b905f2017-04-06 09:59:06 -0700210 SaveLiveRegisters(codegen, locations); // Only saves full width XMM for SIMD.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100211 x86_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000212 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Aart Bik24b905f2017-04-06 09:59:06 -0700213 RestoreLiveRegisters(codegen, locations); // Only restores full width XMM for SIMD.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100214 if (successor_ == nullptr) {
215 __ jmp(GetReturnLabel());
216 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100217 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100218 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000219 }
220
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100221 Label* GetReturnLabel() {
222 DCHECK(successor_ == nullptr);
223 return &return_label_;
224 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000225
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100226 HBasicBlock* GetSuccessor() const {
227 return successor_;
228 }
229
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100230 const char* GetDescription() const override { return "SuspendCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100231
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000232 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100233 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000234 Label return_label_;
235
236 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
237};
238
Vladimir Markoaad75c62016-10-03 08:46:48 +0000239class LoadStringSlowPathX86 : public SlowPathCode {
240 public:
241 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
242
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100243 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000244 LocationSummary* locations = instruction_->GetLocations();
245 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
246
247 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
248 __ Bind(GetEntryLabel());
249 SaveLiveRegisters(codegen, locations);
250
251 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000252 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
253 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index.index_));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000254 x86_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
255 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
256 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
257 RestoreLiveRegisters(codegen, locations);
258
Vladimir Markoaad75c62016-10-03 08:46:48 +0000259 __ jmp(GetExitLabel());
260 }
261
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100262 const char* GetDescription() const override { return "LoadStringSlowPathX86"; }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000263
264 private:
265 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
266};
267
Andreas Gampe85b62f22015-09-09 13:15:38 -0700268class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000269 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100270 LoadClassSlowPathX86(HLoadClass* cls, HInstruction* at)
271 : SlowPathCode(at), cls_(cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000272 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100273 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000274 }
275
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100276 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000277 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100278 Location out = locations->Out();
279 const uint32_t dex_pc = instruction_->GetDexPc();
280 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
281 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
282
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000283 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
284 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000285 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000286
287 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100288 if (must_resolve_type) {
289 DCHECK(IsSameDexFile(cls_->GetDexFile(), x86_codegen->GetGraph()->GetDexFile()));
290 dex::TypeIndex type_index = cls_->GetTypeIndex();
291 __ movl(calling_convention.GetRegisterAt(0), Immediate(type_index.index_));
Vladimir Marko9d479252018-07-24 11:35:20 +0100292 x86_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
293 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100294 // If we also must_do_clinit, the resolved type is now in the correct register.
295 } else {
296 DCHECK(must_do_clinit);
297 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
298 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), source);
299 }
300 if (must_do_clinit) {
301 x86_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
302 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Roland Levillain888d0672015-11-23 18:53:50 +0000303 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000304
305 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000306 if (out.IsValid()) {
307 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
308 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000309 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000310 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000311 __ jmp(GetExitLabel());
312 }
313
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100314 const char* GetDescription() const override { return "LoadClassSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100315
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000316 private:
317 // The class this slow path will load.
318 HLoadClass* const cls_;
319
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000320 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
321};
322
Andreas Gampe85b62f22015-09-09 13:15:38 -0700323class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000325 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000326 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000327
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100328 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000329 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000330 DCHECK(instruction_->IsCheckCast()
331 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000332
333 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
334 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000335
Vladimir Markoe619f6c2017-12-12 16:00:01 +0000336 if (kPoisonHeapReferences &&
337 instruction_->IsCheckCast() &&
338 instruction_->AsCheckCast()->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) {
339 // First, unpoison the `cls` reference that was poisoned for direct memory comparison.
340 __ UnpoisonHeapReference(locations->InAt(1).AsRegister<Register>());
341 }
342
Vladimir Marko87584542017-12-12 17:47:52 +0000343 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000344 SaveLiveRegisters(codegen, locations);
345 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000346
347 // We're moving two locations to locations that could overlap, so we need a parallel
348 // move resolver.
349 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800350 x86_codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800351 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100352 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800353 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800354 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100355 DataType::Type::kReference);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000356 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100357 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100358 instruction_,
359 instruction_->GetDexPc(),
360 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800361 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000362 } else {
363 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800364 x86_codegen->InvokeRuntime(kQuickCheckInstanceOf,
365 instruction_,
366 instruction_->GetDexPc(),
367 this);
368 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000369 }
370
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000371 if (!is_fatal_) {
372 if (instruction_->IsInstanceOf()) {
373 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
374 }
375 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000376
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000377 __ jmp(GetExitLabel());
378 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000379 }
380
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100381 const char* GetDescription() const override { return "TypeCheckSlowPathX86"; }
382 bool IsFatal() const override { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100383
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000384 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000385 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000386
387 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
388};
389
Andreas Gampe85b62f22015-09-09 13:15:38 -0700390class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700391 public:
Aart Bik42249c32016-01-07 15:33:50 -0800392 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000393 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700394
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100395 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +0100396 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700397 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100398 LocationSummary* locations = instruction_->GetLocations();
399 SaveLiveRegisters(codegen, locations);
400 InvokeRuntimeCallingConvention calling_convention;
401 x86_codegen->Load32BitValue(
402 calling_convention.GetRegisterAt(0),
403 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100404 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100405 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700406 }
407
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100408 const char* GetDescription() const override { return "DeoptimizationSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100409
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700410 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700411 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
412};
413
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100414class ArraySetSlowPathX86 : public SlowPathCode {
415 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000416 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100417
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100418 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100419 LocationSummary* locations = instruction_->GetLocations();
420 __ Bind(GetEntryLabel());
421 SaveLiveRegisters(codegen, locations);
422
423 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100424 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100425 parallel_move.AddMove(
426 locations->InAt(0),
427 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100428 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100429 nullptr);
430 parallel_move.AddMove(
431 locations->InAt(1),
432 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100433 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100434 nullptr);
435 parallel_move.AddMove(
436 locations->InAt(2),
437 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100438 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100439 nullptr);
440 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
441
442 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100443 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000444 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100445 RestoreLiveRegisters(codegen, locations);
446 __ jmp(GetExitLabel());
447 }
448
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100449 const char* GetDescription() const override { return "ArraySetSlowPathX86"; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100450
451 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100452 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
453};
454
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100455// Slow path marking an object reference `ref` during a read
456// barrier. The field `obj.field` in the object `obj` holding this
457// reference does not get updated by this slow path after marking (see
458// ReadBarrierMarkAndUpdateFieldSlowPathX86 below for that).
459//
460// This means that after the execution of this slow path, `ref` will
461// always be up-to-date, but `obj.field` may not; i.e., after the
462// flip, `ref` will be a to-space reference, but `obj.field` will
463// probably still be a from-space reference (unless it gets updated by
464// another thread, or if another thread installed another object
465// reference (different from `ref`) in `obj.field`).
Roland Levillain7c1559a2015-12-15 10:55:36 +0000466class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
467 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100468 ReadBarrierMarkSlowPathX86(HInstruction* instruction,
469 Location ref,
470 bool unpoison_ref_before_marking)
471 : SlowPathCode(instruction),
472 ref_(ref),
473 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000474 DCHECK(kEmitCompilerReadBarrier);
475 }
476
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100477 const char* GetDescription() const override { return "ReadBarrierMarkSlowPathX86"; }
Roland Levillain7c1559a2015-12-15 10:55:36 +0000478
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100479 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000480 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100481 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000482 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100483 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000484 DCHECK(instruction_->IsInstanceFieldGet() ||
485 instruction_->IsStaticFieldGet() ||
486 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100487 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000488 instruction_->IsLoadClass() ||
489 instruction_->IsLoadString() ||
490 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100491 instruction_->IsCheckCast() ||
Andra Danciu1ca6f322020-08-12 08:58:07 +0000492 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000493 << "Unexpected instruction in read barrier marking slow path: "
494 << instruction_->DebugName();
495
496 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100497 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000498 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100499 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000500 }
Roland Levillain4359e612016-07-20 11:32:19 +0100501 // No need to save live registers; it's taken care of by the
502 // entrypoint. Also, there is no need to update the stack mask,
503 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000504 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100505 DCHECK_NE(ref_reg, ESP);
506 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100507 // "Compact" slow path, saving two moves.
508 //
509 // Instead of using the standard runtime calling convention (input
510 // and output in EAX):
511 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100512 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100513 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100514 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100515 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100516 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100517 // of a dedicated entrypoint:
518 //
519 // rX <- ReadBarrierMarkRegX(rX)
520 //
Roland Levillain97c46462017-05-11 14:04:03 +0100521 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100522 // This runtime call does not require a stack map.
523 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000524 __ jmp(GetExitLabel());
525 }
526
527 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100528 // The location (register) of the marked object reference.
529 const Location ref_;
530 // Should the reference in `ref_` be unpoisoned prior to marking it?
531 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000532
533 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
534};
535
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100536// Slow path marking an object reference `ref` during a read barrier,
537// and if needed, atomically updating the field `obj.field` in the
538// object `obj` holding this reference after marking (contrary to
539// ReadBarrierMarkSlowPathX86 above, which never tries to update
540// `obj.field`).
541//
542// This means that after the execution of this slow path, both `ref`
543// and `obj.field` will be up-to-date; i.e., after the flip, both will
544// hold the same to-space reference (unless another thread installed
545// another object reference (different from `ref`) in `obj.field`).
546class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
547 public:
548 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
549 Location ref,
550 Register obj,
551 const Address& field_addr,
552 bool unpoison_ref_before_marking,
553 Register temp)
554 : SlowPathCode(instruction),
555 ref_(ref),
556 obj_(obj),
557 field_addr_(field_addr),
558 unpoison_ref_before_marking_(unpoison_ref_before_marking),
559 temp_(temp) {
560 DCHECK(kEmitCompilerReadBarrier);
561 }
562
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100563 const char* GetDescription() const override { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100564
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100565 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100566 LocationSummary* locations = instruction_->GetLocations();
567 Register ref_reg = ref_.AsRegister<Register>();
568 DCHECK(locations->CanCall());
569 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
570 // This slow path is only used by the UnsafeCASObject intrinsic.
571 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
572 << "Unexpected instruction in read barrier marking and field updating slow path: "
573 << instruction_->DebugName();
574 DCHECK(instruction_->GetLocations()->Intrinsified());
575 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
576
577 __ Bind(GetEntryLabel());
578 if (unpoison_ref_before_marking_) {
579 // Object* ref = ref_addr->AsMirrorPtr()
580 __ MaybeUnpoisonHeapReference(ref_reg);
581 }
582
583 // Save the old (unpoisoned) reference.
584 __ movl(temp_, ref_reg);
585
586 // No need to save live registers; it's taken care of by the
587 // entrypoint. Also, there is no need to update the stack mask,
588 // as this runtime call will not trigger a garbage collection.
589 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
590 DCHECK_NE(ref_reg, ESP);
591 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
592 // "Compact" slow path, saving two moves.
593 //
594 // Instead of using the standard runtime calling convention (input
595 // and output in EAX):
596 //
597 // EAX <- ref
598 // EAX <- ReadBarrierMark(EAX)
599 // ref <- EAX
600 //
601 // we just use rX (the register containing `ref`) as input and output
602 // of a dedicated entrypoint:
603 //
604 // rX <- ReadBarrierMarkRegX(rX)
605 //
Roland Levillain97c46462017-05-11 14:04:03 +0100606 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100607 // This runtime call does not require a stack map.
608 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
609
610 // If the new reference is different from the old reference,
611 // update the field in the holder (`*field_addr`).
612 //
613 // Note that this field could also hold a different object, if
614 // another thread had concurrently changed it. In that case, the
615 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
616 // operation below would abort the CAS, leaving the field as-is.
617 NearLabel done;
618 __ cmpl(temp_, ref_reg);
619 __ j(kEqual, &done);
620
621 // Update the the holder's field atomically. This may fail if
622 // mutator updates before us, but it's OK. This is achieved
623 // using a strong compare-and-set (CAS) operation with relaxed
624 // memory synchronization ordering, where the expected value is
625 // the old reference and the desired value is the new reference.
626 // This operation is implemented with a 32-bit LOCK CMPXLCHG
627 // instruction, which requires the expected value (the old
628 // reference) to be in EAX. Save EAX beforehand, and move the
629 // expected value (stored in `temp_`) into EAX.
630 __ pushl(EAX);
631 __ movl(EAX, temp_);
632
633 // Convenience aliases.
634 Register base = obj_;
635 Register expected = EAX;
636 Register value = ref_reg;
637
638 bool base_equals_value = (base == value);
639 if (kPoisonHeapReferences) {
640 if (base_equals_value) {
641 // If `base` and `value` are the same register location, move
642 // `value` to a temporary register. This way, poisoning
643 // `value` won't invalidate `base`.
644 value = temp_;
645 __ movl(value, base);
646 }
647
648 // Check that the register allocator did not assign the location
649 // of `expected` (EAX) to `value` nor to `base`, so that heap
650 // poisoning (when enabled) works as intended below.
651 // - If `value` were equal to `expected`, both references would
652 // be poisoned twice, meaning they would not be poisoned at
653 // all, as heap poisoning uses address negation.
654 // - If `base` were equal to `expected`, poisoning `expected`
655 // would invalidate `base`.
656 DCHECK_NE(value, expected);
657 DCHECK_NE(base, expected);
658
659 __ PoisonHeapReference(expected);
660 __ PoisonHeapReference(value);
661 }
662
663 __ LockCmpxchgl(field_addr_, value);
664
665 // If heap poisoning is enabled, we need to unpoison the values
666 // that were poisoned earlier.
667 if (kPoisonHeapReferences) {
668 if (base_equals_value) {
669 // `value` has been moved to a temporary register, no need
670 // to unpoison it.
671 } else {
672 __ UnpoisonHeapReference(value);
673 }
674 // No need to unpoison `expected` (EAX), as it is be overwritten below.
675 }
676
677 // Restore EAX.
678 __ popl(EAX);
679
680 __ Bind(&done);
681 __ jmp(GetExitLabel());
682 }
683
684 private:
685 // The location (register) of the marked object reference.
686 const Location ref_;
687 // The register containing the object holding the marked object reference field.
688 const Register obj_;
689 // The address of the marked reference field. The base of this address must be `obj_`.
690 const Address field_addr_;
691
692 // Should the reference in `ref_` be unpoisoned prior to marking it?
693 const bool unpoison_ref_before_marking_;
694
695 const Register temp_;
696
697 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
698};
699
Roland Levillain0d5a2812015-11-13 10:07:31 +0000700// Slow path generating a read barrier for a heap reference.
701class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
702 public:
703 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
704 Location out,
705 Location ref,
706 Location obj,
707 uint32_t offset,
708 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000709 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000710 out_(out),
711 ref_(ref),
712 obj_(obj),
713 offset_(offset),
714 index_(index) {
715 DCHECK(kEmitCompilerReadBarrier);
716 // If `obj` is equal to `out` or `ref`, it means the initial object
717 // has been overwritten by (or after) the heap object reference load
718 // to be instrumented, e.g.:
719 //
720 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000721 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000722 //
723 // In that case, we have lost the information about the original
724 // object, and the emitted read barrier cannot work properly.
725 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
726 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
727 }
728
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100729 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000730 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
731 LocationSummary* locations = instruction_->GetLocations();
732 Register reg_out = out_.AsRegister<Register>();
733 DCHECK(locations->CanCall());
734 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100735 DCHECK(instruction_->IsInstanceFieldGet() ||
736 instruction_->IsStaticFieldGet() ||
737 instruction_->IsArrayGet() ||
738 instruction_->IsInstanceOf() ||
739 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700740 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000741 << "Unexpected instruction in read barrier for heap reference slow path: "
742 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000743
744 __ Bind(GetEntryLabel());
745 SaveLiveRegisters(codegen, locations);
746
747 // We may have to change the index's value, but as `index_` is a
748 // constant member (like other "inputs" of this slow path),
749 // introduce a copy of it, `index`.
750 Location index = index_;
751 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100752 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000753 if (instruction_->IsArrayGet()) {
754 // Compute the actual memory offset and store it in `index`.
755 Register index_reg = index_.AsRegister<Register>();
756 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
757 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
758 // We are about to change the value of `index_reg` (see the
759 // calls to art::x86::X86Assembler::shll and
760 // art::x86::X86Assembler::AddImmediate below), but it has
761 // not been saved by the previous call to
762 // art::SlowPathCode::SaveLiveRegisters, as it is a
763 // callee-save register --
764 // art::SlowPathCode::SaveLiveRegisters does not consider
765 // callee-save registers, as it has been designed with the
766 // assumption that callee-save registers are supposed to be
767 // handled by the called function. So, as a callee-save
768 // register, `index_reg` _would_ eventually be saved onto
769 // the stack, but it would be too late: we would have
770 // changed its value earlier. Therefore, we manually save
771 // it here into another freely available register,
772 // `free_reg`, chosen of course among the caller-save
773 // registers (as a callee-save `free_reg` register would
774 // exhibit the same problem).
775 //
776 // Note we could have requested a temporary register from
777 // the register allocator instead; but we prefer not to, as
778 // this is a slow path, and we know we can find a
779 // caller-save register that is available.
780 Register free_reg = FindAvailableCallerSaveRegister(codegen);
781 __ movl(free_reg, index_reg);
782 index_reg = free_reg;
783 index = Location::RegisterLocation(index_reg);
784 } else {
785 // The initial register stored in `index_` has already been
786 // saved in the call to art::SlowPathCode::SaveLiveRegisters
787 // (as it is not a callee-save register), so we can freely
788 // use it.
789 }
790 // Shifting the index value contained in `index_reg` by the scale
791 // factor (2) cannot overflow in practice, as the runtime is
792 // unable to allocate object arrays with a size larger than
793 // 2^26 - 1 (that is, 2^28 - 4 bytes).
794 __ shll(index_reg, Immediate(TIMES_4));
795 static_assert(
796 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
797 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
798 __ AddImmediate(index_reg, Immediate(offset_));
799 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100800 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
801 // intrinsics, `index_` is not shifted by a scale factor of 2
802 // (as in the case of ArrayGet), as it is actually an offset
803 // to an object field within an object.
804 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000805 DCHECK(instruction_->GetLocations()->Intrinsified());
806 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
807 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
808 << instruction_->AsInvoke()->GetIntrinsic();
809 DCHECK_EQ(offset_, 0U);
810 DCHECK(index_.IsRegisterPair());
811 // UnsafeGet's offset location is a register pair, the low
812 // part contains the correct offset.
813 index = index_.ToLow();
814 }
815 }
816
817 // We're moving two or three locations to locations that could
818 // overlap, so we need a parallel move resolver.
819 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100820 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000821 parallel_move.AddMove(ref_,
822 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100823 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000824 nullptr);
825 parallel_move.AddMove(obj_,
826 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100827 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000828 nullptr);
829 if (index.IsValid()) {
830 parallel_move.AddMove(index,
831 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100832 DataType::Type::kInt32,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000833 nullptr);
834 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
835 } else {
836 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
837 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
838 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100839 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000840 CheckEntrypointTypes<
841 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
842 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
843
844 RestoreLiveRegisters(codegen, locations);
845 __ jmp(GetExitLabel());
846 }
847
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100848 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathX86"; }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000849
850 private:
851 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
852 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
853 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
854 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
855 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
856 return static_cast<Register>(i);
857 }
858 }
859 // We shall never fail to find a free caller-save register, as
860 // there are more than two core caller-save registers on x86
861 // (meaning it is possible to find one which is different from
862 // `ref` and `obj`).
863 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
864 LOG(FATAL) << "Could not find a free caller-save register";
865 UNREACHABLE();
866 }
867
Roland Levillain0d5a2812015-11-13 10:07:31 +0000868 const Location out_;
869 const Location ref_;
870 const Location obj_;
871 const uint32_t offset_;
872 // An additional location containing an index to an array.
873 // Only used for HArrayGet and the UnsafeGetObject &
874 // UnsafeGetObjectVolatile intrinsics.
875 const Location index_;
876
877 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
878};
879
880// Slow path generating a read barrier for a GC root.
881class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
882 public:
883 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000884 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000885 DCHECK(kEmitCompilerReadBarrier);
886 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000887
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100888 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000889 LocationSummary* locations = instruction_->GetLocations();
890 Register reg_out = out_.AsRegister<Register>();
891 DCHECK(locations->CanCall());
892 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000893 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
894 << "Unexpected instruction in read barrier for GC root slow path: "
895 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000896
897 __ Bind(GetEntryLabel());
898 SaveLiveRegisters(codegen, locations);
899
900 InvokeRuntimeCallingConvention calling_convention;
901 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
902 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100903 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000904 instruction_,
905 instruction_->GetDexPc(),
906 this);
907 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
908 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
909
910 RestoreLiveRegisters(codegen, locations);
911 __ jmp(GetExitLabel());
912 }
913
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100914 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathX86"; }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000915
916 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000917 const Location out_;
918 const Location root_;
919
920 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
921};
922
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100923#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100924// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
925#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100926
Aart Bike9f37602015-10-09 11:15:55 -0700927inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700928 switch (cond) {
929 case kCondEQ: return kEqual;
930 case kCondNE: return kNotEqual;
931 case kCondLT: return kLess;
932 case kCondLE: return kLessEqual;
933 case kCondGT: return kGreater;
934 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700935 case kCondB: return kBelow;
936 case kCondBE: return kBelowEqual;
937 case kCondA: return kAbove;
938 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700939 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100940 LOG(FATAL) << "Unreachable";
941 UNREACHABLE();
942}
943
Aart Bike9f37602015-10-09 11:15:55 -0700944// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100945inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
946 switch (cond) {
947 case kCondEQ: return kEqual;
948 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700949 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100950 case kCondLT: return kBelow;
951 case kCondLE: return kBelowEqual;
952 case kCondGT: return kAbove;
953 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700954 // Unsigned remain unchanged.
955 case kCondB: return kBelow;
956 case kCondBE: return kBelowEqual;
957 case kCondA: return kAbove;
958 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100959 }
960 LOG(FATAL) << "Unreachable";
961 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700962}
963
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100964void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100965 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100966}
967
968void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100969 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100970}
971
Vladimir Markoa0431112018-06-25 09:32:54 +0100972const X86InstructionSetFeatures& CodeGeneratorX86::GetInstructionSetFeatures() const {
973 return *GetCompilerOptions().GetInstructionSetFeatures()->AsX86InstructionSetFeatures();
974}
975
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100976size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
977 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
978 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100979}
980
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100981size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
982 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
983 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100984}
985
Mark Mendell7c8d0092015-01-26 11:21:33 -0500986size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700987 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700988 __ movups(Address(ESP, stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -0700989 } else {
990 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
991 }
Artem Serov6a0b6572019-07-26 20:38:37 +0100992 return GetSlowPathFPWidth();
Mark Mendell7c8d0092015-01-26 11:21:33 -0500993}
994
995size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700996 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700997 __ movups(XmmRegister(reg_id), Address(ESP, stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -0700998 } else {
999 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
1000 }
Artem Serov6a0b6572019-07-26 20:38:37 +01001001 return GetSlowPathFPWidth();
Mark Mendell7c8d0092015-01-26 11:21:33 -05001002}
1003
Calin Juravle175dc732015-08-25 15:42:32 +01001004void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
1005 HInstruction* instruction,
1006 uint32_t dex_pc,
1007 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001008 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001009 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
1010 if (EntrypointRequiresStackMap(entrypoint)) {
1011 RecordPcInfo(instruction, dex_pc, slow_path);
1012 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001013}
1014
Roland Levillaindec8f632016-07-22 17:10:06 +01001015void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1016 HInstruction* instruction,
1017 SlowPathCode* slow_path) {
1018 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001019 GenerateInvokeRuntime(entry_point_offset);
1020}
1021
1022void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001023 __ fs()->call(Address::Absolute(entry_point_offset));
1024}
1025
Mark Mendellfb8d2792015-03-31 22:16:59 -04001026CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001027 const CompilerOptions& compiler_options,
1028 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001029 : CodeGenerator(graph,
1030 kNumberOfCpuRegisters,
1031 kNumberOfXmmRegisters,
1032 kNumberOfRegisterPairs,
1033 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1034 arraysize(kCoreCalleeSaves))
1035 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001036 0,
1037 compiler_options,
1038 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001039 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001040 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001041 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001042 move_resolver_(graph->GetAllocator(), this),
1043 assembler_(graph->GetAllocator()),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001044 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1045 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1046 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1047 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001048 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001049 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +01001050 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001051 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1052 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001053 constant_area_start_(-1),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001054 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001055 method_address_offset_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001056 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001057 // Use a fake return address register to mimic Quick.
1058 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001059}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001060
David Brazdil58282f42016-01-14 12:45:10 +00001061void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001062 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001063 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001064}
1065
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001066InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001067 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001068 assembler_(codegen->GetAssembler()),
1069 codegen_(codegen) {}
1070
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001071static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001072 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001073}
1074
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001075void CodeGeneratorX86::MaybeIncrementHotness(bool is_frame_entry) {
1076 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1077 Register reg = EAX;
1078 if (is_frame_entry) {
1079 reg = kMethodRegisterArgument;
1080 } else {
1081 __ pushl(EAX);
Vladimir Markodec78172020-06-19 15:31:23 +01001082 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001083 __ movl(EAX, Address(ESP, kX86WordSize));
1084 }
1085 NearLabel overflow;
1086 __ cmpw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()),
1087 Immediate(ArtMethod::MaxCounter()));
1088 __ j(kEqual, &overflow);
1089 __ addw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()),
1090 Immediate(1));
1091 __ Bind(&overflow);
1092 if (!is_frame_entry) {
1093 __ popl(EAX);
Vladimir Markodec78172020-06-19 15:31:23 +01001094 __ cfi().AdjustCFAOffset(-4);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001095 }
1096 }
1097
1098 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffray095dc462020-08-17 16:40:28 +01001099 ScopedProfilingInfoUse spiu(
1100 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
1101 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001102 if (info != nullptr) {
1103 uint32_t address = reinterpret_cast32<uint32_t>(info);
1104 NearLabel done;
1105 if (HasEmptyFrame()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001106 CHECK(is_frame_entry);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001107 // Alignment
Vladimir Markodec78172020-06-19 15:31:23 +01001108 IncreaseFrame(8);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001109 // We need a temporary. The stub also expects the method at bottom of stack.
1110 __ pushl(EAX);
1111 __ cfi().AdjustCFAOffset(4);
1112 __ movl(EAX, Immediate(address));
1113 __ addw(Address(EAX, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()),
1114 Immediate(1));
1115 __ j(kCarryClear, &done);
1116 GenerateInvokeRuntime(
1117 GetThreadOffset<kX86PointerSize>(kQuickCompileOptimized).Int32Value());
1118 __ Bind(&done);
1119 // We don't strictly require to restore EAX, but this makes the generated
1120 // code easier to reason about.
1121 __ popl(EAX);
1122 __ cfi().AdjustCFAOffset(-4);
Vladimir Markodec78172020-06-19 15:31:23 +01001123 DecreaseFrame(8);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001124 } else {
1125 if (!RequiresCurrentMethod()) {
1126 CHECK(is_frame_entry);
1127 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1128 }
1129 // We need a temporary.
1130 __ pushl(EAX);
1131 __ cfi().AdjustCFAOffset(4);
1132 __ movl(EAX, Immediate(address));
1133 __ addw(Address(EAX, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()),
1134 Immediate(1));
1135 __ popl(EAX); // Put stack as expected before exiting or calling stub.
1136 __ cfi().AdjustCFAOffset(-4);
1137 __ j(kCarryClear, &done);
1138 GenerateInvokeRuntime(
1139 GetThreadOffset<kX86PointerSize>(kQuickCompileOptimized).Int32Value());
1140 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001141 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001142 }
1143 }
1144}
1145
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001146void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001147 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001148 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001149 bool skip_overflow_check =
1150 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001151 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001152
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001153 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001154 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86);
1155 __ testl(EAX, Address(ESP, -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001156 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001157 }
1158
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001159 if (!HasEmptyFrame()) {
1160 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1161 Register reg = kCoreCalleeSaves[i];
1162 if (allocated_registers_.ContainsCoreRegister(reg)) {
1163 __ pushl(reg);
1164 __ cfi().AdjustCFAOffset(kX86WordSize);
1165 __ cfi().RelOffset(DWARFReg(reg), 0);
1166 }
1167 }
Mark Mendell5f874182015-03-04 15:42:45 -05001168
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001169 int adjust = GetFrameSize() - FrameEntrySpillSize();
Vladimir Markodec78172020-06-19 15:31:23 +01001170 IncreaseFrame(adjust);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001171 // Save the current method if we need it. Note that we do not
1172 // do this in HCurrentMethod, as the instruction might have been removed
1173 // in the SSA graph.
1174 if (RequiresCurrentMethod()) {
1175 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1176 }
1177
1178 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1179 // Initialize should_deoptimize flag to 0.
1180 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
Mark Mendell5f874182015-03-04 15:42:45 -05001181 }
1182 }
1183
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001184 MaybeIncrementHotness(/* is_frame_entry= */ true);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001185}
1186
1187void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001188 __ cfi().RememberState();
1189 if (!HasEmptyFrame()) {
1190 int adjust = GetFrameSize() - FrameEntrySpillSize();
Vladimir Markodec78172020-06-19 15:31:23 +01001191 DecreaseFrame(adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001192
David Srbeckyc34dc932015-04-12 09:27:43 +01001193 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1194 Register reg = kCoreCalleeSaves[i];
1195 if (allocated_registers_.ContainsCoreRegister(reg)) {
1196 __ popl(reg);
1197 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1198 __ cfi().Restore(DWARFReg(reg));
1199 }
Mark Mendell5f874182015-03-04 15:42:45 -05001200 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001201 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001202 __ ret();
1203 __ cfi().RestoreState();
1204 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001205}
1206
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001207void CodeGeneratorX86::Bind(HBasicBlock* block) {
1208 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001209}
1210
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001211Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001212 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001213 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001214 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001215 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001216 case DataType::Type::kInt8:
1217 case DataType::Type::kUint16:
1218 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08001219 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001220 case DataType::Type::kInt32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001221 return Location::RegisterLocation(EAX);
1222
Aart Bik66c158e2018-01-31 12:55:04 -08001223 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001224 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001225 return Location::RegisterPairLocation(EAX, EDX);
1226
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001227 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001228 return Location::NoLocation();
1229
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001230 case DataType::Type::kFloat64:
1231 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001232 return Location::FpuRegisterLocation(XMM0);
1233 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001234
1235 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001236}
1237
1238Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1239 return Location::RegisterLocation(kMethodRegisterArgument);
1240}
1241
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001242Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001243 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001244 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001245 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001246 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001247 case DataType::Type::kInt8:
1248 case DataType::Type::kUint16:
1249 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001250 case DataType::Type::kInt32: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001251 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001252 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001253 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001254 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001255 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001256 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001257 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001258 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001259
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001260 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001261 uint32_t index = gp_index_;
1262 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001263 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001264 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001265 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1266 calling_convention.GetRegisterPairAt(index));
1267 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001268 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001269 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1270 }
1271 }
1272
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001273 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001274 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001275 stack_index_++;
1276 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1277 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1278 } else {
1279 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1280 }
1281 }
1282
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001283 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001284 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001285 stack_index_ += 2;
1286 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1287 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1288 } else {
1289 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001290 }
1291 }
1292
Aart Bik66c158e2018-01-31 12:55:04 -08001293 case DataType::Type::kUint32:
1294 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001295 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001296 LOG(FATAL) << "Unexpected parameter type " << type;
Elliott Hughesc1896c92018-11-29 11:33:18 -08001297 UNREACHABLE();
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001298 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001299 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001300}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001301
Vladimir Marko86c87522020-05-11 16:55:55 +01001302Location CriticalNativeCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
1303 DCHECK_NE(type, DataType::Type::kReference);
1304
1305 Location location;
1306 if (DataType::Is64BitType(type)) {
1307 location = Location::DoubleStackSlot(stack_offset_);
1308 stack_offset_ += 2 * kFramePointerSize;
1309 } else {
1310 location = Location::StackSlot(stack_offset_);
1311 stack_offset_ += kFramePointerSize;
1312 }
1313 if (for_register_allocation_) {
1314 location = Location::Any();
1315 }
1316 return location;
1317}
1318
1319Location CriticalNativeCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
1320 // We perform conversion to the managed ABI return register after the call if needed.
1321 InvokeDexCallingConventionVisitorX86 dex_calling_convention;
1322 return dex_calling_convention.GetReturnLocation(type);
1323}
1324
1325Location CriticalNativeCallingConventionVisitorX86::GetMethodLocation() const {
1326 // Pass the method in the hidden argument EAX.
1327 return Location::RegisterLocation(EAX);
1328}
1329
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001330void CodeGeneratorX86::Move32(Location destination, Location source) {
1331 if (source.Equals(destination)) {
1332 return;
1333 }
1334 if (destination.IsRegister()) {
1335 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001336 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001337 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001338 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001339 } else {
1340 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001341 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001342 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001343 } else if (destination.IsFpuRegister()) {
1344 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001345 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001346 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001347 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001348 } else {
1349 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001350 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001351 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001352 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001353 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001354 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001355 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001356 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001357 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001358 } else if (source.IsConstant()) {
1359 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001360 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001361 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001362 } else {
1363 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001364 __ pushl(Address(ESP, source.GetStackIndex()));
1365 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001366 }
1367 }
1368}
1369
1370void CodeGeneratorX86::Move64(Location destination, Location source) {
1371 if (source.Equals(destination)) {
1372 return;
1373 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001374 if (destination.IsRegisterPair()) {
1375 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001376 EmitParallelMoves(
1377 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1378 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001379 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001380 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001381 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001382 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001383 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001384 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1385 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1386 __ psrlq(src_reg, Immediate(32));
1387 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001388 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001389 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001390 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001391 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1392 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001393 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1394 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001395 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001396 if (source.IsFpuRegister()) {
1397 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1398 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001399 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001400 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001401 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Vladimir Markodec78172020-06-19 15:31:23 +01001402 // Push the 2 source registers to the stack.
1403 __ pushl(source.AsRegisterPairHigh<Register>());
1404 __ cfi().AdjustCFAOffset(elem_size);
1405 __ pushl(source.AsRegisterPairLow<Register>());
1406 __ cfi().AdjustCFAOffset(elem_size);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001407 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1408 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01001409 DecreaseFrame(2 * elem_size);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001410 } else {
1411 LOG(FATAL) << "Unimplemented";
1412 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001413 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001414 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001415 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001416 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001417 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001418 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001419 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001420 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001421 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001422 } else if (source.IsConstant()) {
1423 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001424 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1425 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001426 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001427 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1428 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001429 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001430 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001431 EmitParallelMoves(
1432 Location::StackSlot(source.GetStackIndex()),
1433 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001434 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001435 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001436 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001437 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001438 }
1439 }
1440}
1441
Andra Danciu1ca6f322020-08-12 08:58:07 +00001442static Address CreateAddress(Register base,
1443 Register index = Register::kNoRegister,
1444 ScaleFactor scale = TIMES_1,
1445 int32_t disp = 0) {
1446 if (index == Register::kNoRegister) {
1447 return Address(base, disp);
1448 }
1449
1450 return Address(base, index, scale, disp);
1451}
1452
1453void CodeGeneratorX86::MoveFromMemory(DataType::Type dst_type,
1454 Location dst,
1455 Register src_base,
1456 Register src_index,
1457 ScaleFactor src_scale,
1458 int32_t src_disp) {
1459 DCHECK(src_base != Register::kNoRegister);
1460 Address src = CreateAddress(src_base, src_index, src_scale, src_disp);
1461
1462 switch (dst_type) {
1463 case DataType::Type::kBool:
1464 case DataType::Type::kUint8:
1465 __ movzxb(dst.AsRegister<Register>(), src);
1466 break;
1467 case DataType::Type::kInt8:
1468 __ movsxb(dst.AsRegister<Register>(), src);
1469 break;
1470 case DataType::Type::kInt16:
1471 __ movsxw(dst.AsRegister<Register>(), src);
1472 break;
1473 case DataType::Type::kUint16:
1474 __ movzxw(dst.AsRegister<Register>(), src);
1475 break;
1476 case DataType::Type::kInt32:
1477 case DataType::Type::kUint32:
1478 __ movl(dst.AsRegister<Register>(), src);
1479 break;
1480 case DataType::Type::kInt64:
1481 case DataType::Type::kUint64: {
1482 Address src_next_4_bytes = CreateAddress(src_base, src_index, src_scale, src_disp + 4);
1483 __ movl(dst.AsRegisterPairLow<Register>(), src);
1484 __ movl(dst.AsRegisterPairHigh<Register>(), src_next_4_bytes);
1485 break;
1486 }
1487 case DataType::Type::kFloat32:
1488 __ movss(dst.AsFpuRegister<XmmRegister>(), src);
1489 break;
1490 case DataType::Type::kFloat64:
1491 __ movsd(dst.AsFpuRegister<XmmRegister>(), src);
1492 break;
1493 case DataType::Type::kVoid:
1494 case DataType::Type::kReference:
1495 LOG(FATAL) << "Unreachable type " << dst_type;
1496 }
1497}
1498
Andra Danciu73c31802020-09-01 13:17:05 +00001499void CodeGeneratorX86::MoveToMemory(DataType::Type src_type,
1500 Location src,
1501 Register dst_base,
1502 Register dst_index,
1503 ScaleFactor dst_scale,
1504 int32_t dst_disp) {
1505 DCHECK(dst_base != Register::kNoRegister);
1506 Address dst = CreateAddress(dst_base, dst_index, dst_scale, dst_disp);
1507
1508 switch (src_type) {
1509 case DataType::Type::kBool:
1510 case DataType::Type::kUint8:
1511 case DataType::Type::kInt8: {
1512 if (src.IsConstant()) {
1513 __ movb(dst, Immediate(CodeGenerator::GetInt8ValueOf(src.GetConstant())));
1514 } else {
1515 __ movb(dst, src.AsRegister<ByteRegister>());
1516 }
1517 break;
1518 }
1519 case DataType::Type::kUint16:
1520 case DataType::Type::kInt16: {
1521 if (src.IsConstant()) {
1522 __ movw(dst, Immediate(CodeGenerator::GetInt16ValueOf(src.GetConstant())));
1523 } else {
1524 __ movw(dst, src.AsRegister<Register>());
1525 }
1526 break;
1527 }
1528 case DataType::Type::kUint32:
1529 case DataType::Type::kInt32: {
1530 if (src.IsConstant()) {
1531 int32_t v = CodeGenerator::GetInt32ValueOf(src.GetConstant());
1532 __ movl(dst, Immediate(v));
1533 } else {
1534 __ movl(dst, src.AsRegister<Register>());
1535 }
1536 break;
1537 }
1538 case DataType::Type::kUint64:
1539 case DataType::Type::kInt64: {
1540 Address dst_next_4_bytes = CreateAddress(dst_base, dst_index, dst_scale, dst_disp + 4);
1541 if (src.IsConstant()) {
1542 int64_t v = CodeGenerator::GetInt64ValueOf(src.GetConstant());
1543 __ movl(dst, Immediate(Low32Bits(v)));
1544 __ movl(dst_next_4_bytes, Immediate(High32Bits(v)));
1545 } else {
1546 __ movl(dst, src.AsRegisterPairLow<Register>());
1547 __ movl(dst_next_4_bytes, src.AsRegisterPairHigh<Register>());
1548 }
1549 break;
1550 }
1551 case DataType::Type::kFloat32: {
1552 if (src.IsConstant()) {
1553 int32_t v = CodeGenerator::GetInt32ValueOf(src.GetConstant());
1554 __ movl(dst, Immediate(v));
1555 } else {
1556 __ movss(dst, src.AsFpuRegister<XmmRegister>());
1557 }
1558 break;
1559 }
1560 case DataType::Type::kFloat64: {
1561 Address dst_next_4_bytes = CreateAddress(dst_base, dst_index, dst_scale, dst_disp + 4);
1562 if (src.IsConstant()) {
1563 int64_t v = CodeGenerator::GetInt64ValueOf(src.GetConstant());
1564 __ movl(dst, Immediate(Low32Bits(v)));
1565 __ movl(dst_next_4_bytes, Immediate(High32Bits(v)));
1566 } else {
1567 __ movsd(dst, src.AsFpuRegister<XmmRegister>());
1568 }
1569 break;
1570 }
1571 case DataType::Type::kVoid:
1572 case DataType::Type::kReference:
1573 LOG(FATAL) << "Unreachable type " << src_type;
1574 }
1575}
1576
Calin Juravle175dc732015-08-25 15:42:32 +01001577void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1578 DCHECK(location.IsRegister());
1579 __ movl(location.AsRegister<Register>(), Immediate(value));
1580}
1581
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001582void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001583 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001584 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1585 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1586 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001587 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001588 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001589 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001590 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001591}
1592
1593void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1594 if (location.IsRegister()) {
1595 locations->AddTemp(location);
1596 } else if (location.IsRegisterPair()) {
1597 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1598 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1599 } else {
1600 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1601 }
1602}
1603
David Brazdilfc6a86a2015-06-26 10:33:45 +00001604void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001605 if (successor->IsExitBlock()) {
1606 DCHECK(got->GetPrevious()->AlwaysThrows());
1607 return; // no code needed
1608 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001609
1610 HBasicBlock* block = got->GetBlock();
1611 HInstruction* previous = got->GetPrevious();
1612
1613 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001614 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001615 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001616 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1617 return;
1618 }
1619
1620 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1621 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1622 }
1623 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001624 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001625 }
1626}
1627
David Brazdilfc6a86a2015-06-26 10:33:45 +00001628void LocationsBuilderX86::VisitGoto(HGoto* got) {
1629 got->SetLocations(nullptr);
1630}
1631
1632void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1633 HandleGoto(got, got->GetSuccessor());
1634}
1635
1636void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1637 try_boundary->SetLocations(nullptr);
1638}
1639
1640void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1641 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1642 if (!successor->IsExitBlock()) {
1643 HandleGoto(try_boundary, successor);
1644 }
1645}
1646
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001647void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001648 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001649}
1650
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001651void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001652}
1653
Mark Mendell152408f2015-12-31 12:28:50 -05001654template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001655void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001656 LabelType* true_label,
1657 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001658 if (cond->IsFPConditionTrueIfNaN()) {
1659 __ j(kUnordered, true_label);
1660 } else if (cond->IsFPConditionFalseIfNaN()) {
1661 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001662 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001663 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001664}
1665
Mark Mendell152408f2015-12-31 12:28:50 -05001666template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001667void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001668 LabelType* true_label,
1669 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001670 LocationSummary* locations = cond->GetLocations();
1671 Location left = locations->InAt(0);
1672 Location right = locations->InAt(1);
1673 IfCondition if_cond = cond->GetCondition();
1674
Mark Mendellc4701932015-04-10 13:18:51 -04001675 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001676 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001677 IfCondition true_high_cond = if_cond;
1678 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001679 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001680
1681 // Set the conditions for the test, remembering that == needs to be
1682 // decided using the low words.
1683 switch (if_cond) {
1684 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001685 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001686 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001687 break;
1688 case kCondLT:
1689 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001690 break;
1691 case kCondLE:
1692 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001693 break;
1694 case kCondGT:
1695 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001696 break;
1697 case kCondGE:
1698 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001699 break;
Aart Bike9f37602015-10-09 11:15:55 -07001700 case kCondB:
1701 false_high_cond = kCondA;
1702 break;
1703 case kCondBE:
1704 true_high_cond = kCondB;
1705 break;
1706 case kCondA:
1707 false_high_cond = kCondB;
1708 break;
1709 case kCondAE:
1710 true_high_cond = kCondA;
1711 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001712 }
1713
1714 if (right.IsConstant()) {
1715 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001716 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001717 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001718
Aart Bika19616e2016-02-01 18:57:58 -08001719 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001720 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001721 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001722 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001723 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001724 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001725 __ j(X86Condition(true_high_cond), true_label);
1726 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001727 }
1728 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001729 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001730 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001731 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001732 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001733
1734 __ cmpl(left_high, right_high);
1735 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001736 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001737 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001738 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001739 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001740 __ j(X86Condition(true_high_cond), true_label);
1741 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001742 }
1743 // Must be equal high, so compare the lows.
1744 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001745 } else {
1746 DCHECK(right.IsDoubleStackSlot());
1747 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1748 if (if_cond == kCondNE) {
1749 __ j(X86Condition(true_high_cond), true_label);
1750 } else if (if_cond == kCondEQ) {
1751 __ j(X86Condition(false_high_cond), false_label);
1752 } else {
1753 __ j(X86Condition(true_high_cond), true_label);
1754 __ j(X86Condition(false_high_cond), false_label);
1755 }
1756 // Must be equal high, so compare the lows.
1757 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001758 }
1759 // The last comparison might be unsigned.
1760 __ j(final_condition, true_label);
1761}
1762
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001763void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1764 Location rhs,
1765 HInstruction* insn,
1766 bool is_double) {
1767 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1768 if (is_double) {
1769 if (rhs.IsFpuRegister()) {
1770 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1771 } else if (const_area != nullptr) {
1772 DCHECK(const_area->IsEmittedAtUseSite());
1773 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1774 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001775 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1776 const_area->GetBaseMethodAddress(),
1777 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001778 } else {
1779 DCHECK(rhs.IsDoubleStackSlot());
1780 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1781 }
1782 } else {
1783 if (rhs.IsFpuRegister()) {
1784 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1785 } else if (const_area != nullptr) {
1786 DCHECK(const_area->IsEmittedAtUseSite());
1787 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1788 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001789 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1790 const_area->GetBaseMethodAddress(),
1791 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001792 } else {
1793 DCHECK(rhs.IsStackSlot());
1794 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1795 }
1796 }
1797}
1798
Mark Mendell152408f2015-12-31 12:28:50 -05001799template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001800void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001801 LabelType* true_target_in,
1802 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001803 // Generated branching requires both targets to be explicit. If either of the
1804 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001805 LabelType fallthrough_target;
1806 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1807 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001808
Mark Mendellc4701932015-04-10 13:18:51 -04001809 LocationSummary* locations = condition->GetLocations();
1810 Location left = locations->InAt(0);
1811 Location right = locations->InAt(1);
1812
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001813 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001814 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001815 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001816 GenerateLongComparesAndJumps(condition, true_target, false_target);
1817 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001818 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001819 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001820 GenerateFPJumps(condition, true_target, false_target);
1821 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001822 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001823 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001824 GenerateFPJumps(condition, true_target, false_target);
1825 break;
1826 default:
1827 LOG(FATAL) << "Unexpected compare type " << type;
1828 }
1829
David Brazdil0debae72015-11-12 18:37:00 +00001830 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001831 __ jmp(false_target);
1832 }
David Brazdil0debae72015-11-12 18:37:00 +00001833
1834 if (fallthrough_target.IsLinked()) {
1835 __ Bind(&fallthrough_target);
1836 }
Mark Mendellc4701932015-04-10 13:18:51 -04001837}
1838
David Brazdil0debae72015-11-12 18:37:00 +00001839static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1840 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1841 // are set only strictly before `branch`. We can't use the eflags on long/FP
1842 // conditions if they are materialized due to the complex branching.
1843 return cond->IsCondition() &&
1844 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001845 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1846 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001847}
1848
Mark Mendell152408f2015-12-31 12:28:50 -05001849template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001850void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001851 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001852 LabelType* true_target,
1853 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001854 HInstruction* cond = instruction->InputAt(condition_input_index);
1855
1856 if (true_target == nullptr && false_target == nullptr) {
1857 // Nothing to do. The code always falls through.
1858 return;
1859 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001860 // Constant condition, statically compared against "true" (integer value 1).
1861 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001862 if (true_target != nullptr) {
1863 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001864 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001865 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001866 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001867 if (false_target != nullptr) {
1868 __ jmp(false_target);
1869 }
1870 }
1871 return;
1872 }
1873
1874 // The following code generates these patterns:
1875 // (1) true_target == nullptr && false_target != nullptr
1876 // - opposite condition true => branch to false_target
1877 // (2) true_target != nullptr && false_target == nullptr
1878 // - condition true => branch to true_target
1879 // (3) true_target != nullptr && false_target != nullptr
1880 // - condition true => branch to true_target
1881 // - branch to false_target
1882 if (IsBooleanValueOrMaterializedCondition(cond)) {
1883 if (AreEflagsSetFrom(cond, instruction)) {
1884 if (true_target == nullptr) {
1885 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1886 } else {
1887 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1888 }
1889 } else {
1890 // Materialized condition, compare against 0.
1891 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1892 if (lhs.IsRegister()) {
1893 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1894 } else {
1895 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1896 }
1897 if (true_target == nullptr) {
1898 __ j(kEqual, false_target);
1899 } else {
1900 __ j(kNotEqual, true_target);
1901 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001902 }
1903 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001904 // Condition has not been materialized, use its inputs as the comparison and
1905 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001906 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001907
1908 // If this is a long or FP comparison that has been folded into
1909 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001910 DataType::Type type = condition->InputAt(0)->GetType();
1911 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001912 GenerateCompareTestAndBranch(condition, true_target, false_target);
1913 return;
1914 }
1915
1916 Location lhs = condition->GetLocations()->InAt(0);
1917 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001918 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001919 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001920 if (true_target == nullptr) {
1921 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1922 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001923 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001924 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001925 }
David Brazdil0debae72015-11-12 18:37:00 +00001926
1927 // If neither branch falls through (case 3), the conditional branch to `true_target`
1928 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1929 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001930 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001931 }
1932}
1933
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001934void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001935 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001936 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001937 locations->SetInAt(0, Location::Any());
1938 }
1939}
1940
1941void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001942 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1943 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1944 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1945 nullptr : codegen_->GetLabelOf(true_successor);
1946 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1947 nullptr : codegen_->GetLabelOf(false_successor);
Andreas Gampe3db70682018-12-26 15:12:03 -08001948 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001949}
1950
1951void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001952 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001953 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001954 InvokeRuntimeCallingConvention calling_convention;
1955 RegisterSet caller_saves = RegisterSet::Empty();
1956 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1957 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001958 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001959 locations->SetInAt(0, Location::Any());
1960 }
1961}
1962
1963void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001964 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001965 GenerateTestAndBranch<Label>(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08001966 /* condition_input_index= */ 0,
David Brazdil74eb1b22015-12-14 11:44:01 +00001967 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08001968 /* false_target= */ nullptr);
David Brazdil74eb1b22015-12-14 11:44:01 +00001969}
1970
Mingyao Yang063fc772016-08-02 11:02:54 -07001971void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001972 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001973 LocationSummary(flag, LocationSummary::kNoCall);
1974 locations->SetOut(Location::RequiresRegister());
1975}
1976
1977void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1978 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1979 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1980}
1981
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001982static bool SelectCanUseCMOV(HSelect* select) {
1983 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001984 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001985 return false;
1986 }
1987
1988 // A FP condition doesn't generate the single CC that we need.
1989 // In 32 bit mode, a long condition doesn't generate a single CC either.
1990 HInstruction* condition = select->GetCondition();
1991 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001992 DataType::Type compare_type = condition->InputAt(0)->GetType();
1993 if (compare_type == DataType::Type::kInt64 ||
1994 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001995 return false;
1996 }
1997 }
1998
1999 // We can generate a CMOV for this Select.
2000 return true;
2001}
2002
David Brazdil74eb1b22015-12-14 11:44:01 +00002003void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002004 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002005 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00002006 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002007 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00002008 } else {
2009 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002010 if (SelectCanUseCMOV(select)) {
2011 if (select->InputAt(1)->IsConstant()) {
2012 // Cmov can't handle a constant value.
2013 locations->SetInAt(1, Location::RequiresRegister());
2014 } else {
2015 locations->SetInAt(1, Location::Any());
2016 }
2017 } else {
2018 locations->SetInAt(1, Location::Any());
2019 }
David Brazdil74eb1b22015-12-14 11:44:01 +00002020 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002021 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2022 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00002023 }
2024 locations->SetOut(Location::SameAsFirstInput());
2025}
2026
2027void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
2028 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002029 DCHECK(locations->InAt(0).Equals(locations->Out()));
2030 if (SelectCanUseCMOV(select)) {
2031 // If both the condition and the source types are integer, we can generate
2032 // a CMOV to implement Select.
2033
2034 HInstruction* select_condition = select->GetCondition();
2035 Condition cond = kNotEqual;
2036
2037 // Figure out how to test the 'condition'.
2038 if (select_condition->IsCondition()) {
2039 HCondition* condition = select_condition->AsCondition();
2040 if (!condition->IsEmittedAtUseSite()) {
2041 // This was a previously materialized condition.
2042 // Can we use the existing condition code?
2043 if (AreEflagsSetFrom(condition, select)) {
2044 // Materialization was the previous instruction. Condition codes are right.
2045 cond = X86Condition(condition->GetCondition());
2046 } else {
2047 // No, we have to recreate the condition code.
2048 Register cond_reg = locations->InAt(2).AsRegister<Register>();
2049 __ testl(cond_reg, cond_reg);
2050 }
2051 } else {
2052 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002053 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
2054 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002055 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01002056 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002057 cond = X86Condition(condition->GetCondition());
2058 }
2059 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002060 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002061 Register cond_reg = locations->InAt(2).AsRegister<Register>();
2062 __ testl(cond_reg, cond_reg);
2063 }
2064
2065 // If the condition is true, overwrite the output, which already contains false.
2066 Location false_loc = locations->InAt(0);
2067 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002068 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002069 // 64 bit conditional move.
2070 Register false_high = false_loc.AsRegisterPairHigh<Register>();
2071 Register false_low = false_loc.AsRegisterPairLow<Register>();
2072 if (true_loc.IsRegisterPair()) {
2073 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
2074 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
2075 } else {
2076 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
2077 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
2078 }
2079 } else {
2080 // 32 bit conditional move.
2081 Register false_reg = false_loc.AsRegister<Register>();
2082 if (true_loc.IsRegister()) {
2083 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
2084 } else {
2085 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
2086 }
2087 }
2088 } else {
2089 NearLabel false_target;
2090 GenerateTestAndBranch<NearLabel>(
Andreas Gampe3db70682018-12-26 15:12:03 -08002091 select, /* condition_input_index= */ 2, /* true_target= */ nullptr, &false_target);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002092 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2093 __ Bind(&false_target);
2094 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002095}
2096
David Srbecky0cf44932015-12-09 14:09:59 +00002097void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002098 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00002099}
2100
David Srbeckyd28f4a02016-03-14 17:14:24 +00002101void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
2102 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002103}
2104
Vladimir Markodec78172020-06-19 15:31:23 +01002105void CodeGeneratorX86::IncreaseFrame(size_t adjustment) {
2106 __ subl(ESP, Immediate(adjustment));
2107 __ cfi().AdjustCFAOffset(adjustment);
2108}
2109
2110void CodeGeneratorX86::DecreaseFrame(size_t adjustment) {
2111 __ addl(ESP, Immediate(adjustment));
2112 __ cfi().AdjustCFAOffset(-adjustment);
2113}
2114
David Srbeckyc7098ff2016-02-09 14:30:11 +00002115void CodeGeneratorX86::GenerateNop() {
2116 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002117}
2118
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002119void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002120 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002121 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04002122 // Handle the long/FP comparisons made in instruction simplification.
2123 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002124 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04002125 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05002126 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00002127 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002128 locations->SetOut(Location::RequiresRegister());
2129 }
2130 break;
2131 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002132 case DataType::Type::kFloat32:
2133 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04002134 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002135 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
2136 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
2137 } else if (cond->InputAt(1)->IsConstant()) {
2138 locations->SetInAt(1, Location::RequiresFpuRegister());
2139 } else {
2140 locations->SetInAt(1, Location::Any());
2141 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002142 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002143 locations->SetOut(Location::RequiresRegister());
2144 }
2145 break;
2146 }
2147 default:
2148 locations->SetInAt(0, Location::RequiresRegister());
2149 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00002150 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002151 // We need a byte register.
2152 locations->SetOut(Location::RegisterLocation(ECX));
2153 }
2154 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002155 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002156}
2157
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002158void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002159 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002160 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002161 }
Mark Mendellc4701932015-04-10 13:18:51 -04002162
2163 LocationSummary* locations = cond->GetLocations();
2164 Location lhs = locations->InAt(0);
2165 Location rhs = locations->InAt(1);
2166 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05002167 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002168
2169 switch (cond->InputAt(0)->GetType()) {
2170 default: {
2171 // Integer case.
2172
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002173 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04002174 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01002175 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07002176 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04002177 return;
2178 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002179 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04002180 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2181 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002182 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002183 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04002184 GenerateFPJumps(cond, &true_label, &false_label);
2185 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002186 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002187 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04002188 GenerateFPJumps(cond, &true_label, &false_label);
2189 break;
2190 }
2191
2192 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002193 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002194
Roland Levillain4fa13f62015-07-06 18:11:54 +01002195 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04002196 __ Bind(&false_label);
2197 __ xorl(reg, reg);
2198 __ jmp(&done_label);
2199
Roland Levillain4fa13f62015-07-06 18:11:54 +01002200 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04002201 __ Bind(&true_label);
2202 __ movl(reg, Immediate(1));
2203 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002204}
2205
2206void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002207 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002208}
2209
2210void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002211 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002212}
2213
2214void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002215 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002216}
2217
2218void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002219 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002220}
2221
2222void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002223 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002224}
2225
2226void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002227 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002228}
2229
2230void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002231 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002232}
2233
2234void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002235 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002236}
2237
2238void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002239 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002240}
2241
2242void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002243 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002244}
2245
2246void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002247 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002248}
2249
2250void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002251 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002252}
2253
Aart Bike9f37602015-10-09 11:15:55 -07002254void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002255 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002256}
2257
2258void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002259 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002260}
2261
2262void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002263 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002264}
2265
2266void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002267 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002268}
2269
2270void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002271 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002272}
2273
2274void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002275 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002276}
2277
2278void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002279 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002280}
2281
2282void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002283 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002284}
2285
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002286void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002287 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002288 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002289 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002290}
2291
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002292void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002293 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002294}
2295
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002296void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2297 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002298 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002299 locations->SetOut(Location::ConstantLocation(constant));
2300}
2301
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002302void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002303 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002304}
2305
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002306void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002307 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002308 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002309 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002310}
2311
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002312void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002313 // Will be generated at use site.
2314}
2315
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002316void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2317 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002318 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002319 locations->SetOut(Location::ConstantLocation(constant));
2320}
2321
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002322void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002323 // Will be generated at use site.
2324}
2325
2326void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2327 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002328 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002329 locations->SetOut(Location::ConstantLocation(constant));
2330}
2331
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002332void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002333 // Will be generated at use site.
2334}
2335
Igor Murashkind01745e2017-04-05 16:40:31 -07002336void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2337 constructor_fence->SetLocations(nullptr);
2338}
2339
2340void InstructionCodeGeneratorX86::VisitConstructorFence(
2341 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2342 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2343}
2344
Calin Juravle27df7582015-04-17 19:12:31 +01002345void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2346 memory_barrier->SetLocations(nullptr);
2347}
2348
2349void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002350 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002351}
2352
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002353void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002354 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002355}
2356
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002357void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002358 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002359}
2360
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002361void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002362 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002363 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002364 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002365 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002366 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002367 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002368 case DataType::Type::kInt8:
2369 case DataType::Type::kUint16:
2370 case DataType::Type::kInt16:
2371 case DataType::Type::kInt32:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002372 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002373 break;
2374
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002375 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002376 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002377 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002378 break;
2379
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002380 case DataType::Type::kFloat32:
2381 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002382 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002383 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002384 break;
2385
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002386 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002387 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002388 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002389}
2390
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002391void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002392 switch (ret->InputAt(0)->GetType()) {
2393 case DataType::Type::kReference:
2394 case DataType::Type::kBool:
2395 case DataType::Type::kUint8:
2396 case DataType::Type::kInt8:
2397 case DataType::Type::kUint16:
2398 case DataType::Type::kInt16:
2399 case DataType::Type::kInt32:
2400 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
2401 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002402
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002403 case DataType::Type::kInt64:
2404 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2405 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
2406 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002407
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002408 case DataType::Type::kFloat32:
2409 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2410 if (GetGraph()->IsCompilingOsr()) {
2411 // To simplify callers of an OSR method, we put the return value in both
2412 // floating point and core registers.
2413 __ movd(EAX, XMM0);
2414 }
2415 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002416
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002417 case DataType::Type::kFloat64:
2418 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2419 if (GetGraph()->IsCompilingOsr()) {
2420 // To simplify callers of an OSR method, we put the return value in both
2421 // floating point and core registers.
2422 __ movd(EAX, XMM0);
2423 // Use XMM1 as temporary register to not clobber XMM0.
2424 __ movaps(XMM1, XMM0);
2425 __ psrlq(XMM1, Immediate(32));
2426 __ movd(EDX, XMM1);
2427 }
2428 break;
2429
2430 default:
2431 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002432 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002433 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002434}
2435
Calin Juravle175dc732015-08-25 15:42:32 +01002436void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2437 // The trampoline uses the same calling convention as dex calling conventions,
2438 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2439 // the method_idx.
2440 HandleInvoke(invoke);
2441}
2442
2443void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2444 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2445}
2446
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002447void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002448 // Explicit clinit checks triggered by static invokes must have been pruned by
2449 // art::PrepareForRegisterAllocation.
2450 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002451
Mark Mendellfb8d2792015-03-31 22:16:59 -04002452 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002453 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002454 if (invoke->GetLocations()->CanCall() &&
2455 invoke->HasPcRelativeMethodLoadKind() &&
2456 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).IsInvalid()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002457 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002458 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002459 return;
2460 }
2461
Vladimir Marko86c87522020-05-11 16:55:55 +01002462 if (invoke->GetCodePtrLocation() == HInvokeStaticOrDirect::CodePtrLocation::kCallCriticalNative) {
2463 CriticalNativeCallingConventionVisitorX86 calling_convention_visitor(
2464 /*for_register_allocation=*/ true);
2465 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2466 } else {
2467 HandleInvoke(invoke);
2468 }
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002469
Vladimir Marko86c87522020-05-11 16:55:55 +01002470 // For PC-relative load kinds the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002471 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002472 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002473 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002474}
2475
Mark Mendell09ed1a32015-03-25 08:30:06 -04002476static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2477 if (invoke->GetLocations()->Intrinsified()) {
2478 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2479 intrinsic.Dispatch(invoke);
2480 return true;
2481 }
2482 return false;
2483}
2484
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002485void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002486 // Explicit clinit checks triggered by static invokes must have been pruned by
2487 // art::PrepareForRegisterAllocation.
2488 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002489
Mark Mendell09ed1a32015-03-25 08:30:06 -04002490 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2491 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002492 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002493
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002494 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002495 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002496 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002497}
2498
2499void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002500 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2501 if (intrinsic.TryDispatch(invoke)) {
2502 return;
2503 }
2504
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002505 HandleInvoke(invoke);
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002506
2507 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002508 // Add one temporary for inline cache update.
2509 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2510 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002511}
2512
2513void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002514 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002515 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002516}
2517
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002518void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002519 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2520 return;
2521 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002522
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002523 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002524 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002525}
2526
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002527void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002528 // This call to HandleInvoke allocates a temporary (core) register
2529 // which is also used to transfer the hidden argument from FP to
2530 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002531 HandleInvoke(invoke);
2532 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002533 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002534
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002535 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002536 // Add one temporary for inline cache update.
2537 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2538 }
2539}
2540
2541void CodeGeneratorX86::MaybeGenerateInlineCacheCheck(HInstruction* instruction, Register klass) {
2542 DCHECK_EQ(EAX, klass);
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002543 // We know the destination of an intrinsic, so no need to record inline
2544 // caches (also the intrinsic location builder doesn't request an additional
2545 // temporary).
2546 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002547 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002548 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002549 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
Nicolas Geoffray095dc462020-08-17 16:40:28 +01002550 ScopedProfilingInfoUse spiu(
2551 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
2552 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002553 if (info != nullptr) {
2554 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
2555 uint32_t address = reinterpret_cast32<uint32_t>(cache);
2556 if (kIsDebugBuild) {
2557 uint32_t temp_index = instruction->GetLocations()->GetTempCount() - 1u;
2558 CHECK_EQ(EBP, instruction->GetLocations()->GetTemp(temp_index).AsRegister<Register>());
2559 }
2560 Register temp = EBP;
2561 NearLabel done;
2562 __ movl(temp, Immediate(address));
2563 // Fast path for a monomorphic cache.
2564 __ cmpl(klass, Address(temp, InlineCache::ClassesOffset().Int32Value()));
2565 __ j(kEqual, &done);
2566 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(kQuickUpdateInlineCache).Int32Value());
2567 __ Bind(&done);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002568 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002569 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002570}
2571
2572void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2573 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002574 LocationSummary* locations = invoke->GetLocations();
2575 Register temp = locations->GetTemp(0).AsRegister<Register>();
2576 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002577 Location receiver = locations->InAt(0);
2578 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2579
Roland Levillain0d5a2812015-11-13 10:07:31 +00002580 // Set the hidden argument. This is safe to do this here, as XMM7
2581 // won't be modified thereafter, before the `call` instruction.
2582 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01002583 __ movl(temp, Immediate(invoke->GetMethodReference().index));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002584 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002585
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002586 if (receiver.IsStackSlot()) {
2587 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002588 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002589 __ movl(temp, Address(temp, class_offset));
2590 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002591 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002592 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002593 }
Roland Levillain4d027112015-07-01 15:41:14 +01002594 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002595 // Instead of simply (possibly) unpoisoning `temp` here, we should
2596 // emit a read barrier for the previous class reference load.
2597 // However this is not required in practice, as this is an
2598 // intermediate/temporary reference and because the current
2599 // concurrent copying collector keeps the from-space memory
2600 // intact/accessible until the end of the marking phase (the
2601 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002602 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002603
2604 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
2605
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002606 // temp = temp->GetAddressOfIMT()
2607 __ movl(temp,
2608 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002609 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002610 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002611 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002612 __ movl(temp, Address(temp, method_offset));
2613 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002614 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002615 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002616
2617 DCHECK(!codegen_->IsLeafMethod());
2618 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2619}
2620
Orion Hodsonac141392017-01-13 11:53:47 +00002621void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00002622 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2623 if (intrinsic.TryDispatch(invoke)) {
2624 return;
2625 }
Orion Hodsonac141392017-01-13 11:53:47 +00002626 HandleInvoke(invoke);
2627}
2628
2629void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00002630 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2631 return;
2632 }
Orion Hodsonac141392017-01-13 11:53:47 +00002633 codegen_->GenerateInvokePolymorphicCall(invoke);
2634}
2635
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002636void LocationsBuilderX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2637 HandleInvoke(invoke);
2638}
2639
2640void InstructionCodeGeneratorX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2641 codegen_->GenerateInvokeCustomCall(invoke);
2642}
2643
Roland Levillain88cb1752014-10-20 16:36:47 +01002644void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2645 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002646 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002647 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002648 case DataType::Type::kInt32:
2649 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002650 locations->SetInAt(0, Location::RequiresRegister());
2651 locations->SetOut(Location::SameAsFirstInput());
2652 break;
2653
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002654 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002655 locations->SetInAt(0, Location::RequiresFpuRegister());
2656 locations->SetOut(Location::SameAsFirstInput());
2657 locations->AddTemp(Location::RequiresRegister());
2658 locations->AddTemp(Location::RequiresFpuRegister());
2659 break;
2660
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002661 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002662 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002663 locations->SetOut(Location::SameAsFirstInput());
2664 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002665 break;
2666
2667 default:
2668 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2669 }
2670}
2671
2672void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2673 LocationSummary* locations = neg->GetLocations();
2674 Location out = locations->Out();
2675 Location in = locations->InAt(0);
2676 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002677 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002678 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002679 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002680 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002681 break;
2682
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002683 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002684 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002685 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002686 __ negl(out.AsRegisterPairLow<Register>());
2687 // Negation is similar to subtraction from zero. The least
2688 // significant byte triggers a borrow when it is different from
2689 // zero; to take it into account, add 1 to the most significant
2690 // byte if the carry flag (CF) is set to 1 after the first NEGL
2691 // operation.
2692 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2693 __ negl(out.AsRegisterPairHigh<Register>());
2694 break;
2695
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002696 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002697 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002698 Register constant = locations->GetTemp(0).AsRegister<Register>();
2699 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002700 // Implement float negation with an exclusive or with value
2701 // 0x80000000 (mask for bit 31, representing the sign of a
2702 // single-precision floating-point number).
2703 __ movl(constant, Immediate(INT32_C(0x80000000)));
2704 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002705 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002706 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002707 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002708
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002709 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002710 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002711 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002712 // Implement double negation with an exclusive or with value
2713 // 0x8000000000000000 (mask for bit 63, representing the sign of
2714 // a double-precision floating-point number).
2715 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002716 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002717 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002718 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002719
2720 default:
2721 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2722 }
2723}
2724
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002725void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2726 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002727 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002728 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002729 locations->SetInAt(0, Location::RequiresFpuRegister());
2730 locations->SetInAt(1, Location::RequiresRegister());
2731 locations->SetOut(Location::SameAsFirstInput());
2732 locations->AddTemp(Location::RequiresFpuRegister());
2733}
2734
2735void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2736 LocationSummary* locations = neg->GetLocations();
2737 Location out = locations->Out();
2738 DCHECK(locations->InAt(0).Equals(out));
2739
2740 Register constant_area = locations->InAt(1).AsRegister<Register>();
2741 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002742 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002743 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2744 neg->GetBaseMethodAddress(),
2745 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002746 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2747 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002748 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2749 neg->GetBaseMethodAddress(),
2750 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002751 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2752 }
2753}
2754
Roland Levillaindff1f282014-11-05 14:15:05 +00002755void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002756 DataType::Type result_type = conversion->GetResultType();
2757 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002758 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2759 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002760
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002761 // The float-to-long and double-to-long type conversions rely on a
2762 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002763 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002764 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2765 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002766 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002767 : LocationSummary::kNoCall;
2768 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002769 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002770
Roland Levillaindff1f282014-11-05 14:15:05 +00002771 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002772 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002773 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002774 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002775 case DataType::Type::kUint8:
2776 case DataType::Type::kInt8:
2777 case DataType::Type::kUint16:
2778 case DataType::Type::kInt16:
2779 case DataType::Type::kInt32:
2780 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2781 // Make the output overlap to please the register allocator. This greatly simplifies
2782 // the validation of the linear scan implementation
2783 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2784 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002785 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002786 HInstruction* input = conversion->InputAt(0);
2787 Location input_location = input->IsConstant()
2788 ? Location::ConstantLocation(input->AsConstant())
2789 : Location::RegisterPairLocation(EAX, EDX);
2790 locations->SetInAt(0, input_location);
2791 // Make the output overlap to please the register allocator. This greatly simplifies
2792 // the validation of the linear scan implementation
2793 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2794 break;
2795 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002796
2797 default:
2798 LOG(FATAL) << "Unexpected type conversion from " << input_type
2799 << " to " << result_type;
2800 }
2801 break;
2802
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002803 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002804 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002805 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2806 locations->SetInAt(0, Location::Any());
2807 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002808 break;
2809
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002810 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002811 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002812 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002813 locations->SetInAt(0, Location::Any());
2814 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2815 break;
2816
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002817 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002818 locations->SetInAt(0, Location::RequiresFpuRegister());
2819 locations->SetOut(Location::RequiresRegister());
2820 locations->AddTemp(Location::RequiresFpuRegister());
2821 break;
2822
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002823 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002824 locations->SetInAt(0, Location::RequiresFpuRegister());
2825 locations->SetOut(Location::RequiresRegister());
2826 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002827 break;
2828
2829 default:
2830 LOG(FATAL) << "Unexpected type conversion from " << input_type
2831 << " to " << result_type;
2832 }
2833 break;
2834
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002835 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002836 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002837 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002838 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002839 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002840 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002841 case DataType::Type::kInt16:
2842 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002843 locations->SetInAt(0, Location::RegisterLocation(EAX));
2844 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2845 break;
2846
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002847 case DataType::Type::kFloat32:
2848 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002849 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002850 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2851 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2852
Vladimir Marko949c91f2015-01-27 10:48:44 +00002853 // The runtime helper puts the result in EAX, EDX.
2854 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002855 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002856 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002857
2858 default:
2859 LOG(FATAL) << "Unexpected type conversion from " << input_type
2860 << " to " << result_type;
2861 }
2862 break;
2863
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002864 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002865 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002866 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002867 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002868 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002869 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002870 case DataType::Type::kInt16:
2871 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002872 locations->SetInAt(0, Location::RequiresRegister());
2873 locations->SetOut(Location::RequiresFpuRegister());
2874 break;
2875
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002876 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002877 locations->SetInAt(0, Location::Any());
2878 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002879 break;
2880
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002881 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002882 locations->SetInAt(0, Location::RequiresFpuRegister());
2883 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002884 break;
2885
2886 default:
2887 LOG(FATAL) << "Unexpected type conversion from " << input_type
2888 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002889 }
Roland Levillaincff13742014-11-17 14:32:17 +00002890 break;
2891
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002892 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002893 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002894 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002895 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002896 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002897 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002898 case DataType::Type::kInt16:
2899 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002900 locations->SetInAt(0, Location::RequiresRegister());
2901 locations->SetOut(Location::RequiresFpuRegister());
2902 break;
2903
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002904 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002905 locations->SetInAt(0, Location::Any());
2906 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002907 break;
2908
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002909 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002910 locations->SetInAt(0, Location::RequiresFpuRegister());
2911 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002912 break;
2913
2914 default:
2915 LOG(FATAL) << "Unexpected type conversion from " << input_type
2916 << " to " << result_type;
2917 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002918 break;
2919
2920 default:
2921 LOG(FATAL) << "Unexpected type conversion from " << input_type
2922 << " to " << result_type;
2923 }
2924}
2925
2926void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2927 LocationSummary* locations = conversion->GetLocations();
2928 Location out = locations->Out();
2929 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002930 DataType::Type result_type = conversion->GetResultType();
2931 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002932 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2933 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002934 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002935 case DataType::Type::kUint8:
2936 switch (input_type) {
2937 case DataType::Type::kInt8:
2938 case DataType::Type::kUint16:
2939 case DataType::Type::kInt16:
2940 case DataType::Type::kInt32:
2941 if (in.IsRegister()) {
2942 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2943 } else {
2944 DCHECK(in.GetConstant()->IsIntConstant());
2945 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2946 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2947 }
2948 break;
2949 case DataType::Type::kInt64:
2950 if (in.IsRegisterPair()) {
2951 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2952 } else {
2953 DCHECK(in.GetConstant()->IsLongConstant());
2954 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2955 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2956 }
2957 break;
2958
2959 default:
2960 LOG(FATAL) << "Unexpected type conversion from " << input_type
2961 << " to " << result_type;
2962 }
2963 break;
2964
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002965 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002966 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002967 case DataType::Type::kUint8:
2968 case DataType::Type::kUint16:
2969 case DataType::Type::kInt16:
2970 case DataType::Type::kInt32:
2971 if (in.IsRegister()) {
2972 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2973 } else {
2974 DCHECK(in.GetConstant()->IsIntConstant());
2975 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2976 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2977 }
2978 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002979 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002980 if (in.IsRegisterPair()) {
2981 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2982 } else {
2983 DCHECK(in.GetConstant()->IsLongConstant());
2984 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2985 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2986 }
2987 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002988
2989 default:
2990 LOG(FATAL) << "Unexpected type conversion from " << input_type
2991 << " to " << result_type;
2992 }
2993 break;
2994
2995 case DataType::Type::kUint16:
2996 switch (input_type) {
2997 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002998 case DataType::Type::kInt16:
2999 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003000 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003001 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
3002 } else if (in.IsStackSlot()) {
3003 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003004 } else {
3005 DCHECK(in.GetConstant()->IsIntConstant());
3006 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003007 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
3008 }
3009 break;
3010 case DataType::Type::kInt64:
3011 if (in.IsRegisterPair()) {
3012 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
3013 } else if (in.IsDoubleStackSlot()) {
3014 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
3015 } else {
3016 DCHECK(in.GetConstant()->IsLongConstant());
3017 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3018 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003019 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00003020 break;
3021
3022 default:
3023 LOG(FATAL) << "Unexpected type conversion from " << input_type
3024 << " to " << result_type;
3025 }
3026 break;
3027
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003028 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00003029 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003030 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003031 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00003032 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003033 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00003034 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003035 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00003036 } else {
3037 DCHECK(in.GetConstant()->IsIntConstant());
3038 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003039 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00003040 }
3041 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003042 case DataType::Type::kInt64:
3043 if (in.IsRegisterPair()) {
3044 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
3045 } else if (in.IsDoubleStackSlot()) {
3046 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
3047 } else {
3048 DCHECK(in.GetConstant()->IsLongConstant());
3049 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3050 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
3051 }
3052 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00003053
3054 default:
3055 LOG(FATAL) << "Unexpected type conversion from " << input_type
3056 << " to " << result_type;
3057 }
3058 break;
3059
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003060 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00003061 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003062 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00003063 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003064 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00003065 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003066 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00003067 } else {
3068 DCHECK(in.IsConstant());
3069 DCHECK(in.GetConstant()->IsLongConstant());
3070 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003071 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00003072 }
3073 break;
3074
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003075 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00003076 XmmRegister input = in.AsFpuRegister<XmmRegister>();
3077 Register output = out.AsRegister<Register>();
3078 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04003079 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00003080
3081 __ movl(output, Immediate(kPrimIntMax));
3082 // temp = int-to-float(output)
3083 __ cvtsi2ss(temp, output);
3084 // if input >= temp goto done
3085 __ comiss(input, temp);
3086 __ j(kAboveEqual, &done);
3087 // if input == NaN goto nan
3088 __ j(kUnordered, &nan);
3089 // output = float-to-int-truncate(input)
3090 __ cvttss2si(output, input);
3091 __ jmp(&done);
3092 __ Bind(&nan);
3093 // output = 0
3094 __ xorl(output, output);
3095 __ Bind(&done);
3096 break;
3097 }
3098
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003099 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003100 XmmRegister input = in.AsFpuRegister<XmmRegister>();
3101 Register output = out.AsRegister<Register>();
3102 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04003103 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003104
3105 __ movl(output, Immediate(kPrimIntMax));
3106 // temp = int-to-double(output)
3107 __ cvtsi2sd(temp, output);
3108 // if input >= temp goto done
3109 __ comisd(input, temp);
3110 __ j(kAboveEqual, &done);
3111 // if input == NaN goto nan
3112 __ j(kUnordered, &nan);
3113 // output = double-to-int-truncate(input)
3114 __ cvttsd2si(output, input);
3115 __ jmp(&done);
3116 __ Bind(&nan);
3117 // output = 0
3118 __ xorl(output, output);
3119 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00003120 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003121 }
Roland Levillain946e1432014-11-11 17:35:19 +00003122
3123 default:
3124 LOG(FATAL) << "Unexpected type conversion from " << input_type
3125 << " to " << result_type;
3126 }
3127 break;
3128
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003129 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00003130 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003131 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003132 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003133 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003134 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003135 case DataType::Type::kInt16:
3136 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00003137 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
3138 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003139 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00003140 __ cdq();
3141 break;
3142
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003143 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01003144 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003145 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00003146 break;
3147
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003148 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01003149 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003150 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00003151 break;
3152
3153 default:
3154 LOG(FATAL) << "Unexpected type conversion from " << input_type
3155 << " to " << result_type;
3156 }
3157 break;
3158
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003159 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00003160 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003161 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003162 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003163 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003164 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003165 case DataType::Type::kInt16:
3166 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003167 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003168 break;
3169
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003170 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003171 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00003172
Roland Levillain232ade02015-04-20 15:14:36 +01003173 // Create stack space for the call to
3174 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
3175 // TODO: enhance register allocator to ask for stack temporaries.
3176 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003177 adjustment = DataType::Size(DataType::Type::kInt64);
Vladimir Markodec78172020-06-19 15:31:23 +01003178 codegen_->IncreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003179 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003180
Roland Levillain232ade02015-04-20 15:14:36 +01003181 // Load the value to the FP stack, using temporaries if needed.
3182 PushOntoFPStack(in, 0, adjustment, false, true);
3183
3184 if (out.IsStackSlot()) {
3185 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
3186 } else {
3187 __ fstps(Address(ESP, 0));
3188 Location stack_temp = Location::StackSlot(0);
3189 codegen_->Move32(out, stack_temp);
3190 }
3191
3192 // Remove the temporary stack space we allocated.
3193 if (adjustment != 0) {
Vladimir Markodec78172020-06-19 15:31:23 +01003194 codegen_->DecreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003195 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003196 break;
3197 }
3198
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003199 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003200 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003201 break;
3202
3203 default:
3204 LOG(FATAL) << "Unexpected type conversion from " << input_type
3205 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003206 }
Roland Levillaincff13742014-11-17 14:32:17 +00003207 break;
3208
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003209 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00003210 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003211 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003212 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003213 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003214 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003215 case DataType::Type::kInt16:
3216 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003217 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003218 break;
3219
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003220 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003221 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00003222
Roland Levillain232ade02015-04-20 15:14:36 +01003223 // Create stack space for the call to
3224 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
3225 // TODO: enhance register allocator to ask for stack temporaries.
3226 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003227 adjustment = DataType::Size(DataType::Type::kInt64);
Vladimir Markodec78172020-06-19 15:31:23 +01003228 codegen_->IncreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003229 }
3230
3231 // Load the value to the FP stack, using temporaries if needed.
3232 PushOntoFPStack(in, 0, adjustment, false, true);
3233
3234 if (out.IsDoubleStackSlot()) {
3235 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
3236 } else {
3237 __ fstpl(Address(ESP, 0));
3238 Location stack_temp = Location::DoubleStackSlot(0);
3239 codegen_->Move64(out, stack_temp);
3240 }
3241
3242 // Remove the temporary stack space we allocated.
3243 if (adjustment != 0) {
Vladimir Markodec78172020-06-19 15:31:23 +01003244 codegen_->DecreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003245 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003246 break;
3247 }
3248
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003249 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003250 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003251 break;
3252
3253 default:
3254 LOG(FATAL) << "Unexpected type conversion from " << input_type
3255 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003256 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003257 break;
3258
3259 default:
3260 LOG(FATAL) << "Unexpected type conversion from " << input_type
3261 << " to " << result_type;
3262 }
3263}
3264
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003265void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003266 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003267 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003268 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003269 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05003270 locations->SetInAt(0, Location::RequiresRegister());
3271 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3272 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3273 break;
3274 }
3275
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003276 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003277 locations->SetInAt(0, Location::RequiresRegister());
3278 locations->SetInAt(1, Location::Any());
3279 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003280 break;
3281 }
3282
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003283 case DataType::Type::kFloat32:
3284 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003285 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003286 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3287 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003288 } else if (add->InputAt(1)->IsConstant()) {
3289 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003290 } else {
3291 locations->SetInAt(1, Location::Any());
3292 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003293 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003294 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003295 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003296
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003297 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003298 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Elliott Hughesc1896c92018-11-29 11:33:18 -08003299 UNREACHABLE();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003300 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003301}
3302
3303void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
3304 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003305 Location first = locations->InAt(0);
3306 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05003307 Location out = locations->Out();
3308
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003309 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003310 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003311 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003312 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3313 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003314 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3315 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05003316 } else {
3317 __ leal(out.AsRegister<Register>(), Address(
3318 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3319 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003320 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003321 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3322 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3323 __ addl(out.AsRegister<Register>(), Immediate(value));
3324 } else {
3325 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3326 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003327 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003328 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003329 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003330 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003331 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003332 }
3333
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003334 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003335 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003336 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3337 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003338 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003339 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3340 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003341 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003342 } else {
3343 DCHECK(second.IsConstant()) << second;
3344 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3345 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3346 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003347 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003348 break;
3349 }
3350
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003351 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003352 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003353 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003354 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3355 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003356 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003357 __ addss(first.AsFpuRegister<XmmRegister>(),
3358 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003359 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3360 const_area->GetBaseMethodAddress(),
3361 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003362 } else {
3363 DCHECK(second.IsStackSlot());
3364 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003365 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003366 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003367 }
3368
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003369 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003370 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003371 __ addsd(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 __ addsd(first.AsFpuRegister<XmmRegister>(),
3376 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003377 const_area->GetConstant()->AsDoubleConstant()->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.IsDoubleStackSlot());
3382 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003383 }
3384 break;
3385 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003386
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003387 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003388 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003389 }
3390}
3391
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003392void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003393 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003394 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003395 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003396 case DataType::Type::kInt32:
3397 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003398 locations->SetInAt(0, Location::RequiresRegister());
3399 locations->SetInAt(1, Location::Any());
3400 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003401 break;
3402 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003403 case DataType::Type::kFloat32:
3404 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003405 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003406 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3407 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003408 } else if (sub->InputAt(1)->IsConstant()) {
3409 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003410 } else {
3411 locations->SetInAt(1, Location::Any());
3412 }
Calin Juravle11351682014-10-23 15:38:15 +01003413 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003414 break;
Calin Juravle11351682014-10-23 15:38:15 +01003415 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003416
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003417 default:
Calin Juravle11351682014-10-23 15:38:15 +01003418 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003419 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003420}
3421
3422void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3423 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003424 Location first = locations->InAt(0);
3425 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003426 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003427 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003428 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003429 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003430 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003431 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003432 __ subl(first.AsRegister<Register>(),
3433 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003434 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003435 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003436 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003437 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003438 }
3439
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003440 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003441 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003442 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3443 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003444 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003445 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003446 __ sbbl(first.AsRegisterPairHigh<Register>(),
3447 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003448 } else {
3449 DCHECK(second.IsConstant()) << second;
3450 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3451 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3452 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003453 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003454 break;
3455 }
3456
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003457 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003458 if (second.IsFpuRegister()) {
3459 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3460 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3461 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003462 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003463 __ subss(first.AsFpuRegister<XmmRegister>(),
3464 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003465 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3466 const_area->GetBaseMethodAddress(),
3467 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003468 } else {
3469 DCHECK(second.IsStackSlot());
3470 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3471 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003472 break;
Calin Juravle11351682014-10-23 15:38:15 +01003473 }
3474
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003475 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003476 if (second.IsFpuRegister()) {
3477 __ subsd(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 __ subsd(first.AsFpuRegister<XmmRegister>(),
3482 codegen_->LiteralDoubleAddress(
3483 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003484 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003485 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3486 } else {
3487 DCHECK(second.IsDoubleStackSlot());
3488 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3489 }
Calin Juravle11351682014-10-23 15:38:15 +01003490 break;
3491 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003492
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003493 default:
Calin Juravle11351682014-10-23 15:38:15 +01003494 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003495 }
3496}
3497
Calin Juravle34bacdf2014-10-07 20:23:36 +01003498void LocationsBuilderX86::VisitMul(HMul* mul) {
3499 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003500 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003501 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003502 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003503 locations->SetInAt(0, Location::RequiresRegister());
3504 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003505 if (mul->InputAt(1)->IsIntConstant()) {
3506 // Can use 3 operand multiply.
3507 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3508 } else {
3509 locations->SetOut(Location::SameAsFirstInput());
3510 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003511 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003512 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003513 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003514 locations->SetInAt(1, Location::Any());
3515 locations->SetOut(Location::SameAsFirstInput());
3516 // Needed for imul on 32bits with 64bits output.
3517 locations->AddTemp(Location::RegisterLocation(EAX));
3518 locations->AddTemp(Location::RegisterLocation(EDX));
3519 break;
3520 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003521 case DataType::Type::kFloat32:
3522 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003523 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003524 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3525 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003526 } else if (mul->InputAt(1)->IsConstant()) {
3527 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003528 } else {
3529 locations->SetInAt(1, Location::Any());
3530 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003531 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003532 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003533 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003534
3535 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003536 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003537 }
3538}
3539
3540void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3541 LocationSummary* locations = mul->GetLocations();
3542 Location first = locations->InAt(0);
3543 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003544 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003545
3546 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003547 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003548 // The constant may have ended up in a register, so test explicitly to avoid
3549 // problems where the output may not be the same as the first operand.
3550 if (mul->InputAt(1)->IsIntConstant()) {
3551 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3552 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3553 } else if (second.IsRegister()) {
3554 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003555 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003556 } else {
3557 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003558 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003559 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003560 }
3561 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003562
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003563 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003564 Register in1_hi = first.AsRegisterPairHigh<Register>();
3565 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003566 Register eax = locations->GetTemp(0).AsRegister<Register>();
3567 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003568
3569 DCHECK_EQ(EAX, eax);
3570 DCHECK_EQ(EDX, edx);
3571
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003572 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003573 // output: in1
3574 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3575 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3576 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003577 if (second.IsConstant()) {
3578 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003579
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003580 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3581 int32_t low_value = Low32Bits(value);
3582 int32_t high_value = High32Bits(value);
3583 Immediate low(low_value);
3584 Immediate high(high_value);
3585
3586 __ movl(eax, high);
3587 // eax <- in1.lo * in2.hi
3588 __ imull(eax, in1_lo);
3589 // in1.hi <- in1.hi * in2.lo
3590 __ imull(in1_hi, low);
3591 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3592 __ addl(in1_hi, eax);
3593 // move in2_lo to eax to prepare for double precision
3594 __ movl(eax, low);
3595 // edx:eax <- in1.lo * in2.lo
3596 __ mull(in1_lo);
3597 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3598 __ addl(in1_hi, edx);
3599 // in1.lo <- (in1.lo * in2.lo)[31:0];
3600 __ movl(in1_lo, eax);
3601 } else if (second.IsRegisterPair()) {
3602 Register in2_hi = second.AsRegisterPairHigh<Register>();
3603 Register in2_lo = second.AsRegisterPairLow<Register>();
3604
3605 __ movl(eax, in2_hi);
3606 // eax <- in1.lo * in2.hi
3607 __ imull(eax, in1_lo);
3608 // in1.hi <- in1.hi * in2.lo
3609 __ imull(in1_hi, in2_lo);
3610 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3611 __ addl(in1_hi, eax);
3612 // move in1_lo to eax to prepare for double precision
3613 __ movl(eax, in1_lo);
3614 // edx:eax <- in1.lo * in2.lo
3615 __ mull(in2_lo);
3616 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3617 __ addl(in1_hi, edx);
3618 // in1.lo <- (in1.lo * in2.lo)[31:0];
3619 __ movl(in1_lo, eax);
3620 } else {
3621 DCHECK(second.IsDoubleStackSlot()) << second;
3622 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3623 Address in2_lo(ESP, second.GetStackIndex());
3624
3625 __ movl(eax, in2_hi);
3626 // eax <- in1.lo * in2.hi
3627 __ imull(eax, in1_lo);
3628 // in1.hi <- in1.hi * in2.lo
3629 __ imull(in1_hi, in2_lo);
3630 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3631 __ addl(in1_hi, eax);
3632 // move in1_lo to eax to prepare for double precision
3633 __ movl(eax, in1_lo);
3634 // edx:eax <- in1.lo * in2.lo
3635 __ mull(in2_lo);
3636 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3637 __ addl(in1_hi, edx);
3638 // in1.lo <- (in1.lo * in2.lo)[31:0];
3639 __ movl(in1_lo, eax);
3640 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003641
3642 break;
3643 }
3644
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003645 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003646 DCHECK(first.Equals(locations->Out()));
3647 if (second.IsFpuRegister()) {
3648 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3649 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3650 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003651 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003652 __ mulss(first.AsFpuRegister<XmmRegister>(),
3653 codegen_->LiteralFloatAddress(
3654 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003655 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003656 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3657 } else {
3658 DCHECK(second.IsStackSlot());
3659 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3660 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003661 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003662 }
3663
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003664 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003665 DCHECK(first.Equals(locations->Out()));
3666 if (second.IsFpuRegister()) {
3667 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3668 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3669 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003670 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003671 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3672 codegen_->LiteralDoubleAddress(
3673 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003674 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003675 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3676 } else {
3677 DCHECK(second.IsDoubleStackSlot());
3678 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3679 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003680 break;
3681 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003682
3683 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003684 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003685 }
3686}
3687
Roland Levillain232ade02015-04-20 15:14:36 +01003688void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3689 uint32_t temp_offset,
3690 uint32_t stack_adjustment,
3691 bool is_fp,
3692 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003693 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003694 DCHECK(!is_wide);
3695 if (is_fp) {
3696 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3697 } else {
3698 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3699 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003700 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003701 DCHECK(is_wide);
3702 if (is_fp) {
3703 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3704 } else {
3705 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3706 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003707 } else {
3708 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003709 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003710 Location stack_temp = Location::StackSlot(temp_offset);
3711 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003712 if (is_fp) {
3713 __ flds(Address(ESP, temp_offset));
3714 } else {
3715 __ filds(Address(ESP, temp_offset));
3716 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003717 } else {
3718 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3719 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003720 if (is_fp) {
3721 __ fldl(Address(ESP, temp_offset));
3722 } else {
3723 __ fildl(Address(ESP, temp_offset));
3724 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003725 }
3726 }
3727}
3728
3729void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003730 DataType::Type type = rem->GetResultType();
3731 bool is_float = type == DataType::Type::kFloat32;
3732 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003733 LocationSummary* locations = rem->GetLocations();
3734 Location first = locations->InAt(0);
3735 Location second = locations->InAt(1);
3736 Location out = locations->Out();
3737
3738 // Create stack space for 2 elements.
3739 // TODO: enhance register allocator to ask for stack temporaries.
Vladimir Markodec78172020-06-19 15:31:23 +01003740 codegen_->IncreaseFrame(2 * elem_size);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003741
3742 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003743 const bool is_wide = !is_float;
Andreas Gampe3db70682018-12-26 15:12:03 -08003744 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp= */ true, is_wide);
3745 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp= */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003746
3747 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003748 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003749 __ Bind(&retry);
3750 __ fprem();
3751
3752 // Move FP status to AX.
3753 __ fstsw();
3754
3755 // And see if the argument reduction is complete. This is signaled by the
3756 // C2 FPU flag bit set to 0.
3757 __ andl(EAX, Immediate(kC2ConditionMask));
3758 __ j(kNotEqual, &retry);
3759
3760 // We have settled on the final value. Retrieve it into an XMM register.
3761 // Store FP top of stack to real stack.
3762 if (is_float) {
3763 __ fsts(Address(ESP, 0));
3764 } else {
3765 __ fstl(Address(ESP, 0));
3766 }
3767
3768 // Pop the 2 items from the FP stack.
3769 __ fucompp();
3770
3771 // Load the value from the stack into an XMM register.
3772 DCHECK(out.IsFpuRegister()) << out;
3773 if (is_float) {
3774 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3775 } else {
3776 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3777 }
3778
3779 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01003780 codegen_->DecreaseFrame(2 * elem_size);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003781}
3782
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003783
3784void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3785 DCHECK(instruction->IsDiv() || instruction->IsRem());
3786
3787 LocationSummary* locations = instruction->GetLocations();
3788 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003789 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003790
3791 Register out_register = locations->Out().AsRegister<Register>();
3792 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003793 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003794
3795 DCHECK(imm == 1 || imm == -1);
3796
3797 if (instruction->IsRem()) {
3798 __ xorl(out_register, out_register);
3799 } else {
3800 __ movl(out_register, input_register);
3801 if (imm == -1) {
3802 __ negl(out_register);
3803 }
3804 }
3805}
3806
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303807void InstructionCodeGeneratorX86::RemByPowerOfTwo(HRem* instruction) {
3808 LocationSummary* locations = instruction->GetLocations();
3809 Location second = locations->InAt(1);
3810
3811 Register out = locations->Out().AsRegister<Register>();
3812 Register numerator = locations->InAt(0).AsRegister<Register>();
3813
3814 int32_t imm = Int64FromConstant(second.GetConstant());
3815 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3816 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3817
3818 Register tmp = locations->GetTemp(0).AsRegister<Register>();
3819 NearLabel done;
3820 __ movl(out, numerator);
3821 __ andl(out, Immediate(abs_imm-1));
3822 __ j(Condition::kZero, &done);
3823 __ leal(tmp, Address(out, static_cast<int32_t>(~(abs_imm-1))));
3824 __ testl(numerator, numerator);
3825 __ cmovl(Condition::kLess, out, tmp);
3826 __ Bind(&done);
3827}
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003828
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003829void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003830 LocationSummary* locations = instruction->GetLocations();
3831
3832 Register out_register = locations->Out().AsRegister<Register>();
3833 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003834 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003835 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3836 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003837
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003838 Register num = locations->GetTemp(0).AsRegister<Register>();
3839
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003840 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003841 __ testl(input_register, input_register);
3842 __ cmovl(kGreaterEqual, num, input_register);
3843 int shift = CTZ(imm);
3844 __ sarl(num, Immediate(shift));
3845
3846 if (imm < 0) {
3847 __ negl(num);
3848 }
3849
3850 __ movl(out_register, num);
3851}
3852
3853void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3854 DCHECK(instruction->IsDiv() || instruction->IsRem());
3855
3856 LocationSummary* locations = instruction->GetLocations();
3857 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3858
3859 Register eax = locations->InAt(0).AsRegister<Register>();
3860 Register out = locations->Out().AsRegister<Register>();
3861 Register num;
3862 Register edx;
3863
3864 if (instruction->IsDiv()) {
3865 edx = locations->GetTemp(0).AsRegister<Register>();
3866 num = locations->GetTemp(1).AsRegister<Register>();
3867 } else {
3868 edx = locations->Out().AsRegister<Register>();
3869 num = locations->GetTemp(0).AsRegister<Register>();
3870 }
3871
3872 DCHECK_EQ(EAX, eax);
3873 DCHECK_EQ(EDX, edx);
3874 if (instruction->IsDiv()) {
3875 DCHECK_EQ(EAX, out);
3876 } else {
3877 DCHECK_EQ(EDX, out);
3878 }
3879
3880 int64_t magic;
3881 int shift;
Andreas Gampe3db70682018-12-26 15:12:03 -08003882 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003883
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003884 // Save the numerator.
3885 __ movl(num, eax);
3886
3887 // EAX = magic
3888 __ movl(eax, Immediate(magic));
3889
3890 // EDX:EAX = magic * numerator
3891 __ imull(num);
3892
3893 if (imm > 0 && magic < 0) {
3894 // EDX += num
3895 __ addl(edx, num);
3896 } else if (imm < 0 && magic > 0) {
3897 __ subl(edx, num);
3898 }
3899
3900 // Shift if needed.
3901 if (shift != 0) {
3902 __ sarl(edx, Immediate(shift));
3903 }
3904
3905 // EDX += 1 if EDX < 0
3906 __ movl(eax, edx);
3907 __ shrl(edx, Immediate(31));
3908 __ addl(edx, eax);
3909
3910 if (instruction->IsRem()) {
3911 __ movl(eax, num);
3912 __ imull(edx, Immediate(imm));
3913 __ subl(eax, edx);
3914 __ movl(edx, eax);
3915 } else {
3916 __ movl(eax, edx);
3917 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003918}
3919
Calin Juravlebacfec32014-11-14 15:54:36 +00003920void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3921 DCHECK(instruction->IsDiv() || instruction->IsRem());
3922
3923 LocationSummary* locations = instruction->GetLocations();
3924 Location out = locations->Out();
3925 Location first = locations->InAt(0);
3926 Location second = locations->InAt(1);
3927 bool is_div = instruction->IsDiv();
3928
3929 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003930 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003931 DCHECK_EQ(EAX, first.AsRegister<Register>());
3932 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003933
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003934 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003935 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003936
3937 if (imm == 0) {
3938 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3939 } else if (imm == 1 || imm == -1) {
3940 DivRemOneOrMinusOne(instruction);
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303941 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
3942 if (is_div) {
3943 DivByPowerOfTwo(instruction->AsDiv());
3944 } else {
3945 RemByPowerOfTwo(instruction->AsRem());
3946 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003947 } else {
3948 DCHECK(imm <= -2 || imm >= 2);
3949 GenerateDivRemWithAnyConstant(instruction);
3950 }
3951 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003952 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00003953 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003954 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003955
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003956 Register second_reg = second.AsRegister<Register>();
3957 // 0x80000000/-1 triggers an arithmetic exception!
3958 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3959 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003960
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003961 __ cmpl(second_reg, Immediate(-1));
3962 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003963
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003964 // edx:eax <- sign-extended of eax
3965 __ cdq();
3966 // eax = quotient, edx = remainder
3967 __ idivl(second_reg);
3968 __ Bind(slow_path->GetExitLabel());
3969 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003970 break;
3971 }
3972
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003973 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003974 InvokeRuntimeCallingConvention calling_convention;
3975 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3976 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3977 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3978 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3979 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3980 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3981
3982 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003983 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003984 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003985 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003986 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003987 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003988 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003989 break;
3990 }
3991
3992 default:
3993 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3994 }
3995}
3996
Calin Juravle7c4954d2014-10-28 16:57:40 +00003997void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003998 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003999 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004000 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01004001 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004002
Calin Juravle7c4954d2014-10-28 16:57:40 +00004003 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004004 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00004005 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004006 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00004007 locations->SetOut(Location::SameAsFirstInput());
4008 // Intel uses edx:eax as the dividend.
4009 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004010 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
4011 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
4012 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01004013 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004014 locations->AddTemp(Location::RequiresRegister());
4015 }
Calin Juravled0d48522014-11-04 16:40:20 +00004016 break;
4017 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004018 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004019 InvokeRuntimeCallingConvention calling_convention;
4020 locations->SetInAt(0, Location::RegisterPairLocation(
4021 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4022 locations->SetInAt(1, Location::RegisterPairLocation(
4023 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4024 // Runtime helper puts the result in EAX, EDX.
4025 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00004026 break;
4027 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004028 case DataType::Type::kFloat32:
4029 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004030 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00004031 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
4032 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00004033 } else if (div->InputAt(1)->IsConstant()) {
4034 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00004035 } else {
4036 locations->SetInAt(1, Location::Any());
4037 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004038 locations->SetOut(Location::SameAsFirstInput());
4039 break;
4040 }
4041
4042 default:
4043 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4044 }
4045}
4046
4047void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
4048 LocationSummary* locations = div->GetLocations();
4049 Location first = locations->InAt(0);
4050 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004051
4052 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004053 case DataType::Type::kInt32:
4054 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004055 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004056 break;
4057 }
4058
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004059 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04004060 if (second.IsFpuRegister()) {
4061 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
4062 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
4063 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00004064 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04004065 __ divss(first.AsFpuRegister<XmmRegister>(),
4066 codegen_->LiteralFloatAddress(
4067 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004068 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04004069 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
4070 } else {
4071 DCHECK(second.IsStackSlot());
4072 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
4073 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004074 break;
4075 }
4076
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004077 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04004078 if (second.IsFpuRegister()) {
4079 __ divsd(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 __ divsd(first.AsFpuRegister<XmmRegister>(),
4084 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004085 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
4086 const_area->GetBaseMethodAddress(),
4087 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04004088 } else {
4089 DCHECK(second.IsDoubleStackSlot());
4090 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
4091 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004092 break;
4093 }
4094
4095 default:
4096 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4097 }
4098}
4099
Calin Juravlebacfec32014-11-14 15:54:36 +00004100void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004101 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004102
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004103 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004104 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004105 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01004106 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00004107
Calin Juravled2ec87d2014-12-08 14:24:46 +00004108 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004109 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004110 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004111 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00004112 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004113 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
4114 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
4115 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01004116 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004117 locations->AddTemp(Location::RequiresRegister());
4118 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004119 break;
4120 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004121 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004122 InvokeRuntimeCallingConvention calling_convention;
4123 locations->SetInAt(0, Location::RegisterPairLocation(
4124 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4125 locations->SetInAt(1, Location::RegisterPairLocation(
4126 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4127 // Runtime helper puts the result in EAX, EDX.
4128 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
4129 break;
4130 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004131 case DataType::Type::kFloat64:
4132 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05004133 locations->SetInAt(0, Location::Any());
4134 locations->SetInAt(1, Location::Any());
4135 locations->SetOut(Location::RequiresFpuRegister());
4136 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00004137 break;
4138 }
4139
4140 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004141 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004142 }
4143}
4144
4145void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004146 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00004147 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004148 case DataType::Type::kInt32:
4149 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004150 GenerateDivRemIntegral(rem);
4151 break;
4152 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004153 case DataType::Type::kFloat32:
4154 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05004155 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00004156 break;
4157 }
4158 default:
4159 LOG(FATAL) << "Unexpected rem type " << type;
4160 }
4161}
4162
Aart Bik1f8d51b2018-02-15 10:42:37 -08004163static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
4164 LocationSummary* locations = new (allocator) LocationSummary(minmax);
4165 switch (minmax->GetResultType()) {
4166 case DataType::Type::kInt32:
4167 locations->SetInAt(0, Location::RequiresRegister());
4168 locations->SetInAt(1, Location::RequiresRegister());
4169 locations->SetOut(Location::SameAsFirstInput());
4170 break;
4171 case DataType::Type::kInt64:
4172 locations->SetInAt(0, Location::RequiresRegister());
4173 locations->SetInAt(1, Location::RequiresRegister());
4174 locations->SetOut(Location::SameAsFirstInput());
4175 // Register to use to perform a long subtract to set cc.
4176 locations->AddTemp(Location::RequiresRegister());
4177 break;
4178 case DataType::Type::kFloat32:
4179 locations->SetInAt(0, Location::RequiresFpuRegister());
4180 locations->SetInAt(1, Location::RequiresFpuRegister());
4181 locations->SetOut(Location::SameAsFirstInput());
4182 locations->AddTemp(Location::RequiresRegister());
4183 break;
4184 case DataType::Type::kFloat64:
4185 locations->SetInAt(0, Location::RequiresFpuRegister());
4186 locations->SetInAt(1, Location::RequiresFpuRegister());
4187 locations->SetOut(Location::SameAsFirstInput());
4188 break;
4189 default:
4190 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
4191 }
4192}
4193
Aart Bik351df3e2018-03-07 11:54:57 -08004194void InstructionCodeGeneratorX86::GenerateMinMaxInt(LocationSummary* locations,
4195 bool is_min,
4196 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08004197 Location op1_loc = locations->InAt(0);
4198 Location op2_loc = locations->InAt(1);
4199
4200 // Shortcut for same input locations.
4201 if (op1_loc.Equals(op2_loc)) {
4202 // Can return immediately, as op1_loc == out_loc.
4203 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
4204 // a copy here.
4205 DCHECK(locations->Out().Equals(op1_loc));
4206 return;
4207 }
4208
4209 if (type == DataType::Type::kInt64) {
4210 // Need to perform a subtract to get the sign right.
4211 // op1 is already in the same location as the output.
4212 Location output = locations->Out();
4213 Register output_lo = output.AsRegisterPairLow<Register>();
4214 Register output_hi = output.AsRegisterPairHigh<Register>();
4215
4216 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
4217 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
4218
4219 // The comparison is performed by subtracting the second operand from
4220 // the first operand and then setting the status flags in the same
4221 // manner as the SUB instruction."
4222 __ cmpl(output_lo, op2_lo);
4223
4224 // Now use a temp and the borrow to finish the subtraction of op2_hi.
4225 Register temp = locations->GetTemp(0).AsRegister<Register>();
4226 __ movl(temp, output_hi);
4227 __ sbbl(temp, op2_hi);
4228
4229 // Now the condition code is correct.
4230 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
4231 __ cmovl(cond, output_lo, op2_lo);
4232 __ cmovl(cond, output_hi, op2_hi);
4233 } else {
4234 DCHECK_EQ(type, DataType::Type::kInt32);
4235 Register out = locations->Out().AsRegister<Register>();
4236 Register op2 = op2_loc.AsRegister<Register>();
4237
4238 // (out := op1)
4239 // out <=? op2
4240 // if out is min jmp done
4241 // out := op2
4242 // done:
4243
4244 __ cmpl(out, op2);
4245 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
4246 __ cmovl(cond, out, op2);
4247 }
4248}
4249
4250void InstructionCodeGeneratorX86::GenerateMinMaxFP(LocationSummary* locations,
4251 bool is_min,
4252 DataType::Type type) {
4253 Location op1_loc = locations->InAt(0);
4254 Location op2_loc = locations->InAt(1);
4255 Location out_loc = locations->Out();
4256 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
4257
4258 // Shortcut for same input locations.
4259 if (op1_loc.Equals(op2_loc)) {
4260 DCHECK(out_loc.Equals(op1_loc));
4261 return;
4262 }
4263
4264 // (out := op1)
4265 // out <=? op2
4266 // if Nan jmp Nan_label
4267 // if out is min jmp done
4268 // if op2 is min jmp op2_label
4269 // handle -0/+0
4270 // jmp done
4271 // Nan_label:
4272 // out := NaN
4273 // op2_label:
4274 // out := op2
4275 // done:
4276 //
4277 // This removes one jmp, but needs to copy one input (op1) to out.
4278 //
4279 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
4280
4281 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
4282
4283 NearLabel nan, done, op2_label;
4284 if (type == DataType::Type::kFloat64) {
4285 __ ucomisd(out, op2);
4286 } else {
4287 DCHECK_EQ(type, DataType::Type::kFloat32);
4288 __ ucomiss(out, op2);
4289 }
4290
4291 __ j(Condition::kParityEven, &nan);
4292
4293 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
4294 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
4295
4296 // Handle 0.0/-0.0.
4297 if (is_min) {
4298 if (type == DataType::Type::kFloat64) {
4299 __ orpd(out, op2);
4300 } else {
4301 __ orps(out, op2);
4302 }
4303 } else {
4304 if (type == DataType::Type::kFloat64) {
4305 __ andpd(out, op2);
4306 } else {
4307 __ andps(out, op2);
4308 }
4309 }
4310 __ jmp(&done);
4311
4312 // NaN handling.
4313 __ Bind(&nan);
4314 if (type == DataType::Type::kFloat64) {
4315 // TODO: Use a constant from the constant table (requires extra input).
4316 __ LoadLongConstant(out, kDoubleNaN);
4317 } else {
4318 Register constant = locations->GetTemp(0).AsRegister<Register>();
4319 __ movl(constant, Immediate(kFloatNaN));
4320 __ movd(out, constant);
4321 }
4322 __ jmp(&done);
4323
4324 // out := op2;
4325 __ Bind(&op2_label);
4326 if (type == DataType::Type::kFloat64) {
4327 __ movsd(out, op2);
4328 } else {
4329 __ movss(out, op2);
4330 }
4331
4332 // Done.
4333 __ Bind(&done);
4334}
4335
Aart Bik351df3e2018-03-07 11:54:57 -08004336void InstructionCodeGeneratorX86::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4337 DataType::Type type = minmax->GetResultType();
4338 switch (type) {
4339 case DataType::Type::kInt32:
4340 case DataType::Type::kInt64:
4341 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
4342 break;
4343 case DataType::Type::kFloat32:
4344 case DataType::Type::kFloat64:
4345 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
4346 break;
4347 default:
4348 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4349 }
4350}
4351
Aart Bik1f8d51b2018-02-15 10:42:37 -08004352void LocationsBuilderX86::VisitMin(HMin* min) {
4353 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4354}
4355
4356void InstructionCodeGeneratorX86::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004357 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004358}
4359
4360void LocationsBuilderX86::VisitMax(HMax* max) {
4361 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4362}
4363
4364void InstructionCodeGeneratorX86::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004365 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004366}
4367
Aart Bik3dad3412018-02-28 12:01:46 -08004368void LocationsBuilderX86::VisitAbs(HAbs* abs) {
4369 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4370 switch (abs->GetResultType()) {
4371 case DataType::Type::kInt32:
4372 locations->SetInAt(0, Location::RegisterLocation(EAX));
4373 locations->SetOut(Location::SameAsFirstInput());
4374 locations->AddTemp(Location::RegisterLocation(EDX));
4375 break;
4376 case DataType::Type::kInt64:
4377 locations->SetInAt(0, Location::RequiresRegister());
4378 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4379 locations->AddTemp(Location::RequiresRegister());
4380 break;
4381 case DataType::Type::kFloat32:
4382 locations->SetInAt(0, Location::RequiresFpuRegister());
4383 locations->SetOut(Location::SameAsFirstInput());
4384 locations->AddTemp(Location::RequiresFpuRegister());
4385 locations->AddTemp(Location::RequiresRegister());
4386 break;
4387 case DataType::Type::kFloat64:
4388 locations->SetInAt(0, Location::RequiresFpuRegister());
4389 locations->SetOut(Location::SameAsFirstInput());
4390 locations->AddTemp(Location::RequiresFpuRegister());
4391 break;
4392 default:
4393 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4394 }
4395}
4396
4397void InstructionCodeGeneratorX86::VisitAbs(HAbs* abs) {
4398 LocationSummary* locations = abs->GetLocations();
4399 switch (abs->GetResultType()) {
4400 case DataType::Type::kInt32: {
4401 Register out = locations->Out().AsRegister<Register>();
4402 DCHECK_EQ(out, EAX);
4403 Register temp = locations->GetTemp(0).AsRegister<Register>();
4404 DCHECK_EQ(temp, EDX);
4405 // Sign extend EAX into EDX.
4406 __ cdq();
4407 // XOR EAX with sign.
4408 __ xorl(EAX, EDX);
4409 // Subtract out sign to correct.
4410 __ subl(EAX, EDX);
4411 // The result is in EAX.
4412 break;
4413 }
4414 case DataType::Type::kInt64: {
4415 Location input = locations->InAt(0);
4416 Register input_lo = input.AsRegisterPairLow<Register>();
4417 Register input_hi = input.AsRegisterPairHigh<Register>();
4418 Location output = locations->Out();
4419 Register output_lo = output.AsRegisterPairLow<Register>();
4420 Register output_hi = output.AsRegisterPairHigh<Register>();
4421 Register temp = locations->GetTemp(0).AsRegister<Register>();
4422 // Compute the sign into the temporary.
4423 __ movl(temp, input_hi);
4424 __ sarl(temp, Immediate(31));
4425 // Store the sign into the output.
4426 __ movl(output_lo, temp);
4427 __ movl(output_hi, temp);
4428 // XOR the input to the output.
4429 __ xorl(output_lo, input_lo);
4430 __ xorl(output_hi, input_hi);
4431 // Subtract the sign.
4432 __ subl(output_lo, temp);
4433 __ sbbl(output_hi, temp);
4434 break;
4435 }
4436 case DataType::Type::kFloat32: {
4437 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4438 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4439 Register constant = locations->GetTemp(1).AsRegister<Register>();
4440 __ movl(constant, Immediate(INT32_C(0x7FFFFFFF)));
4441 __ movd(temp, constant);
4442 __ andps(out, temp);
4443 break;
4444 }
4445 case DataType::Type::kFloat64: {
4446 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4447 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4448 // TODO: Use a constant from the constant table (requires extra input).
4449 __ LoadLongConstant(temp, INT64_C(0x7FFFFFFFFFFFFFFF));
4450 __ andpd(out, temp);
4451 break;
4452 }
4453 default:
4454 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4455 }
4456}
4457
Calin Juravled0d48522014-11-04 16:40:20 +00004458void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004459 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004460 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004461 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004462 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004463 case DataType::Type::kInt8:
4464 case DataType::Type::kUint16:
4465 case DataType::Type::kInt16:
4466 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004467 locations->SetInAt(0, Location::Any());
4468 break;
4469 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004470 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004471 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
4472 if (!instruction->IsConstant()) {
4473 locations->AddTemp(Location::RequiresRegister());
4474 }
4475 break;
4476 }
4477 default:
4478 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4479 }
Calin Juravled0d48522014-11-04 16:40:20 +00004480}
4481
4482void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004483 SlowPathCode* slow_path =
4484 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004485 codegen_->AddSlowPath(slow_path);
4486
4487 LocationSummary* locations = instruction->GetLocations();
4488 Location value = locations->InAt(0);
4489
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004490 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004491 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004492 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004493 case DataType::Type::kInt8:
4494 case DataType::Type::kUint16:
4495 case DataType::Type::kInt16:
4496 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004497 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004498 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004499 __ j(kEqual, slow_path->GetEntryLabel());
4500 } else if (value.IsStackSlot()) {
4501 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
4502 __ j(kEqual, slow_path->GetEntryLabel());
4503 } else {
4504 DCHECK(value.IsConstant()) << value;
4505 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004506 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004507 }
4508 }
4509 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004510 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004511 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004512 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004513 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004514 __ movl(temp, value.AsRegisterPairLow<Register>());
4515 __ orl(temp, value.AsRegisterPairHigh<Register>());
4516 __ j(kEqual, slow_path->GetEntryLabel());
4517 } else {
4518 DCHECK(value.IsConstant()) << value;
4519 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4520 __ jmp(slow_path->GetEntryLabel());
4521 }
4522 }
4523 break;
4524 }
4525 default:
4526 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004527 }
Calin Juravled0d48522014-11-04 16:40:20 +00004528}
4529
Calin Juravle9aec02f2014-11-18 23:06:35 +00004530void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
4531 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4532
4533 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004534 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004535
4536 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004537 case DataType::Type::kInt32:
4538 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004539 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00004540 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00004541 // The shift count needs to be in CL or a constant.
4542 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004543 locations->SetOut(Location::SameAsFirstInput());
4544 break;
4545 }
4546 default:
4547 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4548 }
4549}
4550
4551void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
4552 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4553
4554 LocationSummary* locations = op->GetLocations();
4555 Location first = locations->InAt(0);
4556 Location second = locations->InAt(1);
4557 DCHECK(first.Equals(locations->Out()));
4558
4559 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004560 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00004561 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004562 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004563 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004564 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004565 DCHECK_EQ(ECX, second_reg);
4566 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004567 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004568 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004569 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004570 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004571 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004572 }
4573 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004574 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004575 if (shift == 0) {
4576 return;
4577 }
4578 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004579 if (op->IsShl()) {
4580 __ shll(first_reg, imm);
4581 } else if (op->IsShr()) {
4582 __ sarl(first_reg, imm);
4583 } else {
4584 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004585 }
4586 }
4587 break;
4588 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004589 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004590 if (second.IsRegister()) {
4591 Register second_reg = second.AsRegister<Register>();
4592 DCHECK_EQ(ECX, second_reg);
4593 if (op->IsShl()) {
4594 GenerateShlLong(first, second_reg);
4595 } else if (op->IsShr()) {
4596 GenerateShrLong(first, second_reg);
4597 } else {
4598 GenerateUShrLong(first, second_reg);
4599 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004600 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00004601 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004602 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004603 // Nothing to do if the shift is 0, as the input is already the output.
4604 if (shift != 0) {
4605 if (op->IsShl()) {
4606 GenerateShlLong(first, shift);
4607 } else if (op->IsShr()) {
4608 GenerateShrLong(first, shift);
4609 } else {
4610 GenerateUShrLong(first, shift);
4611 }
4612 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004613 }
4614 break;
4615 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004616 default:
4617 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4618 }
4619}
4620
Mark P Mendell73945692015-04-29 14:56:17 +00004621void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
4622 Register low = loc.AsRegisterPairLow<Register>();
4623 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04004624 if (shift == 1) {
4625 // This is just an addition.
4626 __ addl(low, low);
4627 __ adcl(high, high);
4628 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00004629 // Shift by 32 is easy. High gets low, and low gets 0.
4630 codegen_->EmitParallelMoves(
4631 loc.ToLow(),
4632 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004633 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004634 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4635 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004636 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004637 } else if (shift > 32) {
4638 // Low part becomes 0. High part is low part << (shift-32).
4639 __ movl(high, low);
4640 __ shll(high, Immediate(shift - 32));
4641 __ xorl(low, low);
4642 } else {
4643 // Between 1 and 31.
4644 __ shld(high, low, Immediate(shift));
4645 __ shll(low, Immediate(shift));
4646 }
4647}
4648
Calin Juravle9aec02f2014-11-18 23:06:35 +00004649void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004650 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004651 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4652 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4653 __ testl(shifter, Immediate(32));
4654 __ j(kEqual, &done);
4655 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4656 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4657 __ Bind(&done);
4658}
4659
Mark P Mendell73945692015-04-29 14:56:17 +00004660void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4661 Register low = loc.AsRegisterPairLow<Register>();
4662 Register high = loc.AsRegisterPairHigh<Register>();
4663 if (shift == 32) {
4664 // Need to copy the sign.
4665 DCHECK_NE(low, high);
4666 __ movl(low, high);
4667 __ sarl(high, Immediate(31));
4668 } else if (shift > 32) {
4669 DCHECK_NE(low, high);
4670 // High part becomes sign. Low part is shifted by shift - 32.
4671 __ movl(low, high);
4672 __ sarl(high, Immediate(31));
4673 __ sarl(low, Immediate(shift - 32));
4674 } else {
4675 // Between 1 and 31.
4676 __ shrd(low, high, Immediate(shift));
4677 __ sarl(high, Immediate(shift));
4678 }
4679}
4680
Calin Juravle9aec02f2014-11-18 23:06:35 +00004681void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004682 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004683 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4684 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4685 __ testl(shifter, Immediate(32));
4686 __ j(kEqual, &done);
4687 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4688 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4689 __ Bind(&done);
4690}
4691
Mark P Mendell73945692015-04-29 14:56:17 +00004692void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4693 Register low = loc.AsRegisterPairLow<Register>();
4694 Register high = loc.AsRegisterPairHigh<Register>();
4695 if (shift == 32) {
4696 // Shift by 32 is easy. Low gets high, and high gets 0.
4697 codegen_->EmitParallelMoves(
4698 loc.ToHigh(),
4699 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004700 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004701 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4702 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004703 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004704 } else if (shift > 32) {
4705 // Low part is high >> (shift - 32). High part becomes 0.
4706 __ movl(low, high);
4707 __ shrl(low, Immediate(shift - 32));
4708 __ xorl(high, high);
4709 } else {
4710 // Between 1 and 31.
4711 __ shrd(low, high, Immediate(shift));
4712 __ shrl(high, Immediate(shift));
4713 }
4714}
4715
Calin Juravle9aec02f2014-11-18 23:06:35 +00004716void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004717 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004718 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4719 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4720 __ testl(shifter, Immediate(32));
4721 __ j(kEqual, &done);
4722 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4723 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4724 __ Bind(&done);
4725}
4726
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004727void LocationsBuilderX86::VisitRor(HRor* ror) {
4728 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004729 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004730
4731 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004732 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004733 // Add the temporary needed.
4734 locations->AddTemp(Location::RequiresRegister());
4735 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004736 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004737 locations->SetInAt(0, Location::RequiresRegister());
4738 // The shift count needs to be in CL (unless it is a constant).
4739 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4740 locations->SetOut(Location::SameAsFirstInput());
4741 break;
4742 default:
4743 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4744 UNREACHABLE();
4745 }
4746}
4747
4748void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4749 LocationSummary* locations = ror->GetLocations();
4750 Location first = locations->InAt(0);
4751 Location second = locations->InAt(1);
4752
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004753 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004754 Register first_reg = first.AsRegister<Register>();
4755 if (second.IsRegister()) {
4756 Register second_reg = second.AsRegister<Register>();
4757 __ rorl(first_reg, second_reg);
4758 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004759 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004760 __ rorl(first_reg, imm);
4761 }
4762 return;
4763 }
4764
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004765 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004766 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4767 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4768 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4769 if (second.IsRegister()) {
4770 Register second_reg = second.AsRegister<Register>();
4771 DCHECK_EQ(second_reg, ECX);
4772 __ movl(temp_reg, first_reg_hi);
4773 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4774 __ shrd(first_reg_lo, temp_reg, second_reg);
4775 __ movl(temp_reg, first_reg_hi);
4776 __ testl(second_reg, Immediate(32));
4777 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4778 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4779 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004780 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004781 if (shift_amt == 0) {
4782 // Already fine.
4783 return;
4784 }
4785 if (shift_amt == 32) {
4786 // Just swap.
4787 __ movl(temp_reg, first_reg_lo);
4788 __ movl(first_reg_lo, first_reg_hi);
4789 __ movl(first_reg_hi, temp_reg);
4790 return;
4791 }
4792
4793 Immediate imm(shift_amt);
4794 // Save the constents of the low value.
4795 __ movl(temp_reg, first_reg_lo);
4796
4797 // Shift right into low, feeding bits from high.
4798 __ shrd(first_reg_lo, first_reg_hi, imm);
4799
4800 // Shift right into high, feeding bits from the original low.
4801 __ shrd(first_reg_hi, temp_reg, imm);
4802
4803 // Swap if needed.
4804 if (shift_amt > 32) {
4805 __ movl(temp_reg, first_reg_lo);
4806 __ movl(first_reg_lo, first_reg_hi);
4807 __ movl(first_reg_hi, temp_reg);
4808 }
4809 }
4810}
4811
Calin Juravle9aec02f2014-11-18 23:06:35 +00004812void LocationsBuilderX86::VisitShl(HShl* shl) {
4813 HandleShift(shl);
4814}
4815
4816void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4817 HandleShift(shl);
4818}
4819
4820void LocationsBuilderX86::VisitShr(HShr* shr) {
4821 HandleShift(shr);
4822}
4823
4824void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4825 HandleShift(shr);
4826}
4827
4828void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4829 HandleShift(ushr);
4830}
4831
4832void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4833 HandleShift(ushr);
4834}
4835
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004836void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004837 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4838 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004839 locations->SetOut(Location::RegisterLocation(EAX));
Alex Lightd109e302018-06-27 10:25:41 -07004840 InvokeRuntimeCallingConvention calling_convention;
4841 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004842}
4843
4844void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07004845 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
4846 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4847 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004848}
4849
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004850void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004851 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4852 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004853 locations->SetOut(Location::RegisterLocation(EAX));
4854 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004855 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4856 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004857}
4858
4859void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01004860 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
4861 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004862 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004863 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004864 DCHECK(!codegen_->IsLeafMethod());
4865}
4866
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004867void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004868 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004869 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004870 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4871 if (location.IsStackSlot()) {
4872 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4873 } else if (location.IsDoubleStackSlot()) {
4874 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004875 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004876 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004877}
4878
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004879void InstructionCodeGeneratorX86::VisitParameterValue(
4880 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4881}
4882
4883void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4884 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004885 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004886 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4887}
4888
4889void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004890}
4891
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004892void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4893 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004894 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004895 locations->SetInAt(0, Location::RequiresRegister());
4896 locations->SetOut(Location::RequiresRegister());
4897}
4898
4899void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4900 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004901 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004902 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004903 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004904 __ movl(locations->Out().AsRegister<Register>(),
4905 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004906 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004907 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004908 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004909 __ movl(locations->Out().AsRegister<Register>(),
4910 Address(locations->InAt(0).AsRegister<Register>(),
4911 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4912 // temp = temp->GetImtEntryAt(method_offset);
4913 __ movl(locations->Out().AsRegister<Register>(),
4914 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004915 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004916}
4917
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004918void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004919 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004920 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004921 locations->SetInAt(0, Location::RequiresRegister());
4922 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004923}
4924
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004925void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4926 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004927 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004928 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004929 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004930 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004931 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004932 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004933 break;
4934
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004935 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004936 __ notl(out.AsRegisterPairLow<Register>());
4937 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004938 break;
4939
4940 default:
4941 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4942 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004943}
4944
David Brazdil66d126e2015-04-03 16:02:44 +01004945void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4946 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004947 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004948 locations->SetInAt(0, Location::RequiresRegister());
4949 locations->SetOut(Location::SameAsFirstInput());
4950}
4951
4952void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004953 LocationSummary* locations = bool_not->GetLocations();
4954 Location in = locations->InAt(0);
4955 Location out = locations->Out();
4956 DCHECK(in.Equals(out));
4957 __ xorl(out.AsRegister<Register>(), Immediate(1));
4958}
4959
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004960void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004961 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004962 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004963 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004964 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004965 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004966 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004967 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004968 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004969 case DataType::Type::kInt32:
4970 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004971 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004972 locations->SetInAt(1, Location::Any());
4973 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4974 break;
4975 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004976 case DataType::Type::kFloat32:
4977 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004978 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004979 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4980 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4981 } else if (compare->InputAt(1)->IsConstant()) {
4982 locations->SetInAt(1, Location::RequiresFpuRegister());
4983 } else {
4984 locations->SetInAt(1, Location::Any());
4985 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004986 locations->SetOut(Location::RequiresRegister());
4987 break;
4988 }
4989 default:
4990 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4991 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004992}
4993
4994void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004995 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004996 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004997 Location left = locations->InAt(0);
4998 Location right = locations->InAt(1);
4999
Mark Mendell0c9497d2015-08-21 09:30:05 -04005000 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08005001 Condition less_cond = kLess;
5002
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005003 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005004 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005005 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005006 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005007 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005008 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005009 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01005010 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08005011 break;
5012 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005013 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005014 Register left_low = left.AsRegisterPairLow<Register>();
5015 Register left_high = left.AsRegisterPairHigh<Register>();
5016 int32_t val_low = 0;
5017 int32_t val_high = 0;
5018 bool right_is_const = false;
5019
5020 if (right.IsConstant()) {
5021 DCHECK(right.GetConstant()->IsLongConstant());
5022 right_is_const = true;
5023 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
5024 val_low = Low32Bits(val);
5025 val_high = High32Bits(val);
5026 }
5027
Calin Juravleddb7df22014-11-25 20:56:51 +00005028 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005029 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005030 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005031 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005032 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005033 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08005034 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005035 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005036 __ j(kLess, &less); // Signed compare.
5037 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005038 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005039 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005040 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005041 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005042 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005043 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08005044 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005045 }
Aart Bika19616e2016-02-01 18:57:58 -08005046 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00005047 break;
5048 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005049 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00005050 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00005051 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08005052 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00005053 break;
5054 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005055 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00005056 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00005057 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08005058 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005059 break;
5060 }
5061 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00005062 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005063 }
Aart Bika19616e2016-02-01 18:57:58 -08005064
Calin Juravleddb7df22014-11-25 20:56:51 +00005065 __ movl(out, Immediate(0));
5066 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08005067 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00005068
5069 __ Bind(&greater);
5070 __ movl(out, Immediate(1));
5071 __ jmp(&done);
5072
5073 __ Bind(&less);
5074 __ movl(out, Immediate(-1));
5075
5076 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005077}
5078
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005079void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005080 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005081 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005082 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01005083 locations->SetInAt(i, Location::Any());
5084 }
5085 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005086}
5087
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005088void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005089 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005090}
5091
Roland Levillain7c1559a2015-12-15 10:55:36 +00005092void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00005093 /*
5094 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
5095 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
5096 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
5097 */
5098 switch (kind) {
5099 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00005100 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00005101 break;
5102 }
5103 case MemBarrierKind::kAnyStore:
5104 case MemBarrierKind::kLoadAny:
5105 case MemBarrierKind::kStoreStore: {
5106 // nop
5107 break;
5108 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05005109 case MemBarrierKind::kNTStoreStore:
5110 // Non-Temporal Store/Store needs an explicit fence.
Andreas Gampe3db70682018-12-26 15:12:03 -08005111 MemoryFence(/* non-temporal= */ true);
Mark Mendell7aa04a12016-01-27 22:39:07 -05005112 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005113 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005114}
5115
Vladimir Markodc151b22015-10-15 18:02:30 +01005116HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
5117 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01005118 ArtMethod* method ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005119 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005120}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005121
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005122Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
5123 Register temp) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00005124 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005125 if (!invoke->GetLocations()->Intrinsified()) {
5126 return location.AsRegister<Register>();
5127 }
5128 // For intrinsics we allow any location, so it may be on the stack.
5129 if (!location.IsRegister()) {
5130 __ movl(temp, Address(ESP, location.GetStackIndex()));
5131 return temp;
5132 }
5133 // For register locations, check if the register was saved. If so, get it from the stack.
5134 // Note: There is a chance that the register was saved but not overwritten, so we could
5135 // save one load. However, since this is just an intrinsic slow path we prefer this
5136 // simple and more robust approach rather that trying to determine if that's the case.
5137 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00005138 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
5139 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
5140 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
5141 __ movl(temp, Address(ESP, stack_offset));
5142 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005143 }
5144 return location.AsRegister<Register>();
5145}
5146
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005147void CodeGeneratorX86::GenerateStaticOrDirectCall(
5148 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00005149 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
5150 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005151 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005152 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005153 uint32_t offset =
5154 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
5155 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00005156 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005157 }
Vladimir Marko58155012015-08-19 12:49:41 +00005158 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Marko86c87522020-05-11 16:55:55 +01005159 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005160 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005161 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005162 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko65979462017-05-19 17:25:12 +01005163 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
5164 temp.AsRegister<Register>());
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005165 __ leal(temp.AsRegister<Register>(),
5166 Address(base_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005167 RecordBootImageMethodPatch(invoke);
Vladimir Marko65979462017-05-19 17:25:12 +01005168 break;
5169 }
Vladimir Markob066d432018-01-03 13:14:37 +00005170 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
5171 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
5172 temp.AsRegister<Register>());
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005173 __ movl(temp.AsRegister<Register>(), Address(base_reg, kPlaceholder32BitOffset));
Vladimir Markob066d432018-01-03 13:14:37 +00005174 RecordBootImageRelRoPatch(
5175 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005176 GetBootImageOffset(invoke));
Vladimir Markob066d432018-01-03 13:14:37 +00005177 break;
5178 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005179 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005180 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
5181 temp.AsRegister<Register>());
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005182 __ movl(temp.AsRegister<Register>(), Address(base_reg, kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005183 RecordMethodBssEntryPatch(invoke);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005184 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005185 break;
5186 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005187 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
5188 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
5189 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005190 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
5191 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5192 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01005193 }
Vladimir Marko58155012015-08-19 12:49:41 +00005194 }
5195
5196 switch (invoke->GetCodePtrLocation()) {
5197 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
5198 __ call(GetFrameEntryLabel());
Vladimir Marko86c87522020-05-11 16:55:55 +01005199 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko58155012015-08-19 12:49:41 +00005200 break;
Vladimir Marko86c87522020-05-11 16:55:55 +01005201 case HInvokeStaticOrDirect::CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01005202 size_t out_frame_size =
5203 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorX86,
5204 kNativeStackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01005205 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Marko86c87522020-05-11 16:55:55 +01005206 // (callee_method + offset_of_jni_entry_point)()
5207 __ call(Address(callee_method.AsRegister<Register>(),
5208 ArtMethod::EntryPointFromJniOffset(kX86PointerSize).Int32Value()));
5209 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5210 if (out_frame_size == 0u && DataType::IsFloatingPointType(invoke->GetType())) {
5211 // Create space for conversion.
5212 out_frame_size = 8u;
Vladimir Markodec78172020-06-19 15:31:23 +01005213 IncreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01005214 }
5215 // Zero-/sign-extend or move the result when needed due to native and managed ABI mismatch.
5216 switch (invoke->GetType()) {
5217 case DataType::Type::kBool:
5218 __ movzxb(EAX, AL);
5219 break;
5220 case DataType::Type::kInt8:
5221 __ movsxb(EAX, AL);
5222 break;
5223 case DataType::Type::kUint16:
5224 __ movzxw(EAX, EAX);
5225 break;
5226 case DataType::Type::kInt16:
5227 __ movsxw(EAX, EAX);
5228 break;
5229 case DataType::Type::kFloat32:
5230 __ fstps(Address(ESP, 0));
5231 __ movss(XMM0, Address(ESP, 0));
5232 break;
5233 case DataType::Type::kFloat64:
5234 __ fstpl(Address(ESP, 0));
5235 __ movsd(XMM0, Address(ESP, 0));
5236 break;
5237 case DataType::Type::kInt32:
5238 case DataType::Type::kInt64:
5239 case DataType::Type::kVoid:
5240 break;
5241 default:
5242 DCHECK(false) << invoke->GetType();
5243 break;
5244 }
5245 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01005246 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01005247 }
5248 break;
5249 }
Vladimir Marko58155012015-08-19 12:49:41 +00005250 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5251 // (callee_method + offset_of_quick_compiled_code)()
5252 __ call(Address(callee_method.AsRegister<Register>(),
5253 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005254 kX86PointerSize).Int32Value()));
Vladimir Marko86c87522020-05-11 16:55:55 +01005255 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko58155012015-08-19 12:49:41 +00005256 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04005257 }
5258
5259 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04005260}
5261
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005262void CodeGeneratorX86::GenerateVirtualCall(
5263 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005264 Register temp = temp_in.AsRegister<Register>();
5265 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5266 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005267
5268 // Use the calling convention instead of the location of the receiver, as
5269 // intrinsics may have put the receiver in a different register. In the intrinsics
5270 // slow path, the arguments have been moved to the right place, so here we are
5271 // guaranteed that the receiver is the first register of the calling convention.
5272 InvokeDexCallingConvention calling_convention;
5273 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005274 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005275 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005276 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005277 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005278 // Instead of simply (possibly) unpoisoning `temp` here, we should
5279 // emit a read barrier for the previous class reference load.
5280 // However this is not required in practice, as this is an
5281 // intermediate/temporary reference and because the current
5282 // concurrent copying collector keeps the from-space memory
5283 // intact/accessible until the end of the marking phase (the
5284 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005285 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00005286
5287 MaybeGenerateInlineCacheCheck(invoke, temp);
5288
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005289 // temp = temp->GetMethodAt(method_offset);
5290 __ movl(temp, Address(temp, method_offset));
5291 // call temp->GetEntryPoint();
5292 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07005293 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005294 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005295}
5296
Vladimir Marko6fd16062018-06-26 11:02:04 +01005297void CodeGeneratorX86::RecordBootImageIntrinsicPatch(HX86ComputeBaseMethodAddress* method_address,
5298 uint32_t intrinsic_data) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005299 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005300 method_address, /* target_dex_file= */ nullptr, intrinsic_data);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005301 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005302}
5303
Vladimir Markob066d432018-01-03 13:14:37 +00005304void CodeGeneratorX86::RecordBootImageRelRoPatch(HX86ComputeBaseMethodAddress* method_address,
5305 uint32_t boot_image_offset) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005306 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005307 method_address, /* target_dex_file= */ nullptr, boot_image_offset);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005308 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Markob066d432018-01-03 13:14:37 +00005309}
5310
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005311void CodeGeneratorX86::RecordBootImageMethodPatch(HInvokeStaticOrDirect* invoke) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005312 HX86ComputeBaseMethodAddress* method_address =
Vladimir Marko65979462017-05-19 17:25:12 +01005313 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005314 boot_image_method_patches_.emplace_back(
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005315 method_address,
5316 invoke->GetResolvedMethodReference().dex_file,
5317 invoke->GetResolvedMethodReference().index);
Vladimir Marko65979462017-05-19 17:25:12 +01005318 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005319}
5320
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005321void CodeGeneratorX86::RecordMethodBssEntryPatch(HInvokeStaticOrDirect* invoke) {
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005322 DCHECK(IsSameDexFile(GetGraph()->GetDexFile(), *invoke->GetMethodReference().dex_file));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005323 HX86ComputeBaseMethodAddress* method_address =
5324 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005325 // Add the patch entry and bind its label at the end of the instruction.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005326 method_bss_entry_patches_.emplace_back(
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005327 method_address,
5328 invoke->GetMethodReference().dex_file,
5329 invoke->GetMethodReference().index);
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005330 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005331}
5332
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005333void CodeGeneratorX86::RecordBootImageTypePatch(HLoadClass* load_class) {
5334 HX86ComputeBaseMethodAddress* method_address =
5335 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5336 boot_image_type_patches_.emplace_back(
5337 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005338 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005339}
5340
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005341Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005342 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005343 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5344 type_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005345 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005346 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005347}
5348
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005349void CodeGeneratorX86::RecordBootImageStringPatch(HLoadString* load_string) {
5350 HX86ComputeBaseMethodAddress* method_address =
5351 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
5352 boot_image_string_patches_.emplace_back(
5353 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
5354 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01005355}
5356
Vladimir Markoaad75c62016-10-03 08:46:48 +00005357Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005358 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005359 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005360 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005361 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005362 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005363}
5364
Vladimir Markoeebb8212018-06-05 14:57:24 +01005365void CodeGeneratorX86::LoadBootImageAddress(Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01005366 uint32_t boot_image_reference,
Vladimir Markoeebb8212018-06-05 14:57:24 +01005367 HInvokeStaticOrDirect* invoke) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005368 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005369 HX86ComputeBaseMethodAddress* method_address =
5370 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5371 DCHECK(method_address != nullptr);
5372 Register method_address_reg =
5373 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005374 __ leal(reg, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005375 RecordBootImageIntrinsicPatch(method_address, boot_image_reference);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01005376 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01005377 HX86ComputeBaseMethodAddress* method_address =
5378 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5379 DCHECK(method_address != nullptr);
5380 Register method_address_reg =
5381 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005382 __ movl(reg, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005383 RecordBootImageRelRoPatch(method_address, boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01005384 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01005385 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01005386 gc::Heap* heap = Runtime::Current()->GetHeap();
5387 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01005388 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01005389 __ movl(reg, Immediate(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address))));
5390 }
5391}
5392
Vladimir Marko6fd16062018-06-26 11:02:04 +01005393void CodeGeneratorX86::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
5394 uint32_t boot_image_offset) {
5395 DCHECK(invoke->IsStatic());
5396 InvokeRuntimeCallingConvention calling_convention;
5397 Register argument = calling_convention.GetRegisterAt(0);
5398 if (GetCompilerOptions().IsBootImage()) {
5399 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
5400 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
Vladimir Marko6fd16062018-06-26 11:02:04 +01005401 HX86ComputeBaseMethodAddress* method_address =
5402 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5403 DCHECK(method_address != nullptr);
5404 Register method_address_reg =
5405 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005406 __ leal(argument, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005407 MethodReference target_method = invoke->GetResolvedMethodReference();
Vladimir Marko6fd16062018-06-26 11:02:04 +01005408 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
5409 boot_image_type_patches_.emplace_back(method_address, target_method.dex_file, type_idx.index_);
5410 __ Bind(&boot_image_type_patches_.back().label);
5411 } else {
5412 LoadBootImageAddress(argument, boot_image_offset, invoke);
5413 }
5414 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
5415 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
5416}
5417
Vladimir Markoaad75c62016-10-03 08:46:48 +00005418// The label points to the end of the "movl" or another instruction but the literal offset
5419// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
5420constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
5421
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005422template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005423inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005424 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005425 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005426 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005427 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005428 linker_patches->push_back(Factory(literal_offset,
5429 info.target_dex_file,
5430 GetMethodAddressOffset(info.method_address),
5431 info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005432 }
5433}
5434
Vladimir Marko6fd16062018-06-26 11:02:04 +01005435template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
5436linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
5437 const DexFile* target_dex_file,
5438 uint32_t pc_insn_offset,
5439 uint32_t boot_image_offset) {
5440 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5441 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005442}
5443
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005444void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005445 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005446 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01005447 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005448 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00005449 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01005450 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005451 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01005452 string_bss_entry_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01005453 boot_image_other_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005454 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01005455 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005456 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
5457 boot_image_method_patches_, linker_patches);
5458 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
5459 boot_image_type_patches_, linker_patches);
5460 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005461 boot_image_string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005462 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005463 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005464 DCHECK(boot_image_type_patches_.empty());
5465 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01005466 }
5467 if (GetCompilerOptions().IsBootImage()) {
5468 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5469 boot_image_other_patches_, linker_patches);
5470 } else {
5471 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
5472 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005473 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005474 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5475 method_bss_entry_patches_, linker_patches);
5476 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5477 type_bss_entry_patches_, linker_patches);
5478 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5479 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005480 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005481}
5482
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005483void CodeGeneratorX86::MarkGCCard(Register temp,
5484 Register card,
5485 Register object,
5486 Register value,
5487 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005488 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005489 if (value_can_be_null) {
5490 __ testl(value, value);
5491 __ j(kEqual, &is_null);
5492 }
Roland Levillainc73f0522018-08-14 15:16:50 +01005493 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07005494 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01005495 // Calculate the offset (in the card table) of the card corresponding to
5496 // `object`.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005497 __ movl(temp, object);
5498 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01005499 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
5500 // `object`'s card.
5501 //
5502 // Register `card` contains the address of the card table. Note that the card
5503 // table's base is biased during its creation so that it always starts at an
5504 // address whose least-significant byte is equal to `kCardDirty` (see
5505 // art::gc::accounting::CardTable::Create). Therefore the MOVB instruction
5506 // below writes the `kCardDirty` (byte) value into the `object`'s card
5507 // (located at `card + object >> kCardShift`).
5508 //
5509 // This dual use of the value in register `card` (1. to calculate the location
5510 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
5511 // (no need to explicitly load `kCardDirty` as an immediate value).
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00005512 __ movb(Address(temp, card, TIMES_1, 0),
5513 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005514 if (value_can_be_null) {
5515 __ Bind(&is_null);
5516 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005517}
5518
Calin Juravle52c48962014-12-16 17:02:57 +00005519void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5520 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005521
5522 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005523 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005524 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005525 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5526 kEmitCompilerReadBarrier
5527 ? LocationSummary::kCallOnSlowPath
5528 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005529 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005530 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005531 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005532 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005533
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005534 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005535 locations->SetOut(Location::RequiresFpuRegister());
5536 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005537 // The output overlaps in case of long: we don't want the low move
5538 // to overwrite the object's location. Likewise, in the case of
5539 // an object field get with read barriers enabled, we do not want
5540 // the move to overwrite the object's location, as we need it to emit
5541 // the read barrier.
5542 locations->SetOut(
5543 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005544 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005545 Location::kOutputOverlap :
5546 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005547 }
Calin Juravle52c48962014-12-16 17:02:57 +00005548
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005549 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00005550 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005551 // So we use an XMM register as a temp to achieve atomicity (first
5552 // load the temp into the XMM and then copy the XMM into the
5553 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00005554 locations->AddTemp(Location::RequiresFpuRegister());
5555 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005556}
5557
Calin Juravle52c48962014-12-16 17:02:57 +00005558void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
5559 const FieldInfo& field_info) {
5560 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005561
Calin Juravle52c48962014-12-16 17:02:57 +00005562 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005563 Location base_loc = locations->InAt(0);
5564 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005565 Location out = locations->Out();
5566 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01005567 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5568 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00005569 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5570
Vladimir Marko61b92282017-10-11 13:23:17 +01005571 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005572 case DataType::Type::kBool:
5573 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005574 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005575 break;
5576 }
5577
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005578 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005579 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005580 break;
5581 }
5582
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005583 case DataType::Type::kUint16: {
5584 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005585 break;
5586 }
5587
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005588 case DataType::Type::kInt16: {
5589 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005590 break;
5591 }
5592
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005593 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00005594 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005595 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005596
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005597 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005598 // /* HeapReference<Object> */ out = *(base + offset)
5599 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005600 // Note that a potential implicit null check is handled in this
5601 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
5602 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005603 instruction, out, base, offset, /* needs_null_check= */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005604 if (is_volatile) {
5605 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5606 }
5607 } else {
5608 __ movl(out.AsRegister<Register>(), Address(base, offset));
5609 codegen_->MaybeRecordImplicitNullCheck(instruction);
5610 if (is_volatile) {
5611 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5612 }
5613 // If read barriers are enabled, emit read barriers other than
5614 // Baker's using a slow path (and also unpoison the loaded
5615 // reference, if heap poisoning is enabled).
5616 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5617 }
5618 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005619 }
5620
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005621 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005622 if (is_volatile) {
5623 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5624 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005625 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005626 __ movd(out.AsRegisterPairLow<Register>(), temp);
5627 __ psrlq(temp, Immediate(32));
5628 __ movd(out.AsRegisterPairHigh<Register>(), temp);
5629 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005630 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00005631 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005632 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005633 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
5634 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005635 break;
5636 }
5637
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005638 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00005639 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005640 break;
5641 }
5642
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005643 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005644 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005645 break;
5646 }
5647
Aart Bik66c158e2018-01-31 12:55:04 -08005648 case DataType::Type::kUint32:
5649 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005650 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005651 LOG(FATAL) << "Unreachable type " << load_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005652 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005653 }
Calin Juravle52c48962014-12-16 17:02:57 +00005654
Vladimir Marko61b92282017-10-11 13:23:17 +01005655 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005656 // Potential implicit null checks, in the case of reference or
5657 // long fields, are handled in the previous switch statement.
5658 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005659 codegen_->MaybeRecordImplicitNullCheck(instruction);
5660 }
5661
Calin Juravle52c48962014-12-16 17:02:57 +00005662 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005663 if (load_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005664 // Memory barriers, in the case of references, are also handled
5665 // in the previous switch statement.
5666 } else {
5667 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5668 }
Roland Levillain4d027112015-07-01 15:41:14 +01005669 }
Calin Juravle52c48962014-12-16 17:02:57 +00005670}
5671
5672void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5673 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5674
5675 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005676 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00005677 locations->SetInAt(0, Location::RequiresRegister());
5678 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005679 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005680 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00005681
5682 // The register allocator does not support multiple
5683 // inputs that die at entry with one in a specific register.
5684 if (is_byte_type) {
5685 // Ensure the value is in a byte register.
5686 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005687 } else if (DataType::IsFloatingPointType(field_type)) {
5688 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05005689 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5690 locations->SetInAt(1, Location::RequiresFpuRegister());
5691 } else {
5692 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
5693 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005694 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05005695 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00005696 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005697
Calin Juravle52c48962014-12-16 17:02:57 +00005698 // 64bits value can be atomically written to an address with movsd and an XMM register.
5699 // We need two XMM registers because there's no easier way to (bit) copy a register pair
5700 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
5701 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
5702 // isolated cases when we need this it isn't worth adding the extra complexity.
5703 locations->AddTemp(Location::RequiresFpuRegister());
5704 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005705 } else {
5706 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5707
5708 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5709 // Temporary registers for the write barrier.
5710 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
5711 // Ensure the card is in a byte register.
5712 locations->AddTemp(Location::RegisterLocation(ECX));
5713 }
Calin Juravle52c48962014-12-16 17:02:57 +00005714 }
5715}
5716
5717void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005718 const FieldInfo& field_info,
5719 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005720 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5721
5722 LocationSummary* locations = instruction->GetLocations();
5723 Register base = locations->InAt(0).AsRegister<Register>();
5724 Location value = locations->InAt(1);
5725 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005726 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00005727 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005728 bool needs_write_barrier =
5729 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005730
5731 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005732 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005733 }
5734
Mark Mendell81489372015-11-04 11:30:41 -05005735 bool maybe_record_implicit_null_check_done = false;
5736
Calin Juravle52c48962014-12-16 17:02:57 +00005737 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005738 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005739 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005740 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005741 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
5742 break;
5743 }
5744
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005745 case DataType::Type::kUint16:
5746 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05005747 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005748 __ movw(Address(base, offset),
5749 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05005750 } else {
5751 __ movw(Address(base, offset), value.AsRegister<Register>());
5752 }
Calin Juravle52c48962014-12-16 17:02:57 +00005753 break;
5754 }
5755
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005756 case DataType::Type::kInt32:
5757 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01005758 if (kPoisonHeapReferences && needs_write_barrier) {
5759 // Note that in the case where `value` is a null reference,
5760 // we do not enter this block, as the reference does not
5761 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005762 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01005763 Register temp = locations->GetTemp(0).AsRegister<Register>();
5764 __ movl(temp, value.AsRegister<Register>());
5765 __ PoisonHeapReference(temp);
5766 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05005767 } else if (value.IsConstant()) {
5768 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5769 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005770 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005771 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01005772 __ movl(Address(base, offset), value.AsRegister<Register>());
5773 }
Calin Juravle52c48962014-12-16 17:02:57 +00005774 break;
5775 }
5776
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005777 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005778 if (is_volatile) {
5779 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5780 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5781 __ movd(temp1, value.AsRegisterPairLow<Register>());
5782 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5783 __ punpckldq(temp1, temp2);
5784 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005785 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005786 } else if (value.IsConstant()) {
5787 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5788 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5789 codegen_->MaybeRecordImplicitNullCheck(instruction);
5790 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005791 } else {
5792 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005793 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005794 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5795 }
Mark Mendell81489372015-11-04 11:30:41 -05005796 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005797 break;
5798 }
5799
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005800 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05005801 if (value.IsConstant()) {
5802 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5803 __ movl(Address(base, offset), Immediate(v));
5804 } else {
5805 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5806 }
Calin Juravle52c48962014-12-16 17:02:57 +00005807 break;
5808 }
5809
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005810 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005811 if (value.IsConstant()) {
5812 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5813 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5814 codegen_->MaybeRecordImplicitNullCheck(instruction);
5815 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5816 maybe_record_implicit_null_check_done = true;
5817 } else {
5818 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5819 }
Calin Juravle52c48962014-12-16 17:02:57 +00005820 break;
5821 }
5822
Aart Bik66c158e2018-01-31 12:55:04 -08005823 case DataType::Type::kUint32:
5824 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005825 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005826 LOG(FATAL) << "Unreachable type " << field_type;
5827 UNREACHABLE();
5828 }
5829
Mark Mendell81489372015-11-04 11:30:41 -05005830 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005831 codegen_->MaybeRecordImplicitNullCheck(instruction);
5832 }
5833
Roland Levillain4d027112015-07-01 15:41:14 +01005834 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005835 Register temp = locations->GetTemp(0).AsRegister<Register>();
5836 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005837 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005838 }
5839
Calin Juravle52c48962014-12-16 17:02:57 +00005840 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005841 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005842 }
5843}
5844
5845void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5846 HandleFieldGet(instruction, instruction->GetFieldInfo());
5847}
5848
5849void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5850 HandleFieldGet(instruction, instruction->GetFieldInfo());
5851}
5852
5853void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5854 HandleFieldSet(instruction, instruction->GetFieldInfo());
5855}
5856
5857void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005858 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005859}
5860
5861void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5862 HandleFieldSet(instruction, instruction->GetFieldInfo());
5863}
5864
5865void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005866 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005867}
5868
5869void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5870 HandleFieldGet(instruction, instruction->GetFieldInfo());
5871}
5872
5873void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5874 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005875}
5876
Vladimir Marko552a1342017-10-31 10:56:47 +00005877void LocationsBuilderX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5878 codegen_->CreateStringBuilderAppendLocations(instruction, Location::RegisterLocation(EAX));
5879}
5880
5881void InstructionCodeGeneratorX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5882 __ movl(EAX, Immediate(instruction->GetFormat()->GetValue()));
5883 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
5884}
5885
Calin Juravlee460d1d2015-09-29 04:52:17 +01005886void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5887 HUnresolvedInstanceFieldGet* instruction) {
5888 FieldAccessCallingConventionX86 calling_convention;
5889 codegen_->CreateUnresolvedFieldLocationSummary(
5890 instruction, instruction->GetFieldType(), calling_convention);
5891}
5892
5893void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5894 HUnresolvedInstanceFieldGet* instruction) {
5895 FieldAccessCallingConventionX86 calling_convention;
5896 codegen_->GenerateUnresolvedFieldAccess(instruction,
5897 instruction->GetFieldType(),
5898 instruction->GetFieldIndex(),
5899 instruction->GetDexPc(),
5900 calling_convention);
5901}
5902
5903void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5904 HUnresolvedInstanceFieldSet* instruction) {
5905 FieldAccessCallingConventionX86 calling_convention;
5906 codegen_->CreateUnresolvedFieldLocationSummary(
5907 instruction, instruction->GetFieldType(), calling_convention);
5908}
5909
5910void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5911 HUnresolvedInstanceFieldSet* instruction) {
5912 FieldAccessCallingConventionX86 calling_convention;
5913 codegen_->GenerateUnresolvedFieldAccess(instruction,
5914 instruction->GetFieldType(),
5915 instruction->GetFieldIndex(),
5916 instruction->GetDexPc(),
5917 calling_convention);
5918}
5919
5920void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5921 HUnresolvedStaticFieldGet* instruction) {
5922 FieldAccessCallingConventionX86 calling_convention;
5923 codegen_->CreateUnresolvedFieldLocationSummary(
5924 instruction, instruction->GetFieldType(), calling_convention);
5925}
5926
5927void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5928 HUnresolvedStaticFieldGet* instruction) {
5929 FieldAccessCallingConventionX86 calling_convention;
5930 codegen_->GenerateUnresolvedFieldAccess(instruction,
5931 instruction->GetFieldType(),
5932 instruction->GetFieldIndex(),
5933 instruction->GetDexPc(),
5934 calling_convention);
5935}
5936
5937void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5938 HUnresolvedStaticFieldSet* instruction) {
5939 FieldAccessCallingConventionX86 calling_convention;
5940 codegen_->CreateUnresolvedFieldLocationSummary(
5941 instruction, instruction->GetFieldType(), calling_convention);
5942}
5943
5944void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5945 HUnresolvedStaticFieldSet* instruction) {
5946 FieldAccessCallingConventionX86 calling_convention;
5947 codegen_->GenerateUnresolvedFieldAccess(instruction,
5948 instruction->GetFieldType(),
5949 instruction->GetFieldIndex(),
5950 instruction->GetDexPc(),
5951 calling_convention);
5952}
5953
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005954void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005955 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5956 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5957 ? Location::RequiresRegister()
5958 : Location::Any();
5959 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005960}
5961
Calin Juravle2ae48182016-03-16 14:05:09 +00005962void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5963 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005964 return;
5965 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005966 LocationSummary* locations = instruction->GetLocations();
5967 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005968
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005969 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005970 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005971}
5972
Calin Juravle2ae48182016-03-16 14:05:09 +00005973void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005974 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005975 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005976
5977 LocationSummary* locations = instruction->GetLocations();
5978 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005979
5980 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005981 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005982 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005983 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005984 } else {
5985 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005986 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005987 __ jmp(slow_path->GetEntryLabel());
5988 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005989 }
5990 __ j(kEqual, slow_path->GetEntryLabel());
5991}
5992
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005993void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005994 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005995}
5996
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005997void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005998 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005999 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006000 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006001 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
6002 object_array_get_with_read_barrier
6003 ? LocationSummary::kCallOnSlowPath
6004 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01006005 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006006 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006007 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006008 locations->SetInAt(0, Location::RequiresRegister());
6009 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006010 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01006011 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6012 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006013 // The output overlaps in case of long: we don't want the low move
6014 // to overwrite the array's location. Likewise, in the case of an
6015 // object array get with read barriers enabled, we do not want the
6016 // move to overwrite the array's location, as we need it to emit
6017 // the read barrier.
6018 locations->SetOut(
6019 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006020 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
6021 ? Location::kOutputOverlap
6022 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01006023 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006024}
6025
6026void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
6027 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006028 Location obj_loc = locations->InAt(0);
6029 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006030 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006031 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006032 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006033
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006034 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00006035 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006036 case DataType::Type::kBool:
6037 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006038 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006039 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006040 break;
6041 }
6042
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006043 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006044 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006045 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006046 break;
6047 }
6048
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006049 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006050 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07006051 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6052 // Branch cases into compressed and uncompressed for each index's type.
6053 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
6054 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00006055 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006056 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006057 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6058 "Expecting 0=compressed, 1=uncompressed");
6059 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07006060 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
6061 __ jmp(&done);
6062 __ Bind(&not_compressed);
6063 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
6064 __ Bind(&done);
6065 } else {
6066 // Common case for charAt of array of char or when string compression's
6067 // feature is turned off.
6068 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
6069 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006070 break;
6071 }
6072
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006073 case DataType::Type::kInt16: {
6074 Register out = out_loc.AsRegister<Register>();
6075 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
6076 break;
6077 }
6078
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006079 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006080 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006081 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006082 break;
6083 }
6084
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006085 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006086 static_assert(
6087 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6088 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00006089 // /* HeapReference<Object> */ out =
6090 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6091 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006092 // Note that a potential implicit null check is handled in this
6093 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
6094 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08006095 instruction, out_loc, obj, data_offset, index, /* needs_null_check= */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006096 } else {
6097 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006098 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
6099 codegen_->MaybeRecordImplicitNullCheck(instruction);
6100 // If read barriers are enabled, emit read barriers other than
6101 // Baker's using a slow path (and also unpoison the loaded
6102 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00006103 if (index.IsConstant()) {
6104 uint32_t offset =
6105 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006106 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6107 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006108 codegen_->MaybeGenerateReadBarrierSlow(
6109 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6110 }
6111 }
6112 break;
6113 }
6114
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006115 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006116 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006117 __ movl(out_loc.AsRegisterPairLow<Register>(),
6118 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
6119 codegen_->MaybeRecordImplicitNullCheck(instruction);
6120 __ movl(out_loc.AsRegisterPairHigh<Register>(),
6121 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006122 break;
6123 }
6124
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006125 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006126 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006127 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006128 break;
6129 }
6130
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006131 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006132 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006133 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006134 break;
6135 }
6136
Aart Bik66c158e2018-01-31 12:55:04 -08006137 case DataType::Type::kUint32:
6138 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006139 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00006140 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07006141 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006142 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006143
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006144 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006145 // Potential implicit null checks, in the case of reference or
6146 // long arrays, are handled in the previous switch statement.
6147 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00006148 codegen_->MaybeRecordImplicitNullCheck(instruction);
6149 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006150}
6151
6152void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006153 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006154
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006155 bool needs_write_barrier =
6156 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006157 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006158
Vladimir Markoca6fff82017-10-03 14:49:14 +01006159 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01006160 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006161 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006162
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006163 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006164 // We need the inputs to be different than the output in case of long operation.
6165 // In case of a byte operation, the register allocator does not support multiple
6166 // inputs that die at entry with one in a specific register.
6167 locations->SetInAt(0, Location::RequiresRegister());
6168 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6169 if (is_byte_type) {
6170 // Ensure the value is in a byte register.
6171 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006172 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05006173 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006174 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006175 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
6176 }
6177 if (needs_write_barrier) {
6178 // Temporary registers for the write barrier.
6179 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6180 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00006181 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006182 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006183}
6184
6185void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
6186 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006187 Location array_loc = locations->InAt(0);
6188 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006189 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006190 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006191 DataType::Type value_type = instruction->GetComponentType();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006192 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006193 bool needs_write_barrier =
6194 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006195
6196 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006197 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006198 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006199 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006200 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006201 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006202 if (value.IsRegister()) {
6203 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006204 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01006205 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006206 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006207 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006208 break;
6209 }
6210
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006211 case DataType::Type::kUint16:
6212 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006213 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006214 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006215 if (value.IsRegister()) {
6216 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006217 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01006218 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006219 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006220 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006221 break;
6222 }
6223
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006224 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006225 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006226 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006227
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006228 if (!value.IsRegister()) {
6229 // Just setting null.
6230 DCHECK(instruction->InputAt(2)->IsNullConstant());
6231 DCHECK(value.IsConstant()) << value;
6232 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00006233 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006234 DCHECK(!needs_write_barrier);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006235 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006236 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006237 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006238
6239 DCHECK(needs_write_barrier);
6240 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01006241 Location temp_loc = locations->GetTemp(0);
6242 Register temp = temp_loc.AsRegister<Register>();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006243
6244 bool can_value_be_null = instruction->GetValueCanBeNull();
6245 NearLabel do_store;
6246 if (can_value_be_null) {
6247 __ testl(register_value, register_value);
6248 __ j(kEqual, &do_store);
6249 }
6250
6251 SlowPathCode* slow_path = nullptr;
6252 if (needs_type_check) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006253 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006254 codegen_->AddSlowPath(slow_path);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006255
6256 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6257 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6258 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006259
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006260 // Note that when Baker read barriers are enabled, the type
6261 // checks are performed without read barriers. This is fine,
6262 // even in the case where a class object is in the from-space
6263 // after the flip, as a comparison involving such a type would
6264 // not produce a false positive; it may of course produce a
6265 // false negative, in which case we would take the ArraySet
6266 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01006267
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006268 // /* HeapReference<Class> */ temp = array->klass_
6269 __ movl(temp, Address(array, class_offset));
6270 codegen_->MaybeRecordImplicitNullCheck(instruction);
6271 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01006272
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006273 // /* HeapReference<Class> */ temp = temp->component_type_
6274 __ movl(temp, Address(temp, component_offset));
6275 // If heap poisoning is enabled, no need to unpoison `temp`
6276 // nor the object reference in `register_value->klass`, as
6277 // we are comparing two poisoned references.
6278 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01006279
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006280 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006281 NearLabel do_put;
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006282 __ j(kEqual, &do_put);
6283 // If heap poisoning is enabled, the `temp` reference has
6284 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006285 __ MaybeUnpoisonHeapReference(temp);
6286
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006287 // If heap poisoning is enabled, no need to unpoison the
6288 // heap reference loaded below, as it is only used for a
6289 // comparison with null.
6290 __ cmpl(Address(temp, super_offset), Immediate(0));
6291 __ j(kNotEqual, slow_path->GetEntryLabel());
6292 __ Bind(&do_put);
6293 } else {
6294 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006295 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006296 }
6297
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006298 Register card = locations->GetTemp(1).AsRegister<Register>();
6299 codegen_->MarkGCCard(
6300 temp, card, array, value.AsRegister<Register>(), /* value_can_be_null= */ false);
6301
6302 if (can_value_be_null) {
6303 DCHECK(do_store.IsLinked());
6304 __ Bind(&do_store);
6305 }
6306
6307 Register source = register_value;
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006308 if (kPoisonHeapReferences) {
6309 __ movl(temp, register_value);
6310 __ PoisonHeapReference(temp);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006311 source = temp;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006312 }
6313
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006314 __ movl(address, source);
6315
6316 if (can_value_be_null || !needs_type_check) {
6317 codegen_->MaybeRecordImplicitNullCheck(instruction);
6318 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006319
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006320 if (slow_path != nullptr) {
6321 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006322 }
6323
6324 break;
6325 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006326
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006327 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006328 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006329 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006330 if (value.IsRegister()) {
6331 __ movl(address, value.AsRegister<Register>());
6332 } else {
6333 DCHECK(value.IsConstant()) << value;
6334 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
6335 __ movl(address, Immediate(v));
6336 }
6337 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006338 break;
6339 }
6340
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006341 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006342 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006343 if (value.IsRegisterPair()) {
6344 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6345 value.AsRegisterPairLow<Register>());
6346 codegen_->MaybeRecordImplicitNullCheck(instruction);
6347 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6348 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006349 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006350 DCHECK(value.IsConstant());
6351 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
6352 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6353 Immediate(Low32Bits(val)));
6354 codegen_->MaybeRecordImplicitNullCheck(instruction);
6355 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6356 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006357 }
6358 break;
6359 }
6360
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006361 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006362 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006363 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006364 if (value.IsFpuRegister()) {
6365 __ movss(address, value.AsFpuRegister<XmmRegister>());
6366 } else {
6367 DCHECK(value.IsConstant());
6368 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
6369 __ movl(address, Immediate(v));
6370 }
6371 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006372 break;
6373 }
6374
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006375 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006376 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006377 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006378 if (value.IsFpuRegister()) {
6379 __ movsd(address, value.AsFpuRegister<XmmRegister>());
6380 } else {
6381 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006382 Address address_hi =
6383 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05006384 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
6385 __ movl(address, Immediate(Low32Bits(v)));
6386 codegen_->MaybeRecordImplicitNullCheck(instruction);
6387 __ movl(address_hi, Immediate(High32Bits(v)));
6388 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006389 break;
6390 }
6391
Aart Bik66c158e2018-01-31 12:55:04 -08006392 case DataType::Type::kUint32:
6393 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006394 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006395 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07006396 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006397 }
6398}
6399
6400void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006401 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006402 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04006403 if (!instruction->IsEmittedAtUseSite()) {
6404 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6405 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006406}
6407
6408void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04006409 if (instruction->IsEmittedAtUseSite()) {
6410 return;
6411 }
6412
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006413 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006414 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006415 Register obj = locations->InAt(0).AsRegister<Register>();
6416 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006417 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00006418 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07006419 // Mask out most significant bit in case the array is String's array of char.
6420 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006421 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006422 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006423}
6424
6425void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006426 RegisterSet caller_saves = RegisterSet::Empty();
6427 InvokeRuntimeCallingConvention calling_convention;
6428 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6429 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6430 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05006431 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04006432 HInstruction* length = instruction->InputAt(1);
6433 if (!length->IsEmittedAtUseSite()) {
6434 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6435 }
jessicahandojo4877b792016-09-08 19:49:13 -07006436 // Need register to see array's length.
6437 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6438 locations->AddTemp(Location::RequiresRegister());
6439 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006440}
6441
6442void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07006443 const bool is_string_compressed_char_at =
6444 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006445 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05006446 Location index_loc = locations->InAt(0);
6447 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006448 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006449 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006450
Mark Mendell99dbd682015-04-22 16:18:52 -04006451 if (length_loc.IsConstant()) {
6452 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
6453 if (index_loc.IsConstant()) {
6454 // BCE will remove the bounds check if we are guarenteed to pass.
6455 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6456 if (index < 0 || index >= length) {
6457 codegen_->AddSlowPath(slow_path);
6458 __ jmp(slow_path->GetEntryLabel());
6459 } else {
6460 // Some optimization after BCE may have generated this, and we should not
6461 // generate a bounds check if it is a valid range.
6462 }
6463 return;
6464 }
6465
6466 // We have to reverse the jump condition because the length is the constant.
6467 Register index_reg = index_loc.AsRegister<Register>();
6468 __ cmpl(index_reg, Immediate(length));
6469 codegen_->AddSlowPath(slow_path);
6470 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006471 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04006472 HInstruction* array_length = instruction->InputAt(1);
6473 if (array_length->IsEmittedAtUseSite()) {
6474 // Address the length field in the array.
6475 DCHECK(array_length->IsArrayLength());
6476 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
6477 Location array_loc = array_length->GetLocations()->InAt(0);
6478 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07006479 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006480 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
6481 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07006482 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
6483 __ movl(length_reg, array_len);
6484 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006485 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006486 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04006487 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006488 // Checking bounds for general case:
6489 // Array of char or string's array with feature compression off.
6490 if (index_loc.IsConstant()) {
6491 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6492 __ cmpl(array_len, Immediate(value));
6493 } else {
6494 __ cmpl(array_len, index_loc.AsRegister<Register>());
6495 }
6496 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04006497 }
Mark Mendell99dbd682015-04-22 16:18:52 -04006498 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006499 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04006500 }
6501 codegen_->AddSlowPath(slow_path);
6502 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006503 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006504}
6505
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006506void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006507 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006508}
6509
6510void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006511 if (instruction->GetNext()->IsSuspendCheck() &&
6512 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6513 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6514 // The back edge will generate the suspend check.
6515 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6516 }
6517
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006518 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6519}
6520
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006521void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006522 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6523 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07006524 // In suspend check slow path, usually there are no caller-save registers at all.
6525 // If SIMD instructions are present, however, we force spilling all live SIMD
6526 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07006527 locations->SetCustomSlowPathCallerSaves(
6528 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006529}
6530
6531void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006532 HBasicBlock* block = instruction->GetBlock();
6533 if (block->GetLoopInformation() != nullptr) {
6534 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6535 // The back edge will generate the suspend check.
6536 return;
6537 }
6538 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6539 // The goto will generate the suspend check.
6540 return;
6541 }
6542 GenerateSuspendCheck(instruction, nullptr);
6543}
6544
6545void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
6546 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006547 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006548 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
6549 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006550 slow_path =
6551 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006552 instruction->SetSlowPath(slow_path);
6553 codegen_->AddSlowPath(slow_path);
6554 if (successor != nullptr) {
6555 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006556 }
6557 } else {
6558 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6559 }
6560
Andreas Gampe542451c2016-07-26 09:02:02 -07006561 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006562 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006563 if (successor == nullptr) {
6564 __ j(kNotEqual, slow_path->GetEntryLabel());
6565 __ Bind(slow_path->GetReturnLabel());
6566 } else {
6567 __ j(kEqual, codegen_->GetLabelOf(successor));
6568 __ jmp(slow_path->GetEntryLabel());
6569 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006570}
6571
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006572X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
6573 return codegen_->GetAssembler();
6574}
6575
Aart Bikcfe50bb2017-12-12 14:54:12 -08006576void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006577 ScratchRegisterScope ensure_scratch(
6578 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6579 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6580 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05006581
Aart Bikcfe50bb2017-12-12 14:54:12 -08006582 // Now that temp register is available (possibly spilled), move blocks of memory.
6583 for (int i = 0; i < number_of_words; i++) {
6584 __ movl(temp_reg, Address(ESP, src + stack_offset));
6585 __ movl(Address(ESP, dst + stack_offset), temp_reg);
6586 stack_offset += kX86WordSize;
6587 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006588}
6589
6590void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006591 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006592 Location source = move->GetSource();
6593 Location destination = move->GetDestination();
6594
6595 if (source.IsRegister()) {
6596 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006597 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006598 } else if (destination.IsFpuRegister()) {
6599 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006600 } else {
6601 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006602 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006603 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006604 } else if (source.IsRegisterPair()) {
Vladimir Marko86c87522020-05-11 16:55:55 +01006605 if (destination.IsRegisterPair()) {
6606 __ movl(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
6607 DCHECK_NE(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
6608 __ movl(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
6609 } else if (destination.IsFpuRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006610 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Vladimir Markodec78172020-06-19 15:31:23 +01006611 // Push the 2 source registers to the stack.
Vladimir Marko86c87522020-05-11 16:55:55 +01006612 __ pushl(source.AsRegisterPairHigh<Register>());
6613 __ cfi().AdjustCFAOffset(elem_size);
6614 __ pushl(source.AsRegisterPairLow<Register>());
6615 __ cfi().AdjustCFAOffset(elem_size);
6616 // Load the destination register.
David Brazdil74eb1b22015-12-14 11:44:01 +00006617 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
6618 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01006619 codegen_->DecreaseFrame(2 * elem_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01006620 } else {
6621 DCHECK(destination.IsDoubleStackSlot());
6622 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
6623 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
6624 source.AsRegisterPairHigh<Register>());
6625 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006626 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006627 if (destination.IsRegister()) {
6628 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
6629 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006630 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006631 } else if (destination.IsRegisterPair()) {
Vladimir Marko86c87522020-05-11 16:55:55 +01006632 size_t elem_size = DataType::Size(DataType::Type::kInt32);
6633 // Create stack space for 2 elements.
Vladimir Markodec78172020-06-19 15:31:23 +01006634 codegen_->IncreaseFrame(2 * elem_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01006635 // Store the source register.
6636 __ movsd(Address(ESP, 0), source.AsFpuRegister<XmmRegister>());
6637 // And pop the values into destination registers.
6638 __ popl(destination.AsRegisterPairLow<Register>());
6639 __ cfi().AdjustCFAOffset(-elem_size);
6640 __ popl(destination.AsRegisterPairHigh<Register>());
6641 __ cfi().AdjustCFAOffset(-elem_size);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006642 } else if (destination.IsStackSlot()) {
6643 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006644 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006645 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006646 } else {
6647 DCHECK(destination.IsSIMDStackSlot());
6648 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05006649 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006650 } else if (source.IsStackSlot()) {
6651 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006652 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006653 } else if (destination.IsFpuRegister()) {
6654 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006655 } else {
6656 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08006657 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006658 }
6659 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006660 if (destination.IsRegisterPair()) {
6661 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
6662 __ movl(destination.AsRegisterPairHigh<Register>(),
6663 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
6664 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006665 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6666 } else {
6667 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006668 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006669 }
Aart Bik5576f372017-03-23 16:17:37 -07006670 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006671 if (destination.IsFpuRegister()) {
6672 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6673 } else {
6674 DCHECK(destination.IsSIMDStackSlot());
6675 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6676 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006677 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006678 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006679 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006680 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006681 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006682 if (value == 0) {
6683 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
6684 } else {
6685 __ movl(destination.AsRegister<Register>(), Immediate(value));
6686 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006687 } else {
6688 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05006689 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006690 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006691 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006692 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006693 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006694 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006695 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006696 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6697 if (value == 0) {
6698 // Easy handling of 0.0.
6699 __ xorps(dest, dest);
6700 } else {
6701 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006702 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6703 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
6704 __ movl(temp, Immediate(value));
6705 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006706 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006707 } else {
6708 DCHECK(destination.IsStackSlot()) << destination;
6709 __ movl(Address(ESP, destination.GetStackIndex()), imm);
6710 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006711 } else if (constant->IsLongConstant()) {
6712 int64_t value = constant->AsLongConstant()->GetValue();
6713 int32_t low_value = Low32Bits(value);
6714 int32_t high_value = High32Bits(value);
6715 Immediate low(low_value);
6716 Immediate high(high_value);
6717 if (destination.IsDoubleStackSlot()) {
6718 __ movl(Address(ESP, destination.GetStackIndex()), low);
6719 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6720 } else {
6721 __ movl(destination.AsRegisterPairLow<Register>(), low);
6722 __ movl(destination.AsRegisterPairHigh<Register>(), high);
6723 }
6724 } else {
6725 DCHECK(constant->IsDoubleConstant());
6726 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006727 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006728 int32_t low_value = Low32Bits(value);
6729 int32_t high_value = High32Bits(value);
6730 Immediate low(low_value);
6731 Immediate high(high_value);
6732 if (destination.IsFpuRegister()) {
6733 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6734 if (value == 0) {
6735 // Easy handling of 0.0.
6736 __ xorpd(dest, dest);
6737 } else {
6738 __ pushl(high);
Vladimir Marko86c87522020-05-11 16:55:55 +01006739 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006740 __ pushl(low);
Vladimir Marko86c87522020-05-11 16:55:55 +01006741 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006742 __ movsd(dest, Address(ESP, 0));
Vladimir Markodec78172020-06-19 15:31:23 +01006743 codegen_->DecreaseFrame(8);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006744 }
6745 } else {
6746 DCHECK(destination.IsDoubleStackSlot()) << destination;
6747 __ movl(Address(ESP, destination.GetStackIndex()), low);
6748 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6749 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006750 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006751 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006752 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006753 }
6754}
6755
Mark Mendella5c19ce2015-04-01 12:51:05 -04006756void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006757 Register suggested_scratch = reg == EAX ? EBX : EAX;
6758 ScratchRegisterScope ensure_scratch(
6759 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
6760
6761 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6762 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
6763 __ movl(Address(ESP, mem + stack_offset), reg);
6764 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006765}
6766
Mark Mendell7c8d0092015-01-26 11:21:33 -05006767void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006768 ScratchRegisterScope ensure_scratch(
6769 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6770
6771 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6772 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6773 __ movl(temp_reg, Address(ESP, mem + stack_offset));
6774 __ movss(Address(ESP, mem + stack_offset), reg);
6775 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006776}
6777
Aart Bikcfe50bb2017-12-12 14:54:12 -08006778void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
6779 size_t extra_slot = 4 * kX86WordSize;
Vladimir Markodec78172020-06-19 15:31:23 +01006780 codegen_->IncreaseFrame(extra_slot);
Aart Bikcfe50bb2017-12-12 14:54:12 -08006781 __ movups(Address(ESP, 0), XmmRegister(reg));
6782 ExchangeMemory(0, mem + extra_slot, 4);
6783 __ movups(XmmRegister(reg), Address(ESP, 0));
Vladimir Markodec78172020-06-19 15:31:23 +01006784 codegen_->DecreaseFrame(extra_slot);
Aart Bikcfe50bb2017-12-12 14:54:12 -08006785}
6786
6787void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006788 ScratchRegisterScope ensure_scratch1(
6789 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006790
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006791 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
6792 ScratchRegisterScope ensure_scratch2(
6793 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006794
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006795 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
6796 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006797
6798 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
6799 for (int i = 0; i < number_of_words; i++) {
6800 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
6801 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
6802 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
6803 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
6804 stack_offset += kX86WordSize;
6805 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006806}
6807
6808void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006809 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006810 Location source = move->GetSource();
6811 Location destination = move->GetDestination();
6812
6813 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04006814 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
6815 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
6816 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
6817 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
6818 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006819 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006820 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006821 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006822 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006823 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006824 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006825 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
6826 // Use XOR Swap algorithm to avoid a temporary.
6827 DCHECK_NE(source.reg(), destination.reg());
6828 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6829 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
6830 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6831 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
6832 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6833 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
6834 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006835 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
6836 // Take advantage of the 16 bytes in the XMM register.
6837 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
6838 Address stack(ESP, destination.GetStackIndex());
6839 // Load the double into the high doubleword.
6840 __ movhpd(reg, stack);
6841
6842 // Store the low double into the destination.
6843 __ movsd(stack, reg);
6844
6845 // Move the high double to the low double.
6846 __ psrldq(reg, Immediate(8));
6847 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6848 // Take advantage of the 16 bytes in the XMM register.
6849 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6850 Address stack(ESP, source.GetStackIndex());
6851 // Load the double into the high doubleword.
6852 __ movhpd(reg, stack);
6853
6854 // Store the low double into the destination.
6855 __ movsd(stack, reg);
6856
6857 // Move the high double to the low double.
6858 __ psrldq(reg, Immediate(8));
6859 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006860 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
6861 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
6862 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6863 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
6864 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6865 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
6866 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006867 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006868 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006869 }
6870}
6871
6872void ParallelMoveResolverX86::SpillScratch(int reg) {
6873 __ pushl(static_cast<Register>(reg));
6874}
6875
6876void ParallelMoveResolverX86::RestoreScratch(int reg) {
6877 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006878}
6879
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006880HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6881 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006882 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006883 case HLoadClass::LoadKind::kInvalid:
6884 LOG(FATAL) << "UNREACHABLE";
6885 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006886 case HLoadClass::LoadKind::kReferrersClass:
6887 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006888 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006889 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006890 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01006891 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006892 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006893 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006894 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01006895 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006896 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006897 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006898 break;
6899 }
6900 return desired_class_load_kind;
6901}
6902
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006903void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006904 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006905 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006906 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006907 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006908 cls,
6909 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006910 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006911 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006912 return;
6913 }
Vladimir Marko41559982017-01-06 14:04:23 +00006914 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006915
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006916 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6917 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006918 ? LocationSummary::kCallOnSlowPath
6919 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006920 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006921 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006922 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006923 }
6924
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006925 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006926 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006927 load_kind == HLoadClass::LoadKind::kBootImageRelRo ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006928 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006929 locations->SetInAt(0, Location::RequiresRegister());
6930 }
6931 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006932 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6933 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6934 // Rely on the type resolution and/or initialization to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006935 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006936 } else {
6937 // For non-Baker read barrier we have a temp-clobbering call.
6938 }
6939 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006940}
6941
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006942Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006943 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006944 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006945 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006946 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006947 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006948 PatchInfo<Label>* info = &jit_class_patches_.back();
6949 return &info->label;
6950}
6951
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006952// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6953// move.
6954void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006955 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006956 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006957 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006958 return;
6959 }
Vladimir Marko41559982017-01-06 14:04:23 +00006960 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006961
Vladimir Marko41559982017-01-06 14:04:23 +00006962 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006963 Location out_loc = locations->Out();
6964 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006965
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006966 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006967 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6968 ? kWithoutReadBarrier
6969 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006970 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006971 case HLoadClass::LoadKind::kReferrersClass: {
6972 DCHECK(!cls->CanCallRuntime());
6973 DCHECK(!cls->MustGenerateClinitCheck());
6974 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6975 Register current_method = locations->InAt(0).AsRegister<Register>();
6976 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006977 cls,
6978 out_loc,
6979 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Andreas Gampe3db70682018-12-26 15:12:03 -08006980 /* fixup_label= */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006981 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006982 break;
6983 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006984 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01006985 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
6986 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006987 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006988 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00006989 __ leal(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006990 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006991 break;
6992 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006993 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006994 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6995 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00006996 __ movl(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006997 codegen_->RecordBootImageRelRoPatch(cls->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6998 codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006999 break;
7000 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007001 case HLoadClass::LoadKind::kBssEntry: {
7002 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007003 Address address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007004 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
7005 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007006 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007007 generate_null_check = true;
7008 break;
7009 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007010 case HLoadClass::LoadKind::kJitBootImageAddress: {
7011 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7012 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
7013 DCHECK_NE(address, 0u);
7014 __ movl(out, Immediate(address));
7015 break;
7016 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007017 case HLoadClass::LoadKind::kJitTableAddress: {
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007018 Address address = Address::Absolute(CodeGeneratorX86::kPlaceholder32BitOffset);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007019 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007020 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007021 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00007022 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007023 break;
7024 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007025 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007026 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007027 LOG(FATAL) << "UNREACHABLE";
7028 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007029 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007030
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007031 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7032 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007033 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007034 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007035
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007036 if (generate_null_check) {
7037 __ testl(out, out);
7038 __ j(kEqual, slow_path->GetEntryLabel());
7039 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007040
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007041 if (cls->MustGenerateClinitCheck()) {
7042 GenerateClassInitializationCheck(slow_path, out);
7043 } else {
7044 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007045 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007046 }
7047}
7048
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007049void LocationsBuilderX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7050 InvokeRuntimeCallingConvention calling_convention;
7051 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7052 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
7053}
7054
7055void InstructionCodeGeneratorX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7056 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
7057}
7058
Orion Hodson18259d72018-04-12 11:18:23 +01007059void LocationsBuilderX86::VisitLoadMethodType(HLoadMethodType* load) {
7060 InvokeRuntimeCallingConvention calling_convention;
7061 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7062 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
7063}
7064
7065void InstructionCodeGeneratorX86::VisitLoadMethodType(HLoadMethodType* load) {
7066 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
7067}
7068
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007069void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
7070 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007071 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007072 locations->SetInAt(0, Location::RequiresRegister());
7073 if (check->HasUses()) {
7074 locations->SetOut(Location::SameAsFirstInput());
7075 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007076 // Rely on the type initialization to save everything we need.
7077 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007078}
7079
7080void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007081 // We assume the class to not be null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007082 SlowPathCode* slow_path =
7083 new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(check->GetLoadClass(), check);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007084 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00007085 GenerateClassInitializationCheck(slow_path,
7086 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007087}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007088
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007089void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07007090 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00007091 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
7092 const size_t status_byte_offset =
7093 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
Vladimir Markobf121912019-06-04 13:49:05 +01007094 constexpr uint32_t shifted_visibly_initialized_value =
7095 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << (status_lsb_position % kBitsPerByte);
Vladimir Markodc682aa2018-01-04 18:42:57 +00007096
Vladimir Markobf121912019-06-04 13:49:05 +01007097 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_visibly_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00007098 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007099 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007100}
7101
Vladimir Marko175e7862018-03-27 09:03:13 +00007102void InstructionCodeGeneratorX86::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
7103 Register temp) {
7104 uint32_t path_to_root = check->GetBitstringPathToRoot();
7105 uint32_t mask = check->GetBitstringMask();
7106 DCHECK(IsPowerOfTwo(mask + 1));
7107 size_t mask_bits = WhichPowerOf2(mask + 1);
7108
7109 if (mask_bits == 16u) {
7110 // Compare the bitstring in memory.
7111 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
7112 } else {
7113 // /* uint32_t */ temp = temp->status_
7114 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
7115 // Compare the bitstring bits using SUB.
7116 __ subl(temp, Immediate(path_to_root));
7117 // Shift out bits that do not contribute to the comparison.
7118 __ shll(temp, Immediate(32u - mask_bits));
7119 }
7120}
7121
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007122HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
7123 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007124 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007125 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007126 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007127 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01007128 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007129 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007130 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007131 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01007132 DCHECK(GetCompilerOptions().IsJitCompiler());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007133 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007134 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007135 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007136 }
7137 return desired_string_load_kind;
7138}
7139
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007140void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007141 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007142 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007143 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007144 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007145 load_kind == HLoadString::LoadKind::kBootImageRelRo ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00007146 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007147 locations->SetInAt(0, Location::RequiresRegister());
7148 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007149 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007150 locations->SetOut(Location::RegisterLocation(EAX));
7151 } else {
7152 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007153 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7154 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007155 // Rely on the pResolveString to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007156 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007157 } else {
7158 // For non-Baker read barrier we have a temp-clobbering call.
7159 }
7160 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007161 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007162}
7163
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007164Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01007165 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007166 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007167 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007168 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007169 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007170 PatchInfo<Label>* info = &jit_string_patches_.back();
7171 return &info->label;
7172}
7173
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007174// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7175// move.
7176void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01007177 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007178 Location out_loc = locations->Out();
7179 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007180
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007181 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007182 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007183 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7184 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007185 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007186 __ leal(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007187 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007188 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007189 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007190 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007191 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7192 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007193 __ movl(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007194 codegen_->RecordBootImageRelRoPatch(load->InputAt(0)->AsX86ComputeBaseMethodAddress(),
7195 codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007196 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007197 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007198 case HLoadString::LoadKind::kBssEntry: {
7199 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007200 Address address = Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007201 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007202 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007203 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007204 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007205 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007206 codegen_->AddSlowPath(slow_path);
7207 __ testl(out, out);
7208 __ j(kEqual, slow_path->GetEntryLabel());
7209 __ Bind(slow_path->GetExitLabel());
7210 return;
7211 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007212 case HLoadString::LoadKind::kJitBootImageAddress: {
7213 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
7214 DCHECK_NE(address, 0u);
7215 __ movl(out, Immediate(address));
7216 return;
7217 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007218 case HLoadString::LoadKind::kJitTableAddress: {
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007219 Address address = Address::Absolute(CodeGeneratorX86::kPlaceholder32BitOffset);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007220 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007221 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007222 // /* GcRoot<mirror::String> */ out = *address
7223 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
7224 return;
7225 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007226 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007227 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007228 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007229
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007230 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007231 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007232 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007233 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007234 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7235 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007236}
7237
David Brazdilcb1c0552015-08-04 16:22:25 +01007238static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007239 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01007240}
7241
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007242void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
7243 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007244 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007245 locations->SetOut(Location::RequiresRegister());
7246}
7247
7248void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01007249 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
7250}
7251
7252void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007253 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01007254}
7255
7256void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7257 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007258}
7259
7260void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007261 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7262 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007263 InvokeRuntimeCallingConvention calling_convention;
7264 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7265}
7266
7267void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007268 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007269 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007270}
7271
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007272// Temp is used for read barrier.
7273static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7274 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00007275 !kUseBakerReadBarrier &&
7276 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00007277 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007278 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7279 return 1;
7280 }
7281 return 0;
7282}
7283
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007284// Interface case has 2 temps, one for holding the number of interfaces, one for the current
7285// interface pointer, the current interface is compared in memory.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007286// The other checks have one temp for loading the object's class.
7287static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007288 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007289 return 2;
7290 }
7291 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007292}
7293
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007294void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007295 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007296 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01007297 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007298 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007299 case TypeCheckKind::kExactCheck:
7300 case TypeCheckKind::kAbstractClassCheck:
7301 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007302 case TypeCheckKind::kArrayObjectCheck: {
7303 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7304 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7305 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007306 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007307 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007308 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007309 case TypeCheckKind::kUnresolvedCheck:
7310 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007311 call_kind = LocationSummary::kCallOnSlowPath;
7312 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007313 case TypeCheckKind::kBitstringCheck:
7314 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007315 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007316
Vladimir Markoca6fff82017-10-03 14:49:14 +01007317 LocationSummary* locations =
7318 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01007319 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007320 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007321 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007322 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007323 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7324 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7325 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7326 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7327 } else {
7328 locations->SetInAt(1, Location::Any());
7329 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007330 // Note that TypeCheckSlowPathX86 uses this "out" register too.
7331 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007332 // When read barriers are enabled, we need a temporary register for some cases.
7333 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007334}
7335
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007336void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007337 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007338 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007339 Location obj_loc = locations->InAt(0);
7340 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007341 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007342 Location out_loc = locations->Out();
7343 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007344 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7345 DCHECK_LE(num_temps, 1u);
7346 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007347 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007348 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7349 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7350 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07007351 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007352 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007353
7354 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007355 // Avoid null check if we know obj is not null.
7356 if (instruction->MustDoNullCheck()) {
7357 __ testl(obj, obj);
7358 __ j(kEqual, &zero);
7359 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007360
Roland Levillain7c1559a2015-12-15 10:55:36 +00007361 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007362 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007363 ReadBarrierOption read_barrier_option =
7364 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007365 // /* HeapReference<Class> */ out = obj->klass_
7366 GenerateReferenceLoadTwoRegisters(instruction,
7367 out_loc,
7368 obj_loc,
7369 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007370 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007371 if (cls.IsRegister()) {
7372 __ cmpl(out, cls.AsRegister<Register>());
7373 } else {
7374 DCHECK(cls.IsStackSlot()) << cls;
7375 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7376 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007377
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007378 // Classes must be equal for the instanceof to succeed.
7379 __ j(kNotEqual, &zero);
7380 __ movl(out, Immediate(1));
7381 __ jmp(&done);
7382 break;
7383 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007384
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007385 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007386 ReadBarrierOption read_barrier_option =
7387 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007388 // /* HeapReference<Class> */ out = obj->klass_
7389 GenerateReferenceLoadTwoRegisters(instruction,
7390 out_loc,
7391 obj_loc,
7392 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007393 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007394 // If the class is abstract, we eagerly fetch the super class of the
7395 // object to avoid doing a comparison we know will fail.
7396 NearLabel loop;
7397 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007398 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007399 GenerateReferenceLoadOneRegister(instruction,
7400 out_loc,
7401 super_offset,
7402 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007403 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007404 __ testl(out, out);
7405 // If `out` is null, we use it for the result, and jump to `done`.
7406 __ j(kEqual, &done);
7407 if (cls.IsRegister()) {
7408 __ cmpl(out, cls.AsRegister<Register>());
7409 } else {
7410 DCHECK(cls.IsStackSlot()) << cls;
7411 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7412 }
7413 __ j(kNotEqual, &loop);
7414 __ movl(out, Immediate(1));
7415 if (zero.IsLinked()) {
7416 __ jmp(&done);
7417 }
7418 break;
7419 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007420
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007421 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007422 ReadBarrierOption read_barrier_option =
7423 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007424 // /* HeapReference<Class> */ out = obj->klass_
7425 GenerateReferenceLoadTwoRegisters(instruction,
7426 out_loc,
7427 obj_loc,
7428 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007429 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007430 // Walk over the class hierarchy to find a match.
7431 NearLabel loop, success;
7432 __ Bind(&loop);
7433 if (cls.IsRegister()) {
7434 __ cmpl(out, cls.AsRegister<Register>());
7435 } else {
7436 DCHECK(cls.IsStackSlot()) << cls;
7437 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7438 }
7439 __ j(kEqual, &success);
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 __ j(kNotEqual, &loop);
7448 // If `out` is null, we use it for the result, and jump to `done`.
7449 __ jmp(&done);
7450 __ Bind(&success);
7451 __ movl(out, Immediate(1));
7452 if (zero.IsLinked()) {
7453 __ jmp(&done);
7454 }
7455 break;
7456 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007457
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007458 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007459 ReadBarrierOption read_barrier_option =
7460 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007461 // /* HeapReference<Class> */ out = obj->klass_
7462 GenerateReferenceLoadTwoRegisters(instruction,
7463 out_loc,
7464 obj_loc,
7465 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007466 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007467 // Do an exact check.
7468 NearLabel exact_check;
7469 if (cls.IsRegister()) {
7470 __ cmpl(out, cls.AsRegister<Register>());
7471 } else {
7472 DCHECK(cls.IsStackSlot()) << cls;
7473 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7474 }
7475 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007476 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007477 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007478 GenerateReferenceLoadOneRegister(instruction,
7479 out_loc,
7480 component_offset,
7481 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007482 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007483 __ testl(out, out);
7484 // If `out` is null, we use it for the result, and jump to `done`.
7485 __ j(kEqual, &done);
7486 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
7487 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007488 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007489 __ movl(out, Immediate(1));
7490 __ jmp(&done);
7491 break;
7492 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007493
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007494 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007495 // No read barrier since the slow path will retry upon failure.
7496 // /* HeapReference<Class> */ out = obj->klass_
7497 GenerateReferenceLoadTwoRegisters(instruction,
7498 out_loc,
7499 obj_loc,
7500 class_offset,
7501 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007502 if (cls.IsRegister()) {
7503 __ cmpl(out, cls.AsRegister<Register>());
7504 } else {
7505 DCHECK(cls.IsStackSlot()) << cls;
7506 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7507 }
7508 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007509 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007510 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007511 codegen_->AddSlowPath(slow_path);
7512 __ j(kNotEqual, slow_path->GetEntryLabel());
7513 __ movl(out, Immediate(1));
7514 if (zero.IsLinked()) {
7515 __ jmp(&done);
7516 }
7517 break;
7518 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007519
Calin Juravle98893e12015-10-02 21:05:03 +01007520 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007521 case TypeCheckKind::kInterfaceCheck: {
7522 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007523 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00007524 // cases.
7525 //
7526 // We cannot directly call the InstanceofNonTrivial runtime
7527 // entry point without resorting to a type checking slow path
7528 // here (i.e. by calling InvokeRuntime directly), as it would
7529 // require to assign fixed registers for the inputs of this
7530 // HInstanceOf instruction (following the runtime calling
7531 // convention), which might be cluttered by the potential first
7532 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007533 //
7534 // TODO: Introduce a new runtime entry point taking the object
7535 // to test (instead of its class) as argument, and let it deal
7536 // with the read barrier issues. This will let us refactor this
7537 // case of the `switch` code as it was previously (with a direct
7538 // call to the runtime not using a type checking slow path).
7539 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007540 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007541 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007542 instruction, /* is_fatal= */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007543 codegen_->AddSlowPath(slow_path);
7544 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007545 if (zero.IsLinked()) {
7546 __ jmp(&done);
7547 }
7548 break;
7549 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007550
7551 case TypeCheckKind::kBitstringCheck: {
7552 // /* HeapReference<Class> */ temp = obj->klass_
7553 GenerateReferenceLoadTwoRegisters(instruction,
7554 out_loc,
7555 obj_loc,
7556 class_offset,
7557 kWithoutReadBarrier);
7558
7559 GenerateBitstringTypeCheckCompare(instruction, out);
7560 __ j(kNotEqual, &zero);
7561 __ movl(out, Immediate(1));
7562 __ jmp(&done);
7563 break;
7564 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007565 }
7566
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007567 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007568 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007569 __ xorl(out, out);
7570 }
7571
7572 if (done.IsLinked()) {
7573 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007574 }
7575
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007576 if (slow_path != nullptr) {
7577 __ Bind(slow_path->GetExitLabel());
7578 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007579}
7580
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007581void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007582 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007583 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007584 LocationSummary* locations =
7585 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007586 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007587 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7588 // Require a register for the interface check since there is a loop that compares the class to
7589 // a memory address.
7590 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007591 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7592 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7593 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7594 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007595 } else {
7596 locations->SetInAt(1, Location::Any());
7597 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007598 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007599 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
7600}
7601
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007602void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007603 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007604 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007605 Location obj_loc = locations->InAt(0);
7606 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007607 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007608 Location temp_loc = locations->GetTemp(0);
7609 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007610 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7611 DCHECK_GE(num_temps, 1u);
7612 DCHECK_LE(num_temps, 2u);
7613 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7614 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7615 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7616 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7617 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7618 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7619 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7620 const uint32_t object_array_data_offset =
7621 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007622
Vladimir Marko87584542017-12-12 17:47:52 +00007623 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007624 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007625 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7626 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007627 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007628
Roland Levillain0d5a2812015-11-13 10:07:31 +00007629 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007630 // Avoid null check if we know obj is not null.
7631 if (instruction->MustDoNullCheck()) {
7632 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007633 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007634 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007635
Roland Levillain0d5a2812015-11-13 10:07:31 +00007636 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007637 case TypeCheckKind::kExactCheck:
7638 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007639 // /* HeapReference<Class> */ temp = obj->klass_
7640 GenerateReferenceLoadTwoRegisters(instruction,
7641 temp_loc,
7642 obj_loc,
7643 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007644 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007645
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007646 if (cls.IsRegister()) {
7647 __ cmpl(temp, cls.AsRegister<Register>());
7648 } else {
7649 DCHECK(cls.IsStackSlot()) << cls;
7650 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7651 }
7652 // Jump to slow path for throwing the exception or doing a
7653 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007654 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007655 break;
7656 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007657
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007658 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007659 // /* HeapReference<Class> */ temp = obj->klass_
7660 GenerateReferenceLoadTwoRegisters(instruction,
7661 temp_loc,
7662 obj_loc,
7663 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007664 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007665
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007666 // If the class is abstract, we eagerly fetch the super class of the
7667 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007668 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007669 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007670 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007671 GenerateReferenceLoadOneRegister(instruction,
7672 temp_loc,
7673 super_offset,
7674 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007675 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007676
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007677 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7678 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007679 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007680 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007681
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007682 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007683 if (cls.IsRegister()) {
7684 __ cmpl(temp, cls.AsRegister<Register>());
7685 } else {
7686 DCHECK(cls.IsStackSlot()) << cls;
7687 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7688 }
7689 __ j(kNotEqual, &loop);
7690 break;
7691 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007692
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007693 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007694 // /* HeapReference<Class> */ temp = obj->klass_
7695 GenerateReferenceLoadTwoRegisters(instruction,
7696 temp_loc,
7697 obj_loc,
7698 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007699 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007700
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007701 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007702 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007703 __ Bind(&loop);
7704 if (cls.IsRegister()) {
7705 __ cmpl(temp, cls.AsRegister<Register>());
7706 } else {
7707 DCHECK(cls.IsStackSlot()) << cls;
7708 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7709 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007710 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007711
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
7719 // If the class reference currently in `temp` is not null, jump
7720 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007721 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007722 __ j(kNotZero, &loop);
7723 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007724 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007725 break;
7726 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007727
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007728 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007729 // /* HeapReference<Class> */ temp = obj->klass_
7730 GenerateReferenceLoadTwoRegisters(instruction,
7731 temp_loc,
7732 obj_loc,
7733 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007734 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007735
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007736 // Do an exact check.
7737 if (cls.IsRegister()) {
7738 __ cmpl(temp, cls.AsRegister<Register>());
7739 } else {
7740 DCHECK(cls.IsStackSlot()) << cls;
7741 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7742 }
7743 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007744
7745 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007746 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007747 GenerateReferenceLoadOneRegister(instruction,
7748 temp_loc,
7749 component_offset,
7750 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007751 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007752
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007753 // If the component type is null (i.e. the object not an array), jump to the slow path to
7754 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007755 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007756 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007757
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007758 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007759 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007760 break;
7761 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007762
Calin Juravle98893e12015-10-02 21:05:03 +01007763 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007764 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007765 // We cannot directly call the CheckCast runtime entry point
7766 // without resorting to a type checking slow path here (i.e. by
7767 // calling InvokeRuntime directly), as it would require to
7768 // assign fixed registers for the inputs of this HInstanceOf
7769 // instruction (following the runtime calling convention), which
7770 // might be cluttered by the potential first read barrier
7771 // emission at the beginning of this method.
7772 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007773 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007774
7775 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007776 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
7777 // We can not get false positives by doing this.
7778 // /* HeapReference<Class> */ temp = obj->klass_
7779 GenerateReferenceLoadTwoRegisters(instruction,
7780 temp_loc,
7781 obj_loc,
7782 class_offset,
7783 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007784
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007785 // /* HeapReference<Class> */ temp = temp->iftable_
7786 GenerateReferenceLoadTwoRegisters(instruction,
7787 temp_loc,
7788 temp_loc,
7789 iftable_offset,
7790 kWithoutReadBarrier);
7791 // Iftable is never null.
7792 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
7793 // Maybe poison the `cls` for direct comparison with memory.
7794 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
7795 // Loop through the iftable and check if any class matches.
7796 NearLabel start_loop;
7797 __ Bind(&start_loop);
7798 // Need to subtract first to handle the empty array case.
7799 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
7800 __ j(kNegative, type_check_slow_path->GetEntryLabel());
7801 // Go to next interface if the classes do not match.
7802 __ cmpl(cls.AsRegister<Register>(),
7803 CodeGeneratorX86::ArrayAddress(temp,
7804 maybe_temp2_loc,
7805 TIMES_4,
7806 object_array_data_offset));
7807 __ j(kNotEqual, &start_loop);
7808 // If `cls` was poisoned above, unpoison it.
7809 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007810 break;
7811 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007812
7813 case TypeCheckKind::kBitstringCheck: {
7814 // /* HeapReference<Class> */ temp = obj->klass_
7815 GenerateReferenceLoadTwoRegisters(instruction,
7816 temp_loc,
7817 obj_loc,
7818 class_offset,
7819 kWithoutReadBarrier);
7820
7821 GenerateBitstringTypeCheckCompare(instruction, temp);
7822 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
7823 break;
7824 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007825 }
7826 __ Bind(&done);
7827
Roland Levillain0d5a2812015-11-13 10:07:31 +00007828 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007829}
7830
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007831void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007832 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7833 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007834 InvokeRuntimeCallingConvention calling_convention;
7835 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7836}
7837
7838void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007839 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
7840 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01007841 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01007842 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007843 if (instruction->IsEnter()) {
7844 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7845 } else {
7846 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7847 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007848}
7849
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307850void LocationsBuilderX86::VisitX86AndNot(HX86AndNot* instruction) {
7851 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7852 DCHECK(DataType::IsIntOrLongType(instruction->GetType())) << instruction->GetType();
7853 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7854 locations->SetInAt(0, Location::RequiresRegister());
7855 locations->SetInAt(1, Location::RequiresRegister());
7856 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7857}
7858
7859void InstructionCodeGeneratorX86::VisitX86AndNot(HX86AndNot* instruction) {
7860 LocationSummary* locations = instruction->GetLocations();
7861 Location first = locations->InAt(0);
7862 Location second = locations->InAt(1);
7863 Location dest = locations->Out();
7864 if (instruction->GetResultType() == DataType::Type::kInt32) {
7865 __ andn(dest.AsRegister<Register>(),
7866 first.AsRegister<Register>(),
7867 second.AsRegister<Register>());
7868 } else {
7869 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
7870 __ andn(dest.AsRegisterPairLow<Register>(),
7871 first.AsRegisterPairLow<Register>(),
7872 second.AsRegisterPairLow<Register>());
7873 __ andn(dest.AsRegisterPairHigh<Register>(),
7874 first.AsRegisterPairHigh<Register>(),
7875 second.AsRegisterPairHigh<Register>());
7876 }
7877}
7878
7879void LocationsBuilderX86::VisitX86MaskOrResetLeastSetBit(HX86MaskOrResetLeastSetBit* instruction) {
7880 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7881 DCHECK(instruction->GetType() == DataType::Type::kInt32) << instruction->GetType();
7882 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7883 locations->SetInAt(0, Location::RequiresRegister());
7884 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7885}
7886
7887void InstructionCodeGeneratorX86::VisitX86MaskOrResetLeastSetBit(
7888 HX86MaskOrResetLeastSetBit* instruction) {
7889 LocationSummary* locations = instruction->GetLocations();
7890 Location src = locations->InAt(0);
7891 Location dest = locations->Out();
7892 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
7893 switch (instruction->GetOpKind()) {
7894 case HInstruction::kAnd:
7895 __ blsr(dest.AsRegister<Register>(), src.AsRegister<Register>());
7896 break;
7897 case HInstruction::kXor:
7898 __ blsmsk(dest.AsRegister<Register>(), src.AsRegister<Register>());
7899 break;
7900 default:
7901 LOG(FATAL) << "Unreachable";
7902 }
7903}
7904
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007905void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
7906void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
7907void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
7908
7909void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7910 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007911 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007912 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7913 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007914 locations->SetInAt(0, Location::RequiresRegister());
7915 locations->SetInAt(1, Location::Any());
7916 locations->SetOut(Location::SameAsFirstInput());
7917}
7918
7919void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
7920 HandleBitwiseOperation(instruction);
7921}
7922
7923void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
7924 HandleBitwiseOperation(instruction);
7925}
7926
7927void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
7928 HandleBitwiseOperation(instruction);
7929}
7930
7931void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7932 LocationSummary* locations = instruction->GetLocations();
7933 Location first = locations->InAt(0);
7934 Location second = locations->InAt(1);
7935 DCHECK(first.Equals(locations->Out()));
7936
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007937 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007938 if (second.IsRegister()) {
7939 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007940 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007941 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007942 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007943 } else {
7944 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007945 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007946 }
7947 } else if (second.IsConstant()) {
7948 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007949 __ andl(first.AsRegister<Register>(),
7950 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007951 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007952 __ orl(first.AsRegister<Register>(),
7953 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007954 } else {
7955 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00007956 __ xorl(first.AsRegister<Register>(),
7957 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007958 }
7959 } else {
7960 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007961 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007962 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007963 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007964 } else {
7965 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007966 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007967 }
7968 }
7969 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007970 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007971 if (second.IsRegisterPair()) {
7972 if (instruction->IsAnd()) {
7973 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7974 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7975 } else if (instruction->IsOr()) {
7976 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7977 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7978 } else {
7979 DCHECK(instruction->IsXor());
7980 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7981 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7982 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007983 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007984 if (instruction->IsAnd()) {
7985 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7986 __ andl(first.AsRegisterPairHigh<Register>(),
7987 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7988 } else if (instruction->IsOr()) {
7989 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7990 __ orl(first.AsRegisterPairHigh<Register>(),
7991 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7992 } else {
7993 DCHECK(instruction->IsXor());
7994 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7995 __ xorl(first.AsRegisterPairHigh<Register>(),
7996 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7997 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007998 } else {
7999 DCHECK(second.IsConstant()) << second;
8000 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008001 int32_t low_value = Low32Bits(value);
8002 int32_t high_value = High32Bits(value);
8003 Immediate low(low_value);
8004 Immediate high(high_value);
8005 Register first_low = first.AsRegisterPairLow<Register>();
8006 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008007 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008008 if (low_value == 0) {
8009 __ xorl(first_low, first_low);
8010 } else if (low_value != -1) {
8011 __ andl(first_low, low);
8012 }
8013 if (high_value == 0) {
8014 __ xorl(first_high, first_high);
8015 } else if (high_value != -1) {
8016 __ andl(first_high, high);
8017 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008018 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008019 if (low_value != 0) {
8020 __ orl(first_low, low);
8021 }
8022 if (high_value != 0) {
8023 __ orl(first_high, high);
8024 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008025 } else {
8026 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008027 if (low_value != 0) {
8028 __ xorl(first_low, low);
8029 }
8030 if (high_value != 0) {
8031 __ xorl(first_high, high);
8032 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008033 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008034 }
8035 }
8036}
8037
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008038void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
8039 HInstruction* instruction,
8040 Location out,
8041 uint32_t offset,
8042 Location maybe_temp,
8043 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008044 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008045 if (read_barrier_option == kWithReadBarrier) {
8046 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008047 if (kUseBakerReadBarrier) {
8048 // Load with fast path based Baker's read barrier.
8049 // /* HeapReference<Object> */ out = *(out + offset)
8050 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008051 instruction, out, out_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008052 } else {
8053 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008054 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00008055 // in the following move operation, as we will need it for the
8056 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00008057 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008058 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008059 // /* HeapReference<Object> */ out = *(out + offset)
8060 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008061 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008062 }
8063 } else {
8064 // Plain load with no read barrier.
8065 // /* HeapReference<Object> */ out = *(out + offset)
8066 __ movl(out_reg, Address(out_reg, offset));
8067 __ MaybeUnpoisonHeapReference(out_reg);
8068 }
8069}
8070
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008071void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
8072 HInstruction* instruction,
8073 Location out,
8074 Location obj,
8075 uint32_t offset,
8076 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008077 Register out_reg = out.AsRegister<Register>();
8078 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008079 if (read_barrier_option == kWithReadBarrier) {
8080 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008081 if (kUseBakerReadBarrier) {
8082 // Load with fast path based Baker's read barrier.
8083 // /* HeapReference<Object> */ out = *(obj + offset)
8084 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008085 instruction, out, obj_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008086 } else {
8087 // Load with slow path based read barrier.
8088 // /* HeapReference<Object> */ out = *(obj + offset)
8089 __ movl(out_reg, Address(obj_reg, offset));
8090 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8091 }
8092 } else {
8093 // Plain load with no read barrier.
8094 // /* HeapReference<Object> */ out = *(obj + offset)
8095 __ movl(out_reg, Address(obj_reg, offset));
8096 __ MaybeUnpoisonHeapReference(out_reg);
8097 }
8098}
8099
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008100void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
8101 HInstruction* instruction,
8102 Location root,
8103 const Address& address,
8104 Label* fixup_label,
8105 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008106 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008107 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07008108 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008109 if (kUseBakerReadBarrier) {
8110 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
8111 // Baker's read barrier are used:
8112 //
Roland Levillaind966ce72017-02-09 16:20:14 +00008113 // root = obj.field;
8114 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8115 // if (temp != null) {
8116 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00008117 // }
8118
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008119 // /* GcRoot<mirror::Object> */ root = *address
8120 __ movl(root_reg, address);
8121 if (fixup_label != nullptr) {
8122 __ Bind(fixup_label);
8123 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008124 static_assert(
8125 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8126 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8127 "have different sizes.");
8128 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8129 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8130 "have different sizes.");
8131
Vladimir Marko953437b2016-08-24 08:30:46 +00008132 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008133 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008134 instruction, root, /* unpoison_ref_before_marking= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008135 codegen_->AddSlowPath(slow_path);
8136
Roland Levillaind966ce72017-02-09 16:20:14 +00008137 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
8138 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01008139 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00008140 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
8141 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008142 __ j(kNotEqual, slow_path->GetEntryLabel());
8143 __ Bind(slow_path->GetExitLabel());
8144 } else {
8145 // GC root loaded through a slow path for read barriers other
8146 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008147 // /* GcRoot<mirror::Object>* */ root = address
8148 __ leal(root_reg, address);
8149 if (fixup_label != nullptr) {
8150 __ Bind(fixup_label);
8151 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008152 // /* mirror::Object* */ root = root->Read()
8153 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8154 }
8155 } else {
8156 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008157 // /* GcRoot<mirror::Object> */ root = *address
8158 __ movl(root_reg, address);
8159 if (fixup_label != nullptr) {
8160 __ Bind(fixup_label);
8161 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00008162 // Note that GC roots are not affected by heap poisoning, thus we
8163 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008164 }
8165}
8166
8167void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8168 Location ref,
8169 Register obj,
8170 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00008171 bool needs_null_check) {
8172 DCHECK(kEmitCompilerReadBarrier);
8173 DCHECK(kUseBakerReadBarrier);
8174
8175 // /* HeapReference<Object> */ ref = *(obj + offset)
8176 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00008177 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008178}
8179
8180void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8181 Location ref,
8182 Register obj,
8183 uint32_t data_offset,
8184 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00008185 bool needs_null_check) {
8186 DCHECK(kEmitCompilerReadBarrier);
8187 DCHECK(kUseBakerReadBarrier);
8188
Roland Levillain3d312422016-06-23 13:53:42 +01008189 static_assert(
8190 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8191 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00008192 // /* HeapReference<Object> */ ref =
8193 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008194 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00008195 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008196}
8197
8198void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8199 Location ref,
8200 Register obj,
8201 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008202 bool needs_null_check,
8203 bool always_update_field,
8204 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008205 DCHECK(kEmitCompilerReadBarrier);
8206 DCHECK(kUseBakerReadBarrier);
8207
8208 // In slow path based read barriers, the read barrier call is
8209 // inserted after the original load. However, in fast path based
8210 // Baker's read barriers, we need to perform the load of
8211 // mirror::Object::monitor_ *before* the original reference load.
8212 // This load-load ordering is required by the read barrier.
8213 // The fast path/slow path (for Baker's algorithm) should look like:
8214 //
8215 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8216 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8217 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008218 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00008219 // if (is_gray) {
8220 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
8221 // }
8222 //
8223 // Note: the original implementation in ReadBarrier::Barrier is
8224 // slightly more complex as:
8225 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00008226 // the high-bits of rb_state, which are expected to be all zeroes
8227 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
8228 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008229 // - it performs additional checks that we do not do here for
8230 // performance reasons.
8231
8232 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00008233 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
8234
Vladimir Marko953437b2016-08-24 08:30:46 +00008235 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01008236 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008237 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00008238 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
8239 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
8240 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
8241
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008242 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00008243 // ref = ReadBarrier::Mark(ref);
8244 // At this point, just do the "if" and make sure that flags are preserved until the branch.
8245 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00008246 if (needs_null_check) {
8247 MaybeRecordImplicitNullCheck(instruction);
8248 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008249
8250 // Load fence to prevent load-load reordering.
8251 // Note that this is a no-op, thanks to the x86 memory model.
8252 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
8253
8254 // The actual reference load.
8255 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00008256 __ movl(ref_reg, src); // Flags are unaffected.
8257
8258 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
8259 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008260 SlowPathCode* slow_path;
8261 if (always_update_field) {
8262 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01008263 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008264 instruction, ref, obj, src, /* unpoison_ref_before_marking= */ true, *temp);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008265 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008266 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008267 instruction, ref, /* unpoison_ref_before_marking= */ true);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008268 }
Vladimir Marko953437b2016-08-24 08:30:46 +00008269 AddSlowPath(slow_path);
8270
8271 // We have done the "if" of the gray bit check above, now branch based on the flags.
8272 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00008273
8274 // Object* ref = ref_addr->AsMirrorPtr()
8275 __ MaybeUnpoisonHeapReference(ref_reg);
8276
Roland Levillain7c1559a2015-12-15 10:55:36 +00008277 __ Bind(slow_path->GetExitLabel());
8278}
8279
8280void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
8281 Location out,
8282 Location ref,
8283 Location obj,
8284 uint32_t offset,
8285 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008286 DCHECK(kEmitCompilerReadBarrier);
8287
Roland Levillain7c1559a2015-12-15 10:55:36 +00008288 // Insert a slow path based read barrier *after* the reference load.
8289 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008290 // If heap poisoning is enabled, the unpoisoning of the loaded
8291 // reference will be carried out by the runtime within the slow
8292 // path.
8293 //
8294 // Note that `ref` currently does not get unpoisoned (when heap
8295 // poisoning is enabled), which is alright as the `ref` argument is
8296 // not used by the artReadBarrierSlow entry point.
8297 //
8298 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008299 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00008300 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
8301 AddSlowPath(slow_path);
8302
Roland Levillain0d5a2812015-11-13 10:07:31 +00008303 __ jmp(slow_path->GetEntryLabel());
8304 __ Bind(slow_path->GetExitLabel());
8305}
8306
Roland Levillain7c1559a2015-12-15 10:55:36 +00008307void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
8308 Location out,
8309 Location ref,
8310 Location obj,
8311 uint32_t offset,
8312 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008313 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008314 // Baker's read barriers shall be handled by the fast path
8315 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
8316 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008317 // If heap poisoning is enabled, unpoisoning will be taken care of
8318 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008319 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008320 } else if (kPoisonHeapReferences) {
8321 __ UnpoisonHeapReference(out.AsRegister<Register>());
8322 }
8323}
8324
Roland Levillain7c1559a2015-12-15 10:55:36 +00008325void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8326 Location out,
8327 Location root) {
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 GC root load.
8331 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008332 // Note that GC roots are not affected by heap poisoning, so we do
8333 // not need to do anything special for this here.
8334 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008335 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008336 AddSlowPath(slow_path);
8337
Roland Levillain0d5a2812015-11-13 10:07:31 +00008338 __ jmp(slow_path->GetEntryLabel());
8339 __ Bind(slow_path->GetExitLabel());
8340}
8341
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008342void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008343 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008344 LOG(FATAL) << "Unreachable";
8345}
8346
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008347void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008348 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008349 LOG(FATAL) << "Unreachable";
8350}
8351
Mark Mendellfe57faa2015-09-18 09:26:15 -04008352// Simple implementation of packed switch - generate cascaded compare/jumps.
8353void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8354 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008355 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04008356 locations->SetInAt(0, Location::RequiresRegister());
8357}
8358
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008359void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
8360 int32_t lower_bound,
8361 uint32_t num_entries,
8362 HBasicBlock* switch_block,
8363 HBasicBlock* default_block) {
8364 // Figure out the correct compare values and jump conditions.
8365 // Handle the first compare/branch as a special case because it might
8366 // jump to the default case.
8367 DCHECK_GT(num_entries, 2u);
8368 Condition first_condition;
8369 uint32_t index;
8370 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
8371 if (lower_bound != 0) {
8372 first_condition = kLess;
8373 __ cmpl(value_reg, Immediate(lower_bound));
8374 __ j(first_condition, codegen_->GetLabelOf(default_block));
8375 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008376
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008377 index = 1;
8378 } else {
8379 // Handle all the compare/jumps below.
8380 first_condition = kBelow;
8381 index = 0;
8382 }
8383
8384 // Handle the rest of the compare/jumps.
8385 for (; index + 1 < num_entries; index += 2) {
8386 int32_t compare_to_value = lower_bound + index + 1;
8387 __ cmpl(value_reg, Immediate(compare_to_value));
8388 // Jump to successors[index] if value < case_value[index].
8389 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
8390 // Jump to successors[index + 1] if value == case_value[index + 1].
8391 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
8392 }
8393
8394 if (index != num_entries) {
8395 // There are an odd number of entries. Handle the last one.
8396 DCHECK_EQ(index + 1, num_entries);
8397 __ cmpl(value_reg, Immediate(lower_bound + index));
8398 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008399 }
8400
8401 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008402 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
8403 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008404 }
8405}
8406
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008407void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8408 int32_t lower_bound = switch_instr->GetStartValue();
8409 uint32_t num_entries = switch_instr->GetNumEntries();
8410 LocationSummary* locations = switch_instr->GetLocations();
8411 Register value_reg = locations->InAt(0).AsRegister<Register>();
8412
8413 GenPackedSwitchWithCompares(value_reg,
8414 lower_bound,
8415 num_entries,
8416 switch_instr->GetBlock(),
8417 switch_instr->GetDefaultBlock());
8418}
8419
Mark Mendell805b3b52015-09-18 14:10:29 -04008420void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8421 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008422 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04008423 locations->SetInAt(0, Location::RequiresRegister());
8424
8425 // Constant area pointer.
8426 locations->SetInAt(1, Location::RequiresRegister());
8427
8428 // And the temporary we need.
8429 locations->AddTemp(Location::RequiresRegister());
8430}
8431
8432void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8433 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008434 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04008435 LocationSummary* locations = switch_instr->GetLocations();
8436 Register value_reg = locations->InAt(0).AsRegister<Register>();
8437 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8438
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008439 if (num_entries <= kPackedSwitchJumpTableThreshold) {
8440 GenPackedSwitchWithCompares(value_reg,
8441 lower_bound,
8442 num_entries,
8443 switch_instr->GetBlock(),
8444 default_block);
8445 return;
8446 }
8447
Mark Mendell805b3b52015-09-18 14:10:29 -04008448 // Optimizing has a jump area.
8449 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
8450 Register constant_area = locations->InAt(1).AsRegister<Register>();
8451
8452 // Remove the bias, if needed.
8453 if (lower_bound != 0) {
8454 __ leal(temp_reg, Address(value_reg, -lower_bound));
8455 value_reg = temp_reg;
8456 }
8457
8458 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008459 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04008460 __ cmpl(value_reg, Immediate(num_entries - 1));
8461 __ j(kAbove, codegen_->GetLabelOf(default_block));
8462
8463 // We are in the range of the table.
8464 // Load (target-constant_area) from the jump table, indexing by the value.
8465 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
8466
8467 // Compute the actual target address by adding in constant_area.
8468 __ addl(temp_reg, constant_area);
8469
8470 // And jump.
8471 __ jmp(temp_reg);
8472}
8473
Mark Mendell0616ae02015-04-17 12:49:27 -04008474void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
8475 HX86ComputeBaseMethodAddress* insn) {
8476 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008477 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008478 locations->SetOut(Location::RequiresRegister());
8479}
8480
8481void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
8482 HX86ComputeBaseMethodAddress* insn) {
8483 LocationSummary* locations = insn->GetLocations();
8484 Register reg = locations->Out().AsRegister<Register>();
8485
8486 // Generate call to next instruction.
8487 Label next_instruction;
8488 __ call(&next_instruction);
8489 __ Bind(&next_instruction);
8490
8491 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008492 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04008493
8494 // Grab the return address off the stack.
8495 __ popl(reg);
8496}
8497
8498void LocationsBuilderX86::VisitX86LoadFromConstantTable(
8499 HX86LoadFromConstantTable* insn) {
8500 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008501 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008502
8503 locations->SetInAt(0, Location::RequiresRegister());
8504 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
8505
8506 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00008507 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008508 return;
8509 }
8510
8511 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008512 case DataType::Type::kFloat32:
8513 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008514 locations->SetOut(Location::RequiresFpuRegister());
8515 break;
8516
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008517 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008518 locations->SetOut(Location::RequiresRegister());
8519 break;
8520
8521 default:
8522 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8523 }
8524}
8525
8526void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00008527 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008528 return;
8529 }
8530
8531 LocationSummary* locations = insn->GetLocations();
8532 Location out = locations->Out();
8533 Register const_area = locations->InAt(0).AsRegister<Register>();
8534 HConstant *value = insn->GetConstant();
8535
8536 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008537 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008538 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008539 codegen_->LiteralFloatAddress(
8540 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008541 break;
8542
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008543 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008544 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008545 codegen_->LiteralDoubleAddress(
8546 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008547 break;
8548
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008549 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008550 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008551 codegen_->LiteralInt32Address(
8552 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008553 break;
8554
8555 default:
8556 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8557 }
8558}
8559
Mark Mendell0616ae02015-04-17 12:49:27 -04008560/**
8561 * Class to handle late fixup of offsets into constant area.
8562 */
Vladimir Marko5233f932015-09-29 19:01:15 +01008563class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04008564 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008565 RIPFixup(CodeGeneratorX86& codegen,
8566 HX86ComputeBaseMethodAddress* base_method_address,
8567 size_t offset)
8568 : codegen_(&codegen),
8569 base_method_address_(base_method_address),
8570 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008571
8572 protected:
8573 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
8574
8575 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008576 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008577
8578 private:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008579 void Process(const MemoryRegion& region, int pos) override {
Mark Mendell0616ae02015-04-17 12:49:27 -04008580 // Patch the correct offset for the instruction. The place to patch is the
8581 // last 4 bytes of the instruction.
8582 // The value to patch is the distance from the offset in the constant area
8583 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04008584 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008585 int32_t relative_position =
8586 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04008587
8588 // Patch in the right value.
8589 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
8590 }
8591
Mark Mendell0616ae02015-04-17 12:49:27 -04008592 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04008593 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008594};
8595
Mark Mendell805b3b52015-09-18 14:10:29 -04008596/**
8597 * Class to handle late fixup of offsets to a jump table that will be created in the
8598 * constant area.
8599 */
8600class JumpTableRIPFixup : public RIPFixup {
8601 public:
8602 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008603 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
8604 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008605
8606 void CreateJumpTable() {
8607 X86Assembler* assembler = codegen_->GetAssembler();
8608
8609 // Ensure that the reference to the jump table has the correct offset.
8610 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
8611 SetOffset(offset_in_constant_table);
8612
8613 // The label values in the jump table are computed relative to the
8614 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008615 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04008616
8617 // Populate the jump table with the correct values for the jump table.
8618 int32_t num_entries = switch_instr_->GetNumEntries();
8619 HBasicBlock* block = switch_instr_->GetBlock();
8620 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
8621 // The value that we want is the target offset - the position of the table.
8622 for (int32_t i = 0; i < num_entries; i++) {
8623 HBasicBlock* b = successors[i];
8624 Label* l = codegen_->GetLabelOf(b);
8625 DCHECK(l->IsBound());
8626 int32_t offset_to_block = l->Position() - relative_offset;
8627 assembler->AppendInt32(offset_to_block);
8628 }
8629 }
8630
8631 private:
8632 const HX86PackedSwitch* switch_instr_;
8633};
8634
8635void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
8636 // Generate the constant area if needed.
8637 X86Assembler* assembler = GetAssembler();
jaishank20d1c942019-03-08 15:08:17 +05308638
Mark Mendell805b3b52015-09-18 14:10:29 -04008639 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
8640 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
8641 // byte values.
8642 assembler->Align(4, 0);
8643 constant_area_start_ = assembler->CodeSize();
8644
8645 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008646 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04008647 jump_table->CreateJumpTable();
8648 }
8649
8650 // And now add the constant area to the generated code.
8651 assembler->AddConstantArea();
8652 }
8653
8654 // And finish up.
8655 CodeGenerator::Finalize(allocator);
8656}
8657
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008658Address CodeGeneratorX86::LiteralDoubleAddress(double v,
8659 HX86ComputeBaseMethodAddress* method_base,
8660 Register reg) {
8661 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008662 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008663 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008664}
8665
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008666Address CodeGeneratorX86::LiteralFloatAddress(float v,
8667 HX86ComputeBaseMethodAddress* method_base,
8668 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008669 AssemblerFixup* fixup =
8670 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008671 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008672}
8673
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008674Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
8675 HX86ComputeBaseMethodAddress* method_base,
8676 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008677 AssemblerFixup* fixup =
8678 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008679 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008680}
8681
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008682Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
8683 HX86ComputeBaseMethodAddress* method_base,
8684 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008685 AssemblerFixup* fixup =
8686 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008687 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008688}
8689
Aart Bika19616e2016-02-01 18:57:58 -08008690void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
8691 if (value == 0) {
8692 __ xorl(dest, dest);
8693 } else {
8694 __ movl(dest, Immediate(value));
8695 }
8696}
8697
8698void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
8699 if (value == 0) {
8700 __ testl(dest, dest);
8701 } else {
8702 __ cmpl(dest, Immediate(value));
8703 }
8704}
8705
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008706void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
8707 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07008708 GenerateIntCompare(lhs_reg, rhs);
8709}
8710
8711void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008712 if (rhs.IsConstant()) {
8713 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07008714 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008715 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07008716 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008717 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07008718 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008719 }
8720}
8721
8722Address CodeGeneratorX86::ArrayAddress(Register obj,
8723 Location index,
8724 ScaleFactor scale,
8725 uint32_t data_offset) {
8726 return index.IsConstant() ?
8727 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
8728 Address(obj, index.AsRegister<Register>(), scale, data_offset);
8729}
8730
Mark Mendell805b3b52015-09-18 14:10:29 -04008731Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
8732 Register reg,
8733 Register value) {
8734 // Create a fixup to be used to create and address the jump table.
8735 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008736 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04008737
8738 // We have to populate the jump tables.
8739 fixups_to_jump_tables_.push_back(table_fixup);
8740
8741 // We want a scaled address, as we are extracting the correct offset from the table.
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008742 return Address(reg, value, TIMES_4, kPlaceholder32BitOffset, table_fixup);
Mark Mendell805b3b52015-09-18 14:10:29 -04008743}
8744
Andreas Gampe85b62f22015-09-09 13:15:38 -07008745// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008746void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07008747 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008748 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008749 return;
8750 }
8751
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008752 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008753
8754 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
8755 if (target.Equals(return_loc)) {
8756 return;
8757 }
8758
8759 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8760 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008761 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008762 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008763 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
8764 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008765 GetMoveResolver()->EmitNativeCode(&parallel_move);
8766 } else {
8767 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01008768 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07008769 parallel_move.AddMove(return_loc, target, type, nullptr);
8770 GetMoveResolver()->EmitNativeCode(&parallel_move);
8771 }
8772}
8773
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008774void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
8775 const uint8_t* roots_data,
8776 const PatchInfo<Label>& info,
8777 uint64_t index_in_table) const {
8778 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
8779 uintptr_t address =
8780 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
Andreas Gampec55bb392018-09-21 00:02:02 +00008781 using unaligned_uint32_t __attribute__((__aligned__(1))) = uint32_t;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008782 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
8783 dchecked_integral_cast<uint32_t>(address);
8784}
8785
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008786void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8787 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008788 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008789 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008790 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008791 }
8792
8793 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008794 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008795 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008796 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008797 }
8798}
8799
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008800void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8801 ATTRIBUTE_UNUSED) {
8802 LOG(FATAL) << "Unreachable";
8803}
8804
8805void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8806 ATTRIBUTE_UNUSED) {
8807 LOG(FATAL) << "Unreachable";
8808}
8809
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05308810bool LocationsBuilderX86::CpuHasAvxFeatureFlag() {
8811 return codegen_->GetInstructionSetFeatures().HasAVX();
8812}
8813bool LocationsBuilderX86::CpuHasAvx2FeatureFlag() {
8814 return codegen_->GetInstructionSetFeatures().HasAVX2();
8815}
8816bool InstructionCodeGeneratorX86::CpuHasAvxFeatureFlag() {
8817 return codegen_->GetInstructionSetFeatures().HasAVX();
8818}
8819bool InstructionCodeGeneratorX86::CpuHasAvx2FeatureFlag() {
8820 return codegen_->GetInstructionSetFeatures().HasAVX2();
8821}
8822
Roland Levillain4d027112015-07-01 15:41:14 +01008823#undef __
8824
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008825} // namespace x86
8826} // namespace art