blob: 934eebbb3dd15da77d0f3461e3f7a451e598098b [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() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100492 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
493 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000494 << "Unexpected instruction in read barrier marking slow path: "
495 << instruction_->DebugName();
496
497 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100498 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000499 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100500 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000501 }
Roland Levillain4359e612016-07-20 11:32:19 +0100502 // No need to save live registers; it's taken care of by the
503 // entrypoint. Also, there is no need to update the stack mask,
504 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000505 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100506 DCHECK_NE(ref_reg, ESP);
507 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100508 // "Compact" slow path, saving two moves.
509 //
510 // Instead of using the standard runtime calling convention (input
511 // and output in EAX):
512 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100513 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100514 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100515 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100516 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100517 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100518 // of a dedicated entrypoint:
519 //
520 // rX <- ReadBarrierMarkRegX(rX)
521 //
Roland Levillain97c46462017-05-11 14:04:03 +0100522 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100523 // This runtime call does not require a stack map.
524 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000525 __ jmp(GetExitLabel());
526 }
527
528 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100529 // The location (register) of the marked object reference.
530 const Location ref_;
531 // Should the reference in `ref_` be unpoisoned prior to marking it?
532 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000533
534 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
535};
536
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100537// Slow path marking an object reference `ref` during a read barrier,
538// and if needed, atomically updating the field `obj.field` in the
539// object `obj` holding this reference after marking (contrary to
540// ReadBarrierMarkSlowPathX86 above, which never tries to update
541// `obj.field`).
542//
543// This means that after the execution of this slow path, both `ref`
544// and `obj.field` will be up-to-date; i.e., after the flip, both will
545// hold the same to-space reference (unless another thread installed
546// another object reference (different from `ref`) in `obj.field`).
547class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
548 public:
549 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
550 Location ref,
551 Register obj,
552 const Address& field_addr,
553 bool unpoison_ref_before_marking,
554 Register temp)
555 : SlowPathCode(instruction),
556 ref_(ref),
557 obj_(obj),
558 field_addr_(field_addr),
559 unpoison_ref_before_marking_(unpoison_ref_before_marking),
560 temp_(temp) {
561 DCHECK(kEmitCompilerReadBarrier);
562 }
563
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100564 const char* GetDescription() const override { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100565
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100566 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100567 LocationSummary* locations = instruction_->GetLocations();
568 Register ref_reg = ref_.AsRegister<Register>();
569 DCHECK(locations->CanCall());
570 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
571 // This slow path is only used by the UnsafeCASObject intrinsic.
572 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
573 << "Unexpected instruction in read barrier marking and field updating slow path: "
574 << instruction_->DebugName();
575 DCHECK(instruction_->GetLocations()->Intrinsified());
576 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
577
578 __ Bind(GetEntryLabel());
579 if (unpoison_ref_before_marking_) {
580 // Object* ref = ref_addr->AsMirrorPtr()
581 __ MaybeUnpoisonHeapReference(ref_reg);
582 }
583
584 // Save the old (unpoisoned) reference.
585 __ movl(temp_, ref_reg);
586
587 // No need to save live registers; it's taken care of by the
588 // entrypoint. Also, there is no need to update the stack mask,
589 // as this runtime call will not trigger a garbage collection.
590 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
591 DCHECK_NE(ref_reg, ESP);
592 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
593 // "Compact" slow path, saving two moves.
594 //
595 // Instead of using the standard runtime calling convention (input
596 // and output in EAX):
597 //
598 // EAX <- ref
599 // EAX <- ReadBarrierMark(EAX)
600 // ref <- EAX
601 //
602 // we just use rX (the register containing `ref`) as input and output
603 // of a dedicated entrypoint:
604 //
605 // rX <- ReadBarrierMarkRegX(rX)
606 //
Roland Levillain97c46462017-05-11 14:04:03 +0100607 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100608 // This runtime call does not require a stack map.
609 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
610
611 // If the new reference is different from the old reference,
612 // update the field in the holder (`*field_addr`).
613 //
614 // Note that this field could also hold a different object, if
615 // another thread had concurrently changed it. In that case, the
616 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
617 // operation below would abort the CAS, leaving the field as-is.
618 NearLabel done;
619 __ cmpl(temp_, ref_reg);
620 __ j(kEqual, &done);
621
622 // Update the the holder's field atomically. This may fail if
623 // mutator updates before us, but it's OK. This is achieved
624 // using a strong compare-and-set (CAS) operation with relaxed
625 // memory synchronization ordering, where the expected value is
626 // the old reference and the desired value is the new reference.
627 // This operation is implemented with a 32-bit LOCK CMPXLCHG
628 // instruction, which requires the expected value (the old
629 // reference) to be in EAX. Save EAX beforehand, and move the
630 // expected value (stored in `temp_`) into EAX.
631 __ pushl(EAX);
632 __ movl(EAX, temp_);
633
634 // Convenience aliases.
635 Register base = obj_;
636 Register expected = EAX;
637 Register value = ref_reg;
638
639 bool base_equals_value = (base == value);
640 if (kPoisonHeapReferences) {
641 if (base_equals_value) {
642 // If `base` and `value` are the same register location, move
643 // `value` to a temporary register. This way, poisoning
644 // `value` won't invalidate `base`.
645 value = temp_;
646 __ movl(value, base);
647 }
648
649 // Check that the register allocator did not assign the location
650 // of `expected` (EAX) to `value` nor to `base`, so that heap
651 // poisoning (when enabled) works as intended below.
652 // - If `value` were equal to `expected`, both references would
653 // be poisoned twice, meaning they would not be poisoned at
654 // all, as heap poisoning uses address negation.
655 // - If `base` were equal to `expected`, poisoning `expected`
656 // would invalidate `base`.
657 DCHECK_NE(value, expected);
658 DCHECK_NE(base, expected);
659
660 __ PoisonHeapReference(expected);
661 __ PoisonHeapReference(value);
662 }
663
664 __ LockCmpxchgl(field_addr_, value);
665
666 // If heap poisoning is enabled, we need to unpoison the values
667 // that were poisoned earlier.
668 if (kPoisonHeapReferences) {
669 if (base_equals_value) {
670 // `value` has been moved to a temporary register, no need
671 // to unpoison it.
672 } else {
673 __ UnpoisonHeapReference(value);
674 }
675 // No need to unpoison `expected` (EAX), as it is be overwritten below.
676 }
677
678 // Restore EAX.
679 __ popl(EAX);
680
681 __ Bind(&done);
682 __ jmp(GetExitLabel());
683 }
684
685 private:
686 // The location (register) of the marked object reference.
687 const Location ref_;
688 // The register containing the object holding the marked object reference field.
689 const Register obj_;
690 // The address of the marked reference field. The base of this address must be `obj_`.
691 const Address field_addr_;
692
693 // Should the reference in `ref_` be unpoisoned prior to marking it?
694 const bool unpoison_ref_before_marking_;
695
696 const Register temp_;
697
698 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
699};
700
Roland Levillain0d5a2812015-11-13 10:07:31 +0000701// Slow path generating a read barrier for a heap reference.
702class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
703 public:
704 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
705 Location out,
706 Location ref,
707 Location obj,
708 uint32_t offset,
709 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000710 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000711 out_(out),
712 ref_(ref),
713 obj_(obj),
714 offset_(offset),
715 index_(index) {
716 DCHECK(kEmitCompilerReadBarrier);
717 // If `obj` is equal to `out` or `ref`, it means the initial object
718 // has been overwritten by (or after) the heap object reference load
719 // to be instrumented, e.g.:
720 //
721 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000722 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000723 //
724 // In that case, we have lost the information about the original
725 // object, and the emitted read barrier cannot work properly.
726 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
727 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
728 }
729
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100730 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000731 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
732 LocationSummary* locations = instruction_->GetLocations();
733 Register reg_out = out_.AsRegister<Register>();
734 DCHECK(locations->CanCall());
735 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100736 DCHECK(instruction_->IsInstanceFieldGet() ||
737 instruction_->IsStaticFieldGet() ||
738 instruction_->IsArrayGet() ||
739 instruction_->IsInstanceOf() ||
740 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700741 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000742 << "Unexpected instruction in read barrier for heap reference slow path: "
743 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000744
745 __ Bind(GetEntryLabel());
746 SaveLiveRegisters(codegen, locations);
747
748 // We may have to change the index's value, but as `index_` is a
749 // constant member (like other "inputs" of this slow path),
750 // introduce a copy of it, `index`.
751 Location index = index_;
752 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100753 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000754 if (instruction_->IsArrayGet()) {
755 // Compute the actual memory offset and store it in `index`.
756 Register index_reg = index_.AsRegister<Register>();
757 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
758 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
759 // We are about to change the value of `index_reg` (see the
760 // calls to art::x86::X86Assembler::shll and
761 // art::x86::X86Assembler::AddImmediate below), but it has
762 // not been saved by the previous call to
763 // art::SlowPathCode::SaveLiveRegisters, as it is a
764 // callee-save register --
765 // art::SlowPathCode::SaveLiveRegisters does not consider
766 // callee-save registers, as it has been designed with the
767 // assumption that callee-save registers are supposed to be
768 // handled by the called function. So, as a callee-save
769 // register, `index_reg` _would_ eventually be saved onto
770 // the stack, but it would be too late: we would have
771 // changed its value earlier. Therefore, we manually save
772 // it here into another freely available register,
773 // `free_reg`, chosen of course among the caller-save
774 // registers (as a callee-save `free_reg` register would
775 // exhibit the same problem).
776 //
777 // Note we could have requested a temporary register from
778 // the register allocator instead; but we prefer not to, as
779 // this is a slow path, and we know we can find a
780 // caller-save register that is available.
781 Register free_reg = FindAvailableCallerSaveRegister(codegen);
782 __ movl(free_reg, index_reg);
783 index_reg = free_reg;
784 index = Location::RegisterLocation(index_reg);
785 } else {
786 // The initial register stored in `index_` has already been
787 // saved in the call to art::SlowPathCode::SaveLiveRegisters
788 // (as it is not a callee-save register), so we can freely
789 // use it.
790 }
791 // Shifting the index value contained in `index_reg` by the scale
792 // factor (2) cannot overflow in practice, as the runtime is
793 // unable to allocate object arrays with a size larger than
794 // 2^26 - 1 (that is, 2^28 - 4 bytes).
795 __ shll(index_reg, Immediate(TIMES_4));
796 static_assert(
797 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
798 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
799 __ AddImmediate(index_reg, Immediate(offset_));
800 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100801 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
802 // intrinsics, `index_` is not shifted by a scale factor of 2
803 // (as in the case of ArrayGet), as it is actually an offset
804 // to an object field within an object.
805 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000806 DCHECK(instruction_->GetLocations()->Intrinsified());
807 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
808 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
809 << instruction_->AsInvoke()->GetIntrinsic();
810 DCHECK_EQ(offset_, 0U);
811 DCHECK(index_.IsRegisterPair());
812 // UnsafeGet's offset location is a register pair, the low
813 // part contains the correct offset.
814 index = index_.ToLow();
815 }
816 }
817
818 // We're moving two or three locations to locations that could
819 // overlap, so we need a parallel move resolver.
820 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100821 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000822 parallel_move.AddMove(ref_,
823 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100824 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000825 nullptr);
826 parallel_move.AddMove(obj_,
827 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100828 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000829 nullptr);
830 if (index.IsValid()) {
831 parallel_move.AddMove(index,
832 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100833 DataType::Type::kInt32,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000834 nullptr);
835 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
836 } else {
837 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
838 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
839 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100840 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000841 CheckEntrypointTypes<
842 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
843 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
844
845 RestoreLiveRegisters(codegen, locations);
846 __ jmp(GetExitLabel());
847 }
848
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100849 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathX86"; }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000850
851 private:
852 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
853 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
854 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
855 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
856 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
857 return static_cast<Register>(i);
858 }
859 }
860 // We shall never fail to find a free caller-save register, as
861 // there are more than two core caller-save registers on x86
862 // (meaning it is possible to find one which is different from
863 // `ref` and `obj`).
864 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
865 LOG(FATAL) << "Could not find a free caller-save register";
866 UNREACHABLE();
867 }
868
Roland Levillain0d5a2812015-11-13 10:07:31 +0000869 const Location out_;
870 const Location ref_;
871 const Location obj_;
872 const uint32_t offset_;
873 // An additional location containing an index to an array.
874 // Only used for HArrayGet and the UnsafeGetObject &
875 // UnsafeGetObjectVolatile intrinsics.
876 const Location index_;
877
878 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
879};
880
881// Slow path generating a read barrier for a GC root.
882class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
883 public:
884 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000885 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000886 DCHECK(kEmitCompilerReadBarrier);
887 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000888
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100889 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000890 LocationSummary* locations = instruction_->GetLocations();
891 Register reg_out = out_.AsRegister<Register>();
892 DCHECK(locations->CanCall());
893 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000894 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
895 << "Unexpected instruction in read barrier for GC root slow path: "
896 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000897
898 __ Bind(GetEntryLabel());
899 SaveLiveRegisters(codegen, locations);
900
901 InvokeRuntimeCallingConvention calling_convention;
902 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
903 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100904 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000905 instruction_,
906 instruction_->GetDexPc(),
907 this);
908 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
909 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
910
911 RestoreLiveRegisters(codegen, locations);
912 __ jmp(GetExitLabel());
913 }
914
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100915 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathX86"; }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000916
917 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000918 const Location out_;
919 const Location root_;
920
921 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
922};
923
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100924#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100925// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
926#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100927
Aart Bike9f37602015-10-09 11:15:55 -0700928inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700929 switch (cond) {
930 case kCondEQ: return kEqual;
931 case kCondNE: return kNotEqual;
932 case kCondLT: return kLess;
933 case kCondLE: return kLessEqual;
934 case kCondGT: return kGreater;
935 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700936 case kCondB: return kBelow;
937 case kCondBE: return kBelowEqual;
938 case kCondA: return kAbove;
939 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700940 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100941 LOG(FATAL) << "Unreachable";
942 UNREACHABLE();
943}
944
Aart Bike9f37602015-10-09 11:15:55 -0700945// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100946inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
947 switch (cond) {
948 case kCondEQ: return kEqual;
949 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700950 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100951 case kCondLT: return kBelow;
952 case kCondLE: return kBelowEqual;
953 case kCondGT: return kAbove;
954 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700955 // Unsigned remain unchanged.
956 case kCondB: return kBelow;
957 case kCondBE: return kBelowEqual;
958 case kCondA: return kAbove;
959 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100960 }
961 LOG(FATAL) << "Unreachable";
962 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700963}
964
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100965void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100966 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100967}
968
969void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100970 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100971}
972
Vladimir Markoa0431112018-06-25 09:32:54 +0100973const X86InstructionSetFeatures& CodeGeneratorX86::GetInstructionSetFeatures() const {
974 return *GetCompilerOptions().GetInstructionSetFeatures()->AsX86InstructionSetFeatures();
975}
976
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100977size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
978 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
979 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100980}
981
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100982size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
983 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
984 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100985}
986
Mark Mendell7c8d0092015-01-26 11:21:33 -0500987size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700988 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700989 __ movups(Address(ESP, stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -0700990 } else {
991 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
992 }
Artem Serov6a0b6572019-07-26 20:38:37 +0100993 return GetSlowPathFPWidth();
Mark Mendell7c8d0092015-01-26 11:21:33 -0500994}
995
996size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700997 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700998 __ movups(XmmRegister(reg_id), Address(ESP, stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -0700999 } else {
1000 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
1001 }
Artem Serov6a0b6572019-07-26 20:38:37 +01001002 return GetSlowPathFPWidth();
Mark Mendell7c8d0092015-01-26 11:21:33 -05001003}
1004
Calin Juravle175dc732015-08-25 15:42:32 +01001005void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
1006 HInstruction* instruction,
1007 uint32_t dex_pc,
1008 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001009 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001010 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
1011 if (EntrypointRequiresStackMap(entrypoint)) {
1012 RecordPcInfo(instruction, dex_pc, slow_path);
1013 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001014}
1015
Roland Levillaindec8f632016-07-22 17:10:06 +01001016void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1017 HInstruction* instruction,
1018 SlowPathCode* slow_path) {
1019 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001020 GenerateInvokeRuntime(entry_point_offset);
1021}
1022
1023void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001024 __ fs()->call(Address::Absolute(entry_point_offset));
1025}
1026
Mark Mendellfb8d2792015-03-31 22:16:59 -04001027CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001028 const CompilerOptions& compiler_options,
1029 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001030 : CodeGenerator(graph,
1031 kNumberOfCpuRegisters,
1032 kNumberOfXmmRegisters,
1033 kNumberOfRegisterPairs,
1034 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1035 arraysize(kCoreCalleeSaves))
1036 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001037 0,
1038 compiler_options,
1039 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001040 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001041 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001042 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001043 move_resolver_(graph->GetAllocator(), this),
1044 assembler_(graph->GetAllocator()),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001045 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1046 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1047 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1048 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001049 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001050 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +01001051 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001052 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1053 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001054 constant_area_start_(-1),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001055 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001056 method_address_offset_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001057 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001058 // Use a fake return address register to mimic Quick.
1059 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001060}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001061
David Brazdil58282f42016-01-14 12:45:10 +00001062void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001063 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001064 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001065}
1066
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001067InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001068 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001069 assembler_(codegen->GetAssembler()),
1070 codegen_(codegen) {}
1071
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001072static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001073 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001074}
1075
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001076void CodeGeneratorX86::MaybeIncrementHotness(bool is_frame_entry) {
1077 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1078 Register reg = EAX;
1079 if (is_frame_entry) {
1080 reg = kMethodRegisterArgument;
1081 } else {
1082 __ pushl(EAX);
Vladimir Markodec78172020-06-19 15:31:23 +01001083 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001084 __ movl(EAX, Address(ESP, kX86WordSize));
1085 }
1086 NearLabel overflow;
1087 __ cmpw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()),
1088 Immediate(ArtMethod::MaxCounter()));
1089 __ j(kEqual, &overflow);
1090 __ addw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()),
1091 Immediate(1));
1092 __ Bind(&overflow);
1093 if (!is_frame_entry) {
1094 __ popl(EAX);
Vladimir Markodec78172020-06-19 15:31:23 +01001095 __ cfi().AdjustCFAOffset(-4);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001096 }
1097 }
1098
1099 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
1100 ScopedObjectAccess soa(Thread::Current());
1101 ProfilingInfo* info = GetGraph()->GetArtMethod()->GetProfilingInfo(kRuntimePointerSize);
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
Calin Juravle175dc732015-08-25 15:42:32 +01001442void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1443 DCHECK(location.IsRegister());
1444 __ movl(location.AsRegister<Register>(), Immediate(value));
1445}
1446
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001447void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001448 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001449 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1450 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1451 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001452 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001453 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001454 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001455 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001456}
1457
1458void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1459 if (location.IsRegister()) {
1460 locations->AddTemp(location);
1461 } else if (location.IsRegisterPair()) {
1462 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1463 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1464 } else {
1465 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1466 }
1467}
1468
David Brazdilfc6a86a2015-06-26 10:33:45 +00001469void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001470 if (successor->IsExitBlock()) {
1471 DCHECK(got->GetPrevious()->AlwaysThrows());
1472 return; // no code needed
1473 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001474
1475 HBasicBlock* block = got->GetBlock();
1476 HInstruction* previous = got->GetPrevious();
1477
1478 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001479 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001480 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001481 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1482 return;
1483 }
1484
1485 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1486 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1487 }
1488 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001489 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001490 }
1491}
1492
David Brazdilfc6a86a2015-06-26 10:33:45 +00001493void LocationsBuilderX86::VisitGoto(HGoto* got) {
1494 got->SetLocations(nullptr);
1495}
1496
1497void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1498 HandleGoto(got, got->GetSuccessor());
1499}
1500
1501void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1502 try_boundary->SetLocations(nullptr);
1503}
1504
1505void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1506 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1507 if (!successor->IsExitBlock()) {
1508 HandleGoto(try_boundary, successor);
1509 }
1510}
1511
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001512void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001513 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001514}
1515
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001516void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001517}
1518
Mark Mendell152408f2015-12-31 12:28:50 -05001519template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001520void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001521 LabelType* true_label,
1522 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001523 if (cond->IsFPConditionTrueIfNaN()) {
1524 __ j(kUnordered, true_label);
1525 } else if (cond->IsFPConditionFalseIfNaN()) {
1526 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001527 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001528 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001529}
1530
Mark Mendell152408f2015-12-31 12:28:50 -05001531template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001532void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001533 LabelType* true_label,
1534 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001535 LocationSummary* locations = cond->GetLocations();
1536 Location left = locations->InAt(0);
1537 Location right = locations->InAt(1);
1538 IfCondition if_cond = cond->GetCondition();
1539
Mark Mendellc4701932015-04-10 13:18:51 -04001540 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001541 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001542 IfCondition true_high_cond = if_cond;
1543 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001544 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001545
1546 // Set the conditions for the test, remembering that == needs to be
1547 // decided using the low words.
1548 switch (if_cond) {
1549 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001550 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001551 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001552 break;
1553 case kCondLT:
1554 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001555 break;
1556 case kCondLE:
1557 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001558 break;
1559 case kCondGT:
1560 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001561 break;
1562 case kCondGE:
1563 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001564 break;
Aart Bike9f37602015-10-09 11:15:55 -07001565 case kCondB:
1566 false_high_cond = kCondA;
1567 break;
1568 case kCondBE:
1569 true_high_cond = kCondB;
1570 break;
1571 case kCondA:
1572 false_high_cond = kCondB;
1573 break;
1574 case kCondAE:
1575 true_high_cond = kCondA;
1576 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001577 }
1578
1579 if (right.IsConstant()) {
1580 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001581 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001582 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001583
Aart Bika19616e2016-02-01 18:57:58 -08001584 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001585 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001586 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001587 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001588 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001589 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001590 __ j(X86Condition(true_high_cond), true_label);
1591 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001592 }
1593 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001594 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001595 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001596 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001597 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001598
1599 __ cmpl(left_high, right_high);
1600 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001601 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001602 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001603 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001604 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001605 __ j(X86Condition(true_high_cond), true_label);
1606 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001607 }
1608 // Must be equal high, so compare the lows.
1609 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001610 } else {
1611 DCHECK(right.IsDoubleStackSlot());
1612 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1613 if (if_cond == kCondNE) {
1614 __ j(X86Condition(true_high_cond), true_label);
1615 } else if (if_cond == kCondEQ) {
1616 __ j(X86Condition(false_high_cond), false_label);
1617 } else {
1618 __ j(X86Condition(true_high_cond), true_label);
1619 __ j(X86Condition(false_high_cond), false_label);
1620 }
1621 // Must be equal high, so compare the lows.
1622 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001623 }
1624 // The last comparison might be unsigned.
1625 __ j(final_condition, true_label);
1626}
1627
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001628void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1629 Location rhs,
1630 HInstruction* insn,
1631 bool is_double) {
1632 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1633 if (is_double) {
1634 if (rhs.IsFpuRegister()) {
1635 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1636 } else if (const_area != nullptr) {
1637 DCHECK(const_area->IsEmittedAtUseSite());
1638 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1639 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001640 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1641 const_area->GetBaseMethodAddress(),
1642 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001643 } else {
1644 DCHECK(rhs.IsDoubleStackSlot());
1645 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1646 }
1647 } else {
1648 if (rhs.IsFpuRegister()) {
1649 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1650 } else if (const_area != nullptr) {
1651 DCHECK(const_area->IsEmittedAtUseSite());
1652 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1653 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001654 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1655 const_area->GetBaseMethodAddress(),
1656 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001657 } else {
1658 DCHECK(rhs.IsStackSlot());
1659 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1660 }
1661 }
1662}
1663
Mark Mendell152408f2015-12-31 12:28:50 -05001664template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001665void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001666 LabelType* true_target_in,
1667 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001668 // Generated branching requires both targets to be explicit. If either of the
1669 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001670 LabelType fallthrough_target;
1671 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1672 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001673
Mark Mendellc4701932015-04-10 13:18:51 -04001674 LocationSummary* locations = condition->GetLocations();
1675 Location left = locations->InAt(0);
1676 Location right = locations->InAt(1);
1677
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001678 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001679 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001680 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001681 GenerateLongComparesAndJumps(condition, true_target, false_target);
1682 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001683 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001684 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001685 GenerateFPJumps(condition, true_target, false_target);
1686 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001687 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001688 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001689 GenerateFPJumps(condition, true_target, false_target);
1690 break;
1691 default:
1692 LOG(FATAL) << "Unexpected compare type " << type;
1693 }
1694
David Brazdil0debae72015-11-12 18:37:00 +00001695 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001696 __ jmp(false_target);
1697 }
David Brazdil0debae72015-11-12 18:37:00 +00001698
1699 if (fallthrough_target.IsLinked()) {
1700 __ Bind(&fallthrough_target);
1701 }
Mark Mendellc4701932015-04-10 13:18:51 -04001702}
1703
David Brazdil0debae72015-11-12 18:37:00 +00001704static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1705 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1706 // are set only strictly before `branch`. We can't use the eflags on long/FP
1707 // conditions if they are materialized due to the complex branching.
1708 return cond->IsCondition() &&
1709 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001710 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1711 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001712}
1713
Mark Mendell152408f2015-12-31 12:28:50 -05001714template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001715void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001716 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001717 LabelType* true_target,
1718 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001719 HInstruction* cond = instruction->InputAt(condition_input_index);
1720
1721 if (true_target == nullptr && false_target == nullptr) {
1722 // Nothing to do. The code always falls through.
1723 return;
1724 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001725 // Constant condition, statically compared against "true" (integer value 1).
1726 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001727 if (true_target != nullptr) {
1728 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001729 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001730 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001731 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001732 if (false_target != nullptr) {
1733 __ jmp(false_target);
1734 }
1735 }
1736 return;
1737 }
1738
1739 // The following code generates these patterns:
1740 // (1) true_target == nullptr && false_target != nullptr
1741 // - opposite condition true => branch to false_target
1742 // (2) true_target != nullptr && false_target == nullptr
1743 // - condition true => branch to true_target
1744 // (3) true_target != nullptr && false_target != nullptr
1745 // - condition true => branch to true_target
1746 // - branch to false_target
1747 if (IsBooleanValueOrMaterializedCondition(cond)) {
1748 if (AreEflagsSetFrom(cond, instruction)) {
1749 if (true_target == nullptr) {
1750 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1751 } else {
1752 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1753 }
1754 } else {
1755 // Materialized condition, compare against 0.
1756 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1757 if (lhs.IsRegister()) {
1758 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1759 } else {
1760 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1761 }
1762 if (true_target == nullptr) {
1763 __ j(kEqual, false_target);
1764 } else {
1765 __ j(kNotEqual, true_target);
1766 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001767 }
1768 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001769 // Condition has not been materialized, use its inputs as the comparison and
1770 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001771 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001772
1773 // If this is a long or FP comparison that has been folded into
1774 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001775 DataType::Type type = condition->InputAt(0)->GetType();
1776 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001777 GenerateCompareTestAndBranch(condition, true_target, false_target);
1778 return;
1779 }
1780
1781 Location lhs = condition->GetLocations()->InAt(0);
1782 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001783 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001784 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001785 if (true_target == nullptr) {
1786 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1787 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001788 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001789 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001790 }
David Brazdil0debae72015-11-12 18:37:00 +00001791
1792 // If neither branch falls through (case 3), the conditional branch to `true_target`
1793 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1794 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001795 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001796 }
1797}
1798
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001799void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001800 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001801 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001802 locations->SetInAt(0, Location::Any());
1803 }
1804}
1805
1806void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001807 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1808 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1809 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1810 nullptr : codegen_->GetLabelOf(true_successor);
1811 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1812 nullptr : codegen_->GetLabelOf(false_successor);
Andreas Gampe3db70682018-12-26 15:12:03 -08001813 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001814}
1815
1816void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001817 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001818 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001819 InvokeRuntimeCallingConvention calling_convention;
1820 RegisterSet caller_saves = RegisterSet::Empty();
1821 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1822 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001823 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001824 locations->SetInAt(0, Location::Any());
1825 }
1826}
1827
1828void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001829 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001830 GenerateTestAndBranch<Label>(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08001831 /* condition_input_index= */ 0,
David Brazdil74eb1b22015-12-14 11:44:01 +00001832 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08001833 /* false_target= */ nullptr);
David Brazdil74eb1b22015-12-14 11:44:01 +00001834}
1835
Mingyao Yang063fc772016-08-02 11:02:54 -07001836void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001837 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001838 LocationSummary(flag, LocationSummary::kNoCall);
1839 locations->SetOut(Location::RequiresRegister());
1840}
1841
1842void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1843 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1844 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1845}
1846
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001847static bool SelectCanUseCMOV(HSelect* select) {
1848 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001849 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001850 return false;
1851 }
1852
1853 // A FP condition doesn't generate the single CC that we need.
1854 // In 32 bit mode, a long condition doesn't generate a single CC either.
1855 HInstruction* condition = select->GetCondition();
1856 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001857 DataType::Type compare_type = condition->InputAt(0)->GetType();
1858 if (compare_type == DataType::Type::kInt64 ||
1859 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001860 return false;
1861 }
1862 }
1863
1864 // We can generate a CMOV for this Select.
1865 return true;
1866}
1867
David Brazdil74eb1b22015-12-14 11:44:01 +00001868void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001869 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001870 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001871 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001872 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001873 } else {
1874 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001875 if (SelectCanUseCMOV(select)) {
1876 if (select->InputAt(1)->IsConstant()) {
1877 // Cmov can't handle a constant value.
1878 locations->SetInAt(1, Location::RequiresRegister());
1879 } else {
1880 locations->SetInAt(1, Location::Any());
1881 }
1882 } else {
1883 locations->SetInAt(1, Location::Any());
1884 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001885 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001886 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1887 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001888 }
1889 locations->SetOut(Location::SameAsFirstInput());
1890}
1891
1892void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1893 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001894 DCHECK(locations->InAt(0).Equals(locations->Out()));
1895 if (SelectCanUseCMOV(select)) {
1896 // If both the condition and the source types are integer, we can generate
1897 // a CMOV to implement Select.
1898
1899 HInstruction* select_condition = select->GetCondition();
1900 Condition cond = kNotEqual;
1901
1902 // Figure out how to test the 'condition'.
1903 if (select_condition->IsCondition()) {
1904 HCondition* condition = select_condition->AsCondition();
1905 if (!condition->IsEmittedAtUseSite()) {
1906 // This was a previously materialized condition.
1907 // Can we use the existing condition code?
1908 if (AreEflagsSetFrom(condition, select)) {
1909 // Materialization was the previous instruction. Condition codes are right.
1910 cond = X86Condition(condition->GetCondition());
1911 } else {
1912 // No, we have to recreate the condition code.
1913 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1914 __ testl(cond_reg, cond_reg);
1915 }
1916 } else {
1917 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001918 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
1919 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001920 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001921 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001922 cond = X86Condition(condition->GetCondition());
1923 }
1924 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001925 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001926 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1927 __ testl(cond_reg, cond_reg);
1928 }
1929
1930 // If the condition is true, overwrite the output, which already contains false.
1931 Location false_loc = locations->InAt(0);
1932 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001933 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001934 // 64 bit conditional move.
1935 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1936 Register false_low = false_loc.AsRegisterPairLow<Register>();
1937 if (true_loc.IsRegisterPair()) {
1938 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1939 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1940 } else {
1941 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1942 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1943 }
1944 } else {
1945 // 32 bit conditional move.
1946 Register false_reg = false_loc.AsRegister<Register>();
1947 if (true_loc.IsRegister()) {
1948 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1949 } else {
1950 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1951 }
1952 }
1953 } else {
1954 NearLabel false_target;
1955 GenerateTestAndBranch<NearLabel>(
Andreas Gampe3db70682018-12-26 15:12:03 -08001956 select, /* condition_input_index= */ 2, /* true_target= */ nullptr, &false_target);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001957 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1958 __ Bind(&false_target);
1959 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001960}
1961
David Srbecky0cf44932015-12-09 14:09:59 +00001962void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001963 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00001964}
1965
David Srbeckyd28f4a02016-03-14 17:14:24 +00001966void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1967 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001968}
1969
Vladimir Markodec78172020-06-19 15:31:23 +01001970void CodeGeneratorX86::IncreaseFrame(size_t adjustment) {
1971 __ subl(ESP, Immediate(adjustment));
1972 __ cfi().AdjustCFAOffset(adjustment);
1973}
1974
1975void CodeGeneratorX86::DecreaseFrame(size_t adjustment) {
1976 __ addl(ESP, Immediate(adjustment));
1977 __ cfi().AdjustCFAOffset(-adjustment);
1978}
1979
David Srbeckyc7098ff2016-02-09 14:30:11 +00001980void CodeGeneratorX86::GenerateNop() {
1981 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001982}
1983
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001984void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001985 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001986 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001987 // Handle the long/FP comparisons made in instruction simplification.
1988 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001989 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001990 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001991 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001992 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001993 locations->SetOut(Location::RequiresRegister());
1994 }
1995 break;
1996 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001997 case DataType::Type::kFloat32:
1998 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001999 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002000 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
2001 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
2002 } else if (cond->InputAt(1)->IsConstant()) {
2003 locations->SetInAt(1, Location::RequiresFpuRegister());
2004 } else {
2005 locations->SetInAt(1, Location::Any());
2006 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002007 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002008 locations->SetOut(Location::RequiresRegister());
2009 }
2010 break;
2011 }
2012 default:
2013 locations->SetInAt(0, Location::RequiresRegister());
2014 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00002015 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002016 // We need a byte register.
2017 locations->SetOut(Location::RegisterLocation(ECX));
2018 }
2019 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002020 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002021}
2022
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002023void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002024 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002025 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002026 }
Mark Mendellc4701932015-04-10 13:18:51 -04002027
2028 LocationSummary* locations = cond->GetLocations();
2029 Location lhs = locations->InAt(0);
2030 Location rhs = locations->InAt(1);
2031 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05002032 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002033
2034 switch (cond->InputAt(0)->GetType()) {
2035 default: {
2036 // Integer case.
2037
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002038 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04002039 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01002040 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07002041 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04002042 return;
2043 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002044 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04002045 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2046 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002047 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002048 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04002049 GenerateFPJumps(cond, &true_label, &false_label);
2050 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002051 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002052 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04002053 GenerateFPJumps(cond, &true_label, &false_label);
2054 break;
2055 }
2056
2057 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002058 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002059
Roland Levillain4fa13f62015-07-06 18:11:54 +01002060 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04002061 __ Bind(&false_label);
2062 __ xorl(reg, reg);
2063 __ jmp(&done_label);
2064
Roland Levillain4fa13f62015-07-06 18:11:54 +01002065 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04002066 __ Bind(&true_label);
2067 __ movl(reg, Immediate(1));
2068 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002069}
2070
2071void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002072 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002073}
2074
2075void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002076 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002077}
2078
2079void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002080 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002081}
2082
2083void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002084 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002085}
2086
2087void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002088 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002089}
2090
2091void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002092 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002093}
2094
2095void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002096 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002097}
2098
2099void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002100 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002101}
2102
2103void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002104 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002105}
2106
2107void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002108 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002109}
2110
2111void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002112 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002113}
2114
2115void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002116 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002117}
2118
Aart Bike9f37602015-10-09 11:15:55 -07002119void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002120 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002121}
2122
2123void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002124 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002125}
2126
2127void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002128 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002129}
2130
2131void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002132 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002133}
2134
2135void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002136 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002137}
2138
2139void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002140 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002141}
2142
2143void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002144 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002145}
2146
2147void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002148 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002149}
2150
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002151void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002152 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002153 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002154 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002155}
2156
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002157void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002158 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002159}
2160
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002161void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2162 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002163 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002164 locations->SetOut(Location::ConstantLocation(constant));
2165}
2166
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002167void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002168 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002169}
2170
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002171void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002172 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002173 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002174 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002175}
2176
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002177void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002178 // Will be generated at use site.
2179}
2180
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002181void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2182 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002183 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002184 locations->SetOut(Location::ConstantLocation(constant));
2185}
2186
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002187void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002188 // Will be generated at use site.
2189}
2190
2191void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2192 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002193 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002194 locations->SetOut(Location::ConstantLocation(constant));
2195}
2196
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002197void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002198 // Will be generated at use site.
2199}
2200
Igor Murashkind01745e2017-04-05 16:40:31 -07002201void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2202 constructor_fence->SetLocations(nullptr);
2203}
2204
2205void InstructionCodeGeneratorX86::VisitConstructorFence(
2206 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2207 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2208}
2209
Calin Juravle27df7582015-04-17 19:12:31 +01002210void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2211 memory_barrier->SetLocations(nullptr);
2212}
2213
2214void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002215 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002216}
2217
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002218void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002219 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002220}
2221
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002222void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002223 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002224}
2225
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002226void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002227 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002228 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002229 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002230 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002231 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002232 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002233 case DataType::Type::kInt8:
2234 case DataType::Type::kUint16:
2235 case DataType::Type::kInt16:
2236 case DataType::Type::kInt32:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002237 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002238 break;
2239
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002240 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002241 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002242 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002243 break;
2244
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002245 case DataType::Type::kFloat32:
2246 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002247 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002248 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002249 break;
2250
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002251 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002252 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002253 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002254}
2255
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002256void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002257 switch (ret->InputAt(0)->GetType()) {
2258 case DataType::Type::kReference:
2259 case DataType::Type::kBool:
2260 case DataType::Type::kUint8:
2261 case DataType::Type::kInt8:
2262 case DataType::Type::kUint16:
2263 case DataType::Type::kInt16:
2264 case DataType::Type::kInt32:
2265 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
2266 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002267
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002268 case DataType::Type::kInt64:
2269 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2270 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
2271 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002272
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002273 case DataType::Type::kFloat32:
2274 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2275 if (GetGraph()->IsCompilingOsr()) {
2276 // To simplify callers of an OSR method, we put the return value in both
2277 // floating point and core registers.
2278 __ movd(EAX, XMM0);
2279 }
2280 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002281
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002282 case DataType::Type::kFloat64:
2283 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2284 if (GetGraph()->IsCompilingOsr()) {
2285 // To simplify callers of an OSR method, we put the return value in both
2286 // floating point and core registers.
2287 __ movd(EAX, XMM0);
2288 // Use XMM1 as temporary register to not clobber XMM0.
2289 __ movaps(XMM1, XMM0);
2290 __ psrlq(XMM1, Immediate(32));
2291 __ movd(EDX, XMM1);
2292 }
2293 break;
2294
2295 default:
2296 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002297 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002298 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002299}
2300
Calin Juravle175dc732015-08-25 15:42:32 +01002301void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2302 // The trampoline uses the same calling convention as dex calling conventions,
2303 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2304 // the method_idx.
2305 HandleInvoke(invoke);
2306}
2307
2308void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2309 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2310}
2311
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002312void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002313 // Explicit clinit checks triggered by static invokes must have been pruned by
2314 // art::PrepareForRegisterAllocation.
2315 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002316
Mark Mendellfb8d2792015-03-31 22:16:59 -04002317 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002318 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002319 if (invoke->GetLocations()->CanCall() &&
2320 invoke->HasPcRelativeMethodLoadKind() &&
2321 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).IsInvalid()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002322 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002323 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002324 return;
2325 }
2326
Vladimir Marko86c87522020-05-11 16:55:55 +01002327 if (invoke->GetCodePtrLocation() == HInvokeStaticOrDirect::CodePtrLocation::kCallCriticalNative) {
2328 CriticalNativeCallingConventionVisitorX86 calling_convention_visitor(
2329 /*for_register_allocation=*/ true);
2330 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2331 } else {
2332 HandleInvoke(invoke);
2333 }
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002334
Vladimir Marko86c87522020-05-11 16:55:55 +01002335 // For PC-relative load kinds the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002336 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002337 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002338 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002339}
2340
Mark Mendell09ed1a32015-03-25 08:30:06 -04002341static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2342 if (invoke->GetLocations()->Intrinsified()) {
2343 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2344 intrinsic.Dispatch(invoke);
2345 return true;
2346 }
2347 return false;
2348}
2349
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002350void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002351 // Explicit clinit checks triggered by static invokes must have been pruned by
2352 // art::PrepareForRegisterAllocation.
2353 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002354
Mark Mendell09ed1a32015-03-25 08:30:06 -04002355 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2356 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002357 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002358
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002359 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002360 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002361 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002362}
2363
2364void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002365 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2366 if (intrinsic.TryDispatch(invoke)) {
2367 return;
2368 }
2369
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002370 HandleInvoke(invoke);
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002371
2372 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002373 // Add one temporary for inline cache update.
2374 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2375 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002376}
2377
2378void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002379 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002380 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002381}
2382
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002383void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002384 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2385 return;
2386 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002387
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002388 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002389 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002390}
2391
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002392void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002393 // This call to HandleInvoke allocates a temporary (core) register
2394 // which is also used to transfer the hidden argument from FP to
2395 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002396 HandleInvoke(invoke);
2397 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002398 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002399
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002400 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002401 // Add one temporary for inline cache update.
2402 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2403 }
2404}
2405
2406void CodeGeneratorX86::MaybeGenerateInlineCacheCheck(HInstruction* instruction, Register klass) {
2407 DCHECK_EQ(EAX, klass);
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002408 // We know the destination of an intrinsic, so no need to record inline
2409 // caches (also the intrinsic location builder doesn't request an additional
2410 // temporary).
2411 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002412 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002413 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002414 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
2415 ScopedObjectAccess soa(Thread::Current());
2416 ProfilingInfo* info = GetGraph()->GetArtMethod()->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002417 if (info != nullptr) {
2418 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
2419 uint32_t address = reinterpret_cast32<uint32_t>(cache);
2420 if (kIsDebugBuild) {
2421 uint32_t temp_index = instruction->GetLocations()->GetTempCount() - 1u;
2422 CHECK_EQ(EBP, instruction->GetLocations()->GetTemp(temp_index).AsRegister<Register>());
2423 }
2424 Register temp = EBP;
2425 NearLabel done;
2426 __ movl(temp, Immediate(address));
2427 // Fast path for a monomorphic cache.
2428 __ cmpl(klass, Address(temp, InlineCache::ClassesOffset().Int32Value()));
2429 __ j(kEqual, &done);
2430 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(kQuickUpdateInlineCache).Int32Value());
2431 __ Bind(&done);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002432 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002433 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002434}
2435
2436void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2437 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002438 LocationSummary* locations = invoke->GetLocations();
2439 Register temp = locations->GetTemp(0).AsRegister<Register>();
2440 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002441 Location receiver = locations->InAt(0);
2442 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2443
Roland Levillain0d5a2812015-11-13 10:07:31 +00002444 // Set the hidden argument. This is safe to do this here, as XMM7
2445 // won't be modified thereafter, before the `call` instruction.
2446 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002447 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002448 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002449
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002450 if (receiver.IsStackSlot()) {
2451 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002452 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002453 __ movl(temp, Address(temp, class_offset));
2454 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002455 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002456 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002457 }
Roland Levillain4d027112015-07-01 15:41:14 +01002458 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002459 // Instead of simply (possibly) unpoisoning `temp` here, we should
2460 // emit a read barrier for the previous class reference load.
2461 // However this is not required in practice, as this is an
2462 // intermediate/temporary reference and because the current
2463 // concurrent copying collector keeps the from-space memory
2464 // intact/accessible until the end of the marking phase (the
2465 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002466 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002467
2468 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
2469
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002470 // temp = temp->GetAddressOfIMT()
2471 __ movl(temp,
2472 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002473 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002474 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002475 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002476 __ movl(temp, Address(temp, method_offset));
2477 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002478 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002479 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002480
2481 DCHECK(!codegen_->IsLeafMethod());
2482 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2483}
2484
Orion Hodsonac141392017-01-13 11:53:47 +00002485void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2486 HandleInvoke(invoke);
2487}
2488
2489void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2490 codegen_->GenerateInvokePolymorphicCall(invoke);
2491}
2492
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002493void LocationsBuilderX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2494 HandleInvoke(invoke);
2495}
2496
2497void InstructionCodeGeneratorX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2498 codegen_->GenerateInvokeCustomCall(invoke);
2499}
2500
Roland Levillain88cb1752014-10-20 16:36:47 +01002501void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2502 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002503 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002504 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002505 case DataType::Type::kInt32:
2506 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002507 locations->SetInAt(0, Location::RequiresRegister());
2508 locations->SetOut(Location::SameAsFirstInput());
2509 break;
2510
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002511 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002512 locations->SetInAt(0, Location::RequiresFpuRegister());
2513 locations->SetOut(Location::SameAsFirstInput());
2514 locations->AddTemp(Location::RequiresRegister());
2515 locations->AddTemp(Location::RequiresFpuRegister());
2516 break;
2517
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002518 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002519 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002520 locations->SetOut(Location::SameAsFirstInput());
2521 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002522 break;
2523
2524 default:
2525 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2526 }
2527}
2528
2529void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2530 LocationSummary* locations = neg->GetLocations();
2531 Location out = locations->Out();
2532 Location in = locations->InAt(0);
2533 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002534 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002535 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002536 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002537 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002538 break;
2539
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002540 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002541 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002542 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002543 __ negl(out.AsRegisterPairLow<Register>());
2544 // Negation is similar to subtraction from zero. The least
2545 // significant byte triggers a borrow when it is different from
2546 // zero; to take it into account, add 1 to the most significant
2547 // byte if the carry flag (CF) is set to 1 after the first NEGL
2548 // operation.
2549 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2550 __ negl(out.AsRegisterPairHigh<Register>());
2551 break;
2552
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002553 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002554 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002555 Register constant = locations->GetTemp(0).AsRegister<Register>();
2556 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002557 // Implement float negation with an exclusive or with value
2558 // 0x80000000 (mask for bit 31, representing the sign of a
2559 // single-precision floating-point number).
2560 __ movl(constant, Immediate(INT32_C(0x80000000)));
2561 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002562 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002563 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002564 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002565
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002566 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002567 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002568 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002569 // Implement double negation with an exclusive or with value
2570 // 0x8000000000000000 (mask for bit 63, representing the sign of
2571 // a double-precision floating-point number).
2572 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002573 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002574 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002575 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002576
2577 default:
2578 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2579 }
2580}
2581
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002582void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2583 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002584 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002585 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002586 locations->SetInAt(0, Location::RequiresFpuRegister());
2587 locations->SetInAt(1, Location::RequiresRegister());
2588 locations->SetOut(Location::SameAsFirstInput());
2589 locations->AddTemp(Location::RequiresFpuRegister());
2590}
2591
2592void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2593 LocationSummary* locations = neg->GetLocations();
2594 Location out = locations->Out();
2595 DCHECK(locations->InAt(0).Equals(out));
2596
2597 Register constant_area = locations->InAt(1).AsRegister<Register>();
2598 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002599 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002600 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2601 neg->GetBaseMethodAddress(),
2602 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002603 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2604 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002605 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2606 neg->GetBaseMethodAddress(),
2607 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002608 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2609 }
2610}
2611
Roland Levillaindff1f282014-11-05 14:15:05 +00002612void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002613 DataType::Type result_type = conversion->GetResultType();
2614 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002615 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2616 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002617
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002618 // The float-to-long and double-to-long type conversions rely on a
2619 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002620 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002621 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2622 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002623 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002624 : LocationSummary::kNoCall;
2625 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002626 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002627
Roland Levillaindff1f282014-11-05 14:15:05 +00002628 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002629 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002630 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002631 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002632 case DataType::Type::kUint8:
2633 case DataType::Type::kInt8:
2634 case DataType::Type::kUint16:
2635 case DataType::Type::kInt16:
2636 case DataType::Type::kInt32:
2637 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2638 // Make the output overlap to please the register allocator. This greatly simplifies
2639 // the validation of the linear scan implementation
2640 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2641 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002642 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002643 HInstruction* input = conversion->InputAt(0);
2644 Location input_location = input->IsConstant()
2645 ? Location::ConstantLocation(input->AsConstant())
2646 : Location::RegisterPairLocation(EAX, EDX);
2647 locations->SetInAt(0, input_location);
2648 // Make the output overlap to please the register allocator. This greatly simplifies
2649 // the validation of the linear scan implementation
2650 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2651 break;
2652 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002653
2654 default:
2655 LOG(FATAL) << "Unexpected type conversion from " << input_type
2656 << " to " << result_type;
2657 }
2658 break;
2659
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002660 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002661 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002662 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2663 locations->SetInAt(0, Location::Any());
2664 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002665 break;
2666
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002667 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002668 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002669 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002670 locations->SetInAt(0, Location::Any());
2671 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2672 break;
2673
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002674 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002675 locations->SetInAt(0, Location::RequiresFpuRegister());
2676 locations->SetOut(Location::RequiresRegister());
2677 locations->AddTemp(Location::RequiresFpuRegister());
2678 break;
2679
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002680 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002681 locations->SetInAt(0, Location::RequiresFpuRegister());
2682 locations->SetOut(Location::RequiresRegister());
2683 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002684 break;
2685
2686 default:
2687 LOG(FATAL) << "Unexpected type conversion from " << input_type
2688 << " to " << result_type;
2689 }
2690 break;
2691
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002692 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002693 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002694 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002695 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002696 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002697 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002698 case DataType::Type::kInt16:
2699 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002700 locations->SetInAt(0, Location::RegisterLocation(EAX));
2701 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2702 break;
2703
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002704 case DataType::Type::kFloat32:
2705 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002706 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002707 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2708 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2709
Vladimir Marko949c91f2015-01-27 10:48:44 +00002710 // The runtime helper puts the result in EAX, EDX.
2711 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002712 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002713 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002714
2715 default:
2716 LOG(FATAL) << "Unexpected type conversion from " << input_type
2717 << " to " << result_type;
2718 }
2719 break;
2720
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002721 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002722 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002723 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002724 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002725 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002726 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002727 case DataType::Type::kInt16:
2728 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002729 locations->SetInAt(0, Location::RequiresRegister());
2730 locations->SetOut(Location::RequiresFpuRegister());
2731 break;
2732
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002733 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002734 locations->SetInAt(0, Location::Any());
2735 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002736 break;
2737
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002738 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002739 locations->SetInAt(0, Location::RequiresFpuRegister());
2740 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002741 break;
2742
2743 default:
2744 LOG(FATAL) << "Unexpected type conversion from " << input_type
2745 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002746 }
Roland Levillaincff13742014-11-17 14:32:17 +00002747 break;
2748
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002749 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002750 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002751 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002752 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002753 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002754 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002755 case DataType::Type::kInt16:
2756 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002757 locations->SetInAt(0, Location::RequiresRegister());
2758 locations->SetOut(Location::RequiresFpuRegister());
2759 break;
2760
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002761 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002762 locations->SetInAt(0, Location::Any());
2763 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002764 break;
2765
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002766 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002767 locations->SetInAt(0, Location::RequiresFpuRegister());
2768 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002769 break;
2770
2771 default:
2772 LOG(FATAL) << "Unexpected type conversion from " << input_type
2773 << " to " << result_type;
2774 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002775 break;
2776
2777 default:
2778 LOG(FATAL) << "Unexpected type conversion from " << input_type
2779 << " to " << result_type;
2780 }
2781}
2782
2783void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2784 LocationSummary* locations = conversion->GetLocations();
2785 Location out = locations->Out();
2786 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002787 DataType::Type result_type = conversion->GetResultType();
2788 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002789 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2790 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002791 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002792 case DataType::Type::kUint8:
2793 switch (input_type) {
2794 case DataType::Type::kInt8:
2795 case DataType::Type::kUint16:
2796 case DataType::Type::kInt16:
2797 case DataType::Type::kInt32:
2798 if (in.IsRegister()) {
2799 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2800 } else {
2801 DCHECK(in.GetConstant()->IsIntConstant());
2802 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2803 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2804 }
2805 break;
2806 case DataType::Type::kInt64:
2807 if (in.IsRegisterPair()) {
2808 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2809 } else {
2810 DCHECK(in.GetConstant()->IsLongConstant());
2811 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2812 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2813 }
2814 break;
2815
2816 default:
2817 LOG(FATAL) << "Unexpected type conversion from " << input_type
2818 << " to " << result_type;
2819 }
2820 break;
2821
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002822 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002823 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002824 case DataType::Type::kUint8:
2825 case DataType::Type::kUint16:
2826 case DataType::Type::kInt16:
2827 case DataType::Type::kInt32:
2828 if (in.IsRegister()) {
2829 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2830 } else {
2831 DCHECK(in.GetConstant()->IsIntConstant());
2832 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2833 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2834 }
2835 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002836 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002837 if (in.IsRegisterPair()) {
2838 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2839 } else {
2840 DCHECK(in.GetConstant()->IsLongConstant());
2841 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2842 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2843 }
2844 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002845
2846 default:
2847 LOG(FATAL) << "Unexpected type conversion from " << input_type
2848 << " to " << result_type;
2849 }
2850 break;
2851
2852 case DataType::Type::kUint16:
2853 switch (input_type) {
2854 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002855 case DataType::Type::kInt16:
2856 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002857 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002858 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
2859 } else if (in.IsStackSlot()) {
2860 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002861 } else {
2862 DCHECK(in.GetConstant()->IsIntConstant());
2863 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002864 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2865 }
2866 break;
2867 case DataType::Type::kInt64:
2868 if (in.IsRegisterPair()) {
2869 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2870 } else if (in.IsDoubleStackSlot()) {
2871 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2872 } else {
2873 DCHECK(in.GetConstant()->IsLongConstant());
2874 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2875 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002876 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002877 break;
2878
2879 default:
2880 LOG(FATAL) << "Unexpected type conversion from " << input_type
2881 << " to " << result_type;
2882 }
2883 break;
2884
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002885 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002886 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002887 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002888 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00002889 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002890 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002891 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002892 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002893 } else {
2894 DCHECK(in.GetConstant()->IsIntConstant());
2895 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002896 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002897 }
2898 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002899 case DataType::Type::kInt64:
2900 if (in.IsRegisterPair()) {
2901 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2902 } else if (in.IsDoubleStackSlot()) {
2903 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2904 } else {
2905 DCHECK(in.GetConstant()->IsLongConstant());
2906 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2907 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2908 }
2909 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00002910
2911 default:
2912 LOG(FATAL) << "Unexpected type conversion from " << input_type
2913 << " to " << result_type;
2914 }
2915 break;
2916
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002917 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002918 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002919 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002920 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002921 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002922 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002923 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002924 } else {
2925 DCHECK(in.IsConstant());
2926 DCHECK(in.GetConstant()->IsLongConstant());
2927 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002928 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002929 }
2930 break;
2931
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002932 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002933 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2934 Register output = out.AsRegister<Register>();
2935 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002936 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002937
2938 __ movl(output, Immediate(kPrimIntMax));
2939 // temp = int-to-float(output)
2940 __ cvtsi2ss(temp, output);
2941 // if input >= temp goto done
2942 __ comiss(input, temp);
2943 __ j(kAboveEqual, &done);
2944 // if input == NaN goto nan
2945 __ j(kUnordered, &nan);
2946 // output = float-to-int-truncate(input)
2947 __ cvttss2si(output, input);
2948 __ jmp(&done);
2949 __ Bind(&nan);
2950 // output = 0
2951 __ xorl(output, output);
2952 __ Bind(&done);
2953 break;
2954 }
2955
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002956 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002957 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2958 Register output = out.AsRegister<Register>();
2959 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002960 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002961
2962 __ movl(output, Immediate(kPrimIntMax));
2963 // temp = int-to-double(output)
2964 __ cvtsi2sd(temp, output);
2965 // if input >= temp goto done
2966 __ comisd(input, temp);
2967 __ j(kAboveEqual, &done);
2968 // if input == NaN goto nan
2969 __ j(kUnordered, &nan);
2970 // output = double-to-int-truncate(input)
2971 __ cvttsd2si(output, input);
2972 __ jmp(&done);
2973 __ Bind(&nan);
2974 // output = 0
2975 __ xorl(output, output);
2976 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002977 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002978 }
Roland Levillain946e1432014-11-11 17:35:19 +00002979
2980 default:
2981 LOG(FATAL) << "Unexpected type conversion from " << input_type
2982 << " to " << result_type;
2983 }
2984 break;
2985
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002986 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002987 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002988 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002989 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002990 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002991 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002992 case DataType::Type::kInt16:
2993 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002994 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2995 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002996 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002997 __ cdq();
2998 break;
2999
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003000 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01003001 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003002 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00003003 break;
3004
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003005 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01003006 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003007 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00003008 break;
3009
3010 default:
3011 LOG(FATAL) << "Unexpected type conversion from " << input_type
3012 << " to " << result_type;
3013 }
3014 break;
3015
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003016 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00003017 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003018 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003019 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003020 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003021 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003022 case DataType::Type::kInt16:
3023 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003024 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003025 break;
3026
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003027 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003028 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00003029
Roland Levillain232ade02015-04-20 15:14:36 +01003030 // Create stack space for the call to
3031 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
3032 // TODO: enhance register allocator to ask for stack temporaries.
3033 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003034 adjustment = DataType::Size(DataType::Type::kInt64);
Vladimir Markodec78172020-06-19 15:31:23 +01003035 codegen_->IncreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003036 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003037
Roland Levillain232ade02015-04-20 15:14:36 +01003038 // Load the value to the FP stack, using temporaries if needed.
3039 PushOntoFPStack(in, 0, adjustment, false, true);
3040
3041 if (out.IsStackSlot()) {
3042 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
3043 } else {
3044 __ fstps(Address(ESP, 0));
3045 Location stack_temp = Location::StackSlot(0);
3046 codegen_->Move32(out, stack_temp);
3047 }
3048
3049 // Remove the temporary stack space we allocated.
3050 if (adjustment != 0) {
Vladimir Markodec78172020-06-19 15:31:23 +01003051 codegen_->DecreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003052 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003053 break;
3054 }
3055
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003056 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003057 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003058 break;
3059
3060 default:
3061 LOG(FATAL) << "Unexpected type conversion from " << input_type
3062 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003063 }
Roland Levillaincff13742014-11-17 14:32:17 +00003064 break;
3065
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003066 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00003067 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003068 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003069 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003070 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003071 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003072 case DataType::Type::kInt16:
3073 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003074 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003075 break;
3076
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003077 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003078 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00003079
Roland Levillain232ade02015-04-20 15:14:36 +01003080 // Create stack space for the call to
3081 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
3082 // TODO: enhance register allocator to ask for stack temporaries.
3083 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003084 adjustment = DataType::Size(DataType::Type::kInt64);
Vladimir Markodec78172020-06-19 15:31:23 +01003085 codegen_->IncreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003086 }
3087
3088 // Load the value to the FP stack, using temporaries if needed.
3089 PushOntoFPStack(in, 0, adjustment, false, true);
3090
3091 if (out.IsDoubleStackSlot()) {
3092 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
3093 } else {
3094 __ fstpl(Address(ESP, 0));
3095 Location stack_temp = Location::DoubleStackSlot(0);
3096 codegen_->Move64(out, stack_temp);
3097 }
3098
3099 // Remove the temporary stack space we allocated.
3100 if (adjustment != 0) {
Vladimir Markodec78172020-06-19 15:31:23 +01003101 codegen_->DecreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003102 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003103 break;
3104 }
3105
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003106 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003107 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003108 break;
3109
3110 default:
3111 LOG(FATAL) << "Unexpected type conversion from " << input_type
3112 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003113 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003114 break;
3115
3116 default:
3117 LOG(FATAL) << "Unexpected type conversion from " << input_type
3118 << " to " << result_type;
3119 }
3120}
3121
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003122void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003123 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003124 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003125 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003126 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05003127 locations->SetInAt(0, Location::RequiresRegister());
3128 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3129 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3130 break;
3131 }
3132
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003133 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003134 locations->SetInAt(0, Location::RequiresRegister());
3135 locations->SetInAt(1, Location::Any());
3136 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003137 break;
3138 }
3139
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003140 case DataType::Type::kFloat32:
3141 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003142 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003143 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3144 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003145 } else if (add->InputAt(1)->IsConstant()) {
3146 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003147 } else {
3148 locations->SetInAt(1, Location::Any());
3149 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003150 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003151 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003152 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003153
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003154 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003155 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Elliott Hughesc1896c92018-11-29 11:33:18 -08003156 UNREACHABLE();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003157 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003158}
3159
3160void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
3161 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003162 Location first = locations->InAt(0);
3163 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05003164 Location out = locations->Out();
3165
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003166 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003167 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003168 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003169 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3170 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003171 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3172 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05003173 } else {
3174 __ leal(out.AsRegister<Register>(), Address(
3175 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3176 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003177 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003178 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3179 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3180 __ addl(out.AsRegister<Register>(), Immediate(value));
3181 } else {
3182 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3183 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003184 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003185 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003186 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003187 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003188 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003189 }
3190
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003191 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003192 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003193 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3194 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003195 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003196 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3197 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003198 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003199 } else {
3200 DCHECK(second.IsConstant()) << second;
3201 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3202 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3203 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003204 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003205 break;
3206 }
3207
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003208 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003209 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003210 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003211 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3212 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003213 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003214 __ addss(first.AsFpuRegister<XmmRegister>(),
3215 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003216 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3217 const_area->GetBaseMethodAddress(),
3218 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003219 } else {
3220 DCHECK(second.IsStackSlot());
3221 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003222 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003223 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003224 }
3225
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003226 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003227 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003228 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003229 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3230 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003231 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003232 __ addsd(first.AsFpuRegister<XmmRegister>(),
3233 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003234 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3235 const_area->GetBaseMethodAddress(),
3236 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003237 } else {
3238 DCHECK(second.IsDoubleStackSlot());
3239 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003240 }
3241 break;
3242 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003243
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003244 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003245 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003246 }
3247}
3248
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003249void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003250 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003251 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003252 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003253 case DataType::Type::kInt32:
3254 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003255 locations->SetInAt(0, Location::RequiresRegister());
3256 locations->SetInAt(1, Location::Any());
3257 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003258 break;
3259 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003260 case DataType::Type::kFloat32:
3261 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003262 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003263 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3264 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003265 } else if (sub->InputAt(1)->IsConstant()) {
3266 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003267 } else {
3268 locations->SetInAt(1, Location::Any());
3269 }
Calin Juravle11351682014-10-23 15:38:15 +01003270 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003271 break;
Calin Juravle11351682014-10-23 15:38:15 +01003272 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003273
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003274 default:
Calin Juravle11351682014-10-23 15:38:15 +01003275 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003276 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003277}
3278
3279void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3280 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003281 Location first = locations->InAt(0);
3282 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003283 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003284 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003285 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003286 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003287 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003288 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003289 __ subl(first.AsRegister<Register>(),
3290 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003291 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003292 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003293 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003294 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003295 }
3296
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003297 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003298 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003299 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3300 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003301 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003302 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003303 __ sbbl(first.AsRegisterPairHigh<Register>(),
3304 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003305 } else {
3306 DCHECK(second.IsConstant()) << second;
3307 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3308 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3309 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003310 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003311 break;
3312 }
3313
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003314 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003315 if (second.IsFpuRegister()) {
3316 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3317 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3318 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003319 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003320 __ subss(first.AsFpuRegister<XmmRegister>(),
3321 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003322 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3323 const_area->GetBaseMethodAddress(),
3324 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003325 } else {
3326 DCHECK(second.IsStackSlot());
3327 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3328 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003329 break;
Calin Juravle11351682014-10-23 15:38:15 +01003330 }
3331
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003332 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003333 if (second.IsFpuRegister()) {
3334 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3335 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3336 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003337 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003338 __ subsd(first.AsFpuRegister<XmmRegister>(),
3339 codegen_->LiteralDoubleAddress(
3340 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003341 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003342 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3343 } else {
3344 DCHECK(second.IsDoubleStackSlot());
3345 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3346 }
Calin Juravle11351682014-10-23 15:38:15 +01003347 break;
3348 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003349
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003350 default:
Calin Juravle11351682014-10-23 15:38:15 +01003351 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003352 }
3353}
3354
Calin Juravle34bacdf2014-10-07 20:23:36 +01003355void LocationsBuilderX86::VisitMul(HMul* mul) {
3356 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003357 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003358 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003359 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003360 locations->SetInAt(0, Location::RequiresRegister());
3361 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003362 if (mul->InputAt(1)->IsIntConstant()) {
3363 // Can use 3 operand multiply.
3364 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3365 } else {
3366 locations->SetOut(Location::SameAsFirstInput());
3367 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003368 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003369 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003370 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003371 locations->SetInAt(1, Location::Any());
3372 locations->SetOut(Location::SameAsFirstInput());
3373 // Needed for imul on 32bits with 64bits output.
3374 locations->AddTemp(Location::RegisterLocation(EAX));
3375 locations->AddTemp(Location::RegisterLocation(EDX));
3376 break;
3377 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003378 case DataType::Type::kFloat32:
3379 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003380 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003381 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3382 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003383 } else if (mul->InputAt(1)->IsConstant()) {
3384 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003385 } else {
3386 locations->SetInAt(1, Location::Any());
3387 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003388 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003389 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003390 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003391
3392 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003393 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003394 }
3395}
3396
3397void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3398 LocationSummary* locations = mul->GetLocations();
3399 Location first = locations->InAt(0);
3400 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003401 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003402
3403 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003404 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003405 // The constant may have ended up in a register, so test explicitly to avoid
3406 // problems where the output may not be the same as the first operand.
3407 if (mul->InputAt(1)->IsIntConstant()) {
3408 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3409 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3410 } else if (second.IsRegister()) {
3411 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003412 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003413 } else {
3414 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003415 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003416 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003417 }
3418 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003419
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003420 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003421 Register in1_hi = first.AsRegisterPairHigh<Register>();
3422 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003423 Register eax = locations->GetTemp(0).AsRegister<Register>();
3424 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003425
3426 DCHECK_EQ(EAX, eax);
3427 DCHECK_EQ(EDX, edx);
3428
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003429 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003430 // output: in1
3431 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3432 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3433 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003434 if (second.IsConstant()) {
3435 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003436
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003437 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3438 int32_t low_value = Low32Bits(value);
3439 int32_t high_value = High32Bits(value);
3440 Immediate low(low_value);
3441 Immediate high(high_value);
3442
3443 __ movl(eax, high);
3444 // eax <- in1.lo * in2.hi
3445 __ imull(eax, in1_lo);
3446 // in1.hi <- in1.hi * in2.lo
3447 __ imull(in1_hi, low);
3448 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3449 __ addl(in1_hi, eax);
3450 // move in2_lo to eax to prepare for double precision
3451 __ movl(eax, low);
3452 // edx:eax <- in1.lo * in2.lo
3453 __ mull(in1_lo);
3454 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3455 __ addl(in1_hi, edx);
3456 // in1.lo <- (in1.lo * in2.lo)[31:0];
3457 __ movl(in1_lo, eax);
3458 } else if (second.IsRegisterPair()) {
3459 Register in2_hi = second.AsRegisterPairHigh<Register>();
3460 Register in2_lo = second.AsRegisterPairLow<Register>();
3461
3462 __ movl(eax, in2_hi);
3463 // eax <- in1.lo * in2.hi
3464 __ imull(eax, in1_lo);
3465 // in1.hi <- in1.hi * in2.lo
3466 __ imull(in1_hi, in2_lo);
3467 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3468 __ addl(in1_hi, eax);
3469 // move in1_lo to eax to prepare for double precision
3470 __ movl(eax, in1_lo);
3471 // edx:eax <- in1.lo * in2.lo
3472 __ mull(in2_lo);
3473 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3474 __ addl(in1_hi, edx);
3475 // in1.lo <- (in1.lo * in2.lo)[31:0];
3476 __ movl(in1_lo, eax);
3477 } else {
3478 DCHECK(second.IsDoubleStackSlot()) << second;
3479 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3480 Address in2_lo(ESP, second.GetStackIndex());
3481
3482 __ movl(eax, in2_hi);
3483 // eax <- in1.lo * in2.hi
3484 __ imull(eax, in1_lo);
3485 // in1.hi <- in1.hi * in2.lo
3486 __ imull(in1_hi, in2_lo);
3487 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3488 __ addl(in1_hi, eax);
3489 // move in1_lo to eax to prepare for double precision
3490 __ movl(eax, in1_lo);
3491 // edx:eax <- in1.lo * in2.lo
3492 __ mull(in2_lo);
3493 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3494 __ addl(in1_hi, edx);
3495 // in1.lo <- (in1.lo * in2.lo)[31:0];
3496 __ movl(in1_lo, eax);
3497 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003498
3499 break;
3500 }
3501
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003502 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003503 DCHECK(first.Equals(locations->Out()));
3504 if (second.IsFpuRegister()) {
3505 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3506 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3507 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003508 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003509 __ mulss(first.AsFpuRegister<XmmRegister>(),
3510 codegen_->LiteralFloatAddress(
3511 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003512 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003513 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3514 } else {
3515 DCHECK(second.IsStackSlot());
3516 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3517 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003518 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003519 }
3520
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003521 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003522 DCHECK(first.Equals(locations->Out()));
3523 if (second.IsFpuRegister()) {
3524 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3525 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3526 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003527 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003528 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3529 codegen_->LiteralDoubleAddress(
3530 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003531 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003532 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3533 } else {
3534 DCHECK(second.IsDoubleStackSlot());
3535 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3536 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003537 break;
3538 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003539
3540 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003541 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003542 }
3543}
3544
Roland Levillain232ade02015-04-20 15:14:36 +01003545void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3546 uint32_t temp_offset,
3547 uint32_t stack_adjustment,
3548 bool is_fp,
3549 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003550 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003551 DCHECK(!is_wide);
3552 if (is_fp) {
3553 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3554 } else {
3555 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3556 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003557 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003558 DCHECK(is_wide);
3559 if (is_fp) {
3560 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3561 } else {
3562 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3563 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003564 } else {
3565 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003566 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003567 Location stack_temp = Location::StackSlot(temp_offset);
3568 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003569 if (is_fp) {
3570 __ flds(Address(ESP, temp_offset));
3571 } else {
3572 __ filds(Address(ESP, temp_offset));
3573 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003574 } else {
3575 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3576 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003577 if (is_fp) {
3578 __ fldl(Address(ESP, temp_offset));
3579 } else {
3580 __ fildl(Address(ESP, temp_offset));
3581 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003582 }
3583 }
3584}
3585
3586void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003587 DataType::Type type = rem->GetResultType();
3588 bool is_float = type == DataType::Type::kFloat32;
3589 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003590 LocationSummary* locations = rem->GetLocations();
3591 Location first = locations->InAt(0);
3592 Location second = locations->InAt(1);
3593 Location out = locations->Out();
3594
3595 // Create stack space for 2 elements.
3596 // TODO: enhance register allocator to ask for stack temporaries.
Vladimir Markodec78172020-06-19 15:31:23 +01003597 codegen_->IncreaseFrame(2 * elem_size);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003598
3599 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003600 const bool is_wide = !is_float;
Andreas Gampe3db70682018-12-26 15:12:03 -08003601 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp= */ true, is_wide);
3602 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp= */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003603
3604 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003605 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003606 __ Bind(&retry);
3607 __ fprem();
3608
3609 // Move FP status to AX.
3610 __ fstsw();
3611
3612 // And see if the argument reduction is complete. This is signaled by the
3613 // C2 FPU flag bit set to 0.
3614 __ andl(EAX, Immediate(kC2ConditionMask));
3615 __ j(kNotEqual, &retry);
3616
3617 // We have settled on the final value. Retrieve it into an XMM register.
3618 // Store FP top of stack to real stack.
3619 if (is_float) {
3620 __ fsts(Address(ESP, 0));
3621 } else {
3622 __ fstl(Address(ESP, 0));
3623 }
3624
3625 // Pop the 2 items from the FP stack.
3626 __ fucompp();
3627
3628 // Load the value from the stack into an XMM register.
3629 DCHECK(out.IsFpuRegister()) << out;
3630 if (is_float) {
3631 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3632 } else {
3633 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3634 }
3635
3636 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01003637 codegen_->DecreaseFrame(2 * elem_size);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003638}
3639
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003640
3641void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3642 DCHECK(instruction->IsDiv() || instruction->IsRem());
3643
3644 LocationSummary* locations = instruction->GetLocations();
3645 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003646 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003647
3648 Register out_register = locations->Out().AsRegister<Register>();
3649 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003650 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003651
3652 DCHECK(imm == 1 || imm == -1);
3653
3654 if (instruction->IsRem()) {
3655 __ xorl(out_register, out_register);
3656 } else {
3657 __ movl(out_register, input_register);
3658 if (imm == -1) {
3659 __ negl(out_register);
3660 }
3661 }
3662}
3663
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303664void InstructionCodeGeneratorX86::RemByPowerOfTwo(HRem* instruction) {
3665 LocationSummary* locations = instruction->GetLocations();
3666 Location second = locations->InAt(1);
3667
3668 Register out = locations->Out().AsRegister<Register>();
3669 Register numerator = locations->InAt(0).AsRegister<Register>();
3670
3671 int32_t imm = Int64FromConstant(second.GetConstant());
3672 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3673 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3674
3675 Register tmp = locations->GetTemp(0).AsRegister<Register>();
3676 NearLabel done;
3677 __ movl(out, numerator);
3678 __ andl(out, Immediate(abs_imm-1));
3679 __ j(Condition::kZero, &done);
3680 __ leal(tmp, Address(out, static_cast<int32_t>(~(abs_imm-1))));
3681 __ testl(numerator, numerator);
3682 __ cmovl(Condition::kLess, out, tmp);
3683 __ Bind(&done);
3684}
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003685
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003686void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003687 LocationSummary* locations = instruction->GetLocations();
3688
3689 Register out_register = locations->Out().AsRegister<Register>();
3690 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003691 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003692 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3693 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003694
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003695 Register num = locations->GetTemp(0).AsRegister<Register>();
3696
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003697 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003698 __ testl(input_register, input_register);
3699 __ cmovl(kGreaterEqual, num, input_register);
3700 int shift = CTZ(imm);
3701 __ sarl(num, Immediate(shift));
3702
3703 if (imm < 0) {
3704 __ negl(num);
3705 }
3706
3707 __ movl(out_register, num);
3708}
3709
3710void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3711 DCHECK(instruction->IsDiv() || instruction->IsRem());
3712
3713 LocationSummary* locations = instruction->GetLocations();
3714 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3715
3716 Register eax = locations->InAt(0).AsRegister<Register>();
3717 Register out = locations->Out().AsRegister<Register>();
3718 Register num;
3719 Register edx;
3720
3721 if (instruction->IsDiv()) {
3722 edx = locations->GetTemp(0).AsRegister<Register>();
3723 num = locations->GetTemp(1).AsRegister<Register>();
3724 } else {
3725 edx = locations->Out().AsRegister<Register>();
3726 num = locations->GetTemp(0).AsRegister<Register>();
3727 }
3728
3729 DCHECK_EQ(EAX, eax);
3730 DCHECK_EQ(EDX, edx);
3731 if (instruction->IsDiv()) {
3732 DCHECK_EQ(EAX, out);
3733 } else {
3734 DCHECK_EQ(EDX, out);
3735 }
3736
3737 int64_t magic;
3738 int shift;
Andreas Gampe3db70682018-12-26 15:12:03 -08003739 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003740
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003741 // Save the numerator.
3742 __ movl(num, eax);
3743
3744 // EAX = magic
3745 __ movl(eax, Immediate(magic));
3746
3747 // EDX:EAX = magic * numerator
3748 __ imull(num);
3749
3750 if (imm > 0 && magic < 0) {
3751 // EDX += num
3752 __ addl(edx, num);
3753 } else if (imm < 0 && magic > 0) {
3754 __ subl(edx, num);
3755 }
3756
3757 // Shift if needed.
3758 if (shift != 0) {
3759 __ sarl(edx, Immediate(shift));
3760 }
3761
3762 // EDX += 1 if EDX < 0
3763 __ movl(eax, edx);
3764 __ shrl(edx, Immediate(31));
3765 __ addl(edx, eax);
3766
3767 if (instruction->IsRem()) {
3768 __ movl(eax, num);
3769 __ imull(edx, Immediate(imm));
3770 __ subl(eax, edx);
3771 __ movl(edx, eax);
3772 } else {
3773 __ movl(eax, edx);
3774 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003775}
3776
Calin Juravlebacfec32014-11-14 15:54:36 +00003777void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3778 DCHECK(instruction->IsDiv() || instruction->IsRem());
3779
3780 LocationSummary* locations = instruction->GetLocations();
3781 Location out = locations->Out();
3782 Location first = locations->InAt(0);
3783 Location second = locations->InAt(1);
3784 bool is_div = instruction->IsDiv();
3785
3786 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003787 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003788 DCHECK_EQ(EAX, first.AsRegister<Register>());
3789 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003790
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003791 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003792 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003793
3794 if (imm == 0) {
3795 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3796 } else if (imm == 1 || imm == -1) {
3797 DivRemOneOrMinusOne(instruction);
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303798 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
3799 if (is_div) {
3800 DivByPowerOfTwo(instruction->AsDiv());
3801 } else {
3802 RemByPowerOfTwo(instruction->AsRem());
3803 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003804 } else {
3805 DCHECK(imm <= -2 || imm >= 2);
3806 GenerateDivRemWithAnyConstant(instruction);
3807 }
3808 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003809 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00003810 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003811 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003812
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003813 Register second_reg = second.AsRegister<Register>();
3814 // 0x80000000/-1 triggers an arithmetic exception!
3815 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3816 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003817
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003818 __ cmpl(second_reg, Immediate(-1));
3819 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003820
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003821 // edx:eax <- sign-extended of eax
3822 __ cdq();
3823 // eax = quotient, edx = remainder
3824 __ idivl(second_reg);
3825 __ Bind(slow_path->GetExitLabel());
3826 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003827 break;
3828 }
3829
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003830 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003831 InvokeRuntimeCallingConvention calling_convention;
3832 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3833 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3834 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3835 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3836 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3837 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3838
3839 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003840 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003841 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003842 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003843 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003844 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003845 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003846 break;
3847 }
3848
3849 default:
3850 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3851 }
3852}
3853
Calin Juravle7c4954d2014-10-28 16:57:40 +00003854void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003855 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003856 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003857 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003858 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003859
Calin Juravle7c4954d2014-10-28 16:57:40 +00003860 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003861 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00003862 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003863 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003864 locations->SetOut(Location::SameAsFirstInput());
3865 // Intel uses edx:eax as the dividend.
3866 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003867 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3868 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3869 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003870 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003871 locations->AddTemp(Location::RequiresRegister());
3872 }
Calin Juravled0d48522014-11-04 16:40:20 +00003873 break;
3874 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003875 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003876 InvokeRuntimeCallingConvention calling_convention;
3877 locations->SetInAt(0, Location::RegisterPairLocation(
3878 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3879 locations->SetInAt(1, Location::RegisterPairLocation(
3880 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3881 // Runtime helper puts the result in EAX, EDX.
3882 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003883 break;
3884 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003885 case DataType::Type::kFloat32:
3886 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003887 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003888 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3889 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003890 } else if (div->InputAt(1)->IsConstant()) {
3891 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003892 } else {
3893 locations->SetInAt(1, Location::Any());
3894 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003895 locations->SetOut(Location::SameAsFirstInput());
3896 break;
3897 }
3898
3899 default:
3900 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3901 }
3902}
3903
3904void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3905 LocationSummary* locations = div->GetLocations();
3906 Location first = locations->InAt(0);
3907 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003908
3909 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003910 case DataType::Type::kInt32:
3911 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003912 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003913 break;
3914 }
3915
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003916 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003917 if (second.IsFpuRegister()) {
3918 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3919 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3920 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003921 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003922 __ divss(first.AsFpuRegister<XmmRegister>(),
3923 codegen_->LiteralFloatAddress(
3924 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003925 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003926 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3927 } else {
3928 DCHECK(second.IsStackSlot());
3929 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3930 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003931 break;
3932 }
3933
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003934 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003935 if (second.IsFpuRegister()) {
3936 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3937 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3938 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003939 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003940 __ divsd(first.AsFpuRegister<XmmRegister>(),
3941 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003942 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3943 const_area->GetBaseMethodAddress(),
3944 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003945 } else {
3946 DCHECK(second.IsDoubleStackSlot());
3947 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3948 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003949 break;
3950 }
3951
3952 default:
3953 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3954 }
3955}
3956
Calin Juravlebacfec32014-11-14 15:54:36 +00003957void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003958 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003959
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003960 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003961 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003962 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003963 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003964
Calin Juravled2ec87d2014-12-08 14:24:46 +00003965 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003966 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003967 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003968 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003969 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003970 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3971 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3972 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003973 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003974 locations->AddTemp(Location::RequiresRegister());
3975 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003976 break;
3977 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003978 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003979 InvokeRuntimeCallingConvention calling_convention;
3980 locations->SetInAt(0, Location::RegisterPairLocation(
3981 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3982 locations->SetInAt(1, Location::RegisterPairLocation(
3983 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3984 // Runtime helper puts the result in EAX, EDX.
3985 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3986 break;
3987 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003988 case DataType::Type::kFloat64:
3989 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003990 locations->SetInAt(0, Location::Any());
3991 locations->SetInAt(1, Location::Any());
3992 locations->SetOut(Location::RequiresFpuRegister());
3993 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003994 break;
3995 }
3996
3997 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003998 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003999 }
4000}
4001
4002void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004003 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00004004 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004005 case DataType::Type::kInt32:
4006 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004007 GenerateDivRemIntegral(rem);
4008 break;
4009 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004010 case DataType::Type::kFloat32:
4011 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05004012 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00004013 break;
4014 }
4015 default:
4016 LOG(FATAL) << "Unexpected rem type " << type;
4017 }
4018}
4019
Aart Bik1f8d51b2018-02-15 10:42:37 -08004020static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
4021 LocationSummary* locations = new (allocator) LocationSummary(minmax);
4022 switch (minmax->GetResultType()) {
4023 case DataType::Type::kInt32:
4024 locations->SetInAt(0, Location::RequiresRegister());
4025 locations->SetInAt(1, Location::RequiresRegister());
4026 locations->SetOut(Location::SameAsFirstInput());
4027 break;
4028 case DataType::Type::kInt64:
4029 locations->SetInAt(0, Location::RequiresRegister());
4030 locations->SetInAt(1, Location::RequiresRegister());
4031 locations->SetOut(Location::SameAsFirstInput());
4032 // Register to use to perform a long subtract to set cc.
4033 locations->AddTemp(Location::RequiresRegister());
4034 break;
4035 case DataType::Type::kFloat32:
4036 locations->SetInAt(0, Location::RequiresFpuRegister());
4037 locations->SetInAt(1, Location::RequiresFpuRegister());
4038 locations->SetOut(Location::SameAsFirstInput());
4039 locations->AddTemp(Location::RequiresRegister());
4040 break;
4041 case DataType::Type::kFloat64:
4042 locations->SetInAt(0, Location::RequiresFpuRegister());
4043 locations->SetInAt(1, Location::RequiresFpuRegister());
4044 locations->SetOut(Location::SameAsFirstInput());
4045 break;
4046 default:
4047 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
4048 }
4049}
4050
Aart Bik351df3e2018-03-07 11:54:57 -08004051void InstructionCodeGeneratorX86::GenerateMinMaxInt(LocationSummary* locations,
4052 bool is_min,
4053 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08004054 Location op1_loc = locations->InAt(0);
4055 Location op2_loc = locations->InAt(1);
4056
4057 // Shortcut for same input locations.
4058 if (op1_loc.Equals(op2_loc)) {
4059 // Can return immediately, as op1_loc == out_loc.
4060 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
4061 // a copy here.
4062 DCHECK(locations->Out().Equals(op1_loc));
4063 return;
4064 }
4065
4066 if (type == DataType::Type::kInt64) {
4067 // Need to perform a subtract to get the sign right.
4068 // op1 is already in the same location as the output.
4069 Location output = locations->Out();
4070 Register output_lo = output.AsRegisterPairLow<Register>();
4071 Register output_hi = output.AsRegisterPairHigh<Register>();
4072
4073 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
4074 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
4075
4076 // The comparison is performed by subtracting the second operand from
4077 // the first operand and then setting the status flags in the same
4078 // manner as the SUB instruction."
4079 __ cmpl(output_lo, op2_lo);
4080
4081 // Now use a temp and the borrow to finish the subtraction of op2_hi.
4082 Register temp = locations->GetTemp(0).AsRegister<Register>();
4083 __ movl(temp, output_hi);
4084 __ sbbl(temp, op2_hi);
4085
4086 // Now the condition code is correct.
4087 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
4088 __ cmovl(cond, output_lo, op2_lo);
4089 __ cmovl(cond, output_hi, op2_hi);
4090 } else {
4091 DCHECK_EQ(type, DataType::Type::kInt32);
4092 Register out = locations->Out().AsRegister<Register>();
4093 Register op2 = op2_loc.AsRegister<Register>();
4094
4095 // (out := op1)
4096 // out <=? op2
4097 // if out is min jmp done
4098 // out := op2
4099 // done:
4100
4101 __ cmpl(out, op2);
4102 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
4103 __ cmovl(cond, out, op2);
4104 }
4105}
4106
4107void InstructionCodeGeneratorX86::GenerateMinMaxFP(LocationSummary* locations,
4108 bool is_min,
4109 DataType::Type type) {
4110 Location op1_loc = locations->InAt(0);
4111 Location op2_loc = locations->InAt(1);
4112 Location out_loc = locations->Out();
4113 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
4114
4115 // Shortcut for same input locations.
4116 if (op1_loc.Equals(op2_loc)) {
4117 DCHECK(out_loc.Equals(op1_loc));
4118 return;
4119 }
4120
4121 // (out := op1)
4122 // out <=? op2
4123 // if Nan jmp Nan_label
4124 // if out is min jmp done
4125 // if op2 is min jmp op2_label
4126 // handle -0/+0
4127 // jmp done
4128 // Nan_label:
4129 // out := NaN
4130 // op2_label:
4131 // out := op2
4132 // done:
4133 //
4134 // This removes one jmp, but needs to copy one input (op1) to out.
4135 //
4136 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
4137
4138 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
4139
4140 NearLabel nan, done, op2_label;
4141 if (type == DataType::Type::kFloat64) {
4142 __ ucomisd(out, op2);
4143 } else {
4144 DCHECK_EQ(type, DataType::Type::kFloat32);
4145 __ ucomiss(out, op2);
4146 }
4147
4148 __ j(Condition::kParityEven, &nan);
4149
4150 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
4151 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
4152
4153 // Handle 0.0/-0.0.
4154 if (is_min) {
4155 if (type == DataType::Type::kFloat64) {
4156 __ orpd(out, op2);
4157 } else {
4158 __ orps(out, op2);
4159 }
4160 } else {
4161 if (type == DataType::Type::kFloat64) {
4162 __ andpd(out, op2);
4163 } else {
4164 __ andps(out, op2);
4165 }
4166 }
4167 __ jmp(&done);
4168
4169 // NaN handling.
4170 __ Bind(&nan);
4171 if (type == DataType::Type::kFloat64) {
4172 // TODO: Use a constant from the constant table (requires extra input).
4173 __ LoadLongConstant(out, kDoubleNaN);
4174 } else {
4175 Register constant = locations->GetTemp(0).AsRegister<Register>();
4176 __ movl(constant, Immediate(kFloatNaN));
4177 __ movd(out, constant);
4178 }
4179 __ jmp(&done);
4180
4181 // out := op2;
4182 __ Bind(&op2_label);
4183 if (type == DataType::Type::kFloat64) {
4184 __ movsd(out, op2);
4185 } else {
4186 __ movss(out, op2);
4187 }
4188
4189 // Done.
4190 __ Bind(&done);
4191}
4192
Aart Bik351df3e2018-03-07 11:54:57 -08004193void InstructionCodeGeneratorX86::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4194 DataType::Type type = minmax->GetResultType();
4195 switch (type) {
4196 case DataType::Type::kInt32:
4197 case DataType::Type::kInt64:
4198 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
4199 break;
4200 case DataType::Type::kFloat32:
4201 case DataType::Type::kFloat64:
4202 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
4203 break;
4204 default:
4205 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4206 }
4207}
4208
Aart Bik1f8d51b2018-02-15 10:42:37 -08004209void LocationsBuilderX86::VisitMin(HMin* min) {
4210 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4211}
4212
4213void InstructionCodeGeneratorX86::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004214 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004215}
4216
4217void LocationsBuilderX86::VisitMax(HMax* max) {
4218 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4219}
4220
4221void InstructionCodeGeneratorX86::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004222 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004223}
4224
Aart Bik3dad3412018-02-28 12:01:46 -08004225void LocationsBuilderX86::VisitAbs(HAbs* abs) {
4226 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4227 switch (abs->GetResultType()) {
4228 case DataType::Type::kInt32:
4229 locations->SetInAt(0, Location::RegisterLocation(EAX));
4230 locations->SetOut(Location::SameAsFirstInput());
4231 locations->AddTemp(Location::RegisterLocation(EDX));
4232 break;
4233 case DataType::Type::kInt64:
4234 locations->SetInAt(0, Location::RequiresRegister());
4235 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4236 locations->AddTemp(Location::RequiresRegister());
4237 break;
4238 case DataType::Type::kFloat32:
4239 locations->SetInAt(0, Location::RequiresFpuRegister());
4240 locations->SetOut(Location::SameAsFirstInput());
4241 locations->AddTemp(Location::RequiresFpuRegister());
4242 locations->AddTemp(Location::RequiresRegister());
4243 break;
4244 case DataType::Type::kFloat64:
4245 locations->SetInAt(0, Location::RequiresFpuRegister());
4246 locations->SetOut(Location::SameAsFirstInput());
4247 locations->AddTemp(Location::RequiresFpuRegister());
4248 break;
4249 default:
4250 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4251 }
4252}
4253
4254void InstructionCodeGeneratorX86::VisitAbs(HAbs* abs) {
4255 LocationSummary* locations = abs->GetLocations();
4256 switch (abs->GetResultType()) {
4257 case DataType::Type::kInt32: {
4258 Register out = locations->Out().AsRegister<Register>();
4259 DCHECK_EQ(out, EAX);
4260 Register temp = locations->GetTemp(0).AsRegister<Register>();
4261 DCHECK_EQ(temp, EDX);
4262 // Sign extend EAX into EDX.
4263 __ cdq();
4264 // XOR EAX with sign.
4265 __ xorl(EAX, EDX);
4266 // Subtract out sign to correct.
4267 __ subl(EAX, EDX);
4268 // The result is in EAX.
4269 break;
4270 }
4271 case DataType::Type::kInt64: {
4272 Location input = locations->InAt(0);
4273 Register input_lo = input.AsRegisterPairLow<Register>();
4274 Register input_hi = input.AsRegisterPairHigh<Register>();
4275 Location output = locations->Out();
4276 Register output_lo = output.AsRegisterPairLow<Register>();
4277 Register output_hi = output.AsRegisterPairHigh<Register>();
4278 Register temp = locations->GetTemp(0).AsRegister<Register>();
4279 // Compute the sign into the temporary.
4280 __ movl(temp, input_hi);
4281 __ sarl(temp, Immediate(31));
4282 // Store the sign into the output.
4283 __ movl(output_lo, temp);
4284 __ movl(output_hi, temp);
4285 // XOR the input to the output.
4286 __ xorl(output_lo, input_lo);
4287 __ xorl(output_hi, input_hi);
4288 // Subtract the sign.
4289 __ subl(output_lo, temp);
4290 __ sbbl(output_hi, temp);
4291 break;
4292 }
4293 case DataType::Type::kFloat32: {
4294 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4295 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4296 Register constant = locations->GetTemp(1).AsRegister<Register>();
4297 __ movl(constant, Immediate(INT32_C(0x7FFFFFFF)));
4298 __ movd(temp, constant);
4299 __ andps(out, temp);
4300 break;
4301 }
4302 case DataType::Type::kFloat64: {
4303 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4304 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4305 // TODO: Use a constant from the constant table (requires extra input).
4306 __ LoadLongConstant(temp, INT64_C(0x7FFFFFFFFFFFFFFF));
4307 __ andpd(out, temp);
4308 break;
4309 }
4310 default:
4311 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4312 }
4313}
4314
Calin Juravled0d48522014-11-04 16:40:20 +00004315void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004316 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004317 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004318 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004319 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004320 case DataType::Type::kInt8:
4321 case DataType::Type::kUint16:
4322 case DataType::Type::kInt16:
4323 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004324 locations->SetInAt(0, Location::Any());
4325 break;
4326 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004327 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004328 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
4329 if (!instruction->IsConstant()) {
4330 locations->AddTemp(Location::RequiresRegister());
4331 }
4332 break;
4333 }
4334 default:
4335 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4336 }
Calin Juravled0d48522014-11-04 16:40:20 +00004337}
4338
4339void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004340 SlowPathCode* slow_path =
4341 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004342 codegen_->AddSlowPath(slow_path);
4343
4344 LocationSummary* locations = instruction->GetLocations();
4345 Location value = locations->InAt(0);
4346
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004347 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004348 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004349 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004350 case DataType::Type::kInt8:
4351 case DataType::Type::kUint16:
4352 case DataType::Type::kInt16:
4353 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004354 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004355 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004356 __ j(kEqual, slow_path->GetEntryLabel());
4357 } else if (value.IsStackSlot()) {
4358 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
4359 __ j(kEqual, slow_path->GetEntryLabel());
4360 } else {
4361 DCHECK(value.IsConstant()) << value;
4362 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004363 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004364 }
4365 }
4366 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004367 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004368 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004369 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004370 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004371 __ movl(temp, value.AsRegisterPairLow<Register>());
4372 __ orl(temp, value.AsRegisterPairHigh<Register>());
4373 __ j(kEqual, slow_path->GetEntryLabel());
4374 } else {
4375 DCHECK(value.IsConstant()) << value;
4376 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4377 __ jmp(slow_path->GetEntryLabel());
4378 }
4379 }
4380 break;
4381 }
4382 default:
4383 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004384 }
Calin Juravled0d48522014-11-04 16:40:20 +00004385}
4386
Calin Juravle9aec02f2014-11-18 23:06:35 +00004387void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
4388 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4389
4390 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004391 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004392
4393 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004394 case DataType::Type::kInt32:
4395 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004396 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00004397 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00004398 // The shift count needs to be in CL or a constant.
4399 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004400 locations->SetOut(Location::SameAsFirstInput());
4401 break;
4402 }
4403 default:
4404 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4405 }
4406}
4407
4408void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
4409 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4410
4411 LocationSummary* locations = op->GetLocations();
4412 Location first = locations->InAt(0);
4413 Location second = locations->InAt(1);
4414 DCHECK(first.Equals(locations->Out()));
4415
4416 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004417 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00004418 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004419 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004420 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004421 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004422 DCHECK_EQ(ECX, second_reg);
4423 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004424 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004425 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004426 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004427 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004428 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004429 }
4430 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004431 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004432 if (shift == 0) {
4433 return;
4434 }
4435 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004436 if (op->IsShl()) {
4437 __ shll(first_reg, imm);
4438 } else if (op->IsShr()) {
4439 __ sarl(first_reg, imm);
4440 } else {
4441 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004442 }
4443 }
4444 break;
4445 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004446 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004447 if (second.IsRegister()) {
4448 Register second_reg = second.AsRegister<Register>();
4449 DCHECK_EQ(ECX, second_reg);
4450 if (op->IsShl()) {
4451 GenerateShlLong(first, second_reg);
4452 } else if (op->IsShr()) {
4453 GenerateShrLong(first, second_reg);
4454 } else {
4455 GenerateUShrLong(first, second_reg);
4456 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004457 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00004458 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004459 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004460 // Nothing to do if the shift is 0, as the input is already the output.
4461 if (shift != 0) {
4462 if (op->IsShl()) {
4463 GenerateShlLong(first, shift);
4464 } else if (op->IsShr()) {
4465 GenerateShrLong(first, shift);
4466 } else {
4467 GenerateUShrLong(first, shift);
4468 }
4469 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004470 }
4471 break;
4472 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004473 default:
4474 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4475 }
4476}
4477
Mark P Mendell73945692015-04-29 14:56:17 +00004478void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
4479 Register low = loc.AsRegisterPairLow<Register>();
4480 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04004481 if (shift == 1) {
4482 // This is just an addition.
4483 __ addl(low, low);
4484 __ adcl(high, high);
4485 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00004486 // Shift by 32 is easy. High gets low, and low gets 0.
4487 codegen_->EmitParallelMoves(
4488 loc.ToLow(),
4489 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004490 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004491 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4492 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004493 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004494 } else if (shift > 32) {
4495 // Low part becomes 0. High part is low part << (shift-32).
4496 __ movl(high, low);
4497 __ shll(high, Immediate(shift - 32));
4498 __ xorl(low, low);
4499 } else {
4500 // Between 1 and 31.
4501 __ shld(high, low, Immediate(shift));
4502 __ shll(low, Immediate(shift));
4503 }
4504}
4505
Calin Juravle9aec02f2014-11-18 23:06:35 +00004506void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004507 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004508 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4509 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4510 __ testl(shifter, Immediate(32));
4511 __ j(kEqual, &done);
4512 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4513 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4514 __ Bind(&done);
4515}
4516
Mark P Mendell73945692015-04-29 14:56:17 +00004517void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4518 Register low = loc.AsRegisterPairLow<Register>();
4519 Register high = loc.AsRegisterPairHigh<Register>();
4520 if (shift == 32) {
4521 // Need to copy the sign.
4522 DCHECK_NE(low, high);
4523 __ movl(low, high);
4524 __ sarl(high, Immediate(31));
4525 } else if (shift > 32) {
4526 DCHECK_NE(low, high);
4527 // High part becomes sign. Low part is shifted by shift - 32.
4528 __ movl(low, high);
4529 __ sarl(high, Immediate(31));
4530 __ sarl(low, Immediate(shift - 32));
4531 } else {
4532 // Between 1 and 31.
4533 __ shrd(low, high, Immediate(shift));
4534 __ sarl(high, Immediate(shift));
4535 }
4536}
4537
Calin Juravle9aec02f2014-11-18 23:06:35 +00004538void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004539 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004540 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4541 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4542 __ testl(shifter, Immediate(32));
4543 __ j(kEqual, &done);
4544 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4545 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4546 __ Bind(&done);
4547}
4548
Mark P Mendell73945692015-04-29 14:56:17 +00004549void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4550 Register low = loc.AsRegisterPairLow<Register>();
4551 Register high = loc.AsRegisterPairHigh<Register>();
4552 if (shift == 32) {
4553 // Shift by 32 is easy. Low gets high, and high gets 0.
4554 codegen_->EmitParallelMoves(
4555 loc.ToHigh(),
4556 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004557 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004558 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4559 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004560 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004561 } else if (shift > 32) {
4562 // Low part is high >> (shift - 32). High part becomes 0.
4563 __ movl(low, high);
4564 __ shrl(low, Immediate(shift - 32));
4565 __ xorl(high, high);
4566 } else {
4567 // Between 1 and 31.
4568 __ shrd(low, high, Immediate(shift));
4569 __ shrl(high, Immediate(shift));
4570 }
4571}
4572
Calin Juravle9aec02f2014-11-18 23:06:35 +00004573void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004574 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004575 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4576 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4577 __ testl(shifter, Immediate(32));
4578 __ j(kEqual, &done);
4579 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4580 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4581 __ Bind(&done);
4582}
4583
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004584void LocationsBuilderX86::VisitRor(HRor* ror) {
4585 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004586 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004587
4588 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004589 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004590 // Add the temporary needed.
4591 locations->AddTemp(Location::RequiresRegister());
4592 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004593 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004594 locations->SetInAt(0, Location::RequiresRegister());
4595 // The shift count needs to be in CL (unless it is a constant).
4596 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4597 locations->SetOut(Location::SameAsFirstInput());
4598 break;
4599 default:
4600 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4601 UNREACHABLE();
4602 }
4603}
4604
4605void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4606 LocationSummary* locations = ror->GetLocations();
4607 Location first = locations->InAt(0);
4608 Location second = locations->InAt(1);
4609
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004610 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004611 Register first_reg = first.AsRegister<Register>();
4612 if (second.IsRegister()) {
4613 Register second_reg = second.AsRegister<Register>();
4614 __ rorl(first_reg, second_reg);
4615 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004616 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004617 __ rorl(first_reg, imm);
4618 }
4619 return;
4620 }
4621
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004622 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004623 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4624 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4625 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4626 if (second.IsRegister()) {
4627 Register second_reg = second.AsRegister<Register>();
4628 DCHECK_EQ(second_reg, ECX);
4629 __ movl(temp_reg, first_reg_hi);
4630 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4631 __ shrd(first_reg_lo, temp_reg, second_reg);
4632 __ movl(temp_reg, first_reg_hi);
4633 __ testl(second_reg, Immediate(32));
4634 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4635 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4636 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004637 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004638 if (shift_amt == 0) {
4639 // Already fine.
4640 return;
4641 }
4642 if (shift_amt == 32) {
4643 // Just swap.
4644 __ movl(temp_reg, first_reg_lo);
4645 __ movl(first_reg_lo, first_reg_hi);
4646 __ movl(first_reg_hi, temp_reg);
4647 return;
4648 }
4649
4650 Immediate imm(shift_amt);
4651 // Save the constents of the low value.
4652 __ movl(temp_reg, first_reg_lo);
4653
4654 // Shift right into low, feeding bits from high.
4655 __ shrd(first_reg_lo, first_reg_hi, imm);
4656
4657 // Shift right into high, feeding bits from the original low.
4658 __ shrd(first_reg_hi, temp_reg, imm);
4659
4660 // Swap if needed.
4661 if (shift_amt > 32) {
4662 __ movl(temp_reg, first_reg_lo);
4663 __ movl(first_reg_lo, first_reg_hi);
4664 __ movl(first_reg_hi, temp_reg);
4665 }
4666 }
4667}
4668
Calin Juravle9aec02f2014-11-18 23:06:35 +00004669void LocationsBuilderX86::VisitShl(HShl* shl) {
4670 HandleShift(shl);
4671}
4672
4673void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4674 HandleShift(shl);
4675}
4676
4677void LocationsBuilderX86::VisitShr(HShr* shr) {
4678 HandleShift(shr);
4679}
4680
4681void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4682 HandleShift(shr);
4683}
4684
4685void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4686 HandleShift(ushr);
4687}
4688
4689void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4690 HandleShift(ushr);
4691}
4692
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004693void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004694 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4695 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004696 locations->SetOut(Location::RegisterLocation(EAX));
Alex Lightd109e302018-06-27 10:25:41 -07004697 InvokeRuntimeCallingConvention calling_convention;
4698 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004699}
4700
4701void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07004702 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
4703 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4704 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004705}
4706
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004707void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004708 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4709 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004710 locations->SetOut(Location::RegisterLocation(EAX));
4711 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004712 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4713 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004714}
4715
4716void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01004717 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
4718 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004719 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004720 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004721 DCHECK(!codegen_->IsLeafMethod());
4722}
4723
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004724void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004725 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004726 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004727 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4728 if (location.IsStackSlot()) {
4729 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4730 } else if (location.IsDoubleStackSlot()) {
4731 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004732 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004733 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004734}
4735
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004736void InstructionCodeGeneratorX86::VisitParameterValue(
4737 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4738}
4739
4740void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4741 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004742 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004743 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4744}
4745
4746void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004747}
4748
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004749void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4750 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004751 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004752 locations->SetInAt(0, Location::RequiresRegister());
4753 locations->SetOut(Location::RequiresRegister());
4754}
4755
4756void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4757 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004758 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004759 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004760 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004761 __ movl(locations->Out().AsRegister<Register>(),
4762 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004763 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004764 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004765 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004766 __ movl(locations->Out().AsRegister<Register>(),
4767 Address(locations->InAt(0).AsRegister<Register>(),
4768 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4769 // temp = temp->GetImtEntryAt(method_offset);
4770 __ movl(locations->Out().AsRegister<Register>(),
4771 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004772 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004773}
4774
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004775void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004776 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004777 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004778 locations->SetInAt(0, Location::RequiresRegister());
4779 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004780}
4781
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004782void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4783 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004784 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004785 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004786 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004787 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004788 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004789 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004790 break;
4791
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004792 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004793 __ notl(out.AsRegisterPairLow<Register>());
4794 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004795 break;
4796
4797 default:
4798 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4799 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004800}
4801
David Brazdil66d126e2015-04-03 16:02:44 +01004802void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4803 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004804 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004805 locations->SetInAt(0, Location::RequiresRegister());
4806 locations->SetOut(Location::SameAsFirstInput());
4807}
4808
4809void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004810 LocationSummary* locations = bool_not->GetLocations();
4811 Location in = locations->InAt(0);
4812 Location out = locations->Out();
4813 DCHECK(in.Equals(out));
4814 __ xorl(out.AsRegister<Register>(), Immediate(1));
4815}
4816
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004817void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004818 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004819 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004820 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004821 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004822 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004823 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004824 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004825 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004826 case DataType::Type::kInt32:
4827 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004828 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004829 locations->SetInAt(1, Location::Any());
4830 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4831 break;
4832 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004833 case DataType::Type::kFloat32:
4834 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004835 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004836 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4837 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4838 } else if (compare->InputAt(1)->IsConstant()) {
4839 locations->SetInAt(1, Location::RequiresFpuRegister());
4840 } else {
4841 locations->SetInAt(1, Location::Any());
4842 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004843 locations->SetOut(Location::RequiresRegister());
4844 break;
4845 }
4846 default:
4847 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4848 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004849}
4850
4851void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004852 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004853 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004854 Location left = locations->InAt(0);
4855 Location right = locations->InAt(1);
4856
Mark Mendell0c9497d2015-08-21 09:30:05 -04004857 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004858 Condition less_cond = kLess;
4859
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004860 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004861 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004862 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004863 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004864 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004865 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004866 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004867 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004868 break;
4869 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004870 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004871 Register left_low = left.AsRegisterPairLow<Register>();
4872 Register left_high = left.AsRegisterPairHigh<Register>();
4873 int32_t val_low = 0;
4874 int32_t val_high = 0;
4875 bool right_is_const = false;
4876
4877 if (right.IsConstant()) {
4878 DCHECK(right.GetConstant()->IsLongConstant());
4879 right_is_const = true;
4880 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4881 val_low = Low32Bits(val);
4882 val_high = High32Bits(val);
4883 }
4884
Calin Juravleddb7df22014-11-25 20:56:51 +00004885 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004886 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004887 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004888 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004889 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004890 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004891 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004892 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004893 __ j(kLess, &less); // Signed compare.
4894 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004895 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004896 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004897 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004898 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004899 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004900 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004901 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004902 }
Aart Bika19616e2016-02-01 18:57:58 -08004903 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004904 break;
4905 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004906 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004907 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004908 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004909 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004910 break;
4911 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004912 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004913 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004914 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004915 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004916 break;
4917 }
4918 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004919 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004920 }
Aart Bika19616e2016-02-01 18:57:58 -08004921
Calin Juravleddb7df22014-11-25 20:56:51 +00004922 __ movl(out, Immediate(0));
4923 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004924 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004925
4926 __ Bind(&greater);
4927 __ movl(out, Immediate(1));
4928 __ jmp(&done);
4929
4930 __ Bind(&less);
4931 __ movl(out, Immediate(-1));
4932
4933 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004934}
4935
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004936void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004937 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004938 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004939 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004940 locations->SetInAt(i, Location::Any());
4941 }
4942 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004943}
4944
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004945void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004946 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004947}
4948
Roland Levillain7c1559a2015-12-15 10:55:36 +00004949void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004950 /*
4951 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4952 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4953 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4954 */
4955 switch (kind) {
4956 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004957 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004958 break;
4959 }
4960 case MemBarrierKind::kAnyStore:
4961 case MemBarrierKind::kLoadAny:
4962 case MemBarrierKind::kStoreStore: {
4963 // nop
4964 break;
4965 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004966 case MemBarrierKind::kNTStoreStore:
4967 // Non-Temporal Store/Store needs an explicit fence.
Andreas Gampe3db70682018-12-26 15:12:03 -08004968 MemoryFence(/* non-temporal= */ true);
Mark Mendell7aa04a12016-01-27 22:39:07 -05004969 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004970 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004971}
4972
Vladimir Markodc151b22015-10-15 18:02:30 +01004973HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4974 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01004975 ArtMethod* method ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004976 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004977}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004978
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004979Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4980 Register temp) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004981 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004982 if (!invoke->GetLocations()->Intrinsified()) {
4983 return location.AsRegister<Register>();
4984 }
4985 // For intrinsics we allow any location, so it may be on the stack.
4986 if (!location.IsRegister()) {
4987 __ movl(temp, Address(ESP, location.GetStackIndex()));
4988 return temp;
4989 }
4990 // For register locations, check if the register was saved. If so, get it from the stack.
4991 // Note: There is a chance that the register was saved but not overwritten, so we could
4992 // save one load. However, since this is just an intrinsic slow path we prefer this
4993 // simple and more robust approach rather that trying to determine if that's the case.
4994 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00004995 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4996 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4997 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4998 __ movl(temp, Address(ESP, stack_offset));
4999 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005000 }
5001 return location.AsRegister<Register>();
5002}
5003
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005004void CodeGeneratorX86::GenerateStaticOrDirectCall(
5005 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00005006 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
5007 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005008 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005009 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005010 uint32_t offset =
5011 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
5012 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00005013 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005014 }
Vladimir Marko58155012015-08-19 12:49:41 +00005015 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Marko86c87522020-05-11 16:55:55 +01005016 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005017 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005018 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005019 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko65979462017-05-19 17:25:12 +01005020 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
5021 temp.AsRegister<Register>());
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005022 __ leal(temp.AsRegister<Register>(),
5023 Address(base_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005024 RecordBootImageMethodPatch(invoke);
Vladimir Marko65979462017-05-19 17:25:12 +01005025 break;
5026 }
Vladimir Markob066d432018-01-03 13:14:37 +00005027 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
5028 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
5029 temp.AsRegister<Register>());
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005030 __ movl(temp.AsRegister<Register>(), Address(base_reg, kPlaceholder32BitOffset));
Vladimir Markob066d432018-01-03 13:14:37 +00005031 RecordBootImageRelRoPatch(
5032 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005033 GetBootImageOffset(invoke));
Vladimir Markob066d432018-01-03 13:14:37 +00005034 break;
5035 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005036 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005037 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
5038 temp.AsRegister<Register>());
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005039 __ movl(temp.AsRegister<Register>(), Address(base_reg, kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005040 RecordMethodBssEntryPatch(invoke);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005041 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005042 break;
5043 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005044 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
5045 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
5046 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005047 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
5048 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5049 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01005050 }
Vladimir Marko58155012015-08-19 12:49:41 +00005051 }
5052
5053 switch (invoke->GetCodePtrLocation()) {
5054 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
5055 __ call(GetFrameEntryLabel());
Vladimir Marko86c87522020-05-11 16:55:55 +01005056 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko58155012015-08-19 12:49:41 +00005057 break;
Vladimir Marko86c87522020-05-11 16:55:55 +01005058 case HInvokeStaticOrDirect::CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01005059 size_t out_frame_size =
5060 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorX86,
5061 kNativeStackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01005062 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Marko86c87522020-05-11 16:55:55 +01005063 // (callee_method + offset_of_jni_entry_point)()
5064 __ call(Address(callee_method.AsRegister<Register>(),
5065 ArtMethod::EntryPointFromJniOffset(kX86PointerSize).Int32Value()));
5066 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5067 if (out_frame_size == 0u && DataType::IsFloatingPointType(invoke->GetType())) {
5068 // Create space for conversion.
5069 out_frame_size = 8u;
Vladimir Markodec78172020-06-19 15:31:23 +01005070 IncreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01005071 }
5072 // Zero-/sign-extend or move the result when needed due to native and managed ABI mismatch.
5073 switch (invoke->GetType()) {
5074 case DataType::Type::kBool:
5075 __ movzxb(EAX, AL);
5076 break;
5077 case DataType::Type::kInt8:
5078 __ movsxb(EAX, AL);
5079 break;
5080 case DataType::Type::kUint16:
5081 __ movzxw(EAX, EAX);
5082 break;
5083 case DataType::Type::kInt16:
5084 __ movsxw(EAX, EAX);
5085 break;
5086 case DataType::Type::kFloat32:
5087 __ fstps(Address(ESP, 0));
5088 __ movss(XMM0, Address(ESP, 0));
5089 break;
5090 case DataType::Type::kFloat64:
5091 __ fstpl(Address(ESP, 0));
5092 __ movsd(XMM0, Address(ESP, 0));
5093 break;
5094 case DataType::Type::kInt32:
5095 case DataType::Type::kInt64:
5096 case DataType::Type::kVoid:
5097 break;
5098 default:
5099 DCHECK(false) << invoke->GetType();
5100 break;
5101 }
5102 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01005103 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01005104 }
5105 break;
5106 }
Vladimir Marko58155012015-08-19 12:49:41 +00005107 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5108 // (callee_method + offset_of_quick_compiled_code)()
5109 __ call(Address(callee_method.AsRegister<Register>(),
5110 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005111 kX86PointerSize).Int32Value()));
Vladimir Marko86c87522020-05-11 16:55:55 +01005112 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko58155012015-08-19 12:49:41 +00005113 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04005114 }
5115
5116 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04005117}
5118
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005119void CodeGeneratorX86::GenerateVirtualCall(
5120 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005121 Register temp = temp_in.AsRegister<Register>();
5122 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5123 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005124
5125 // Use the calling convention instead of the location of the receiver, as
5126 // intrinsics may have put the receiver in a different register. In the intrinsics
5127 // slow path, the arguments have been moved to the right place, so here we are
5128 // guaranteed that the receiver is the first register of the calling convention.
5129 InvokeDexCallingConvention calling_convention;
5130 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005131 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005132 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005133 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005134 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005135 // Instead of simply (possibly) unpoisoning `temp` here, we should
5136 // emit a read barrier for the previous class reference load.
5137 // However this is not required in practice, as this is an
5138 // intermediate/temporary reference and because the current
5139 // concurrent copying collector keeps the from-space memory
5140 // intact/accessible until the end of the marking phase (the
5141 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005142 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00005143
5144 MaybeGenerateInlineCacheCheck(invoke, temp);
5145
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005146 // temp = temp->GetMethodAt(method_offset);
5147 __ movl(temp, Address(temp, method_offset));
5148 // call temp->GetEntryPoint();
5149 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07005150 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005151 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005152}
5153
Vladimir Marko6fd16062018-06-26 11:02:04 +01005154void CodeGeneratorX86::RecordBootImageIntrinsicPatch(HX86ComputeBaseMethodAddress* method_address,
5155 uint32_t intrinsic_data) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005156 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005157 method_address, /* target_dex_file= */ nullptr, intrinsic_data);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005158 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005159}
5160
Vladimir Markob066d432018-01-03 13:14:37 +00005161void CodeGeneratorX86::RecordBootImageRelRoPatch(HX86ComputeBaseMethodAddress* method_address,
5162 uint32_t boot_image_offset) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005163 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005164 method_address, /* target_dex_file= */ nullptr, boot_image_offset);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005165 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Markob066d432018-01-03 13:14:37 +00005166}
5167
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005168void CodeGeneratorX86::RecordBootImageMethodPatch(HInvokeStaticOrDirect* invoke) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005169 HX86ComputeBaseMethodAddress* method_address =
Vladimir Marko65979462017-05-19 17:25:12 +01005170 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005171 boot_image_method_patches_.emplace_back(
5172 method_address, invoke->GetTargetMethod().dex_file, invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01005173 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005174}
5175
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005176void CodeGeneratorX86::RecordMethodBssEntryPatch(HInvokeStaticOrDirect* invoke) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005177 HX86ComputeBaseMethodAddress* method_address =
5178 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005179 // Add the patch entry and bind its label at the end of the instruction.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005180 method_bss_entry_patches_.emplace_back(
5181 method_address, &GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
5182 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005183}
5184
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005185void CodeGeneratorX86::RecordBootImageTypePatch(HLoadClass* load_class) {
5186 HX86ComputeBaseMethodAddress* method_address =
5187 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5188 boot_image_type_patches_.emplace_back(
5189 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005190 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005191}
5192
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005193Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005194 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005195 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5196 type_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005197 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005198 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005199}
5200
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005201void CodeGeneratorX86::RecordBootImageStringPatch(HLoadString* load_string) {
5202 HX86ComputeBaseMethodAddress* method_address =
5203 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
5204 boot_image_string_patches_.emplace_back(
5205 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
5206 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01005207}
5208
Vladimir Markoaad75c62016-10-03 08:46:48 +00005209Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005210 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005211 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005212 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005213 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005214 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005215}
5216
Vladimir Markoeebb8212018-06-05 14:57:24 +01005217void CodeGeneratorX86::LoadBootImageAddress(Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01005218 uint32_t boot_image_reference,
Vladimir Markoeebb8212018-06-05 14:57:24 +01005219 HInvokeStaticOrDirect* invoke) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005220 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005221 HX86ComputeBaseMethodAddress* method_address =
5222 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5223 DCHECK(method_address != nullptr);
5224 Register method_address_reg =
5225 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005226 __ leal(reg, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005227 RecordBootImageIntrinsicPatch(method_address, boot_image_reference);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01005228 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01005229 HX86ComputeBaseMethodAddress* method_address =
5230 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5231 DCHECK(method_address != nullptr);
5232 Register method_address_reg =
5233 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005234 __ movl(reg, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005235 RecordBootImageRelRoPatch(method_address, boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01005236 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01005237 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01005238 gc::Heap* heap = Runtime::Current()->GetHeap();
5239 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01005240 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01005241 __ movl(reg, Immediate(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address))));
5242 }
5243}
5244
Vladimir Marko6fd16062018-06-26 11:02:04 +01005245void CodeGeneratorX86::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
5246 uint32_t boot_image_offset) {
5247 DCHECK(invoke->IsStatic());
5248 InvokeRuntimeCallingConvention calling_convention;
5249 Register argument = calling_convention.GetRegisterAt(0);
5250 if (GetCompilerOptions().IsBootImage()) {
5251 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
5252 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
Vladimir Marko6fd16062018-06-26 11:02:04 +01005253 HX86ComputeBaseMethodAddress* method_address =
5254 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5255 DCHECK(method_address != nullptr);
5256 Register method_address_reg =
5257 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005258 __ leal(argument, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005259 MethodReference target_method = invoke->GetTargetMethod();
5260 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
5261 boot_image_type_patches_.emplace_back(method_address, target_method.dex_file, type_idx.index_);
5262 __ Bind(&boot_image_type_patches_.back().label);
5263 } else {
5264 LoadBootImageAddress(argument, boot_image_offset, invoke);
5265 }
5266 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
5267 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
5268}
5269
Vladimir Markoaad75c62016-10-03 08:46:48 +00005270// The label points to the end of the "movl" or another instruction but the literal offset
5271// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
5272constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
5273
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005274template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005275inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005276 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005277 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005278 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005279 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005280 linker_patches->push_back(Factory(literal_offset,
5281 info.target_dex_file,
5282 GetMethodAddressOffset(info.method_address),
5283 info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005284 }
5285}
5286
Vladimir Marko6fd16062018-06-26 11:02:04 +01005287template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
5288linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
5289 const DexFile* target_dex_file,
5290 uint32_t pc_insn_offset,
5291 uint32_t boot_image_offset) {
5292 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5293 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005294}
5295
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005296void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005297 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005298 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01005299 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005300 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00005301 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01005302 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005303 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01005304 string_bss_entry_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01005305 boot_image_other_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005306 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01005307 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005308 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
5309 boot_image_method_patches_, linker_patches);
5310 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
5311 boot_image_type_patches_, linker_patches);
5312 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005313 boot_image_string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005314 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005315 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005316 DCHECK(boot_image_type_patches_.empty());
5317 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01005318 }
5319 if (GetCompilerOptions().IsBootImage()) {
5320 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5321 boot_image_other_patches_, linker_patches);
5322 } else {
5323 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
5324 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005325 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005326 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5327 method_bss_entry_patches_, linker_patches);
5328 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5329 type_bss_entry_patches_, linker_patches);
5330 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5331 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005332 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005333}
5334
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005335void CodeGeneratorX86::MarkGCCard(Register temp,
5336 Register card,
5337 Register object,
5338 Register value,
5339 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005340 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005341 if (value_can_be_null) {
5342 __ testl(value, value);
5343 __ j(kEqual, &is_null);
5344 }
Roland Levillainc73f0522018-08-14 15:16:50 +01005345 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07005346 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01005347 // Calculate the offset (in the card table) of the card corresponding to
5348 // `object`.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005349 __ movl(temp, object);
5350 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01005351 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
5352 // `object`'s card.
5353 //
5354 // Register `card` contains the address of the card table. Note that the card
5355 // table's base is biased during its creation so that it always starts at an
5356 // address whose least-significant byte is equal to `kCardDirty` (see
5357 // art::gc::accounting::CardTable::Create). Therefore the MOVB instruction
5358 // below writes the `kCardDirty` (byte) value into the `object`'s card
5359 // (located at `card + object >> kCardShift`).
5360 //
5361 // This dual use of the value in register `card` (1. to calculate the location
5362 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
5363 // (no need to explicitly load `kCardDirty` as an immediate value).
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00005364 __ movb(Address(temp, card, TIMES_1, 0),
5365 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005366 if (value_can_be_null) {
5367 __ Bind(&is_null);
5368 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005369}
5370
Calin Juravle52c48962014-12-16 17:02:57 +00005371void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5372 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005373
5374 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005375 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005376 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005377 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5378 kEmitCompilerReadBarrier
5379 ? LocationSummary::kCallOnSlowPath
5380 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005381 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005382 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005383 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005384 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005385
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005386 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005387 locations->SetOut(Location::RequiresFpuRegister());
5388 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005389 // The output overlaps in case of long: we don't want the low move
5390 // to overwrite the object's location. Likewise, in the case of
5391 // an object field get with read barriers enabled, we do not want
5392 // the move to overwrite the object's location, as we need it to emit
5393 // the read barrier.
5394 locations->SetOut(
5395 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005396 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005397 Location::kOutputOverlap :
5398 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005399 }
Calin Juravle52c48962014-12-16 17:02:57 +00005400
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005401 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00005402 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005403 // So we use an XMM register as a temp to achieve atomicity (first
5404 // load the temp into the XMM and then copy the XMM into the
5405 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00005406 locations->AddTemp(Location::RequiresFpuRegister());
5407 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005408}
5409
Calin Juravle52c48962014-12-16 17:02:57 +00005410void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
5411 const FieldInfo& field_info) {
5412 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005413
Calin Juravle52c48962014-12-16 17:02:57 +00005414 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005415 Location base_loc = locations->InAt(0);
5416 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005417 Location out = locations->Out();
5418 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01005419 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5420 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00005421 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5422
Vladimir Marko61b92282017-10-11 13:23:17 +01005423 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005424 case DataType::Type::kBool:
5425 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005426 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005427 break;
5428 }
5429
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005430 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005431 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005432 break;
5433 }
5434
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005435 case DataType::Type::kUint16: {
5436 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005437 break;
5438 }
5439
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005440 case DataType::Type::kInt16: {
5441 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005442 break;
5443 }
5444
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005445 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00005446 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005447 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005448
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005449 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005450 // /* HeapReference<Object> */ out = *(base + offset)
5451 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005452 // Note that a potential implicit null check is handled in this
5453 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
5454 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005455 instruction, out, base, offset, /* needs_null_check= */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005456 if (is_volatile) {
5457 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5458 }
5459 } else {
5460 __ movl(out.AsRegister<Register>(), Address(base, offset));
5461 codegen_->MaybeRecordImplicitNullCheck(instruction);
5462 if (is_volatile) {
5463 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5464 }
5465 // If read barriers are enabled, emit read barriers other than
5466 // Baker's using a slow path (and also unpoison the loaded
5467 // reference, if heap poisoning is enabled).
5468 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5469 }
5470 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005471 }
5472
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005473 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005474 if (is_volatile) {
5475 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5476 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005477 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005478 __ movd(out.AsRegisterPairLow<Register>(), temp);
5479 __ psrlq(temp, Immediate(32));
5480 __ movd(out.AsRegisterPairHigh<Register>(), temp);
5481 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005482 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00005483 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005484 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005485 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
5486 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005487 break;
5488 }
5489
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005490 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00005491 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005492 break;
5493 }
5494
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005495 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005496 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005497 break;
5498 }
5499
Aart Bik66c158e2018-01-31 12:55:04 -08005500 case DataType::Type::kUint32:
5501 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005502 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005503 LOG(FATAL) << "Unreachable type " << load_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005504 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005505 }
Calin Juravle52c48962014-12-16 17:02:57 +00005506
Vladimir Marko61b92282017-10-11 13:23:17 +01005507 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005508 // Potential implicit null checks, in the case of reference or
5509 // long fields, are handled in the previous switch statement.
5510 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005511 codegen_->MaybeRecordImplicitNullCheck(instruction);
5512 }
5513
Calin Juravle52c48962014-12-16 17:02:57 +00005514 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005515 if (load_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005516 // Memory barriers, in the case of references, are also handled
5517 // in the previous switch statement.
5518 } else {
5519 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5520 }
Roland Levillain4d027112015-07-01 15:41:14 +01005521 }
Calin Juravle52c48962014-12-16 17:02:57 +00005522}
5523
5524void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5525 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5526
5527 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005528 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00005529 locations->SetInAt(0, Location::RequiresRegister());
5530 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005531 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005532 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00005533
5534 // The register allocator does not support multiple
5535 // inputs that die at entry with one in a specific register.
5536 if (is_byte_type) {
5537 // Ensure the value is in a byte register.
5538 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005539 } else if (DataType::IsFloatingPointType(field_type)) {
5540 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05005541 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5542 locations->SetInAt(1, Location::RequiresFpuRegister());
5543 } else {
5544 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
5545 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005546 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05005547 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00005548 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005549
Calin Juravle52c48962014-12-16 17:02:57 +00005550 // 64bits value can be atomically written to an address with movsd and an XMM register.
5551 // We need two XMM registers because there's no easier way to (bit) copy a register pair
5552 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
5553 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
5554 // isolated cases when we need this it isn't worth adding the extra complexity.
5555 locations->AddTemp(Location::RequiresFpuRegister());
5556 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005557 } else {
5558 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5559
5560 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5561 // Temporary registers for the write barrier.
5562 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
5563 // Ensure the card is in a byte register.
5564 locations->AddTemp(Location::RegisterLocation(ECX));
5565 }
Calin Juravle52c48962014-12-16 17:02:57 +00005566 }
5567}
5568
5569void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005570 const FieldInfo& field_info,
5571 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005572 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5573
5574 LocationSummary* locations = instruction->GetLocations();
5575 Register base = locations->InAt(0).AsRegister<Register>();
5576 Location value = locations->InAt(1);
5577 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005578 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00005579 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005580 bool needs_write_barrier =
5581 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005582
5583 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005584 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005585 }
5586
Mark Mendell81489372015-11-04 11:30:41 -05005587 bool maybe_record_implicit_null_check_done = false;
5588
Calin Juravle52c48962014-12-16 17:02:57 +00005589 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005590 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005591 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005592 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005593 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
5594 break;
5595 }
5596
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005597 case DataType::Type::kUint16:
5598 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05005599 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005600 __ movw(Address(base, offset),
5601 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05005602 } else {
5603 __ movw(Address(base, offset), value.AsRegister<Register>());
5604 }
Calin Juravle52c48962014-12-16 17:02:57 +00005605 break;
5606 }
5607
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005608 case DataType::Type::kInt32:
5609 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01005610 if (kPoisonHeapReferences && needs_write_barrier) {
5611 // Note that in the case where `value` is a null reference,
5612 // we do not enter this block, as the reference does not
5613 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005614 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01005615 Register temp = locations->GetTemp(0).AsRegister<Register>();
5616 __ movl(temp, value.AsRegister<Register>());
5617 __ PoisonHeapReference(temp);
5618 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05005619 } else if (value.IsConstant()) {
5620 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5621 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005622 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005623 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01005624 __ movl(Address(base, offset), value.AsRegister<Register>());
5625 }
Calin Juravle52c48962014-12-16 17:02:57 +00005626 break;
5627 }
5628
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005629 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005630 if (is_volatile) {
5631 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5632 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5633 __ movd(temp1, value.AsRegisterPairLow<Register>());
5634 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5635 __ punpckldq(temp1, temp2);
5636 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005637 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005638 } else if (value.IsConstant()) {
5639 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5640 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5641 codegen_->MaybeRecordImplicitNullCheck(instruction);
5642 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005643 } else {
5644 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005645 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005646 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5647 }
Mark Mendell81489372015-11-04 11:30:41 -05005648 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005649 break;
5650 }
5651
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005652 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05005653 if (value.IsConstant()) {
5654 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5655 __ movl(Address(base, offset), Immediate(v));
5656 } else {
5657 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5658 }
Calin Juravle52c48962014-12-16 17:02:57 +00005659 break;
5660 }
5661
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005662 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005663 if (value.IsConstant()) {
5664 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5665 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5666 codegen_->MaybeRecordImplicitNullCheck(instruction);
5667 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5668 maybe_record_implicit_null_check_done = true;
5669 } else {
5670 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5671 }
Calin Juravle52c48962014-12-16 17:02:57 +00005672 break;
5673 }
5674
Aart Bik66c158e2018-01-31 12:55:04 -08005675 case DataType::Type::kUint32:
5676 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005677 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005678 LOG(FATAL) << "Unreachable type " << field_type;
5679 UNREACHABLE();
5680 }
5681
Mark Mendell81489372015-11-04 11:30:41 -05005682 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005683 codegen_->MaybeRecordImplicitNullCheck(instruction);
5684 }
5685
Roland Levillain4d027112015-07-01 15:41:14 +01005686 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005687 Register temp = locations->GetTemp(0).AsRegister<Register>();
5688 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005689 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005690 }
5691
Calin Juravle52c48962014-12-16 17:02:57 +00005692 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005693 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005694 }
5695}
5696
5697void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5698 HandleFieldGet(instruction, instruction->GetFieldInfo());
5699}
5700
5701void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5702 HandleFieldGet(instruction, instruction->GetFieldInfo());
5703}
5704
5705void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5706 HandleFieldSet(instruction, instruction->GetFieldInfo());
5707}
5708
5709void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005710 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005711}
5712
5713void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5714 HandleFieldSet(instruction, instruction->GetFieldInfo());
5715}
5716
5717void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005718 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005719}
5720
5721void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5722 HandleFieldGet(instruction, instruction->GetFieldInfo());
5723}
5724
5725void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5726 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005727}
5728
Vladimir Marko552a1342017-10-31 10:56:47 +00005729void LocationsBuilderX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5730 codegen_->CreateStringBuilderAppendLocations(instruction, Location::RegisterLocation(EAX));
5731}
5732
5733void InstructionCodeGeneratorX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5734 __ movl(EAX, Immediate(instruction->GetFormat()->GetValue()));
5735 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
5736}
5737
Calin Juravlee460d1d2015-09-29 04:52:17 +01005738void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5739 HUnresolvedInstanceFieldGet* instruction) {
5740 FieldAccessCallingConventionX86 calling_convention;
5741 codegen_->CreateUnresolvedFieldLocationSummary(
5742 instruction, instruction->GetFieldType(), calling_convention);
5743}
5744
5745void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5746 HUnresolvedInstanceFieldGet* instruction) {
5747 FieldAccessCallingConventionX86 calling_convention;
5748 codegen_->GenerateUnresolvedFieldAccess(instruction,
5749 instruction->GetFieldType(),
5750 instruction->GetFieldIndex(),
5751 instruction->GetDexPc(),
5752 calling_convention);
5753}
5754
5755void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5756 HUnresolvedInstanceFieldSet* instruction) {
5757 FieldAccessCallingConventionX86 calling_convention;
5758 codegen_->CreateUnresolvedFieldLocationSummary(
5759 instruction, instruction->GetFieldType(), calling_convention);
5760}
5761
5762void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5763 HUnresolvedInstanceFieldSet* instruction) {
5764 FieldAccessCallingConventionX86 calling_convention;
5765 codegen_->GenerateUnresolvedFieldAccess(instruction,
5766 instruction->GetFieldType(),
5767 instruction->GetFieldIndex(),
5768 instruction->GetDexPc(),
5769 calling_convention);
5770}
5771
5772void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5773 HUnresolvedStaticFieldGet* instruction) {
5774 FieldAccessCallingConventionX86 calling_convention;
5775 codegen_->CreateUnresolvedFieldLocationSummary(
5776 instruction, instruction->GetFieldType(), calling_convention);
5777}
5778
5779void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5780 HUnresolvedStaticFieldGet* instruction) {
5781 FieldAccessCallingConventionX86 calling_convention;
5782 codegen_->GenerateUnresolvedFieldAccess(instruction,
5783 instruction->GetFieldType(),
5784 instruction->GetFieldIndex(),
5785 instruction->GetDexPc(),
5786 calling_convention);
5787}
5788
5789void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5790 HUnresolvedStaticFieldSet* instruction) {
5791 FieldAccessCallingConventionX86 calling_convention;
5792 codegen_->CreateUnresolvedFieldLocationSummary(
5793 instruction, instruction->GetFieldType(), calling_convention);
5794}
5795
5796void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5797 HUnresolvedStaticFieldSet* instruction) {
5798 FieldAccessCallingConventionX86 calling_convention;
5799 codegen_->GenerateUnresolvedFieldAccess(instruction,
5800 instruction->GetFieldType(),
5801 instruction->GetFieldIndex(),
5802 instruction->GetDexPc(),
5803 calling_convention);
5804}
5805
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005806void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005807 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5808 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5809 ? Location::RequiresRegister()
5810 : Location::Any();
5811 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005812}
5813
Calin Juravle2ae48182016-03-16 14:05:09 +00005814void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5815 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005816 return;
5817 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005818 LocationSummary* locations = instruction->GetLocations();
5819 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005820
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005821 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005822 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005823}
5824
Calin Juravle2ae48182016-03-16 14:05:09 +00005825void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005826 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005827 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005828
5829 LocationSummary* locations = instruction->GetLocations();
5830 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005831
5832 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005833 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005834 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005835 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005836 } else {
5837 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005838 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005839 __ jmp(slow_path->GetEntryLabel());
5840 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005841 }
5842 __ j(kEqual, slow_path->GetEntryLabel());
5843}
5844
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005845void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005846 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005847}
5848
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005849void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005850 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005851 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005852 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005853 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5854 object_array_get_with_read_barrier
5855 ? LocationSummary::kCallOnSlowPath
5856 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005857 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005858 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005859 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005860 locations->SetInAt(0, Location::RequiresRegister());
5861 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005862 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005863 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5864 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005865 // The output overlaps in case of long: we don't want the low move
5866 // to overwrite the array's location. Likewise, in the case of an
5867 // object array get with read barriers enabled, we do not want the
5868 // move to overwrite the array's location, as we need it to emit
5869 // the read barrier.
5870 locations->SetOut(
5871 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005872 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
5873 ? Location::kOutputOverlap
5874 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005875 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005876}
5877
5878void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5879 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005880 Location obj_loc = locations->InAt(0);
5881 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005882 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005883 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005884 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005885
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005886 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00005887 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005888 case DataType::Type::kBool:
5889 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005890 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005891 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005892 break;
5893 }
5894
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005895 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005896 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005897 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005898 break;
5899 }
5900
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005901 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005902 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005903 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5904 // Branch cases into compressed and uncompressed for each index's type.
5905 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5906 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005907 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005908 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005909 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5910 "Expecting 0=compressed, 1=uncompressed");
5911 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005912 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5913 __ jmp(&done);
5914 __ Bind(&not_compressed);
5915 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5916 __ Bind(&done);
5917 } else {
5918 // Common case for charAt of array of char or when string compression's
5919 // feature is turned off.
5920 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5921 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005922 break;
5923 }
5924
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005925 case DataType::Type::kInt16: {
5926 Register out = out_loc.AsRegister<Register>();
5927 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5928 break;
5929 }
5930
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005931 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005932 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005933 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005934 break;
5935 }
5936
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005937 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005938 static_assert(
5939 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5940 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005941 // /* HeapReference<Object> */ out =
5942 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5943 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005944 // Note that a potential implicit null check is handled in this
5945 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5946 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005947 instruction, out_loc, obj, data_offset, index, /* needs_null_check= */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005948 } else {
5949 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005950 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5951 codegen_->MaybeRecordImplicitNullCheck(instruction);
5952 // If read barriers are enabled, emit read barriers other than
5953 // Baker's using a slow path (and also unpoison the loaded
5954 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005955 if (index.IsConstant()) {
5956 uint32_t offset =
5957 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005958 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5959 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005960 codegen_->MaybeGenerateReadBarrierSlow(
5961 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5962 }
5963 }
5964 break;
5965 }
5966
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005967 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005968 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005969 __ movl(out_loc.AsRegisterPairLow<Register>(),
5970 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5971 codegen_->MaybeRecordImplicitNullCheck(instruction);
5972 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5973 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005974 break;
5975 }
5976
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005977 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005978 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005979 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005980 break;
5981 }
5982
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005983 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005984 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005985 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005986 break;
5987 }
5988
Aart Bik66c158e2018-01-31 12:55:04 -08005989 case DataType::Type::kUint32:
5990 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005991 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005992 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005993 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005994 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005995
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005996 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005997 // Potential implicit null checks, in the case of reference or
5998 // long arrays, are handled in the previous switch statement.
5999 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00006000 codegen_->MaybeRecordImplicitNullCheck(instruction);
6001 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006002}
6003
6004void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006005 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006006
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006007 bool needs_write_barrier =
6008 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006009 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006010
Vladimir Markoca6fff82017-10-03 14:49:14 +01006011 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01006012 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006013 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006014
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006015 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006016 // We need the inputs to be different than the output in case of long operation.
6017 // In case of a byte operation, the register allocator does not support multiple
6018 // inputs that die at entry with one in a specific register.
6019 locations->SetInAt(0, Location::RequiresRegister());
6020 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6021 if (is_byte_type) {
6022 // Ensure the value is in a byte register.
6023 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006024 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05006025 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006026 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006027 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
6028 }
6029 if (needs_write_barrier) {
6030 // Temporary registers for the write barrier.
6031 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6032 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00006033 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006034 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006035}
6036
6037void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
6038 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006039 Location array_loc = locations->InAt(0);
6040 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006041 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006042 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006043 DataType::Type value_type = instruction->GetComponentType();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006044 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006045 bool needs_write_barrier =
6046 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006047
6048 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006049 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006050 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006051 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006052 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006053 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006054 if (value.IsRegister()) {
6055 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006056 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01006057 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006058 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006059 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006060 break;
6061 }
6062
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006063 case DataType::Type::kUint16:
6064 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006065 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006066 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006067 if (value.IsRegister()) {
6068 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006069 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01006070 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006071 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006072 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006073 break;
6074 }
6075
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006076 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006077 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006078 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006079
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006080 if (!value.IsRegister()) {
6081 // Just setting null.
6082 DCHECK(instruction->InputAt(2)->IsNullConstant());
6083 DCHECK(value.IsConstant()) << value;
6084 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00006085 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006086 DCHECK(!needs_write_barrier);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006087 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006088 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006089 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006090
6091 DCHECK(needs_write_barrier);
6092 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01006093 Location temp_loc = locations->GetTemp(0);
6094 Register temp = temp_loc.AsRegister<Register>();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006095
6096 bool can_value_be_null = instruction->GetValueCanBeNull();
6097 NearLabel do_store;
6098 if (can_value_be_null) {
6099 __ testl(register_value, register_value);
6100 __ j(kEqual, &do_store);
6101 }
6102
6103 SlowPathCode* slow_path = nullptr;
6104 if (needs_type_check) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006105 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006106 codegen_->AddSlowPath(slow_path);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006107
6108 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6109 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6110 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006111
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006112 // Note that when Baker read barriers are enabled, the type
6113 // checks are performed without read barriers. This is fine,
6114 // even in the case where a class object is in the from-space
6115 // after the flip, as a comparison involving such a type would
6116 // not produce a false positive; it may of course produce a
6117 // false negative, in which case we would take the ArraySet
6118 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01006119
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006120 // /* HeapReference<Class> */ temp = array->klass_
6121 __ movl(temp, Address(array, class_offset));
6122 codegen_->MaybeRecordImplicitNullCheck(instruction);
6123 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01006124
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006125 // /* HeapReference<Class> */ temp = temp->component_type_
6126 __ movl(temp, Address(temp, component_offset));
6127 // If heap poisoning is enabled, no need to unpoison `temp`
6128 // nor the object reference in `register_value->klass`, as
6129 // we are comparing two poisoned references.
6130 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01006131
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006132 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006133 NearLabel do_put;
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006134 __ j(kEqual, &do_put);
6135 // If heap poisoning is enabled, the `temp` reference has
6136 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006137 __ MaybeUnpoisonHeapReference(temp);
6138
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006139 // If heap poisoning is enabled, no need to unpoison the
6140 // heap reference loaded below, as it is only used for a
6141 // comparison with null.
6142 __ cmpl(Address(temp, super_offset), Immediate(0));
6143 __ j(kNotEqual, slow_path->GetEntryLabel());
6144 __ Bind(&do_put);
6145 } else {
6146 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006147 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006148 }
6149
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006150 Register card = locations->GetTemp(1).AsRegister<Register>();
6151 codegen_->MarkGCCard(
6152 temp, card, array, value.AsRegister<Register>(), /* value_can_be_null= */ false);
6153
6154 if (can_value_be_null) {
6155 DCHECK(do_store.IsLinked());
6156 __ Bind(&do_store);
6157 }
6158
6159 Register source = register_value;
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006160 if (kPoisonHeapReferences) {
6161 __ movl(temp, register_value);
6162 __ PoisonHeapReference(temp);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006163 source = temp;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006164 }
6165
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006166 __ movl(address, source);
6167
6168 if (can_value_be_null || !needs_type_check) {
6169 codegen_->MaybeRecordImplicitNullCheck(instruction);
6170 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006171
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006172 if (slow_path != nullptr) {
6173 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006174 }
6175
6176 break;
6177 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006178
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006179 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006180 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006181 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006182 if (value.IsRegister()) {
6183 __ movl(address, value.AsRegister<Register>());
6184 } else {
6185 DCHECK(value.IsConstant()) << value;
6186 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
6187 __ movl(address, Immediate(v));
6188 }
6189 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006190 break;
6191 }
6192
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006193 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006194 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006195 if (value.IsRegisterPair()) {
6196 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6197 value.AsRegisterPairLow<Register>());
6198 codegen_->MaybeRecordImplicitNullCheck(instruction);
6199 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6200 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006201 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006202 DCHECK(value.IsConstant());
6203 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
6204 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6205 Immediate(Low32Bits(val)));
6206 codegen_->MaybeRecordImplicitNullCheck(instruction);
6207 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6208 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006209 }
6210 break;
6211 }
6212
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006213 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006214 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006215 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006216 if (value.IsFpuRegister()) {
6217 __ movss(address, value.AsFpuRegister<XmmRegister>());
6218 } else {
6219 DCHECK(value.IsConstant());
6220 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
6221 __ movl(address, Immediate(v));
6222 }
6223 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006224 break;
6225 }
6226
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006227 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006228 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006229 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006230 if (value.IsFpuRegister()) {
6231 __ movsd(address, value.AsFpuRegister<XmmRegister>());
6232 } else {
6233 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006234 Address address_hi =
6235 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05006236 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
6237 __ movl(address, Immediate(Low32Bits(v)));
6238 codegen_->MaybeRecordImplicitNullCheck(instruction);
6239 __ movl(address_hi, Immediate(High32Bits(v)));
6240 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006241 break;
6242 }
6243
Aart Bik66c158e2018-01-31 12:55:04 -08006244 case DataType::Type::kUint32:
6245 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006246 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006247 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07006248 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006249 }
6250}
6251
6252void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006253 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006254 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04006255 if (!instruction->IsEmittedAtUseSite()) {
6256 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6257 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006258}
6259
6260void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04006261 if (instruction->IsEmittedAtUseSite()) {
6262 return;
6263 }
6264
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006265 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006266 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006267 Register obj = locations->InAt(0).AsRegister<Register>();
6268 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006269 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00006270 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07006271 // Mask out most significant bit in case the array is String's array of char.
6272 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006273 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006274 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006275}
6276
6277void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006278 RegisterSet caller_saves = RegisterSet::Empty();
6279 InvokeRuntimeCallingConvention calling_convention;
6280 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6281 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6282 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05006283 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04006284 HInstruction* length = instruction->InputAt(1);
6285 if (!length->IsEmittedAtUseSite()) {
6286 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6287 }
jessicahandojo4877b792016-09-08 19:49:13 -07006288 // Need register to see array's length.
6289 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6290 locations->AddTemp(Location::RequiresRegister());
6291 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006292}
6293
6294void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07006295 const bool is_string_compressed_char_at =
6296 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006297 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05006298 Location index_loc = locations->InAt(0);
6299 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006300 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006301 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006302
Mark Mendell99dbd682015-04-22 16:18:52 -04006303 if (length_loc.IsConstant()) {
6304 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
6305 if (index_loc.IsConstant()) {
6306 // BCE will remove the bounds check if we are guarenteed to pass.
6307 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6308 if (index < 0 || index >= length) {
6309 codegen_->AddSlowPath(slow_path);
6310 __ jmp(slow_path->GetEntryLabel());
6311 } else {
6312 // Some optimization after BCE may have generated this, and we should not
6313 // generate a bounds check if it is a valid range.
6314 }
6315 return;
6316 }
6317
6318 // We have to reverse the jump condition because the length is the constant.
6319 Register index_reg = index_loc.AsRegister<Register>();
6320 __ cmpl(index_reg, Immediate(length));
6321 codegen_->AddSlowPath(slow_path);
6322 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006323 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04006324 HInstruction* array_length = instruction->InputAt(1);
6325 if (array_length->IsEmittedAtUseSite()) {
6326 // Address the length field in the array.
6327 DCHECK(array_length->IsArrayLength());
6328 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
6329 Location array_loc = array_length->GetLocations()->InAt(0);
6330 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07006331 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006332 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
6333 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07006334 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
6335 __ movl(length_reg, array_len);
6336 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006337 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006338 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04006339 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006340 // Checking bounds for general case:
6341 // Array of char or string's array with feature compression off.
6342 if (index_loc.IsConstant()) {
6343 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6344 __ cmpl(array_len, Immediate(value));
6345 } else {
6346 __ cmpl(array_len, index_loc.AsRegister<Register>());
6347 }
6348 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04006349 }
Mark Mendell99dbd682015-04-22 16:18:52 -04006350 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006351 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04006352 }
6353 codegen_->AddSlowPath(slow_path);
6354 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006355 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006356}
6357
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006358void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006359 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006360}
6361
6362void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006363 if (instruction->GetNext()->IsSuspendCheck() &&
6364 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6365 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6366 // The back edge will generate the suspend check.
6367 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6368 }
6369
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006370 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6371}
6372
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006373void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006374 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6375 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07006376 // In suspend check slow path, usually there are no caller-save registers at all.
6377 // If SIMD instructions are present, however, we force spilling all live SIMD
6378 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07006379 locations->SetCustomSlowPathCallerSaves(
6380 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006381}
6382
6383void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006384 HBasicBlock* block = instruction->GetBlock();
6385 if (block->GetLoopInformation() != nullptr) {
6386 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6387 // The back edge will generate the suspend check.
6388 return;
6389 }
6390 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6391 // The goto will generate the suspend check.
6392 return;
6393 }
6394 GenerateSuspendCheck(instruction, nullptr);
6395}
6396
6397void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
6398 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006399 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006400 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
6401 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006402 slow_path =
6403 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006404 instruction->SetSlowPath(slow_path);
6405 codegen_->AddSlowPath(slow_path);
6406 if (successor != nullptr) {
6407 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006408 }
6409 } else {
6410 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6411 }
6412
Andreas Gampe542451c2016-07-26 09:02:02 -07006413 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006414 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006415 if (successor == nullptr) {
6416 __ j(kNotEqual, slow_path->GetEntryLabel());
6417 __ Bind(slow_path->GetReturnLabel());
6418 } else {
6419 __ j(kEqual, codegen_->GetLabelOf(successor));
6420 __ jmp(slow_path->GetEntryLabel());
6421 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006422}
6423
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006424X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
6425 return codegen_->GetAssembler();
6426}
6427
Aart Bikcfe50bb2017-12-12 14:54:12 -08006428void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006429 ScratchRegisterScope ensure_scratch(
6430 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6431 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6432 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05006433
Aart Bikcfe50bb2017-12-12 14:54:12 -08006434 // Now that temp register is available (possibly spilled), move blocks of memory.
6435 for (int i = 0; i < number_of_words; i++) {
6436 __ movl(temp_reg, Address(ESP, src + stack_offset));
6437 __ movl(Address(ESP, dst + stack_offset), temp_reg);
6438 stack_offset += kX86WordSize;
6439 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006440}
6441
6442void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006443 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006444 Location source = move->GetSource();
6445 Location destination = move->GetDestination();
6446
6447 if (source.IsRegister()) {
6448 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006449 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006450 } else if (destination.IsFpuRegister()) {
6451 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006452 } else {
6453 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006454 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006455 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006456 } else if (source.IsRegisterPair()) {
Vladimir Marko86c87522020-05-11 16:55:55 +01006457 if (destination.IsRegisterPair()) {
6458 __ movl(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
6459 DCHECK_NE(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
6460 __ movl(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
6461 } else if (destination.IsFpuRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006462 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Vladimir Markodec78172020-06-19 15:31:23 +01006463 // Push the 2 source registers to the stack.
Vladimir Marko86c87522020-05-11 16:55:55 +01006464 __ pushl(source.AsRegisterPairHigh<Register>());
6465 __ cfi().AdjustCFAOffset(elem_size);
6466 __ pushl(source.AsRegisterPairLow<Register>());
6467 __ cfi().AdjustCFAOffset(elem_size);
6468 // Load the destination register.
David Brazdil74eb1b22015-12-14 11:44:01 +00006469 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
6470 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01006471 codegen_->DecreaseFrame(2 * elem_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01006472 } else {
6473 DCHECK(destination.IsDoubleStackSlot());
6474 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
6475 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
6476 source.AsRegisterPairHigh<Register>());
6477 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006478 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006479 if (destination.IsRegister()) {
6480 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
6481 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006482 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006483 } else if (destination.IsRegisterPair()) {
Vladimir Marko86c87522020-05-11 16:55:55 +01006484 size_t elem_size = DataType::Size(DataType::Type::kInt32);
6485 // Create stack space for 2 elements.
Vladimir Markodec78172020-06-19 15:31:23 +01006486 codegen_->IncreaseFrame(2 * elem_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01006487 // Store the source register.
6488 __ movsd(Address(ESP, 0), source.AsFpuRegister<XmmRegister>());
6489 // And pop the values into destination registers.
6490 __ popl(destination.AsRegisterPairLow<Register>());
6491 __ cfi().AdjustCFAOffset(-elem_size);
6492 __ popl(destination.AsRegisterPairHigh<Register>());
6493 __ cfi().AdjustCFAOffset(-elem_size);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006494 } else if (destination.IsStackSlot()) {
6495 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006496 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006497 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006498 } else {
6499 DCHECK(destination.IsSIMDStackSlot());
6500 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05006501 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006502 } else if (source.IsStackSlot()) {
6503 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006504 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006505 } else if (destination.IsFpuRegister()) {
6506 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006507 } else {
6508 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08006509 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006510 }
6511 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006512 if (destination.IsRegisterPair()) {
6513 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
6514 __ movl(destination.AsRegisterPairHigh<Register>(),
6515 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
6516 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006517 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6518 } else {
6519 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006520 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006521 }
Aart Bik5576f372017-03-23 16:17:37 -07006522 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006523 if (destination.IsFpuRegister()) {
6524 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6525 } else {
6526 DCHECK(destination.IsSIMDStackSlot());
6527 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6528 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006529 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006530 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006531 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006532 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006533 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006534 if (value == 0) {
6535 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
6536 } else {
6537 __ movl(destination.AsRegister<Register>(), Immediate(value));
6538 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006539 } else {
6540 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05006541 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006542 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006543 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006544 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006545 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006546 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006547 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006548 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6549 if (value == 0) {
6550 // Easy handling of 0.0.
6551 __ xorps(dest, dest);
6552 } else {
6553 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006554 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6555 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
6556 __ movl(temp, Immediate(value));
6557 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006558 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006559 } else {
6560 DCHECK(destination.IsStackSlot()) << destination;
6561 __ movl(Address(ESP, destination.GetStackIndex()), imm);
6562 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006563 } else if (constant->IsLongConstant()) {
6564 int64_t value = constant->AsLongConstant()->GetValue();
6565 int32_t low_value = Low32Bits(value);
6566 int32_t high_value = High32Bits(value);
6567 Immediate low(low_value);
6568 Immediate high(high_value);
6569 if (destination.IsDoubleStackSlot()) {
6570 __ movl(Address(ESP, destination.GetStackIndex()), low);
6571 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6572 } else {
6573 __ movl(destination.AsRegisterPairLow<Register>(), low);
6574 __ movl(destination.AsRegisterPairHigh<Register>(), high);
6575 }
6576 } else {
6577 DCHECK(constant->IsDoubleConstant());
6578 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006579 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006580 int32_t low_value = Low32Bits(value);
6581 int32_t high_value = High32Bits(value);
6582 Immediate low(low_value);
6583 Immediate high(high_value);
6584 if (destination.IsFpuRegister()) {
6585 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6586 if (value == 0) {
6587 // Easy handling of 0.0.
6588 __ xorpd(dest, dest);
6589 } else {
6590 __ pushl(high);
Vladimir Marko86c87522020-05-11 16:55:55 +01006591 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006592 __ pushl(low);
Vladimir Marko86c87522020-05-11 16:55:55 +01006593 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006594 __ movsd(dest, Address(ESP, 0));
Vladimir Markodec78172020-06-19 15:31:23 +01006595 codegen_->DecreaseFrame(8);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006596 }
6597 } else {
6598 DCHECK(destination.IsDoubleStackSlot()) << destination;
6599 __ movl(Address(ESP, destination.GetStackIndex()), low);
6600 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6601 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006602 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006603 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006604 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006605 }
6606}
6607
Mark Mendella5c19ce2015-04-01 12:51:05 -04006608void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006609 Register suggested_scratch = reg == EAX ? EBX : EAX;
6610 ScratchRegisterScope ensure_scratch(
6611 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
6612
6613 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6614 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
6615 __ movl(Address(ESP, mem + stack_offset), reg);
6616 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006617}
6618
Mark Mendell7c8d0092015-01-26 11:21:33 -05006619void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006620 ScratchRegisterScope ensure_scratch(
6621 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6622
6623 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6624 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6625 __ movl(temp_reg, Address(ESP, mem + stack_offset));
6626 __ movss(Address(ESP, mem + stack_offset), reg);
6627 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006628}
6629
Aart Bikcfe50bb2017-12-12 14:54:12 -08006630void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
6631 size_t extra_slot = 4 * kX86WordSize;
Vladimir Markodec78172020-06-19 15:31:23 +01006632 codegen_->IncreaseFrame(extra_slot);
Aart Bikcfe50bb2017-12-12 14:54:12 -08006633 __ movups(Address(ESP, 0), XmmRegister(reg));
6634 ExchangeMemory(0, mem + extra_slot, 4);
6635 __ movups(XmmRegister(reg), Address(ESP, 0));
Vladimir Markodec78172020-06-19 15:31:23 +01006636 codegen_->DecreaseFrame(extra_slot);
Aart Bikcfe50bb2017-12-12 14:54:12 -08006637}
6638
6639void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006640 ScratchRegisterScope ensure_scratch1(
6641 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006642
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006643 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
6644 ScratchRegisterScope ensure_scratch2(
6645 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006646
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006647 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
6648 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006649
6650 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
6651 for (int i = 0; i < number_of_words; i++) {
6652 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
6653 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
6654 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
6655 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
6656 stack_offset += kX86WordSize;
6657 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006658}
6659
6660void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006661 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006662 Location source = move->GetSource();
6663 Location destination = move->GetDestination();
6664
6665 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04006666 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
6667 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
6668 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
6669 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
6670 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006671 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006672 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006673 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006674 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006675 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006676 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006677 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
6678 // Use XOR Swap algorithm to avoid a temporary.
6679 DCHECK_NE(source.reg(), destination.reg());
6680 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6681 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
6682 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6683 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
6684 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6685 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
6686 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006687 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
6688 // Take advantage of the 16 bytes in the XMM register.
6689 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
6690 Address stack(ESP, destination.GetStackIndex());
6691 // Load the double into the high doubleword.
6692 __ movhpd(reg, stack);
6693
6694 // Store the low double into the destination.
6695 __ movsd(stack, reg);
6696
6697 // Move the high double to the low double.
6698 __ psrldq(reg, Immediate(8));
6699 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6700 // Take advantage of the 16 bytes in the XMM register.
6701 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6702 Address stack(ESP, source.GetStackIndex());
6703 // Load the double into the high doubleword.
6704 __ movhpd(reg, stack);
6705
6706 // Store the low double into the destination.
6707 __ movsd(stack, reg);
6708
6709 // Move the high double to the low double.
6710 __ psrldq(reg, Immediate(8));
6711 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006712 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
6713 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
6714 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6715 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
6716 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6717 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
6718 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006719 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006720 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006721 }
6722}
6723
6724void ParallelMoveResolverX86::SpillScratch(int reg) {
6725 __ pushl(static_cast<Register>(reg));
6726}
6727
6728void ParallelMoveResolverX86::RestoreScratch(int reg) {
6729 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006730}
6731
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006732HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6733 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006734 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006735 case HLoadClass::LoadKind::kInvalid:
6736 LOG(FATAL) << "UNREACHABLE";
6737 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006738 case HLoadClass::LoadKind::kReferrersClass:
6739 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006740 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006741 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006742 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01006743 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006744 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006745 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006746 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01006747 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006748 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006749 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006750 break;
6751 }
6752 return desired_class_load_kind;
6753}
6754
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006755void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006756 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006757 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006758 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006759 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006760 cls,
6761 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006762 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006763 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006764 return;
6765 }
Vladimir Marko41559982017-01-06 14:04:23 +00006766 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006767
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006768 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6769 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006770 ? LocationSummary::kCallOnSlowPath
6771 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006772 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006773 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006774 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006775 }
6776
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006777 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006778 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006779 load_kind == HLoadClass::LoadKind::kBootImageRelRo ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006780 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006781 locations->SetInAt(0, Location::RequiresRegister());
6782 }
6783 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006784 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6785 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6786 // Rely on the type resolution and/or initialization to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006787 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006788 } else {
6789 // For non-Baker read barrier we have a temp-clobbering call.
6790 }
6791 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006792}
6793
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006794Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006795 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006796 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006797 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006798 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006799 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006800 PatchInfo<Label>* info = &jit_class_patches_.back();
6801 return &info->label;
6802}
6803
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006804// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6805// move.
6806void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006807 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006808 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006809 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006810 return;
6811 }
Vladimir Marko41559982017-01-06 14:04:23 +00006812 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006813
Vladimir Marko41559982017-01-06 14:04:23 +00006814 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006815 Location out_loc = locations->Out();
6816 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006817
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006818 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006819 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6820 ? kWithoutReadBarrier
6821 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006822 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006823 case HLoadClass::LoadKind::kReferrersClass: {
6824 DCHECK(!cls->CanCallRuntime());
6825 DCHECK(!cls->MustGenerateClinitCheck());
6826 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6827 Register current_method = locations->InAt(0).AsRegister<Register>();
6828 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006829 cls,
6830 out_loc,
6831 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Andreas Gampe3db70682018-12-26 15:12:03 -08006832 /* fixup_label= */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006833 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006834 break;
6835 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006836 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01006837 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
6838 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006839 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006840 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00006841 __ leal(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006842 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006843 break;
6844 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006845 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006846 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6847 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00006848 __ movl(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006849 codegen_->RecordBootImageRelRoPatch(cls->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6850 codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006851 break;
6852 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006853 case HLoadClass::LoadKind::kBssEntry: {
6854 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00006855 Address address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006856 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6857 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01006858 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006859 generate_null_check = true;
6860 break;
6861 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006862 case HLoadClass::LoadKind::kJitBootImageAddress: {
6863 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
6864 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
6865 DCHECK_NE(address, 0u);
6866 __ movl(out, Immediate(address));
6867 break;
6868 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006869 case HLoadClass::LoadKind::kJitTableAddress: {
Vladimir Marko4ef451a2020-07-23 09:54:27 +00006870 Address address = Address::Absolute(CodeGeneratorX86::kPlaceholder32BitOffset);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006871 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006872 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006873 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006874 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006875 break;
6876 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006877 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006878 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006879 LOG(FATAL) << "UNREACHABLE";
6880 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006881 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006882
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006883 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6884 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006885 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006886 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006887
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006888 if (generate_null_check) {
6889 __ testl(out, out);
6890 __ j(kEqual, slow_path->GetEntryLabel());
6891 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006892
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006893 if (cls->MustGenerateClinitCheck()) {
6894 GenerateClassInitializationCheck(slow_path, out);
6895 } else {
6896 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006897 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006898 }
6899}
6900
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006901void LocationsBuilderX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6902 InvokeRuntimeCallingConvention calling_convention;
6903 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6904 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
6905}
6906
6907void InstructionCodeGeneratorX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6908 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
6909}
6910
Orion Hodson18259d72018-04-12 11:18:23 +01006911void LocationsBuilderX86::VisitLoadMethodType(HLoadMethodType* load) {
6912 InvokeRuntimeCallingConvention calling_convention;
6913 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6914 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
6915}
6916
6917void InstructionCodeGeneratorX86::VisitLoadMethodType(HLoadMethodType* load) {
6918 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
6919}
6920
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006921void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6922 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006923 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006924 locations->SetInAt(0, Location::RequiresRegister());
6925 if (check->HasUses()) {
6926 locations->SetOut(Location::SameAsFirstInput());
6927 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006928 // Rely on the type initialization to save everything we need.
6929 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006930}
6931
6932void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006933 // We assume the class to not be null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006934 SlowPathCode* slow_path =
6935 new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(check->GetLoadClass(), check);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006936 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006937 GenerateClassInitializationCheck(slow_path,
6938 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006939}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006940
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006941void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006942 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00006943 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
6944 const size_t status_byte_offset =
6945 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
Vladimir Markobf121912019-06-04 13:49:05 +01006946 constexpr uint32_t shifted_visibly_initialized_value =
6947 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << (status_lsb_position % kBitsPerByte);
Vladimir Markodc682aa2018-01-04 18:42:57 +00006948
Vladimir Markobf121912019-06-04 13:49:05 +01006949 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_visibly_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00006950 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006951 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006952}
6953
Vladimir Marko175e7862018-03-27 09:03:13 +00006954void InstructionCodeGeneratorX86::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
6955 Register temp) {
6956 uint32_t path_to_root = check->GetBitstringPathToRoot();
6957 uint32_t mask = check->GetBitstringMask();
6958 DCHECK(IsPowerOfTwo(mask + 1));
6959 size_t mask_bits = WhichPowerOf2(mask + 1);
6960
6961 if (mask_bits == 16u) {
6962 // Compare the bitstring in memory.
6963 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
6964 } else {
6965 // /* uint32_t */ temp = temp->status_
6966 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
6967 // Compare the bitstring bits using SUB.
6968 __ subl(temp, Immediate(path_to_root));
6969 // Shift out bits that do not contribute to the comparison.
6970 __ shll(temp, Immediate(32u - mask_bits));
6971 }
6972}
6973
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006974HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6975 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006976 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006977 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006978 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006979 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01006980 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006981 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006982 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006983 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01006984 DCHECK(GetCompilerOptions().IsJitCompiler());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006985 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006986 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006987 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006988 }
6989 return desired_string_load_kind;
6990}
6991
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006992void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006993 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006994 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006995 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006996 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006997 load_kind == HLoadString::LoadKind::kBootImageRelRo ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006998 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006999 locations->SetInAt(0, Location::RequiresRegister());
7000 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007001 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007002 locations->SetOut(Location::RegisterLocation(EAX));
7003 } else {
7004 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007005 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7006 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007007 // Rely on the pResolveString to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007008 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007009 } else {
7010 // For non-Baker read barrier we have a temp-clobbering call.
7011 }
7012 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007013 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007014}
7015
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007016Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01007017 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007018 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007019 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007020 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007021 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007022 PatchInfo<Label>* info = &jit_string_patches_.back();
7023 return &info->label;
7024}
7025
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007026// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7027// move.
7028void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01007029 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007030 Location out_loc = locations->Out();
7031 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007032
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007033 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007034 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007035 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7036 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007037 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007038 __ leal(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007039 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007040 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007041 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007042 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007043 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7044 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007045 __ movl(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007046 codegen_->RecordBootImageRelRoPatch(load->InputAt(0)->AsX86ComputeBaseMethodAddress(),
7047 codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007048 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007049 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007050 case HLoadString::LoadKind::kBssEntry: {
7051 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007052 Address address = Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007053 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007054 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007055 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007056 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007057 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007058 codegen_->AddSlowPath(slow_path);
7059 __ testl(out, out);
7060 __ j(kEqual, slow_path->GetEntryLabel());
7061 __ Bind(slow_path->GetExitLabel());
7062 return;
7063 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007064 case HLoadString::LoadKind::kJitBootImageAddress: {
7065 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
7066 DCHECK_NE(address, 0u);
7067 __ movl(out, Immediate(address));
7068 return;
7069 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007070 case HLoadString::LoadKind::kJitTableAddress: {
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007071 Address address = Address::Absolute(CodeGeneratorX86::kPlaceholder32BitOffset);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007072 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007073 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007074 // /* GcRoot<mirror::String> */ out = *address
7075 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
7076 return;
7077 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007078 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007079 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007080 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007081
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007082 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007083 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007084 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007085 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007086 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7087 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007088}
7089
David Brazdilcb1c0552015-08-04 16:22:25 +01007090static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007091 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01007092}
7093
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007094void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
7095 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007096 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007097 locations->SetOut(Location::RequiresRegister());
7098}
7099
7100void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01007101 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
7102}
7103
7104void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007105 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01007106}
7107
7108void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7109 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007110}
7111
7112void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007113 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7114 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007115 InvokeRuntimeCallingConvention calling_convention;
7116 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7117}
7118
7119void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007120 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007121 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007122}
7123
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007124// Temp is used for read barrier.
7125static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7126 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00007127 !kUseBakerReadBarrier &&
7128 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00007129 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007130 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7131 return 1;
7132 }
7133 return 0;
7134}
7135
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007136// Interface case has 2 temps, one for holding the number of interfaces, one for the current
7137// interface pointer, the current interface is compared in memory.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007138// The other checks have one temp for loading the object's class.
7139static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007140 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007141 return 2;
7142 }
7143 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007144}
7145
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007146void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007147 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007148 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01007149 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007150 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007151 case TypeCheckKind::kExactCheck:
7152 case TypeCheckKind::kAbstractClassCheck:
7153 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007154 case TypeCheckKind::kArrayObjectCheck: {
7155 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7156 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7157 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007158 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007159 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007160 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007161 case TypeCheckKind::kUnresolvedCheck:
7162 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007163 call_kind = LocationSummary::kCallOnSlowPath;
7164 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007165 case TypeCheckKind::kBitstringCheck:
7166 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007167 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007168
Vladimir Markoca6fff82017-10-03 14:49:14 +01007169 LocationSummary* locations =
7170 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01007171 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007172 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007173 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007174 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007175 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7176 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7177 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7178 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7179 } else {
7180 locations->SetInAt(1, Location::Any());
7181 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007182 // Note that TypeCheckSlowPathX86 uses this "out" register too.
7183 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007184 // When read barriers are enabled, we need a temporary register for some cases.
7185 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007186}
7187
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007188void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007189 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007190 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007191 Location obj_loc = locations->InAt(0);
7192 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007193 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007194 Location out_loc = locations->Out();
7195 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007196 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7197 DCHECK_LE(num_temps, 1u);
7198 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007199 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007200 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7201 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7202 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07007203 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007204 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007205
7206 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007207 // Avoid null check if we know obj is not null.
7208 if (instruction->MustDoNullCheck()) {
7209 __ testl(obj, obj);
7210 __ j(kEqual, &zero);
7211 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007212
Roland Levillain7c1559a2015-12-15 10:55:36 +00007213 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007214 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007215 ReadBarrierOption read_barrier_option =
7216 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007217 // /* HeapReference<Class> */ out = obj->klass_
7218 GenerateReferenceLoadTwoRegisters(instruction,
7219 out_loc,
7220 obj_loc,
7221 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007222 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007223 if (cls.IsRegister()) {
7224 __ cmpl(out, cls.AsRegister<Register>());
7225 } else {
7226 DCHECK(cls.IsStackSlot()) << cls;
7227 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7228 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007229
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007230 // Classes must be equal for the instanceof to succeed.
7231 __ j(kNotEqual, &zero);
7232 __ movl(out, Immediate(1));
7233 __ jmp(&done);
7234 break;
7235 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007236
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007237 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007238 ReadBarrierOption read_barrier_option =
7239 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007240 // /* HeapReference<Class> */ out = obj->klass_
7241 GenerateReferenceLoadTwoRegisters(instruction,
7242 out_loc,
7243 obj_loc,
7244 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007245 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007246 // If the class is abstract, we eagerly fetch the super class of the
7247 // object to avoid doing a comparison we know will fail.
7248 NearLabel loop;
7249 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007250 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007251 GenerateReferenceLoadOneRegister(instruction,
7252 out_loc,
7253 super_offset,
7254 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007255 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007256 __ testl(out, out);
7257 // If `out` is null, we use it for the result, and jump to `done`.
7258 __ j(kEqual, &done);
7259 if (cls.IsRegister()) {
7260 __ cmpl(out, cls.AsRegister<Register>());
7261 } else {
7262 DCHECK(cls.IsStackSlot()) << cls;
7263 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7264 }
7265 __ j(kNotEqual, &loop);
7266 __ movl(out, Immediate(1));
7267 if (zero.IsLinked()) {
7268 __ jmp(&done);
7269 }
7270 break;
7271 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007272
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007273 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007274 ReadBarrierOption read_barrier_option =
7275 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007276 // /* HeapReference<Class> */ out = obj->klass_
7277 GenerateReferenceLoadTwoRegisters(instruction,
7278 out_loc,
7279 obj_loc,
7280 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007281 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007282 // Walk over the class hierarchy to find a match.
7283 NearLabel loop, success;
7284 __ Bind(&loop);
7285 if (cls.IsRegister()) {
7286 __ cmpl(out, cls.AsRegister<Register>());
7287 } else {
7288 DCHECK(cls.IsStackSlot()) << cls;
7289 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7290 }
7291 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007292 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007293 GenerateReferenceLoadOneRegister(instruction,
7294 out_loc,
7295 super_offset,
7296 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007297 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007298 __ testl(out, out);
7299 __ j(kNotEqual, &loop);
7300 // If `out` is null, we use it for the result, and jump to `done`.
7301 __ jmp(&done);
7302 __ Bind(&success);
7303 __ movl(out, Immediate(1));
7304 if (zero.IsLinked()) {
7305 __ jmp(&done);
7306 }
7307 break;
7308 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007309
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007310 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007311 ReadBarrierOption read_barrier_option =
7312 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007313 // /* HeapReference<Class> */ out = obj->klass_
7314 GenerateReferenceLoadTwoRegisters(instruction,
7315 out_loc,
7316 obj_loc,
7317 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007318 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007319 // Do an exact check.
7320 NearLabel exact_check;
7321 if (cls.IsRegister()) {
7322 __ cmpl(out, cls.AsRegister<Register>());
7323 } else {
7324 DCHECK(cls.IsStackSlot()) << cls;
7325 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7326 }
7327 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007328 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007329 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007330 GenerateReferenceLoadOneRegister(instruction,
7331 out_loc,
7332 component_offset,
7333 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007334 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007335 __ testl(out, out);
7336 // If `out` is null, we use it for the result, and jump to `done`.
7337 __ j(kEqual, &done);
7338 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
7339 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007340 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007341 __ movl(out, Immediate(1));
7342 __ jmp(&done);
7343 break;
7344 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007345
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007346 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007347 // No read barrier since the slow path will retry upon failure.
7348 // /* HeapReference<Class> */ out = obj->klass_
7349 GenerateReferenceLoadTwoRegisters(instruction,
7350 out_loc,
7351 obj_loc,
7352 class_offset,
7353 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007354 if (cls.IsRegister()) {
7355 __ cmpl(out, cls.AsRegister<Register>());
7356 } else {
7357 DCHECK(cls.IsStackSlot()) << cls;
7358 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7359 }
7360 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007361 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007362 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007363 codegen_->AddSlowPath(slow_path);
7364 __ j(kNotEqual, slow_path->GetEntryLabel());
7365 __ movl(out, Immediate(1));
7366 if (zero.IsLinked()) {
7367 __ jmp(&done);
7368 }
7369 break;
7370 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007371
Calin Juravle98893e12015-10-02 21:05:03 +01007372 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007373 case TypeCheckKind::kInterfaceCheck: {
7374 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007375 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00007376 // cases.
7377 //
7378 // We cannot directly call the InstanceofNonTrivial runtime
7379 // entry point without resorting to a type checking slow path
7380 // here (i.e. by calling InvokeRuntime directly), as it would
7381 // require to assign fixed registers for the inputs of this
7382 // HInstanceOf instruction (following the runtime calling
7383 // convention), which might be cluttered by the potential first
7384 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007385 //
7386 // TODO: Introduce a new runtime entry point taking the object
7387 // to test (instead of its class) as argument, and let it deal
7388 // with the read barrier issues. This will let us refactor this
7389 // case of the `switch` code as it was previously (with a direct
7390 // call to the runtime not using a type checking slow path).
7391 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007392 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007393 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007394 instruction, /* is_fatal= */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007395 codegen_->AddSlowPath(slow_path);
7396 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007397 if (zero.IsLinked()) {
7398 __ jmp(&done);
7399 }
7400 break;
7401 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007402
7403 case TypeCheckKind::kBitstringCheck: {
7404 // /* HeapReference<Class> */ temp = obj->klass_
7405 GenerateReferenceLoadTwoRegisters(instruction,
7406 out_loc,
7407 obj_loc,
7408 class_offset,
7409 kWithoutReadBarrier);
7410
7411 GenerateBitstringTypeCheckCompare(instruction, out);
7412 __ j(kNotEqual, &zero);
7413 __ movl(out, Immediate(1));
7414 __ jmp(&done);
7415 break;
7416 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007417 }
7418
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007419 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007420 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007421 __ xorl(out, out);
7422 }
7423
7424 if (done.IsLinked()) {
7425 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007426 }
7427
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007428 if (slow_path != nullptr) {
7429 __ Bind(slow_path->GetExitLabel());
7430 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007431}
7432
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007433void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007434 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007435 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007436 LocationSummary* locations =
7437 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007438 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007439 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7440 // Require a register for the interface check since there is a loop that compares the class to
7441 // a memory address.
7442 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007443 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7444 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7445 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7446 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007447 } else {
7448 locations->SetInAt(1, Location::Any());
7449 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007450 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007451 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
7452}
7453
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007454void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007455 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007456 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007457 Location obj_loc = locations->InAt(0);
7458 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007459 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007460 Location temp_loc = locations->GetTemp(0);
7461 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007462 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7463 DCHECK_GE(num_temps, 1u);
7464 DCHECK_LE(num_temps, 2u);
7465 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7466 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7467 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7468 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7469 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7470 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7471 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7472 const uint32_t object_array_data_offset =
7473 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007474
Vladimir Marko87584542017-12-12 17:47:52 +00007475 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007476 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007477 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7478 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007479 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007480
Roland Levillain0d5a2812015-11-13 10:07:31 +00007481 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007482 // Avoid null check if we know obj is not null.
7483 if (instruction->MustDoNullCheck()) {
7484 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007485 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007486 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007487
Roland Levillain0d5a2812015-11-13 10:07:31 +00007488 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007489 case TypeCheckKind::kExactCheck:
7490 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007491 // /* HeapReference<Class> */ temp = obj->klass_
7492 GenerateReferenceLoadTwoRegisters(instruction,
7493 temp_loc,
7494 obj_loc,
7495 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007496 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007497
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007498 if (cls.IsRegister()) {
7499 __ cmpl(temp, cls.AsRegister<Register>());
7500 } else {
7501 DCHECK(cls.IsStackSlot()) << cls;
7502 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7503 }
7504 // Jump to slow path for throwing the exception or doing a
7505 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007506 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007507 break;
7508 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007509
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007510 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007511 // /* HeapReference<Class> */ temp = obj->klass_
7512 GenerateReferenceLoadTwoRegisters(instruction,
7513 temp_loc,
7514 obj_loc,
7515 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007516 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007517
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007518 // If the class is abstract, we eagerly fetch the super class of the
7519 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007520 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007521 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007522 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007523 GenerateReferenceLoadOneRegister(instruction,
7524 temp_loc,
7525 super_offset,
7526 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007527 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007528
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007529 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7530 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007531 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007532 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007533
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007534 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007535 if (cls.IsRegister()) {
7536 __ cmpl(temp, cls.AsRegister<Register>());
7537 } else {
7538 DCHECK(cls.IsStackSlot()) << cls;
7539 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7540 }
7541 __ j(kNotEqual, &loop);
7542 break;
7543 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007544
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007545 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007546 // /* HeapReference<Class> */ temp = obj->klass_
7547 GenerateReferenceLoadTwoRegisters(instruction,
7548 temp_loc,
7549 obj_loc,
7550 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007551 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007552
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007553 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007554 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007555 __ Bind(&loop);
7556 if (cls.IsRegister()) {
7557 __ cmpl(temp, cls.AsRegister<Register>());
7558 } else {
7559 DCHECK(cls.IsStackSlot()) << cls;
7560 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7561 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007562 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007563
Roland Levillain0d5a2812015-11-13 10:07:31 +00007564 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007565 GenerateReferenceLoadOneRegister(instruction,
7566 temp_loc,
7567 super_offset,
7568 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007569 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007570
7571 // If the class reference currently in `temp` is not null, jump
7572 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007573 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007574 __ j(kNotZero, &loop);
7575 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007576 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007577 break;
7578 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007579
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007580 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007581 // /* HeapReference<Class> */ temp = obj->klass_
7582 GenerateReferenceLoadTwoRegisters(instruction,
7583 temp_loc,
7584 obj_loc,
7585 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007586 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007587
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007588 // Do an exact check.
7589 if (cls.IsRegister()) {
7590 __ cmpl(temp, cls.AsRegister<Register>());
7591 } else {
7592 DCHECK(cls.IsStackSlot()) << cls;
7593 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7594 }
7595 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007596
7597 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007598 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007599 GenerateReferenceLoadOneRegister(instruction,
7600 temp_loc,
7601 component_offset,
7602 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007603 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007604
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007605 // If the component type is null (i.e. the object not an array), jump to the slow path to
7606 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007607 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007608 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007609
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007610 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007611 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007612 break;
7613 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007614
Calin Juravle98893e12015-10-02 21:05:03 +01007615 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007616 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007617 // We cannot directly call the CheckCast runtime entry point
7618 // without resorting to a type checking slow path here (i.e. by
7619 // calling InvokeRuntime directly), as it would require to
7620 // assign fixed registers for the inputs of this HInstanceOf
7621 // instruction (following the runtime calling convention), which
7622 // might be cluttered by the potential first read barrier
7623 // emission at the beginning of this method.
7624 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007625 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007626
7627 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007628 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
7629 // We can not get false positives by doing this.
7630 // /* HeapReference<Class> */ temp = obj->klass_
7631 GenerateReferenceLoadTwoRegisters(instruction,
7632 temp_loc,
7633 obj_loc,
7634 class_offset,
7635 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007636
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007637 // /* HeapReference<Class> */ temp = temp->iftable_
7638 GenerateReferenceLoadTwoRegisters(instruction,
7639 temp_loc,
7640 temp_loc,
7641 iftable_offset,
7642 kWithoutReadBarrier);
7643 // Iftable is never null.
7644 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
7645 // Maybe poison the `cls` for direct comparison with memory.
7646 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
7647 // Loop through the iftable and check if any class matches.
7648 NearLabel start_loop;
7649 __ Bind(&start_loop);
7650 // Need to subtract first to handle the empty array case.
7651 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
7652 __ j(kNegative, type_check_slow_path->GetEntryLabel());
7653 // Go to next interface if the classes do not match.
7654 __ cmpl(cls.AsRegister<Register>(),
7655 CodeGeneratorX86::ArrayAddress(temp,
7656 maybe_temp2_loc,
7657 TIMES_4,
7658 object_array_data_offset));
7659 __ j(kNotEqual, &start_loop);
7660 // If `cls` was poisoned above, unpoison it.
7661 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007662 break;
7663 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007664
7665 case TypeCheckKind::kBitstringCheck: {
7666 // /* HeapReference<Class> */ temp = obj->klass_
7667 GenerateReferenceLoadTwoRegisters(instruction,
7668 temp_loc,
7669 obj_loc,
7670 class_offset,
7671 kWithoutReadBarrier);
7672
7673 GenerateBitstringTypeCheckCompare(instruction, temp);
7674 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
7675 break;
7676 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007677 }
7678 __ Bind(&done);
7679
Roland Levillain0d5a2812015-11-13 10:07:31 +00007680 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007681}
7682
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007683void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007684 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7685 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007686 InvokeRuntimeCallingConvention calling_convention;
7687 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7688}
7689
7690void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007691 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
7692 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01007693 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01007694 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007695 if (instruction->IsEnter()) {
7696 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7697 } else {
7698 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7699 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007700}
7701
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307702void LocationsBuilderX86::VisitX86AndNot(HX86AndNot* instruction) {
7703 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7704 DCHECK(DataType::IsIntOrLongType(instruction->GetType())) << instruction->GetType();
7705 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7706 locations->SetInAt(0, Location::RequiresRegister());
7707 locations->SetInAt(1, Location::RequiresRegister());
7708 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7709}
7710
7711void InstructionCodeGeneratorX86::VisitX86AndNot(HX86AndNot* instruction) {
7712 LocationSummary* locations = instruction->GetLocations();
7713 Location first = locations->InAt(0);
7714 Location second = locations->InAt(1);
7715 Location dest = locations->Out();
7716 if (instruction->GetResultType() == DataType::Type::kInt32) {
7717 __ andn(dest.AsRegister<Register>(),
7718 first.AsRegister<Register>(),
7719 second.AsRegister<Register>());
7720 } else {
7721 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
7722 __ andn(dest.AsRegisterPairLow<Register>(),
7723 first.AsRegisterPairLow<Register>(),
7724 second.AsRegisterPairLow<Register>());
7725 __ andn(dest.AsRegisterPairHigh<Register>(),
7726 first.AsRegisterPairHigh<Register>(),
7727 second.AsRegisterPairHigh<Register>());
7728 }
7729}
7730
7731void LocationsBuilderX86::VisitX86MaskOrResetLeastSetBit(HX86MaskOrResetLeastSetBit* instruction) {
7732 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7733 DCHECK(instruction->GetType() == DataType::Type::kInt32) << instruction->GetType();
7734 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7735 locations->SetInAt(0, Location::RequiresRegister());
7736 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7737}
7738
7739void InstructionCodeGeneratorX86::VisitX86MaskOrResetLeastSetBit(
7740 HX86MaskOrResetLeastSetBit* instruction) {
7741 LocationSummary* locations = instruction->GetLocations();
7742 Location src = locations->InAt(0);
7743 Location dest = locations->Out();
7744 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
7745 switch (instruction->GetOpKind()) {
7746 case HInstruction::kAnd:
7747 __ blsr(dest.AsRegister<Register>(), src.AsRegister<Register>());
7748 break;
7749 case HInstruction::kXor:
7750 __ blsmsk(dest.AsRegister<Register>(), src.AsRegister<Register>());
7751 break;
7752 default:
7753 LOG(FATAL) << "Unreachable";
7754 }
7755}
7756
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007757void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
7758void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
7759void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
7760
7761void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7762 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007763 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007764 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7765 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007766 locations->SetInAt(0, Location::RequiresRegister());
7767 locations->SetInAt(1, Location::Any());
7768 locations->SetOut(Location::SameAsFirstInput());
7769}
7770
7771void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
7772 HandleBitwiseOperation(instruction);
7773}
7774
7775void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
7776 HandleBitwiseOperation(instruction);
7777}
7778
7779void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
7780 HandleBitwiseOperation(instruction);
7781}
7782
7783void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7784 LocationSummary* locations = instruction->GetLocations();
7785 Location first = locations->InAt(0);
7786 Location second = locations->InAt(1);
7787 DCHECK(first.Equals(locations->Out()));
7788
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007789 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007790 if (second.IsRegister()) {
7791 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007792 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007793 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007794 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007795 } else {
7796 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007797 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007798 }
7799 } else if (second.IsConstant()) {
7800 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007801 __ andl(first.AsRegister<Register>(),
7802 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007803 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007804 __ orl(first.AsRegister<Register>(),
7805 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007806 } else {
7807 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00007808 __ xorl(first.AsRegister<Register>(),
7809 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007810 }
7811 } else {
7812 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007813 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007814 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007815 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007816 } else {
7817 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007818 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007819 }
7820 }
7821 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007822 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007823 if (second.IsRegisterPair()) {
7824 if (instruction->IsAnd()) {
7825 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7826 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7827 } else if (instruction->IsOr()) {
7828 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7829 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7830 } else {
7831 DCHECK(instruction->IsXor());
7832 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7833 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7834 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007835 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007836 if (instruction->IsAnd()) {
7837 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7838 __ andl(first.AsRegisterPairHigh<Register>(),
7839 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7840 } else if (instruction->IsOr()) {
7841 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7842 __ orl(first.AsRegisterPairHigh<Register>(),
7843 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7844 } else {
7845 DCHECK(instruction->IsXor());
7846 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7847 __ xorl(first.AsRegisterPairHigh<Register>(),
7848 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7849 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007850 } else {
7851 DCHECK(second.IsConstant()) << second;
7852 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007853 int32_t low_value = Low32Bits(value);
7854 int32_t high_value = High32Bits(value);
7855 Immediate low(low_value);
7856 Immediate high(high_value);
7857 Register first_low = first.AsRegisterPairLow<Register>();
7858 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007859 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007860 if (low_value == 0) {
7861 __ xorl(first_low, first_low);
7862 } else if (low_value != -1) {
7863 __ andl(first_low, low);
7864 }
7865 if (high_value == 0) {
7866 __ xorl(first_high, first_high);
7867 } else if (high_value != -1) {
7868 __ andl(first_high, high);
7869 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007870 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007871 if (low_value != 0) {
7872 __ orl(first_low, low);
7873 }
7874 if (high_value != 0) {
7875 __ orl(first_high, high);
7876 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007877 } else {
7878 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007879 if (low_value != 0) {
7880 __ xorl(first_low, low);
7881 }
7882 if (high_value != 0) {
7883 __ xorl(first_high, high);
7884 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007885 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007886 }
7887 }
7888}
7889
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007890void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7891 HInstruction* instruction,
7892 Location out,
7893 uint32_t offset,
7894 Location maybe_temp,
7895 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007896 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007897 if (read_barrier_option == kWithReadBarrier) {
7898 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007899 if (kUseBakerReadBarrier) {
7900 // Load with fast path based Baker's read barrier.
7901 // /* HeapReference<Object> */ out = *(out + offset)
7902 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08007903 instruction, out, out_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007904 } else {
7905 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007906 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007907 // in the following move operation, as we will need it for the
7908 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007909 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007910 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007911 // /* HeapReference<Object> */ out = *(out + offset)
7912 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007913 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007914 }
7915 } else {
7916 // Plain load with no read barrier.
7917 // /* HeapReference<Object> */ out = *(out + offset)
7918 __ movl(out_reg, Address(out_reg, offset));
7919 __ MaybeUnpoisonHeapReference(out_reg);
7920 }
7921}
7922
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007923void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7924 HInstruction* instruction,
7925 Location out,
7926 Location obj,
7927 uint32_t offset,
7928 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007929 Register out_reg = out.AsRegister<Register>();
7930 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007931 if (read_barrier_option == kWithReadBarrier) {
7932 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007933 if (kUseBakerReadBarrier) {
7934 // Load with fast path based Baker's read barrier.
7935 // /* HeapReference<Object> */ out = *(obj + offset)
7936 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08007937 instruction, out, obj_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007938 } else {
7939 // Load with slow path based read barrier.
7940 // /* HeapReference<Object> */ out = *(obj + offset)
7941 __ movl(out_reg, Address(obj_reg, offset));
7942 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7943 }
7944 } else {
7945 // Plain load with no read barrier.
7946 // /* HeapReference<Object> */ out = *(obj + offset)
7947 __ movl(out_reg, Address(obj_reg, offset));
7948 __ MaybeUnpoisonHeapReference(out_reg);
7949 }
7950}
7951
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007952void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7953 HInstruction* instruction,
7954 Location root,
7955 const Address& address,
7956 Label* fixup_label,
7957 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007958 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007959 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007960 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007961 if (kUseBakerReadBarrier) {
7962 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7963 // Baker's read barrier are used:
7964 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007965 // root = obj.field;
7966 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7967 // if (temp != null) {
7968 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007969 // }
7970
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007971 // /* GcRoot<mirror::Object> */ root = *address
7972 __ movl(root_reg, address);
7973 if (fixup_label != nullptr) {
7974 __ Bind(fixup_label);
7975 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007976 static_assert(
7977 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7978 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7979 "have different sizes.");
7980 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7981 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7982 "have different sizes.");
7983
Vladimir Marko953437b2016-08-24 08:30:46 +00007984 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007985 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007986 instruction, root, /* unpoison_ref_before_marking= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007987 codegen_->AddSlowPath(slow_path);
7988
Roland Levillaind966ce72017-02-09 16:20:14 +00007989 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7990 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007991 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007992 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
7993 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007994 __ j(kNotEqual, slow_path->GetEntryLabel());
7995 __ Bind(slow_path->GetExitLabel());
7996 } else {
7997 // GC root loaded through a slow path for read barriers other
7998 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007999 // /* GcRoot<mirror::Object>* */ root = address
8000 __ leal(root_reg, address);
8001 if (fixup_label != nullptr) {
8002 __ Bind(fixup_label);
8003 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008004 // /* mirror::Object* */ root = root->Read()
8005 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8006 }
8007 } else {
8008 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008009 // /* GcRoot<mirror::Object> */ root = *address
8010 __ movl(root_reg, address);
8011 if (fixup_label != nullptr) {
8012 __ Bind(fixup_label);
8013 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00008014 // Note that GC roots are not affected by heap poisoning, thus we
8015 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008016 }
8017}
8018
8019void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8020 Location ref,
8021 Register obj,
8022 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00008023 bool needs_null_check) {
8024 DCHECK(kEmitCompilerReadBarrier);
8025 DCHECK(kUseBakerReadBarrier);
8026
8027 // /* HeapReference<Object> */ ref = *(obj + offset)
8028 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00008029 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008030}
8031
8032void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8033 Location ref,
8034 Register obj,
8035 uint32_t data_offset,
8036 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00008037 bool needs_null_check) {
8038 DCHECK(kEmitCompilerReadBarrier);
8039 DCHECK(kUseBakerReadBarrier);
8040
Roland Levillain3d312422016-06-23 13:53:42 +01008041 static_assert(
8042 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8043 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00008044 // /* HeapReference<Object> */ ref =
8045 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008046 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00008047 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008048}
8049
8050void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8051 Location ref,
8052 Register obj,
8053 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008054 bool needs_null_check,
8055 bool always_update_field,
8056 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008057 DCHECK(kEmitCompilerReadBarrier);
8058 DCHECK(kUseBakerReadBarrier);
8059
8060 // In slow path based read barriers, the read barrier call is
8061 // inserted after the original load. However, in fast path based
8062 // Baker's read barriers, we need to perform the load of
8063 // mirror::Object::monitor_ *before* the original reference load.
8064 // This load-load ordering is required by the read barrier.
8065 // The fast path/slow path (for Baker's algorithm) should look like:
8066 //
8067 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8068 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8069 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008070 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00008071 // if (is_gray) {
8072 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
8073 // }
8074 //
8075 // Note: the original implementation in ReadBarrier::Barrier is
8076 // slightly more complex as:
8077 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00008078 // the high-bits of rb_state, which are expected to be all zeroes
8079 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
8080 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008081 // - it performs additional checks that we do not do here for
8082 // performance reasons.
8083
8084 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00008085 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
8086
Vladimir Marko953437b2016-08-24 08:30:46 +00008087 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01008088 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008089 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00008090 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
8091 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
8092 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
8093
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008094 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00008095 // ref = ReadBarrier::Mark(ref);
8096 // At this point, just do the "if" and make sure that flags are preserved until the branch.
8097 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00008098 if (needs_null_check) {
8099 MaybeRecordImplicitNullCheck(instruction);
8100 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008101
8102 // Load fence to prevent load-load reordering.
8103 // Note that this is a no-op, thanks to the x86 memory model.
8104 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
8105
8106 // The actual reference load.
8107 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00008108 __ movl(ref_reg, src); // Flags are unaffected.
8109
8110 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
8111 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008112 SlowPathCode* slow_path;
8113 if (always_update_field) {
8114 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01008115 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008116 instruction, ref, obj, src, /* unpoison_ref_before_marking= */ true, *temp);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008117 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008118 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008119 instruction, ref, /* unpoison_ref_before_marking= */ true);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008120 }
Vladimir Marko953437b2016-08-24 08:30:46 +00008121 AddSlowPath(slow_path);
8122
8123 // We have done the "if" of the gray bit check above, now branch based on the flags.
8124 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00008125
8126 // Object* ref = ref_addr->AsMirrorPtr()
8127 __ MaybeUnpoisonHeapReference(ref_reg);
8128
Roland Levillain7c1559a2015-12-15 10:55:36 +00008129 __ Bind(slow_path->GetExitLabel());
8130}
8131
8132void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
8133 Location out,
8134 Location ref,
8135 Location obj,
8136 uint32_t offset,
8137 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008138 DCHECK(kEmitCompilerReadBarrier);
8139
Roland Levillain7c1559a2015-12-15 10:55:36 +00008140 // Insert a slow path based read barrier *after* the reference load.
8141 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008142 // If heap poisoning is enabled, the unpoisoning of the loaded
8143 // reference will be carried out by the runtime within the slow
8144 // path.
8145 //
8146 // Note that `ref` currently does not get unpoisoned (when heap
8147 // poisoning is enabled), which is alright as the `ref` argument is
8148 // not used by the artReadBarrierSlow entry point.
8149 //
8150 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008151 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00008152 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
8153 AddSlowPath(slow_path);
8154
Roland Levillain0d5a2812015-11-13 10:07:31 +00008155 __ jmp(slow_path->GetEntryLabel());
8156 __ Bind(slow_path->GetExitLabel());
8157}
8158
Roland Levillain7c1559a2015-12-15 10:55:36 +00008159void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
8160 Location out,
8161 Location ref,
8162 Location obj,
8163 uint32_t offset,
8164 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008165 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008166 // Baker's read barriers shall be handled by the fast path
8167 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
8168 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008169 // If heap poisoning is enabled, unpoisoning will be taken care of
8170 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008171 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008172 } else if (kPoisonHeapReferences) {
8173 __ UnpoisonHeapReference(out.AsRegister<Register>());
8174 }
8175}
8176
Roland Levillain7c1559a2015-12-15 10:55:36 +00008177void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8178 Location out,
8179 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008180 DCHECK(kEmitCompilerReadBarrier);
8181
Roland Levillain7c1559a2015-12-15 10:55:36 +00008182 // Insert a slow path based read barrier *after* the GC root load.
8183 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008184 // Note that GC roots are not affected by heap poisoning, so we do
8185 // not need to do anything special for this here.
8186 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008187 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008188 AddSlowPath(slow_path);
8189
Roland Levillain0d5a2812015-11-13 10:07:31 +00008190 __ jmp(slow_path->GetEntryLabel());
8191 __ Bind(slow_path->GetExitLabel());
8192}
8193
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008194void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008195 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008196 LOG(FATAL) << "Unreachable";
8197}
8198
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008199void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008200 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008201 LOG(FATAL) << "Unreachable";
8202}
8203
Mark Mendellfe57faa2015-09-18 09:26:15 -04008204// Simple implementation of packed switch - generate cascaded compare/jumps.
8205void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8206 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008207 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04008208 locations->SetInAt(0, Location::RequiresRegister());
8209}
8210
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008211void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
8212 int32_t lower_bound,
8213 uint32_t num_entries,
8214 HBasicBlock* switch_block,
8215 HBasicBlock* default_block) {
8216 // Figure out the correct compare values and jump conditions.
8217 // Handle the first compare/branch as a special case because it might
8218 // jump to the default case.
8219 DCHECK_GT(num_entries, 2u);
8220 Condition first_condition;
8221 uint32_t index;
8222 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
8223 if (lower_bound != 0) {
8224 first_condition = kLess;
8225 __ cmpl(value_reg, Immediate(lower_bound));
8226 __ j(first_condition, codegen_->GetLabelOf(default_block));
8227 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008228
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008229 index = 1;
8230 } else {
8231 // Handle all the compare/jumps below.
8232 first_condition = kBelow;
8233 index = 0;
8234 }
8235
8236 // Handle the rest of the compare/jumps.
8237 for (; index + 1 < num_entries; index += 2) {
8238 int32_t compare_to_value = lower_bound + index + 1;
8239 __ cmpl(value_reg, Immediate(compare_to_value));
8240 // Jump to successors[index] if value < case_value[index].
8241 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
8242 // Jump to successors[index + 1] if value == case_value[index + 1].
8243 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
8244 }
8245
8246 if (index != num_entries) {
8247 // There are an odd number of entries. Handle the last one.
8248 DCHECK_EQ(index + 1, num_entries);
8249 __ cmpl(value_reg, Immediate(lower_bound + index));
8250 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008251 }
8252
8253 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008254 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
8255 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008256 }
8257}
8258
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008259void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8260 int32_t lower_bound = switch_instr->GetStartValue();
8261 uint32_t num_entries = switch_instr->GetNumEntries();
8262 LocationSummary* locations = switch_instr->GetLocations();
8263 Register value_reg = locations->InAt(0).AsRegister<Register>();
8264
8265 GenPackedSwitchWithCompares(value_reg,
8266 lower_bound,
8267 num_entries,
8268 switch_instr->GetBlock(),
8269 switch_instr->GetDefaultBlock());
8270}
8271
Mark Mendell805b3b52015-09-18 14:10:29 -04008272void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8273 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008274 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04008275 locations->SetInAt(0, Location::RequiresRegister());
8276
8277 // Constant area pointer.
8278 locations->SetInAt(1, Location::RequiresRegister());
8279
8280 // And the temporary we need.
8281 locations->AddTemp(Location::RequiresRegister());
8282}
8283
8284void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8285 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008286 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04008287 LocationSummary* locations = switch_instr->GetLocations();
8288 Register value_reg = locations->InAt(0).AsRegister<Register>();
8289 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8290
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008291 if (num_entries <= kPackedSwitchJumpTableThreshold) {
8292 GenPackedSwitchWithCompares(value_reg,
8293 lower_bound,
8294 num_entries,
8295 switch_instr->GetBlock(),
8296 default_block);
8297 return;
8298 }
8299
Mark Mendell805b3b52015-09-18 14:10:29 -04008300 // Optimizing has a jump area.
8301 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
8302 Register constant_area = locations->InAt(1).AsRegister<Register>();
8303
8304 // Remove the bias, if needed.
8305 if (lower_bound != 0) {
8306 __ leal(temp_reg, Address(value_reg, -lower_bound));
8307 value_reg = temp_reg;
8308 }
8309
8310 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008311 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04008312 __ cmpl(value_reg, Immediate(num_entries - 1));
8313 __ j(kAbove, codegen_->GetLabelOf(default_block));
8314
8315 // We are in the range of the table.
8316 // Load (target-constant_area) from the jump table, indexing by the value.
8317 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
8318
8319 // Compute the actual target address by adding in constant_area.
8320 __ addl(temp_reg, constant_area);
8321
8322 // And jump.
8323 __ jmp(temp_reg);
8324}
8325
Mark Mendell0616ae02015-04-17 12:49:27 -04008326void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
8327 HX86ComputeBaseMethodAddress* insn) {
8328 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008329 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008330 locations->SetOut(Location::RequiresRegister());
8331}
8332
8333void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
8334 HX86ComputeBaseMethodAddress* insn) {
8335 LocationSummary* locations = insn->GetLocations();
8336 Register reg = locations->Out().AsRegister<Register>();
8337
8338 // Generate call to next instruction.
8339 Label next_instruction;
8340 __ call(&next_instruction);
8341 __ Bind(&next_instruction);
8342
8343 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008344 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04008345
8346 // Grab the return address off the stack.
8347 __ popl(reg);
8348}
8349
8350void LocationsBuilderX86::VisitX86LoadFromConstantTable(
8351 HX86LoadFromConstantTable* insn) {
8352 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008353 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008354
8355 locations->SetInAt(0, Location::RequiresRegister());
8356 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
8357
8358 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00008359 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008360 return;
8361 }
8362
8363 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008364 case DataType::Type::kFloat32:
8365 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008366 locations->SetOut(Location::RequiresFpuRegister());
8367 break;
8368
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008369 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008370 locations->SetOut(Location::RequiresRegister());
8371 break;
8372
8373 default:
8374 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8375 }
8376}
8377
8378void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00008379 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008380 return;
8381 }
8382
8383 LocationSummary* locations = insn->GetLocations();
8384 Location out = locations->Out();
8385 Register const_area = locations->InAt(0).AsRegister<Register>();
8386 HConstant *value = insn->GetConstant();
8387
8388 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008389 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008390 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008391 codegen_->LiteralFloatAddress(
8392 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008393 break;
8394
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008395 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008396 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008397 codegen_->LiteralDoubleAddress(
8398 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008399 break;
8400
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008401 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008402 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008403 codegen_->LiteralInt32Address(
8404 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008405 break;
8406
8407 default:
8408 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8409 }
8410}
8411
Mark Mendell0616ae02015-04-17 12:49:27 -04008412/**
8413 * Class to handle late fixup of offsets into constant area.
8414 */
Vladimir Marko5233f932015-09-29 19:01:15 +01008415class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04008416 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008417 RIPFixup(CodeGeneratorX86& codegen,
8418 HX86ComputeBaseMethodAddress* base_method_address,
8419 size_t offset)
8420 : codegen_(&codegen),
8421 base_method_address_(base_method_address),
8422 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008423
8424 protected:
8425 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
8426
8427 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008428 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008429
8430 private:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008431 void Process(const MemoryRegion& region, int pos) override {
Mark Mendell0616ae02015-04-17 12:49:27 -04008432 // Patch the correct offset for the instruction. The place to patch is the
8433 // last 4 bytes of the instruction.
8434 // The value to patch is the distance from the offset in the constant area
8435 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04008436 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008437 int32_t relative_position =
8438 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04008439
8440 // Patch in the right value.
8441 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
8442 }
8443
Mark Mendell0616ae02015-04-17 12:49:27 -04008444 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04008445 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008446};
8447
Mark Mendell805b3b52015-09-18 14:10:29 -04008448/**
8449 * Class to handle late fixup of offsets to a jump table that will be created in the
8450 * constant area.
8451 */
8452class JumpTableRIPFixup : public RIPFixup {
8453 public:
8454 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008455 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
8456 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008457
8458 void CreateJumpTable() {
8459 X86Assembler* assembler = codegen_->GetAssembler();
8460
8461 // Ensure that the reference to the jump table has the correct offset.
8462 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
8463 SetOffset(offset_in_constant_table);
8464
8465 // The label values in the jump table are computed relative to the
8466 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008467 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04008468
8469 // Populate the jump table with the correct values for the jump table.
8470 int32_t num_entries = switch_instr_->GetNumEntries();
8471 HBasicBlock* block = switch_instr_->GetBlock();
8472 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
8473 // The value that we want is the target offset - the position of the table.
8474 for (int32_t i = 0; i < num_entries; i++) {
8475 HBasicBlock* b = successors[i];
8476 Label* l = codegen_->GetLabelOf(b);
8477 DCHECK(l->IsBound());
8478 int32_t offset_to_block = l->Position() - relative_offset;
8479 assembler->AppendInt32(offset_to_block);
8480 }
8481 }
8482
8483 private:
8484 const HX86PackedSwitch* switch_instr_;
8485};
8486
8487void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
8488 // Generate the constant area if needed.
8489 X86Assembler* assembler = GetAssembler();
jaishank20d1c942019-03-08 15:08:17 +05308490
Mark Mendell805b3b52015-09-18 14:10:29 -04008491 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
8492 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
8493 // byte values.
8494 assembler->Align(4, 0);
8495 constant_area_start_ = assembler->CodeSize();
8496
8497 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008498 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04008499 jump_table->CreateJumpTable();
8500 }
8501
8502 // And now add the constant area to the generated code.
8503 assembler->AddConstantArea();
8504 }
8505
8506 // And finish up.
8507 CodeGenerator::Finalize(allocator);
8508}
8509
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008510Address CodeGeneratorX86::LiteralDoubleAddress(double v,
8511 HX86ComputeBaseMethodAddress* method_base,
8512 Register reg) {
8513 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008514 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008515 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008516}
8517
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008518Address CodeGeneratorX86::LiteralFloatAddress(float v,
8519 HX86ComputeBaseMethodAddress* method_base,
8520 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008521 AssemblerFixup* fixup =
8522 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008523 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008524}
8525
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008526Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
8527 HX86ComputeBaseMethodAddress* method_base,
8528 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008529 AssemblerFixup* fixup =
8530 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008531 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008532}
8533
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008534Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
8535 HX86ComputeBaseMethodAddress* method_base,
8536 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008537 AssemblerFixup* fixup =
8538 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008539 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008540}
8541
Aart Bika19616e2016-02-01 18:57:58 -08008542void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
8543 if (value == 0) {
8544 __ xorl(dest, dest);
8545 } else {
8546 __ movl(dest, Immediate(value));
8547 }
8548}
8549
8550void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
8551 if (value == 0) {
8552 __ testl(dest, dest);
8553 } else {
8554 __ cmpl(dest, Immediate(value));
8555 }
8556}
8557
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008558void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
8559 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07008560 GenerateIntCompare(lhs_reg, rhs);
8561}
8562
8563void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008564 if (rhs.IsConstant()) {
8565 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07008566 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008567 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07008568 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008569 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07008570 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008571 }
8572}
8573
8574Address CodeGeneratorX86::ArrayAddress(Register obj,
8575 Location index,
8576 ScaleFactor scale,
8577 uint32_t data_offset) {
8578 return index.IsConstant() ?
8579 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
8580 Address(obj, index.AsRegister<Register>(), scale, data_offset);
8581}
8582
Mark Mendell805b3b52015-09-18 14:10:29 -04008583Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
8584 Register reg,
8585 Register value) {
8586 // Create a fixup to be used to create and address the jump table.
8587 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008588 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04008589
8590 // We have to populate the jump tables.
8591 fixups_to_jump_tables_.push_back(table_fixup);
8592
8593 // We want a scaled address, as we are extracting the correct offset from the table.
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008594 return Address(reg, value, TIMES_4, kPlaceholder32BitOffset, table_fixup);
Mark Mendell805b3b52015-09-18 14:10:29 -04008595}
8596
Andreas Gampe85b62f22015-09-09 13:15:38 -07008597// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008598void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07008599 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008600 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008601 return;
8602 }
8603
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008604 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008605
8606 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
8607 if (target.Equals(return_loc)) {
8608 return;
8609 }
8610
8611 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8612 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008613 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008614 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008615 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
8616 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008617 GetMoveResolver()->EmitNativeCode(&parallel_move);
8618 } else {
8619 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01008620 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07008621 parallel_move.AddMove(return_loc, target, type, nullptr);
8622 GetMoveResolver()->EmitNativeCode(&parallel_move);
8623 }
8624}
8625
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008626void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
8627 const uint8_t* roots_data,
8628 const PatchInfo<Label>& info,
8629 uint64_t index_in_table) const {
8630 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
8631 uintptr_t address =
8632 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
Andreas Gampec55bb392018-09-21 00:02:02 +00008633 using unaligned_uint32_t __attribute__((__aligned__(1))) = uint32_t;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008634 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
8635 dchecked_integral_cast<uint32_t>(address);
8636}
8637
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008638void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8639 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008640 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008641 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008642 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008643 }
8644
8645 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008646 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008647 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008648 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008649 }
8650}
8651
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008652void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8653 ATTRIBUTE_UNUSED) {
8654 LOG(FATAL) << "Unreachable";
8655}
8656
8657void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8658 ATTRIBUTE_UNUSED) {
8659 LOG(FATAL) << "Unreachable";
8660}
8661
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05308662bool LocationsBuilderX86::CpuHasAvxFeatureFlag() {
8663 return codegen_->GetInstructionSetFeatures().HasAVX();
8664}
8665bool LocationsBuilderX86::CpuHasAvx2FeatureFlag() {
8666 return codegen_->GetInstructionSetFeatures().HasAVX2();
8667}
8668bool InstructionCodeGeneratorX86::CpuHasAvxFeatureFlag() {
8669 return codegen_->GetInstructionSetFeatures().HasAVX();
8670}
8671bool InstructionCodeGeneratorX86::CpuHasAvx2FeatureFlag() {
8672 return codegen_->GetInstructionSetFeatures().HasAVX2();
8673}
8674
Roland Levillain4d027112015-07-01 15:41:14 +01008675#undef __
8676
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008677} // namespace x86
8678} // namespace art