blob: 4a0eb2e42ed03d53bdaf0f6ec3dee353dad9f90a [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Vladimir Marko86c87522020-05-11 16:55:55 +010019#include "arch/x86/jni_frame_x86.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000020#include "art_method-inl.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010021#include "class_table.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010022#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000023#include "compiled_method.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010024#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000025#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010026#include "gc/accounting/card_table.h"
Vladimir Markoeebb8212018-06-05 14:57:24 +010027#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070028#include "heap_poisoning.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040029#include "intrinsics.h"
30#include "intrinsics_x86.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000031#include "jit/profiling_info.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010032#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070033#include "lock_word.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070034#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070035#include "mirror/class-inl.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000036#include "scoped_thread_state_change-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010037#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000038#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010039#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010041#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000042
Vladimir Marko0a516052019-10-14 13:00:44 +000043namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010044
Roland Levillain0d5a2812015-11-13 10:07:31 +000045template<class MirrorType>
46class GcRoot;
47
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000048namespace x86 {
49
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010050static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010051static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050052static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010053
Mark Mendell24f2dfa2015-01-14 19:51:45 -050054static constexpr int kC2ConditionMask = 0x400;
55
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000056static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000057
Aart Bik1f8d51b2018-02-15 10:42:37 -080058static constexpr int64_t kDoubleNaN = INT64_C(0x7FF8000000000000);
59static constexpr int32_t kFloatNaN = INT32_C(0x7FC00000);
60
Vladimir Marko3232dbb2018-07-25 15:42:46 +010061static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
62 InvokeRuntimeCallingConvention calling_convention;
63 RegisterSet caller_saves = RegisterSet::Empty();
64 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
65 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
66 // that the the kPrimNot result register is the same as the first argument register.
67 return caller_saves;
68}
69
Roland Levillain7cbd27f2016-08-11 23:53:33 +010070// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
71#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070072#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010073
Andreas Gampe85b62f22015-09-09 13:15:38 -070074class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010075 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000076 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010078 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +010079 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000081 if (instruction_->CanThrowIntoCatchBlock()) {
82 // Live registers will be restored in the catch block if caught.
83 SaveLiveRegisters(codegen, instruction_->GetLocations());
84 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010085 x86_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexandre Rames8158f282015-08-07 10:26:17 +010086 instruction_,
87 instruction_->GetDexPc(),
88 this);
Roland Levillain888d0672015-11-23 18:53:50 +000089 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010090 }
91
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010092 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +010093
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010094 const char* GetDescription() const override { return "NullCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +010095
Nicolas Geoffraye5038322014-07-04 09:41:32 +010096 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010097 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
98};
99
Andreas Gampe85b62f22015-09-09 13:15:38 -0700100class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000101 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000102 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000103
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100104 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +0100105 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +0000106 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100107 x86_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000108 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000109 }
110
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100111 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100112
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100113 const char* GetDescription() const override { return "DivZeroCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100114
Calin Juravled0d48522014-11-04 16:40:20 +0000115 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000116 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
117};
118
Andreas Gampe85b62f22015-09-09 13:15:38 -0700119class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000120 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000121 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
122 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000123
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100124 void EmitNativeCode(CodeGenerator* codegen) override {
Calin Juravled0d48522014-11-04 16:40:20 +0000125 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000126 if (is_div_) {
127 __ negl(reg_);
128 } else {
129 __ movl(reg_, Immediate(0));
130 }
Calin Juravled0d48522014-11-04 16:40:20 +0000131 __ jmp(GetExitLabel());
132 }
133
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100134 const char* GetDescription() const override { return "DivRemMinusOneSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100135
Calin Juravled0d48522014-11-04 16:40:20 +0000136 private:
137 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000138 bool is_div_;
139 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000140};
141
Andreas Gampe85b62f22015-09-09 13:15:38 -0700142class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100143 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000144 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100145
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100146 void EmitNativeCode(CodeGenerator* codegen) override {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100147 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100148 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100149 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000150 // We're moving two locations to locations that could overlap, so we need a parallel
151 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000152 if (instruction_->CanThrowIntoCatchBlock()) {
153 // Live registers will be restored in the catch block if caught.
154 SaveLiveRegisters(codegen, instruction_->GetLocations());
155 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400156
157 // Are we using an array length from memory?
158 HInstruction* array_length = instruction_->InputAt(1);
159 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100160 InvokeRuntimeCallingConvention calling_convention;
Mark Mendellee8d9712016-07-12 11:13:15 -0400161 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
162 // Load the array length into our temporary.
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100163 HArrayLength* length = array_length->AsArrayLength();
164 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(length);
Mark Mendellee8d9712016-07-12 11:13:15 -0400165 Location array_loc = array_length->GetLocations()->InAt(0);
166 Address array_len(array_loc.AsRegister<Register>(), len_offset);
167 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
168 // Check for conflicts with index.
169 if (length_loc.Equals(locations->InAt(0))) {
170 // We know we aren't using parameter 2.
171 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
172 }
173 __ movl(length_loc.AsRegister<Register>(), array_len);
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100174 if (mirror::kUseStringCompression && length->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100175 __ shrl(length_loc.AsRegister<Register>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700176 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400177 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000178 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100179 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000180 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100181 DataType::Type::kInt32,
Mark Mendellee8d9712016-07-12 11:13:15 -0400182 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100183 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100184 DataType::Type::kInt32);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100185 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
186 ? kQuickThrowStringBounds
187 : kQuickThrowArrayBounds;
188 x86_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100189 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000190 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100191 }
192
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100193 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100194
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100195 const char* GetDescription() const override { return "BoundsCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100196
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100197 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100198 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
199};
200
Andreas Gampe85b62f22015-09-09 13:15:38 -0700201class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000202 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000203 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000204 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000205
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100206 void EmitNativeCode(CodeGenerator* codegen) override {
Aart Bikb13c65b2017-03-21 20:14:07 -0700207 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100208 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000209 __ Bind(GetEntryLabel());
Aart Bik24b905f2017-04-06 09:59:06 -0700210 SaveLiveRegisters(codegen, locations); // Only saves full width XMM for SIMD.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100211 x86_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000212 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Aart Bik24b905f2017-04-06 09:59:06 -0700213 RestoreLiveRegisters(codegen, locations); // Only restores full width XMM for SIMD.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100214 if (successor_ == nullptr) {
215 __ jmp(GetReturnLabel());
216 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100217 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100218 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000219 }
220
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100221 Label* GetReturnLabel() {
222 DCHECK(successor_ == nullptr);
223 return &return_label_;
224 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000225
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100226 HBasicBlock* GetSuccessor() const {
227 return successor_;
228 }
229
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100230 const char* GetDescription() const override { return "SuspendCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100231
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000232 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100233 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000234 Label return_label_;
235
236 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
237};
238
Vladimir Markoaad75c62016-10-03 08:46:48 +0000239class LoadStringSlowPathX86 : public SlowPathCode {
240 public:
241 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
242
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100243 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000244 LocationSummary* locations = instruction_->GetLocations();
245 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
246
247 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
248 __ Bind(GetEntryLabel());
249 SaveLiveRegisters(codegen, locations);
250
251 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000252 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
253 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index.index_));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000254 x86_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
255 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
256 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
257 RestoreLiveRegisters(codegen, locations);
258
Vladimir Markoaad75c62016-10-03 08:46:48 +0000259 __ jmp(GetExitLabel());
260 }
261
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100262 const char* GetDescription() const override { return "LoadStringSlowPathX86"; }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000263
264 private:
265 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
266};
267
Andreas Gampe85b62f22015-09-09 13:15:38 -0700268class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000269 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100270 LoadClassSlowPathX86(HLoadClass* cls, HInstruction* at)
271 : SlowPathCode(at), cls_(cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000272 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100273 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000274 }
275
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100276 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000277 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100278 Location out = locations->Out();
279 const uint32_t dex_pc = instruction_->GetDexPc();
280 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
281 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
282
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000283 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
284 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000285 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000286
287 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100288 if (must_resolve_type) {
289 DCHECK(IsSameDexFile(cls_->GetDexFile(), x86_codegen->GetGraph()->GetDexFile()));
290 dex::TypeIndex type_index = cls_->GetTypeIndex();
291 __ movl(calling_convention.GetRegisterAt(0), Immediate(type_index.index_));
Vladimir Marko9d479252018-07-24 11:35:20 +0100292 x86_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
293 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100294 // If we also must_do_clinit, the resolved type is now in the correct register.
295 } else {
296 DCHECK(must_do_clinit);
297 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
298 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), source);
299 }
300 if (must_do_clinit) {
301 x86_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
302 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Roland Levillain888d0672015-11-23 18:53:50 +0000303 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000304
305 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000306 if (out.IsValid()) {
307 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
308 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000309 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000310 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000311 __ jmp(GetExitLabel());
312 }
313
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100314 const char* GetDescription() const override { return "LoadClassSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100315
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000316 private:
317 // The class this slow path will load.
318 HLoadClass* const cls_;
319
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000320 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
321};
322
Andreas Gampe85b62f22015-09-09 13:15:38 -0700323class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000325 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000326 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000327
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100328 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000329 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000330 DCHECK(instruction_->IsCheckCast()
331 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000332
333 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
334 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000335
Vladimir Markoe619f6c2017-12-12 16:00:01 +0000336 if (kPoisonHeapReferences &&
337 instruction_->IsCheckCast() &&
338 instruction_->AsCheckCast()->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) {
339 // First, unpoison the `cls` reference that was poisoned for direct memory comparison.
340 __ UnpoisonHeapReference(locations->InAt(1).AsRegister<Register>());
341 }
342
Vladimir Marko87584542017-12-12 17:47:52 +0000343 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000344 SaveLiveRegisters(codegen, locations);
345 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000346
347 // We're moving two locations to locations that could overlap, so we need a parallel
348 // move resolver.
349 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800350 x86_codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800351 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100352 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800353 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800354 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100355 DataType::Type::kReference);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000356 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100357 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100358 instruction_,
359 instruction_->GetDexPc(),
360 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800361 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000362 } else {
363 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800364 x86_codegen->InvokeRuntime(kQuickCheckInstanceOf,
365 instruction_,
366 instruction_->GetDexPc(),
367 this);
368 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000369 }
370
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000371 if (!is_fatal_) {
372 if (instruction_->IsInstanceOf()) {
373 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
374 }
375 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000376
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000377 __ jmp(GetExitLabel());
378 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000379 }
380
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100381 const char* GetDescription() const override { return "TypeCheckSlowPathX86"; }
382 bool IsFatal() const override { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100383
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000384 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000385 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000386
387 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
388};
389
Andreas Gampe85b62f22015-09-09 13:15:38 -0700390class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700391 public:
Aart Bik42249c32016-01-07 15:33:50 -0800392 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000393 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700394
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100395 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +0100396 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700397 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100398 LocationSummary* locations = instruction_->GetLocations();
399 SaveLiveRegisters(codegen, locations);
400 InvokeRuntimeCallingConvention calling_convention;
401 x86_codegen->Load32BitValue(
402 calling_convention.GetRegisterAt(0),
403 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100404 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100405 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700406 }
407
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100408 const char* GetDescription() const override { return "DeoptimizationSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100409
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700410 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700411 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
412};
413
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100414class ArraySetSlowPathX86 : public SlowPathCode {
415 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000416 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100417
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100418 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100419 LocationSummary* locations = instruction_->GetLocations();
420 __ Bind(GetEntryLabel());
421 SaveLiveRegisters(codegen, locations);
422
423 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100424 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100425 parallel_move.AddMove(
426 locations->InAt(0),
427 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100428 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100429 nullptr);
430 parallel_move.AddMove(
431 locations->InAt(1),
432 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100433 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100434 nullptr);
435 parallel_move.AddMove(
436 locations->InAt(2),
437 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100438 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100439 nullptr);
440 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
441
442 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100443 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000444 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100445 RestoreLiveRegisters(codegen, locations);
446 __ jmp(GetExitLabel());
447 }
448
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100449 const char* GetDescription() const override { return "ArraySetSlowPathX86"; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100450
451 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100452 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
453};
454
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100455// Slow path marking an object reference `ref` during a read
456// barrier. The field `obj.field` in the object `obj` holding this
457// reference does not get updated by this slow path after marking (see
458// ReadBarrierMarkAndUpdateFieldSlowPathX86 below for that).
459//
460// This means that after the execution of this slow path, `ref` will
461// always be up-to-date, but `obj.field` may not; i.e., after the
462// flip, `ref` will be a to-space reference, but `obj.field` will
463// probably still be a from-space reference (unless it gets updated by
464// another thread, or if another thread installed another object
465// reference (different from `ref`) in `obj.field`).
Roland Levillain7c1559a2015-12-15 10:55:36 +0000466class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
467 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100468 ReadBarrierMarkSlowPathX86(HInstruction* instruction,
469 Location ref,
470 bool unpoison_ref_before_marking)
471 : SlowPathCode(instruction),
472 ref_(ref),
473 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000474 DCHECK(kEmitCompilerReadBarrier);
475 }
476
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100477 const char* GetDescription() const override { return "ReadBarrierMarkSlowPathX86"; }
Roland Levillain7c1559a2015-12-15 10:55:36 +0000478
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100479 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000480 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100481 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000482 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100483 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000484 DCHECK(instruction_->IsInstanceFieldGet() ||
485 instruction_->IsStaticFieldGet() ||
486 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100487 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000488 instruction_->IsLoadClass() ||
489 instruction_->IsLoadString() ||
490 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100491 instruction_->IsCheckCast() ||
Andra Danciu1ca6f322020-08-12 08:58:07 +0000492 (instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000493 << "Unexpected instruction in read barrier marking slow path: "
494 << instruction_->DebugName();
495
496 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100497 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000498 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100499 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000500 }
Roland Levillain4359e612016-07-20 11:32:19 +0100501 // No need to save live registers; it's taken care of by the
502 // entrypoint. Also, there is no need to update the stack mask,
503 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000504 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100505 DCHECK_NE(ref_reg, ESP);
506 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100507 // "Compact" slow path, saving two moves.
508 //
509 // Instead of using the standard runtime calling convention (input
510 // and output in EAX):
511 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100512 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100513 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100514 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100515 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100516 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100517 // of a dedicated entrypoint:
518 //
519 // rX <- ReadBarrierMarkRegX(rX)
520 //
Roland Levillain97c46462017-05-11 14:04:03 +0100521 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100522 // This runtime call does not require a stack map.
523 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000524 __ jmp(GetExitLabel());
525 }
526
527 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100528 // The location (register) of the marked object reference.
529 const Location ref_;
530 // Should the reference in `ref_` be unpoisoned prior to marking it?
531 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000532
533 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
534};
535
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100536// Slow path marking an object reference `ref` during a read barrier,
537// and if needed, atomically updating the field `obj.field` in the
538// object `obj` holding this reference after marking (contrary to
539// ReadBarrierMarkSlowPathX86 above, which never tries to update
540// `obj.field`).
541//
542// This means that after the execution of this slow path, both `ref`
543// and `obj.field` will be up-to-date; i.e., after the flip, both will
544// hold the same to-space reference (unless another thread installed
545// another object reference (different from `ref`) in `obj.field`).
546class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
547 public:
548 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
549 Location ref,
550 Register obj,
551 const Address& field_addr,
552 bool unpoison_ref_before_marking,
553 Register temp)
554 : SlowPathCode(instruction),
555 ref_(ref),
556 obj_(obj),
557 field_addr_(field_addr),
558 unpoison_ref_before_marking_(unpoison_ref_before_marking),
559 temp_(temp) {
560 DCHECK(kEmitCompilerReadBarrier);
561 }
562
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100563 const char* GetDescription() const override { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100564
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100565 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100566 LocationSummary* locations = instruction_->GetLocations();
567 Register ref_reg = ref_.AsRegister<Register>();
568 DCHECK(locations->CanCall());
569 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
570 // This slow path is only used by the UnsafeCASObject intrinsic.
Andra Danciu5e13d452020-09-08 14:35:09 +0000571 DCHECK((instruction_->IsInvoke() && instruction_->GetLocations()->Intrinsified()))
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100572 << "Unexpected instruction in read barrier marking and field updating slow path: "
573 << instruction_->DebugName();
574 DCHECK(instruction_->GetLocations()->Intrinsified());
Andra Danciu5e13d452020-09-08 14:35:09 +0000575 DCHECK(instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeCASObject ||
576 instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kVarHandleCompareAndSet);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100577
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()) {
Nicolas Geoffray095dc462020-08-17 16:40:28 +01001100 ScopedProfilingInfoUse spiu(
1101 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
1102 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001103 if (info != nullptr) {
1104 uint32_t address = reinterpret_cast32<uint32_t>(info);
1105 NearLabel done;
1106 if (HasEmptyFrame()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001107 CHECK(is_frame_entry);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001108 // Alignment
Vladimir Markodec78172020-06-19 15:31:23 +01001109 IncreaseFrame(8);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001110 // We need a temporary. The stub also expects the method at bottom of stack.
1111 __ pushl(EAX);
1112 __ cfi().AdjustCFAOffset(4);
1113 __ movl(EAX, Immediate(address));
1114 __ addw(Address(EAX, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()),
1115 Immediate(1));
1116 __ j(kCarryClear, &done);
1117 GenerateInvokeRuntime(
1118 GetThreadOffset<kX86PointerSize>(kQuickCompileOptimized).Int32Value());
1119 __ Bind(&done);
1120 // We don't strictly require to restore EAX, but this makes the generated
1121 // code easier to reason about.
1122 __ popl(EAX);
1123 __ cfi().AdjustCFAOffset(-4);
Vladimir Markodec78172020-06-19 15:31:23 +01001124 DecreaseFrame(8);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001125 } else {
1126 if (!RequiresCurrentMethod()) {
1127 CHECK(is_frame_entry);
1128 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1129 }
1130 // We need a temporary.
1131 __ pushl(EAX);
1132 __ cfi().AdjustCFAOffset(4);
1133 __ movl(EAX, Immediate(address));
1134 __ addw(Address(EAX, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()),
1135 Immediate(1));
1136 __ popl(EAX); // Put stack as expected before exiting or calling stub.
1137 __ cfi().AdjustCFAOffset(-4);
1138 __ j(kCarryClear, &done);
1139 GenerateInvokeRuntime(
1140 GetThreadOffset<kX86PointerSize>(kQuickCompileOptimized).Int32Value());
1141 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001142 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001143 }
1144 }
1145}
1146
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001147void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001148 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001149 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001150 bool skip_overflow_check =
1151 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001152 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001153
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001154 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001155 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86);
1156 __ testl(EAX, Address(ESP, -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001157 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001158 }
1159
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001160 if (!HasEmptyFrame()) {
1161 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1162 Register reg = kCoreCalleeSaves[i];
1163 if (allocated_registers_.ContainsCoreRegister(reg)) {
1164 __ pushl(reg);
1165 __ cfi().AdjustCFAOffset(kX86WordSize);
1166 __ cfi().RelOffset(DWARFReg(reg), 0);
1167 }
1168 }
Mark Mendell5f874182015-03-04 15:42:45 -05001169
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001170 int adjust = GetFrameSize() - FrameEntrySpillSize();
Vladimir Markodec78172020-06-19 15:31:23 +01001171 IncreaseFrame(adjust);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001172 // Save the current method if we need it. Note that we do not
1173 // do this in HCurrentMethod, as the instruction might have been removed
1174 // in the SSA graph.
1175 if (RequiresCurrentMethod()) {
1176 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1177 }
1178
1179 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1180 // Initialize should_deoptimize flag to 0.
1181 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
Mark Mendell5f874182015-03-04 15:42:45 -05001182 }
1183 }
1184
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001185 MaybeIncrementHotness(/* is_frame_entry= */ true);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001186}
1187
1188void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001189 __ cfi().RememberState();
1190 if (!HasEmptyFrame()) {
1191 int adjust = GetFrameSize() - FrameEntrySpillSize();
Vladimir Markodec78172020-06-19 15:31:23 +01001192 DecreaseFrame(adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001193
David Srbeckyc34dc932015-04-12 09:27:43 +01001194 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1195 Register reg = kCoreCalleeSaves[i];
1196 if (allocated_registers_.ContainsCoreRegister(reg)) {
1197 __ popl(reg);
1198 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1199 __ cfi().Restore(DWARFReg(reg));
1200 }
Mark Mendell5f874182015-03-04 15:42:45 -05001201 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001202 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001203 __ ret();
1204 __ cfi().RestoreState();
1205 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001206}
1207
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001208void CodeGeneratorX86::Bind(HBasicBlock* block) {
1209 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001210}
1211
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001212Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001213 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001214 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001215 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001216 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001217 case DataType::Type::kInt8:
1218 case DataType::Type::kUint16:
1219 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08001220 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001221 case DataType::Type::kInt32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001222 return Location::RegisterLocation(EAX);
1223
Aart Bik66c158e2018-01-31 12:55:04 -08001224 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001225 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001226 return Location::RegisterPairLocation(EAX, EDX);
1227
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001228 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001229 return Location::NoLocation();
1230
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001231 case DataType::Type::kFloat64:
1232 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001233 return Location::FpuRegisterLocation(XMM0);
1234 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001235
1236 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001237}
1238
1239Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1240 return Location::RegisterLocation(kMethodRegisterArgument);
1241}
1242
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001243Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001244 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001245 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001246 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001247 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001248 case DataType::Type::kInt8:
1249 case DataType::Type::kUint16:
1250 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001251 case DataType::Type::kInt32: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001252 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001253 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001254 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001255 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001256 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001257 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001258 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001259 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001260
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001261 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001262 uint32_t index = gp_index_;
1263 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001264 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001265 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001266 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1267 calling_convention.GetRegisterPairAt(index));
1268 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001269 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001270 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1271 }
1272 }
1273
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001274 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001275 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001276 stack_index_++;
1277 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1278 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1279 } else {
1280 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1281 }
1282 }
1283
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001284 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001285 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001286 stack_index_ += 2;
1287 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1288 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1289 } else {
1290 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001291 }
1292 }
1293
Aart Bik66c158e2018-01-31 12:55:04 -08001294 case DataType::Type::kUint32:
1295 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001296 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001297 LOG(FATAL) << "Unexpected parameter type " << type;
Elliott Hughesc1896c92018-11-29 11:33:18 -08001298 UNREACHABLE();
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001299 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001300 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001301}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001302
Vladimir Marko86c87522020-05-11 16:55:55 +01001303Location CriticalNativeCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
1304 DCHECK_NE(type, DataType::Type::kReference);
1305
1306 Location location;
1307 if (DataType::Is64BitType(type)) {
1308 location = Location::DoubleStackSlot(stack_offset_);
1309 stack_offset_ += 2 * kFramePointerSize;
1310 } else {
1311 location = Location::StackSlot(stack_offset_);
1312 stack_offset_ += kFramePointerSize;
1313 }
1314 if (for_register_allocation_) {
1315 location = Location::Any();
1316 }
1317 return location;
1318}
1319
1320Location CriticalNativeCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
1321 // We perform conversion to the managed ABI return register after the call if needed.
1322 InvokeDexCallingConventionVisitorX86 dex_calling_convention;
1323 return dex_calling_convention.GetReturnLocation(type);
1324}
1325
1326Location CriticalNativeCallingConventionVisitorX86::GetMethodLocation() const {
1327 // Pass the method in the hidden argument EAX.
1328 return Location::RegisterLocation(EAX);
1329}
1330
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001331void CodeGeneratorX86::Move32(Location destination, Location source) {
1332 if (source.Equals(destination)) {
1333 return;
1334 }
1335 if (destination.IsRegister()) {
1336 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001337 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001338 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001339 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Andra Danciu5e13d452020-09-08 14:35:09 +00001340 } else if (source.IsConstant()) {
1341 int32_t value = GetInt32ValueOf(source.GetConstant());
1342 __ movl(destination.AsRegister<Register>(), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001343 } else {
1344 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001345 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001346 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001347 } else if (destination.IsFpuRegister()) {
1348 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001349 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001350 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001351 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001352 } else {
1353 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001354 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001355 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001356 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001357 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001358 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001359 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001360 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001361 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001362 } else if (source.IsConstant()) {
1363 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001364 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001365 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001366 } else {
1367 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001368 __ pushl(Address(ESP, source.GetStackIndex()));
1369 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001370 }
1371 }
1372}
1373
1374void CodeGeneratorX86::Move64(Location destination, Location source) {
1375 if (source.Equals(destination)) {
1376 return;
1377 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001378 if (destination.IsRegisterPair()) {
1379 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001380 EmitParallelMoves(
1381 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1382 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001383 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001384 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001385 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001386 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001387 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001388 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1389 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1390 __ psrlq(src_reg, Immediate(32));
1391 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001392 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001393 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001394 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001395 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1396 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001397 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1398 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001399 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001400 if (source.IsFpuRegister()) {
1401 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1402 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001403 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001404 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001405 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Vladimir Markodec78172020-06-19 15:31:23 +01001406 // Push the 2 source registers to the stack.
1407 __ pushl(source.AsRegisterPairHigh<Register>());
1408 __ cfi().AdjustCFAOffset(elem_size);
1409 __ pushl(source.AsRegisterPairLow<Register>());
1410 __ cfi().AdjustCFAOffset(elem_size);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001411 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1412 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01001413 DecreaseFrame(2 * elem_size);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001414 } else {
1415 LOG(FATAL) << "Unimplemented";
1416 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001417 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001418 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001419 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001420 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001421 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001422 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001423 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001424 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001425 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001426 } else if (source.IsConstant()) {
1427 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001428 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1429 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001430 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001431 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1432 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001433 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001434 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001435 EmitParallelMoves(
1436 Location::StackSlot(source.GetStackIndex()),
1437 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001438 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001439 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001440 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001441 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001442 }
1443 }
1444}
1445
Andra Danciu1ca6f322020-08-12 08:58:07 +00001446static Address CreateAddress(Register base,
1447 Register index = Register::kNoRegister,
1448 ScaleFactor scale = TIMES_1,
1449 int32_t disp = 0) {
1450 if (index == Register::kNoRegister) {
1451 return Address(base, disp);
1452 }
1453
1454 return Address(base, index, scale, disp);
1455}
1456
1457void CodeGeneratorX86::MoveFromMemory(DataType::Type dst_type,
1458 Location dst,
1459 Register src_base,
1460 Register src_index,
1461 ScaleFactor src_scale,
1462 int32_t src_disp) {
1463 DCHECK(src_base != Register::kNoRegister);
1464 Address src = CreateAddress(src_base, src_index, src_scale, src_disp);
1465
1466 switch (dst_type) {
1467 case DataType::Type::kBool:
1468 case DataType::Type::kUint8:
1469 __ movzxb(dst.AsRegister<Register>(), src);
1470 break;
1471 case DataType::Type::kInt8:
1472 __ movsxb(dst.AsRegister<Register>(), src);
1473 break;
1474 case DataType::Type::kInt16:
1475 __ movsxw(dst.AsRegister<Register>(), src);
1476 break;
1477 case DataType::Type::kUint16:
1478 __ movzxw(dst.AsRegister<Register>(), src);
1479 break;
1480 case DataType::Type::kInt32:
1481 case DataType::Type::kUint32:
1482 __ movl(dst.AsRegister<Register>(), src);
1483 break;
1484 case DataType::Type::kInt64:
1485 case DataType::Type::kUint64: {
1486 Address src_next_4_bytes = CreateAddress(src_base, src_index, src_scale, src_disp + 4);
1487 __ movl(dst.AsRegisterPairLow<Register>(), src);
1488 __ movl(dst.AsRegisterPairHigh<Register>(), src_next_4_bytes);
1489 break;
1490 }
1491 case DataType::Type::kFloat32:
1492 __ movss(dst.AsFpuRegister<XmmRegister>(), src);
1493 break;
1494 case DataType::Type::kFloat64:
1495 __ movsd(dst.AsFpuRegister<XmmRegister>(), src);
1496 break;
1497 case DataType::Type::kVoid:
1498 case DataType::Type::kReference:
1499 LOG(FATAL) << "Unreachable type " << dst_type;
1500 }
1501}
1502
Andra Danciu73c31802020-09-01 13:17:05 +00001503void CodeGeneratorX86::MoveToMemory(DataType::Type src_type,
1504 Location src,
1505 Register dst_base,
1506 Register dst_index,
1507 ScaleFactor dst_scale,
1508 int32_t dst_disp) {
1509 DCHECK(dst_base != Register::kNoRegister);
1510 Address dst = CreateAddress(dst_base, dst_index, dst_scale, dst_disp);
1511
1512 switch (src_type) {
1513 case DataType::Type::kBool:
1514 case DataType::Type::kUint8:
1515 case DataType::Type::kInt8: {
1516 if (src.IsConstant()) {
1517 __ movb(dst, Immediate(CodeGenerator::GetInt8ValueOf(src.GetConstant())));
1518 } else {
1519 __ movb(dst, src.AsRegister<ByteRegister>());
1520 }
1521 break;
1522 }
1523 case DataType::Type::kUint16:
1524 case DataType::Type::kInt16: {
1525 if (src.IsConstant()) {
1526 __ movw(dst, Immediate(CodeGenerator::GetInt16ValueOf(src.GetConstant())));
1527 } else {
1528 __ movw(dst, src.AsRegister<Register>());
1529 }
1530 break;
1531 }
1532 case DataType::Type::kUint32:
1533 case DataType::Type::kInt32: {
1534 if (src.IsConstant()) {
1535 int32_t v = CodeGenerator::GetInt32ValueOf(src.GetConstant());
1536 __ movl(dst, Immediate(v));
1537 } else {
1538 __ movl(dst, src.AsRegister<Register>());
1539 }
1540 break;
1541 }
1542 case DataType::Type::kUint64:
1543 case DataType::Type::kInt64: {
1544 Address dst_next_4_bytes = CreateAddress(dst_base, dst_index, dst_scale, dst_disp + 4);
1545 if (src.IsConstant()) {
1546 int64_t v = CodeGenerator::GetInt64ValueOf(src.GetConstant());
1547 __ movl(dst, Immediate(Low32Bits(v)));
1548 __ movl(dst_next_4_bytes, Immediate(High32Bits(v)));
1549 } else {
1550 __ movl(dst, src.AsRegisterPairLow<Register>());
1551 __ movl(dst_next_4_bytes, src.AsRegisterPairHigh<Register>());
1552 }
1553 break;
1554 }
1555 case DataType::Type::kFloat32: {
1556 if (src.IsConstant()) {
1557 int32_t v = CodeGenerator::GetInt32ValueOf(src.GetConstant());
1558 __ movl(dst, Immediate(v));
1559 } else {
1560 __ movss(dst, src.AsFpuRegister<XmmRegister>());
1561 }
1562 break;
1563 }
1564 case DataType::Type::kFloat64: {
1565 Address dst_next_4_bytes = CreateAddress(dst_base, dst_index, dst_scale, dst_disp + 4);
1566 if (src.IsConstant()) {
1567 int64_t v = CodeGenerator::GetInt64ValueOf(src.GetConstant());
1568 __ movl(dst, Immediate(Low32Bits(v)));
1569 __ movl(dst_next_4_bytes, Immediate(High32Bits(v)));
1570 } else {
1571 __ movsd(dst, src.AsFpuRegister<XmmRegister>());
1572 }
1573 break;
1574 }
1575 case DataType::Type::kVoid:
1576 case DataType::Type::kReference:
1577 LOG(FATAL) << "Unreachable type " << src_type;
1578 }
1579}
1580
Calin Juravle175dc732015-08-25 15:42:32 +01001581void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1582 DCHECK(location.IsRegister());
1583 __ movl(location.AsRegister<Register>(), Immediate(value));
1584}
1585
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001586void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001587 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001588 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1589 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1590 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001591 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001592 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001593 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001594 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001595}
1596
1597void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1598 if (location.IsRegister()) {
1599 locations->AddTemp(location);
1600 } else if (location.IsRegisterPair()) {
1601 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1602 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1603 } else {
1604 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1605 }
1606}
1607
David Brazdilfc6a86a2015-06-26 10:33:45 +00001608void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001609 if (successor->IsExitBlock()) {
1610 DCHECK(got->GetPrevious()->AlwaysThrows());
1611 return; // no code needed
1612 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001613
1614 HBasicBlock* block = got->GetBlock();
1615 HInstruction* previous = got->GetPrevious();
1616
1617 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001618 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001619 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001620 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1621 return;
1622 }
1623
1624 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1625 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1626 }
1627 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001628 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001629 }
1630}
1631
David Brazdilfc6a86a2015-06-26 10:33:45 +00001632void LocationsBuilderX86::VisitGoto(HGoto* got) {
1633 got->SetLocations(nullptr);
1634}
1635
1636void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1637 HandleGoto(got, got->GetSuccessor());
1638}
1639
1640void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1641 try_boundary->SetLocations(nullptr);
1642}
1643
1644void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1645 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1646 if (!successor->IsExitBlock()) {
1647 HandleGoto(try_boundary, successor);
1648 }
1649}
1650
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001651void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001652 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001653}
1654
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001655void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001656}
1657
Mark Mendell152408f2015-12-31 12:28:50 -05001658template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001659void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001660 LabelType* true_label,
1661 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001662 if (cond->IsFPConditionTrueIfNaN()) {
1663 __ j(kUnordered, true_label);
1664 } else if (cond->IsFPConditionFalseIfNaN()) {
1665 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001666 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001667 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001668}
1669
Mark Mendell152408f2015-12-31 12:28:50 -05001670template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001671void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001672 LabelType* true_label,
1673 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001674 LocationSummary* locations = cond->GetLocations();
1675 Location left = locations->InAt(0);
1676 Location right = locations->InAt(1);
1677 IfCondition if_cond = cond->GetCondition();
1678
Mark Mendellc4701932015-04-10 13:18:51 -04001679 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001680 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001681 IfCondition true_high_cond = if_cond;
1682 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001683 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001684
1685 // Set the conditions for the test, remembering that == needs to be
1686 // decided using the low words.
1687 switch (if_cond) {
1688 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001689 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001690 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001691 break;
1692 case kCondLT:
1693 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001694 break;
1695 case kCondLE:
1696 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001697 break;
1698 case kCondGT:
1699 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001700 break;
1701 case kCondGE:
1702 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001703 break;
Aart Bike9f37602015-10-09 11:15:55 -07001704 case kCondB:
1705 false_high_cond = kCondA;
1706 break;
1707 case kCondBE:
1708 true_high_cond = kCondB;
1709 break;
1710 case kCondA:
1711 false_high_cond = kCondB;
1712 break;
1713 case kCondAE:
1714 true_high_cond = kCondA;
1715 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001716 }
1717
1718 if (right.IsConstant()) {
1719 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001720 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001721 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001722
Aart Bika19616e2016-02-01 18:57:58 -08001723 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001724 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001725 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001726 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001727 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001728 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001729 __ j(X86Condition(true_high_cond), true_label);
1730 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001731 }
1732 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001733 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001734 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001735 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001736 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001737
1738 __ cmpl(left_high, right_high);
1739 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001740 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001741 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001742 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001743 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001744 __ j(X86Condition(true_high_cond), true_label);
1745 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001746 }
1747 // Must be equal high, so compare the lows.
1748 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001749 } else {
1750 DCHECK(right.IsDoubleStackSlot());
1751 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1752 if (if_cond == kCondNE) {
1753 __ j(X86Condition(true_high_cond), true_label);
1754 } else if (if_cond == kCondEQ) {
1755 __ j(X86Condition(false_high_cond), false_label);
1756 } else {
1757 __ j(X86Condition(true_high_cond), true_label);
1758 __ j(X86Condition(false_high_cond), false_label);
1759 }
1760 // Must be equal high, so compare the lows.
1761 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001762 }
1763 // The last comparison might be unsigned.
1764 __ j(final_condition, true_label);
1765}
1766
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001767void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1768 Location rhs,
1769 HInstruction* insn,
1770 bool is_double) {
1771 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1772 if (is_double) {
1773 if (rhs.IsFpuRegister()) {
1774 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1775 } else if (const_area != nullptr) {
1776 DCHECK(const_area->IsEmittedAtUseSite());
1777 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1778 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001779 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1780 const_area->GetBaseMethodAddress(),
1781 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001782 } else {
1783 DCHECK(rhs.IsDoubleStackSlot());
1784 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1785 }
1786 } else {
1787 if (rhs.IsFpuRegister()) {
1788 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1789 } else if (const_area != nullptr) {
1790 DCHECK(const_area->IsEmittedAtUseSite());
1791 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1792 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001793 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1794 const_area->GetBaseMethodAddress(),
1795 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001796 } else {
1797 DCHECK(rhs.IsStackSlot());
1798 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1799 }
1800 }
1801}
1802
Mark Mendell152408f2015-12-31 12:28:50 -05001803template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001804void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001805 LabelType* true_target_in,
1806 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001807 // Generated branching requires both targets to be explicit. If either of the
1808 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001809 LabelType fallthrough_target;
1810 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1811 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001812
Mark Mendellc4701932015-04-10 13:18:51 -04001813 LocationSummary* locations = condition->GetLocations();
1814 Location left = locations->InAt(0);
1815 Location right = locations->InAt(1);
1816
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001817 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001818 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001819 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001820 GenerateLongComparesAndJumps(condition, true_target, false_target);
1821 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001822 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001823 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001824 GenerateFPJumps(condition, true_target, false_target);
1825 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001826 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001827 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001828 GenerateFPJumps(condition, true_target, false_target);
1829 break;
1830 default:
1831 LOG(FATAL) << "Unexpected compare type " << type;
1832 }
1833
David Brazdil0debae72015-11-12 18:37:00 +00001834 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001835 __ jmp(false_target);
1836 }
David Brazdil0debae72015-11-12 18:37:00 +00001837
1838 if (fallthrough_target.IsLinked()) {
1839 __ Bind(&fallthrough_target);
1840 }
Mark Mendellc4701932015-04-10 13:18:51 -04001841}
1842
David Brazdil0debae72015-11-12 18:37:00 +00001843static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1844 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1845 // are set only strictly before `branch`. We can't use the eflags on long/FP
1846 // conditions if they are materialized due to the complex branching.
1847 return cond->IsCondition() &&
1848 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001849 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1850 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001851}
1852
Mark Mendell152408f2015-12-31 12:28:50 -05001853template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001854void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001855 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001856 LabelType* true_target,
1857 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001858 HInstruction* cond = instruction->InputAt(condition_input_index);
1859
1860 if (true_target == nullptr && false_target == nullptr) {
1861 // Nothing to do. The code always falls through.
1862 return;
1863 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001864 // Constant condition, statically compared against "true" (integer value 1).
1865 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001866 if (true_target != nullptr) {
1867 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001868 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001869 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001870 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001871 if (false_target != nullptr) {
1872 __ jmp(false_target);
1873 }
1874 }
1875 return;
1876 }
1877
1878 // The following code generates these patterns:
1879 // (1) true_target == nullptr && false_target != nullptr
1880 // - opposite condition true => branch to false_target
1881 // (2) true_target != nullptr && false_target == nullptr
1882 // - condition true => branch to true_target
1883 // (3) true_target != nullptr && false_target != nullptr
1884 // - condition true => branch to true_target
1885 // - branch to false_target
1886 if (IsBooleanValueOrMaterializedCondition(cond)) {
1887 if (AreEflagsSetFrom(cond, instruction)) {
1888 if (true_target == nullptr) {
1889 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1890 } else {
1891 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1892 }
1893 } else {
1894 // Materialized condition, compare against 0.
1895 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1896 if (lhs.IsRegister()) {
1897 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1898 } else {
1899 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1900 }
1901 if (true_target == nullptr) {
1902 __ j(kEqual, false_target);
1903 } else {
1904 __ j(kNotEqual, true_target);
1905 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001906 }
1907 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001908 // Condition has not been materialized, use its inputs as the comparison and
1909 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001910 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001911
1912 // If this is a long or FP comparison that has been folded into
1913 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001914 DataType::Type type = condition->InputAt(0)->GetType();
1915 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001916 GenerateCompareTestAndBranch(condition, true_target, false_target);
1917 return;
1918 }
1919
1920 Location lhs = condition->GetLocations()->InAt(0);
1921 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001922 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001923 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001924 if (true_target == nullptr) {
1925 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1926 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001927 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001928 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001929 }
David Brazdil0debae72015-11-12 18:37:00 +00001930
1931 // If neither branch falls through (case 3), the conditional branch to `true_target`
1932 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1933 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001934 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001935 }
1936}
1937
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001938void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001939 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001940 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001941 locations->SetInAt(0, Location::Any());
1942 }
1943}
1944
1945void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001946 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1947 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1948 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1949 nullptr : codegen_->GetLabelOf(true_successor);
1950 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1951 nullptr : codegen_->GetLabelOf(false_successor);
Andreas Gampe3db70682018-12-26 15:12:03 -08001952 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001953}
1954
1955void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001956 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001957 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001958 InvokeRuntimeCallingConvention calling_convention;
1959 RegisterSet caller_saves = RegisterSet::Empty();
1960 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1961 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001962 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001963 locations->SetInAt(0, Location::Any());
1964 }
1965}
1966
1967void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001968 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001969 GenerateTestAndBranch<Label>(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08001970 /* condition_input_index= */ 0,
David Brazdil74eb1b22015-12-14 11:44:01 +00001971 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08001972 /* false_target= */ nullptr);
David Brazdil74eb1b22015-12-14 11:44:01 +00001973}
1974
Mingyao Yang063fc772016-08-02 11:02:54 -07001975void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001976 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001977 LocationSummary(flag, LocationSummary::kNoCall);
1978 locations->SetOut(Location::RequiresRegister());
1979}
1980
1981void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1982 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1983 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1984}
1985
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001986static bool SelectCanUseCMOV(HSelect* select) {
1987 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001988 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001989 return false;
1990 }
1991
1992 // A FP condition doesn't generate the single CC that we need.
1993 // In 32 bit mode, a long condition doesn't generate a single CC either.
1994 HInstruction* condition = select->GetCondition();
1995 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001996 DataType::Type compare_type = condition->InputAt(0)->GetType();
1997 if (compare_type == DataType::Type::kInt64 ||
1998 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001999 return false;
2000 }
2001 }
2002
2003 // We can generate a CMOV for this Select.
2004 return true;
2005}
2006
David Brazdil74eb1b22015-12-14 11:44:01 +00002007void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002008 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002009 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00002010 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002011 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00002012 } else {
2013 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002014 if (SelectCanUseCMOV(select)) {
2015 if (select->InputAt(1)->IsConstant()) {
2016 // Cmov can't handle a constant value.
2017 locations->SetInAt(1, Location::RequiresRegister());
2018 } else {
2019 locations->SetInAt(1, Location::Any());
2020 }
2021 } else {
2022 locations->SetInAt(1, Location::Any());
2023 }
David Brazdil74eb1b22015-12-14 11:44:01 +00002024 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002025 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2026 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00002027 }
2028 locations->SetOut(Location::SameAsFirstInput());
2029}
2030
2031void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
2032 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002033 DCHECK(locations->InAt(0).Equals(locations->Out()));
2034 if (SelectCanUseCMOV(select)) {
2035 // If both the condition and the source types are integer, we can generate
2036 // a CMOV to implement Select.
2037
2038 HInstruction* select_condition = select->GetCondition();
2039 Condition cond = kNotEqual;
2040
2041 // Figure out how to test the 'condition'.
2042 if (select_condition->IsCondition()) {
2043 HCondition* condition = select_condition->AsCondition();
2044 if (!condition->IsEmittedAtUseSite()) {
2045 // This was a previously materialized condition.
2046 // Can we use the existing condition code?
2047 if (AreEflagsSetFrom(condition, select)) {
2048 // Materialization was the previous instruction. Condition codes are right.
2049 cond = X86Condition(condition->GetCondition());
2050 } else {
2051 // No, we have to recreate the condition code.
2052 Register cond_reg = locations->InAt(2).AsRegister<Register>();
2053 __ testl(cond_reg, cond_reg);
2054 }
2055 } else {
2056 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002057 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
2058 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002059 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01002060 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002061 cond = X86Condition(condition->GetCondition());
2062 }
2063 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01002064 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002065 Register cond_reg = locations->InAt(2).AsRegister<Register>();
2066 __ testl(cond_reg, cond_reg);
2067 }
2068
2069 // If the condition is true, overwrite the output, which already contains false.
2070 Location false_loc = locations->InAt(0);
2071 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002072 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002073 // 64 bit conditional move.
2074 Register false_high = false_loc.AsRegisterPairHigh<Register>();
2075 Register false_low = false_loc.AsRegisterPairLow<Register>();
2076 if (true_loc.IsRegisterPair()) {
2077 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
2078 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
2079 } else {
2080 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
2081 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
2082 }
2083 } else {
2084 // 32 bit conditional move.
2085 Register false_reg = false_loc.AsRegister<Register>();
2086 if (true_loc.IsRegister()) {
2087 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
2088 } else {
2089 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
2090 }
2091 }
2092 } else {
2093 NearLabel false_target;
2094 GenerateTestAndBranch<NearLabel>(
Andreas Gampe3db70682018-12-26 15:12:03 -08002095 select, /* condition_input_index= */ 2, /* true_target= */ nullptr, &false_target);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05002096 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2097 __ Bind(&false_target);
2098 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002099}
2100
David Srbecky0cf44932015-12-09 14:09:59 +00002101void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002102 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00002103}
2104
David Srbeckyd28f4a02016-03-14 17:14:24 +00002105void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
2106 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002107}
2108
Vladimir Markodec78172020-06-19 15:31:23 +01002109void CodeGeneratorX86::IncreaseFrame(size_t adjustment) {
2110 __ subl(ESP, Immediate(adjustment));
2111 __ cfi().AdjustCFAOffset(adjustment);
2112}
2113
2114void CodeGeneratorX86::DecreaseFrame(size_t adjustment) {
2115 __ addl(ESP, Immediate(adjustment));
2116 __ cfi().AdjustCFAOffset(-adjustment);
2117}
2118
David Srbeckyc7098ff2016-02-09 14:30:11 +00002119void CodeGeneratorX86::GenerateNop() {
2120 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002121}
2122
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002123void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002124 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002125 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04002126 // Handle the long/FP comparisons made in instruction simplification.
2127 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002128 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04002129 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05002130 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00002131 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002132 locations->SetOut(Location::RequiresRegister());
2133 }
2134 break;
2135 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002136 case DataType::Type::kFloat32:
2137 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04002138 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002139 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
2140 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
2141 } else if (cond->InputAt(1)->IsConstant()) {
2142 locations->SetInAt(1, Location::RequiresFpuRegister());
2143 } else {
2144 locations->SetInAt(1, Location::Any());
2145 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002146 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002147 locations->SetOut(Location::RequiresRegister());
2148 }
2149 break;
2150 }
2151 default:
2152 locations->SetInAt(0, Location::RequiresRegister());
2153 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00002154 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002155 // We need a byte register.
2156 locations->SetOut(Location::RegisterLocation(ECX));
2157 }
2158 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002159 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002160}
2161
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002162void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002163 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002164 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002165 }
Mark Mendellc4701932015-04-10 13:18:51 -04002166
2167 LocationSummary* locations = cond->GetLocations();
2168 Location lhs = locations->InAt(0);
2169 Location rhs = locations->InAt(1);
2170 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05002171 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002172
2173 switch (cond->InputAt(0)->GetType()) {
2174 default: {
2175 // Integer case.
2176
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002177 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04002178 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01002179 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07002180 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04002181 return;
2182 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002183 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04002184 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2185 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002186 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002187 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04002188 GenerateFPJumps(cond, &true_label, &false_label);
2189 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002190 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002191 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04002192 GenerateFPJumps(cond, &true_label, &false_label);
2193 break;
2194 }
2195
2196 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002197 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002198
Roland Levillain4fa13f62015-07-06 18:11:54 +01002199 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04002200 __ Bind(&false_label);
2201 __ xorl(reg, reg);
2202 __ jmp(&done_label);
2203
Roland Levillain4fa13f62015-07-06 18:11:54 +01002204 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04002205 __ Bind(&true_label);
2206 __ movl(reg, Immediate(1));
2207 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002208}
2209
2210void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002211 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002212}
2213
2214void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002215 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002216}
2217
2218void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002219 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002220}
2221
2222void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002223 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002224}
2225
2226void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002227 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002228}
2229
2230void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002231 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002232}
2233
2234void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002235 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002236}
2237
2238void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002239 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002240}
2241
2242void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002243 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002244}
2245
2246void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002247 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002248}
2249
2250void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002251 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002252}
2253
2254void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002255 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002256}
2257
Aart Bike9f37602015-10-09 11:15:55 -07002258void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002259 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002260}
2261
2262void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002263 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002264}
2265
2266void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002267 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002268}
2269
2270void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002271 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002272}
2273
2274void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002275 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002276}
2277
2278void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002279 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002280}
2281
2282void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002283 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002284}
2285
2286void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002287 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002288}
2289
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002290void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002291 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002292 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002293 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002294}
2295
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002296void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002297 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002298}
2299
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002300void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2301 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002302 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002303 locations->SetOut(Location::ConstantLocation(constant));
2304}
2305
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002306void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002307 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002308}
2309
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002310void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002311 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002312 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002313 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002314}
2315
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002316void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002317 // Will be generated at use site.
2318}
2319
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002320void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2321 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002322 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002323 locations->SetOut(Location::ConstantLocation(constant));
2324}
2325
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002326void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002327 // Will be generated at use site.
2328}
2329
2330void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2331 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002332 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002333 locations->SetOut(Location::ConstantLocation(constant));
2334}
2335
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002336void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002337 // Will be generated at use site.
2338}
2339
Igor Murashkind01745e2017-04-05 16:40:31 -07002340void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2341 constructor_fence->SetLocations(nullptr);
2342}
2343
2344void InstructionCodeGeneratorX86::VisitConstructorFence(
2345 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2346 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2347}
2348
Calin Juravle27df7582015-04-17 19:12:31 +01002349void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2350 memory_barrier->SetLocations(nullptr);
2351}
2352
2353void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002354 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002355}
2356
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002357void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002358 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002359}
2360
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002361void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002362 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002363}
2364
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002365void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002366 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002367 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002368 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002369 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002370 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002371 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002372 case DataType::Type::kInt8:
2373 case DataType::Type::kUint16:
2374 case DataType::Type::kInt16:
2375 case DataType::Type::kInt32:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002376 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002377 break;
2378
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002379 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002380 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002381 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002382 break;
2383
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002384 case DataType::Type::kFloat32:
2385 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002386 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002387 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002388 break;
2389
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002390 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002391 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002392 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002393}
2394
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002395void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002396 switch (ret->InputAt(0)->GetType()) {
2397 case DataType::Type::kReference:
2398 case DataType::Type::kBool:
2399 case DataType::Type::kUint8:
2400 case DataType::Type::kInt8:
2401 case DataType::Type::kUint16:
2402 case DataType::Type::kInt16:
2403 case DataType::Type::kInt32:
2404 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
2405 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002406
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002407 case DataType::Type::kInt64:
2408 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2409 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
2410 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002411
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002412 case DataType::Type::kFloat32:
2413 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2414 if (GetGraph()->IsCompilingOsr()) {
2415 // To simplify callers of an OSR method, we put the return value in both
2416 // floating point and core registers.
2417 __ movd(EAX, XMM0);
2418 }
2419 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002420
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002421 case DataType::Type::kFloat64:
2422 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2423 if (GetGraph()->IsCompilingOsr()) {
2424 // To simplify callers of an OSR method, we put the return value in both
2425 // floating point and core registers.
2426 __ movd(EAX, XMM0);
2427 // Use XMM1 as temporary register to not clobber XMM0.
2428 __ movaps(XMM1, XMM0);
2429 __ psrlq(XMM1, Immediate(32));
2430 __ movd(EDX, XMM1);
2431 }
2432 break;
2433
2434 default:
2435 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002436 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002437 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002438}
2439
Calin Juravle175dc732015-08-25 15:42:32 +01002440void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2441 // The trampoline uses the same calling convention as dex calling conventions,
2442 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2443 // the method_idx.
2444 HandleInvoke(invoke);
2445}
2446
2447void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2448 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2449}
2450
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002451void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002452 // Explicit clinit checks triggered by static invokes must have been pruned by
2453 // art::PrepareForRegisterAllocation.
2454 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002455
Mark Mendellfb8d2792015-03-31 22:16:59 -04002456 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002457 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002458 if (invoke->GetLocations()->CanCall() &&
2459 invoke->HasPcRelativeMethodLoadKind() &&
2460 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).IsInvalid()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002461 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002462 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002463 return;
2464 }
2465
Vladimir Marko86c87522020-05-11 16:55:55 +01002466 if (invoke->GetCodePtrLocation() == HInvokeStaticOrDirect::CodePtrLocation::kCallCriticalNative) {
2467 CriticalNativeCallingConventionVisitorX86 calling_convention_visitor(
2468 /*for_register_allocation=*/ true);
2469 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2470 } else {
2471 HandleInvoke(invoke);
2472 }
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002473
Vladimir Marko86c87522020-05-11 16:55:55 +01002474 // For PC-relative load kinds the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002475 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002476 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002477 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002478}
2479
Mark Mendell09ed1a32015-03-25 08:30:06 -04002480static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2481 if (invoke->GetLocations()->Intrinsified()) {
2482 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2483 intrinsic.Dispatch(invoke);
2484 return true;
2485 }
2486 return false;
2487}
2488
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002489void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002490 // Explicit clinit checks triggered by static invokes must have been pruned by
2491 // art::PrepareForRegisterAllocation.
2492 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002493
Mark Mendell09ed1a32015-03-25 08:30:06 -04002494 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2495 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002496 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002497
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002498 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002499 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002500 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002501}
2502
2503void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002504 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2505 if (intrinsic.TryDispatch(invoke)) {
2506 return;
2507 }
2508
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002509 HandleInvoke(invoke);
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002510
2511 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002512 // Add one temporary for inline cache update.
2513 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2514 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002515}
2516
2517void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002518 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002519 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002520}
2521
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002522void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002523 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2524 return;
2525 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002526
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002527 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002528 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002529}
2530
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002531void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002532 // This call to HandleInvoke allocates a temporary (core) register
2533 // which is also used to transfer the hidden argument from FP to
2534 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002535 HandleInvoke(invoke);
2536 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002537 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002538
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002539 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002540 // Add one temporary for inline cache update.
2541 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2542 }
2543}
2544
2545void CodeGeneratorX86::MaybeGenerateInlineCacheCheck(HInstruction* instruction, Register klass) {
2546 DCHECK_EQ(EAX, klass);
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002547 // We know the destination of an intrinsic, so no need to record inline
2548 // caches (also the intrinsic location builder doesn't request an additional
2549 // temporary).
2550 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002551 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002552 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002553 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
Nicolas Geoffray095dc462020-08-17 16:40:28 +01002554 ScopedProfilingInfoUse spiu(
2555 Runtime::Current()->GetJit(), GetGraph()->GetArtMethod(), Thread::Current());
2556 ProfilingInfo* info = spiu.GetProfilingInfo();
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002557 if (info != nullptr) {
2558 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
2559 uint32_t address = reinterpret_cast32<uint32_t>(cache);
2560 if (kIsDebugBuild) {
2561 uint32_t temp_index = instruction->GetLocations()->GetTempCount() - 1u;
2562 CHECK_EQ(EBP, instruction->GetLocations()->GetTemp(temp_index).AsRegister<Register>());
2563 }
2564 Register temp = EBP;
2565 NearLabel done;
2566 __ movl(temp, Immediate(address));
2567 // Fast path for a monomorphic cache.
2568 __ cmpl(klass, Address(temp, InlineCache::ClassesOffset().Int32Value()));
2569 __ j(kEqual, &done);
2570 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(kQuickUpdateInlineCache).Int32Value());
2571 __ Bind(&done);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002572 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002573 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002574}
2575
2576void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2577 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002578 LocationSummary* locations = invoke->GetLocations();
2579 Register temp = locations->GetTemp(0).AsRegister<Register>();
2580 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002581 Location receiver = locations->InAt(0);
2582 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2583
Roland Levillain0d5a2812015-11-13 10:07:31 +00002584 // Set the hidden argument. This is safe to do this here, as XMM7
2585 // won't be modified thereafter, before the `call` instruction.
2586 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01002587 __ movl(temp, Immediate(invoke->GetMethodReference().index));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002588 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002589
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002590 if (receiver.IsStackSlot()) {
2591 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002592 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002593 __ movl(temp, Address(temp, class_offset));
2594 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002595 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002596 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002597 }
Roland Levillain4d027112015-07-01 15:41:14 +01002598 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002599 // Instead of simply (possibly) unpoisoning `temp` here, we should
2600 // emit a read barrier for the previous class reference load.
2601 // However this is not required in practice, as this is an
2602 // intermediate/temporary reference and because the current
2603 // concurrent copying collector keeps the from-space memory
2604 // intact/accessible until the end of the marking phase (the
2605 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002606 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002607
2608 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
2609
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002610 // temp = temp->GetAddressOfIMT()
2611 __ movl(temp,
2612 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002613 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002614 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002615 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002616 __ movl(temp, Address(temp, method_offset));
2617 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002618 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002619 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002620
2621 DCHECK(!codegen_->IsLeafMethod());
2622 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2623}
2624
Orion Hodsonac141392017-01-13 11:53:47 +00002625void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00002626 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2627 if (intrinsic.TryDispatch(invoke)) {
2628 return;
2629 }
Orion Hodsonac141392017-01-13 11:53:47 +00002630 HandleInvoke(invoke);
2631}
2632
2633void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00002634 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2635 return;
2636 }
Orion Hodsonac141392017-01-13 11:53:47 +00002637 codegen_->GenerateInvokePolymorphicCall(invoke);
2638}
2639
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002640void LocationsBuilderX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2641 HandleInvoke(invoke);
2642}
2643
2644void InstructionCodeGeneratorX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2645 codegen_->GenerateInvokeCustomCall(invoke);
2646}
2647
Roland Levillain88cb1752014-10-20 16:36:47 +01002648void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2649 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002650 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002651 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002652 case DataType::Type::kInt32:
2653 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002654 locations->SetInAt(0, Location::RequiresRegister());
2655 locations->SetOut(Location::SameAsFirstInput());
2656 break;
2657
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002658 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002659 locations->SetInAt(0, Location::RequiresFpuRegister());
2660 locations->SetOut(Location::SameAsFirstInput());
2661 locations->AddTemp(Location::RequiresRegister());
2662 locations->AddTemp(Location::RequiresFpuRegister());
2663 break;
2664
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002665 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002666 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002667 locations->SetOut(Location::SameAsFirstInput());
2668 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002669 break;
2670
2671 default:
2672 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2673 }
2674}
2675
2676void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2677 LocationSummary* locations = neg->GetLocations();
2678 Location out = locations->Out();
2679 Location in = locations->InAt(0);
2680 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002681 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002682 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002683 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002684 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002685 break;
2686
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002687 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002688 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002689 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002690 __ negl(out.AsRegisterPairLow<Register>());
2691 // Negation is similar to subtraction from zero. The least
2692 // significant byte triggers a borrow when it is different from
2693 // zero; to take it into account, add 1 to the most significant
2694 // byte if the carry flag (CF) is set to 1 after the first NEGL
2695 // operation.
2696 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2697 __ negl(out.AsRegisterPairHigh<Register>());
2698 break;
2699
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002700 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002701 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002702 Register constant = locations->GetTemp(0).AsRegister<Register>();
2703 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002704 // Implement float negation with an exclusive or with value
2705 // 0x80000000 (mask for bit 31, representing the sign of a
2706 // single-precision floating-point number).
2707 __ movl(constant, Immediate(INT32_C(0x80000000)));
2708 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002709 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002710 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002711 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002712
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002713 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002714 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002715 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002716 // Implement double negation with an exclusive or with value
2717 // 0x8000000000000000 (mask for bit 63, representing the sign of
2718 // a double-precision floating-point number).
2719 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002720 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002721 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002722 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002723
2724 default:
2725 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2726 }
2727}
2728
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002729void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2730 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002731 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002732 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002733 locations->SetInAt(0, Location::RequiresFpuRegister());
2734 locations->SetInAt(1, Location::RequiresRegister());
2735 locations->SetOut(Location::SameAsFirstInput());
2736 locations->AddTemp(Location::RequiresFpuRegister());
2737}
2738
2739void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2740 LocationSummary* locations = neg->GetLocations();
2741 Location out = locations->Out();
2742 DCHECK(locations->InAt(0).Equals(out));
2743
2744 Register constant_area = locations->InAt(1).AsRegister<Register>();
2745 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002746 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002747 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2748 neg->GetBaseMethodAddress(),
2749 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002750 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2751 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002752 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2753 neg->GetBaseMethodAddress(),
2754 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002755 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2756 }
2757}
2758
Roland Levillaindff1f282014-11-05 14:15:05 +00002759void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002760 DataType::Type result_type = conversion->GetResultType();
2761 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002762 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2763 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002764
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002765 // The float-to-long and double-to-long type conversions rely on a
2766 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002767 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002768 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2769 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002770 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002771 : LocationSummary::kNoCall;
2772 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002773 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002774
Roland Levillaindff1f282014-11-05 14:15:05 +00002775 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002776 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002777 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002778 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002779 case DataType::Type::kUint8:
2780 case DataType::Type::kInt8:
2781 case DataType::Type::kUint16:
2782 case DataType::Type::kInt16:
2783 case DataType::Type::kInt32:
2784 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2785 // Make the output overlap to please the register allocator. This greatly simplifies
2786 // the validation of the linear scan implementation
2787 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2788 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002789 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002790 HInstruction* input = conversion->InputAt(0);
2791 Location input_location = input->IsConstant()
2792 ? Location::ConstantLocation(input->AsConstant())
2793 : Location::RegisterPairLocation(EAX, EDX);
2794 locations->SetInAt(0, input_location);
2795 // Make the output overlap to please the register allocator. This greatly simplifies
2796 // the validation of the linear scan implementation
2797 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2798 break;
2799 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002800
2801 default:
2802 LOG(FATAL) << "Unexpected type conversion from " << input_type
2803 << " to " << result_type;
2804 }
2805 break;
2806
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002807 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002808 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002809 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2810 locations->SetInAt(0, Location::Any());
2811 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002812 break;
2813
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002814 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002815 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002816 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002817 locations->SetInAt(0, Location::Any());
2818 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2819 break;
2820
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002821 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002822 locations->SetInAt(0, Location::RequiresFpuRegister());
2823 locations->SetOut(Location::RequiresRegister());
2824 locations->AddTemp(Location::RequiresFpuRegister());
2825 break;
2826
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002827 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002828 locations->SetInAt(0, Location::RequiresFpuRegister());
2829 locations->SetOut(Location::RequiresRegister());
2830 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002831 break;
2832
2833 default:
2834 LOG(FATAL) << "Unexpected type conversion from " << input_type
2835 << " to " << result_type;
2836 }
2837 break;
2838
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002839 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002840 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002841 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002842 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002843 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002844 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002845 case DataType::Type::kInt16:
2846 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002847 locations->SetInAt(0, Location::RegisterLocation(EAX));
2848 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2849 break;
2850
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002851 case DataType::Type::kFloat32:
2852 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002853 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002854 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2855 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2856
Vladimir Marko949c91f2015-01-27 10:48:44 +00002857 // The runtime helper puts the result in EAX, EDX.
2858 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002859 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002860 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002861
2862 default:
2863 LOG(FATAL) << "Unexpected type conversion from " << input_type
2864 << " to " << result_type;
2865 }
2866 break;
2867
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002868 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002869 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002870 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002871 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002872 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002873 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002874 case DataType::Type::kInt16:
2875 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002876 locations->SetInAt(0, Location::RequiresRegister());
2877 locations->SetOut(Location::RequiresFpuRegister());
2878 break;
2879
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002880 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002881 locations->SetInAt(0, Location::Any());
2882 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002883 break;
2884
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002885 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002886 locations->SetInAt(0, Location::RequiresFpuRegister());
2887 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002888 break;
2889
2890 default:
2891 LOG(FATAL) << "Unexpected type conversion from " << input_type
2892 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002893 }
Roland Levillaincff13742014-11-17 14:32:17 +00002894 break;
2895
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002896 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002897 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002898 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002899 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002900 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002901 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002902 case DataType::Type::kInt16:
2903 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002904 locations->SetInAt(0, Location::RequiresRegister());
2905 locations->SetOut(Location::RequiresFpuRegister());
2906 break;
2907
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002908 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002909 locations->SetInAt(0, Location::Any());
2910 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002911 break;
2912
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002913 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002914 locations->SetInAt(0, Location::RequiresFpuRegister());
2915 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002916 break;
2917
2918 default:
2919 LOG(FATAL) << "Unexpected type conversion from " << input_type
2920 << " to " << result_type;
2921 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002922 break;
2923
2924 default:
2925 LOG(FATAL) << "Unexpected type conversion from " << input_type
2926 << " to " << result_type;
2927 }
2928}
2929
2930void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2931 LocationSummary* locations = conversion->GetLocations();
2932 Location out = locations->Out();
2933 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002934 DataType::Type result_type = conversion->GetResultType();
2935 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002936 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2937 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002938 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002939 case DataType::Type::kUint8:
2940 switch (input_type) {
2941 case DataType::Type::kInt8:
2942 case DataType::Type::kUint16:
2943 case DataType::Type::kInt16:
2944 case DataType::Type::kInt32:
2945 if (in.IsRegister()) {
2946 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2947 } else {
2948 DCHECK(in.GetConstant()->IsIntConstant());
2949 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2950 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2951 }
2952 break;
2953 case DataType::Type::kInt64:
2954 if (in.IsRegisterPair()) {
2955 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2956 } else {
2957 DCHECK(in.GetConstant()->IsLongConstant());
2958 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2959 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2960 }
2961 break;
2962
2963 default:
2964 LOG(FATAL) << "Unexpected type conversion from " << input_type
2965 << " to " << result_type;
2966 }
2967 break;
2968
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002969 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002970 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002971 case DataType::Type::kUint8:
2972 case DataType::Type::kUint16:
2973 case DataType::Type::kInt16:
2974 case DataType::Type::kInt32:
2975 if (in.IsRegister()) {
2976 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2977 } else {
2978 DCHECK(in.GetConstant()->IsIntConstant());
2979 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2980 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2981 }
2982 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002983 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002984 if (in.IsRegisterPair()) {
2985 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2986 } else {
2987 DCHECK(in.GetConstant()->IsLongConstant());
2988 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2989 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2990 }
2991 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002992
2993 default:
2994 LOG(FATAL) << "Unexpected type conversion from " << input_type
2995 << " to " << result_type;
2996 }
2997 break;
2998
2999 case DataType::Type::kUint16:
3000 switch (input_type) {
3001 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003002 case DataType::Type::kInt16:
3003 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003004 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003005 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
3006 } else if (in.IsStackSlot()) {
3007 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003008 } else {
3009 DCHECK(in.GetConstant()->IsIntConstant());
3010 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003011 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
3012 }
3013 break;
3014 case DataType::Type::kInt64:
3015 if (in.IsRegisterPair()) {
3016 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
3017 } else if (in.IsDoubleStackSlot()) {
3018 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
3019 } else {
3020 DCHECK(in.GetConstant()->IsLongConstant());
3021 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3022 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003023 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00003024 break;
3025
3026 default:
3027 LOG(FATAL) << "Unexpected type conversion from " << input_type
3028 << " to " << result_type;
3029 }
3030 break;
3031
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003032 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00003033 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003034 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003035 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00003036 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003037 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00003038 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003039 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00003040 } else {
3041 DCHECK(in.GetConstant()->IsIntConstant());
3042 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003043 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00003044 }
3045 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003046 case DataType::Type::kInt64:
3047 if (in.IsRegisterPair()) {
3048 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
3049 } else if (in.IsDoubleStackSlot()) {
3050 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
3051 } else {
3052 DCHECK(in.GetConstant()->IsLongConstant());
3053 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
3054 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
3055 }
3056 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00003057
3058 default:
3059 LOG(FATAL) << "Unexpected type conversion from " << input_type
3060 << " to " << result_type;
3061 }
3062 break;
3063
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003064 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00003065 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003066 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00003067 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003068 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00003069 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003070 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00003071 } else {
3072 DCHECK(in.IsConstant());
3073 DCHECK(in.GetConstant()->IsLongConstant());
3074 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003075 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00003076 }
3077 break;
3078
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003079 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00003080 XmmRegister input = in.AsFpuRegister<XmmRegister>();
3081 Register output = out.AsRegister<Register>();
3082 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04003083 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00003084
3085 __ movl(output, Immediate(kPrimIntMax));
3086 // temp = int-to-float(output)
3087 __ cvtsi2ss(temp, output);
3088 // if input >= temp goto done
3089 __ comiss(input, temp);
3090 __ j(kAboveEqual, &done);
3091 // if input == NaN goto nan
3092 __ j(kUnordered, &nan);
3093 // output = float-to-int-truncate(input)
3094 __ cvttss2si(output, input);
3095 __ jmp(&done);
3096 __ Bind(&nan);
3097 // output = 0
3098 __ xorl(output, output);
3099 __ Bind(&done);
3100 break;
3101 }
3102
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003103 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003104 XmmRegister input = in.AsFpuRegister<XmmRegister>();
3105 Register output = out.AsRegister<Register>();
3106 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04003107 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003108
3109 __ movl(output, Immediate(kPrimIntMax));
3110 // temp = int-to-double(output)
3111 __ cvtsi2sd(temp, output);
3112 // if input >= temp goto done
3113 __ comisd(input, temp);
3114 __ j(kAboveEqual, &done);
3115 // if input == NaN goto nan
3116 __ j(kUnordered, &nan);
3117 // output = double-to-int-truncate(input)
3118 __ cvttsd2si(output, input);
3119 __ jmp(&done);
3120 __ Bind(&nan);
3121 // output = 0
3122 __ xorl(output, output);
3123 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00003124 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003125 }
Roland Levillain946e1432014-11-11 17:35:19 +00003126
3127 default:
3128 LOG(FATAL) << "Unexpected type conversion from " << input_type
3129 << " to " << result_type;
3130 }
3131 break;
3132
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003133 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00003134 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003135 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003136 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003137 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003138 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003139 case DataType::Type::kInt16:
3140 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00003141 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
3142 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003143 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00003144 __ cdq();
3145 break;
3146
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003147 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01003148 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003149 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00003150 break;
3151
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003152 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01003153 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003154 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00003155 break;
3156
3157 default:
3158 LOG(FATAL) << "Unexpected type conversion from " << input_type
3159 << " to " << result_type;
3160 }
3161 break;
3162
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003163 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00003164 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003165 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003166 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003167 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003168 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003169 case DataType::Type::kInt16:
3170 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003171 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003172 break;
3173
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003174 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003175 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00003176
Roland Levillain232ade02015-04-20 15:14:36 +01003177 // Create stack space for the call to
3178 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
3179 // TODO: enhance register allocator to ask for stack temporaries.
3180 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003181 adjustment = DataType::Size(DataType::Type::kInt64);
Vladimir Markodec78172020-06-19 15:31:23 +01003182 codegen_->IncreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003183 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003184
Roland Levillain232ade02015-04-20 15:14:36 +01003185 // Load the value to the FP stack, using temporaries if needed.
3186 PushOntoFPStack(in, 0, adjustment, false, true);
3187
3188 if (out.IsStackSlot()) {
3189 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
3190 } else {
3191 __ fstps(Address(ESP, 0));
3192 Location stack_temp = Location::StackSlot(0);
3193 codegen_->Move32(out, stack_temp);
3194 }
3195
3196 // Remove the temporary stack space we allocated.
3197 if (adjustment != 0) {
Vladimir Markodec78172020-06-19 15:31:23 +01003198 codegen_->DecreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003199 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003200 break;
3201 }
3202
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003203 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003204 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003205 break;
3206
3207 default:
3208 LOG(FATAL) << "Unexpected type conversion from " << input_type
3209 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003210 }
Roland Levillaincff13742014-11-17 14:32:17 +00003211 break;
3212
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003213 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00003214 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003215 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003216 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003217 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003218 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003219 case DataType::Type::kInt16:
3220 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003221 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003222 break;
3223
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003224 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003225 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00003226
Roland Levillain232ade02015-04-20 15:14:36 +01003227 // Create stack space for the call to
3228 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
3229 // TODO: enhance register allocator to ask for stack temporaries.
3230 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003231 adjustment = DataType::Size(DataType::Type::kInt64);
Vladimir Markodec78172020-06-19 15:31:23 +01003232 codegen_->IncreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003233 }
3234
3235 // Load the value to the FP stack, using temporaries if needed.
3236 PushOntoFPStack(in, 0, adjustment, false, true);
3237
3238 if (out.IsDoubleStackSlot()) {
3239 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
3240 } else {
3241 __ fstpl(Address(ESP, 0));
3242 Location stack_temp = Location::DoubleStackSlot(0);
3243 codegen_->Move64(out, stack_temp);
3244 }
3245
3246 // Remove the temporary stack space we allocated.
3247 if (adjustment != 0) {
Vladimir Markodec78172020-06-19 15:31:23 +01003248 codegen_->DecreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003249 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003250 break;
3251 }
3252
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003253 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003254 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003255 break;
3256
3257 default:
3258 LOG(FATAL) << "Unexpected type conversion from " << input_type
3259 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003260 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003261 break;
3262
3263 default:
3264 LOG(FATAL) << "Unexpected type conversion from " << input_type
3265 << " to " << result_type;
3266 }
3267}
3268
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003269void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003270 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003271 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003272 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003273 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05003274 locations->SetInAt(0, Location::RequiresRegister());
3275 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3276 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3277 break;
3278 }
3279
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003280 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003281 locations->SetInAt(0, Location::RequiresRegister());
3282 locations->SetInAt(1, Location::Any());
3283 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003284 break;
3285 }
3286
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003287 case DataType::Type::kFloat32:
3288 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003289 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003290 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3291 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003292 } else if (add->InputAt(1)->IsConstant()) {
3293 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003294 } else {
3295 locations->SetInAt(1, Location::Any());
3296 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003297 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003298 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003299 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003300
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003301 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003302 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Elliott Hughesc1896c92018-11-29 11:33:18 -08003303 UNREACHABLE();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003304 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003305}
3306
3307void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
3308 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003309 Location first = locations->InAt(0);
3310 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05003311 Location out = locations->Out();
3312
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003313 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003314 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003315 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003316 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3317 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003318 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3319 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05003320 } else {
3321 __ leal(out.AsRegister<Register>(), Address(
3322 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3323 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003324 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003325 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3326 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3327 __ addl(out.AsRegister<Register>(), Immediate(value));
3328 } else {
3329 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3330 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003331 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003332 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003333 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003334 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003335 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003336 }
3337
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003338 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003339 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003340 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3341 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003342 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003343 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3344 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003345 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003346 } else {
3347 DCHECK(second.IsConstant()) << second;
3348 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3349 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3350 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003351 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003352 break;
3353 }
3354
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003355 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003356 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003357 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003358 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3359 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003360 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003361 __ addss(first.AsFpuRegister<XmmRegister>(),
3362 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003363 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3364 const_area->GetBaseMethodAddress(),
3365 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003366 } else {
3367 DCHECK(second.IsStackSlot());
3368 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003369 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003370 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003371 }
3372
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003373 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003374 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003375 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003376 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3377 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003378 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003379 __ addsd(first.AsFpuRegister<XmmRegister>(),
3380 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003381 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3382 const_area->GetBaseMethodAddress(),
3383 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003384 } else {
3385 DCHECK(second.IsDoubleStackSlot());
3386 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003387 }
3388 break;
3389 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003390
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003391 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003392 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003393 }
3394}
3395
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003396void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003397 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003398 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003399 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003400 case DataType::Type::kInt32:
3401 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003402 locations->SetInAt(0, Location::RequiresRegister());
3403 locations->SetInAt(1, Location::Any());
3404 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003405 break;
3406 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003407 case DataType::Type::kFloat32:
3408 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003409 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003410 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3411 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003412 } else if (sub->InputAt(1)->IsConstant()) {
3413 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003414 } else {
3415 locations->SetInAt(1, Location::Any());
3416 }
Calin Juravle11351682014-10-23 15:38:15 +01003417 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003418 break;
Calin Juravle11351682014-10-23 15:38:15 +01003419 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003420
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003421 default:
Calin Juravle11351682014-10-23 15:38:15 +01003422 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003423 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003424}
3425
3426void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3427 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003428 Location first = locations->InAt(0);
3429 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003430 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003431 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003432 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003433 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003434 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003435 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003436 __ subl(first.AsRegister<Register>(),
3437 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003438 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003439 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003440 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003441 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003442 }
3443
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003444 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003445 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003446 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3447 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003448 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003449 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003450 __ sbbl(first.AsRegisterPairHigh<Register>(),
3451 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003452 } else {
3453 DCHECK(second.IsConstant()) << second;
3454 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3455 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3456 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003457 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003458 break;
3459 }
3460
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003461 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003462 if (second.IsFpuRegister()) {
3463 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3464 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3465 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003466 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003467 __ subss(first.AsFpuRegister<XmmRegister>(),
3468 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003469 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3470 const_area->GetBaseMethodAddress(),
3471 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003472 } else {
3473 DCHECK(second.IsStackSlot());
3474 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3475 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003476 break;
Calin Juravle11351682014-10-23 15:38:15 +01003477 }
3478
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003479 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003480 if (second.IsFpuRegister()) {
3481 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3482 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3483 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003484 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003485 __ subsd(first.AsFpuRegister<XmmRegister>(),
3486 codegen_->LiteralDoubleAddress(
3487 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003488 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003489 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3490 } else {
3491 DCHECK(second.IsDoubleStackSlot());
3492 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3493 }
Calin Juravle11351682014-10-23 15:38:15 +01003494 break;
3495 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003496
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003497 default:
Calin Juravle11351682014-10-23 15:38:15 +01003498 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003499 }
3500}
3501
Calin Juravle34bacdf2014-10-07 20:23:36 +01003502void LocationsBuilderX86::VisitMul(HMul* mul) {
3503 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003504 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003505 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003506 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003507 locations->SetInAt(0, Location::RequiresRegister());
3508 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003509 if (mul->InputAt(1)->IsIntConstant()) {
3510 // Can use 3 operand multiply.
3511 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3512 } else {
3513 locations->SetOut(Location::SameAsFirstInput());
3514 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003515 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003516 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003517 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003518 locations->SetInAt(1, Location::Any());
3519 locations->SetOut(Location::SameAsFirstInput());
3520 // Needed for imul on 32bits with 64bits output.
3521 locations->AddTemp(Location::RegisterLocation(EAX));
3522 locations->AddTemp(Location::RegisterLocation(EDX));
3523 break;
3524 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003525 case DataType::Type::kFloat32:
3526 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003527 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003528 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3529 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003530 } else if (mul->InputAt(1)->IsConstant()) {
3531 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003532 } else {
3533 locations->SetInAt(1, Location::Any());
3534 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003535 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003536 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003537 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003538
3539 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003540 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003541 }
3542}
3543
3544void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3545 LocationSummary* locations = mul->GetLocations();
3546 Location first = locations->InAt(0);
3547 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003548 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003549
3550 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003551 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003552 // The constant may have ended up in a register, so test explicitly to avoid
3553 // problems where the output may not be the same as the first operand.
3554 if (mul->InputAt(1)->IsIntConstant()) {
3555 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3556 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3557 } else if (second.IsRegister()) {
3558 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003559 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003560 } else {
3561 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003562 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003563 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003564 }
3565 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003566
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003567 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003568 Register in1_hi = first.AsRegisterPairHigh<Register>();
3569 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003570 Register eax = locations->GetTemp(0).AsRegister<Register>();
3571 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003572
3573 DCHECK_EQ(EAX, eax);
3574 DCHECK_EQ(EDX, edx);
3575
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003576 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003577 // output: in1
3578 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3579 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3580 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003581 if (second.IsConstant()) {
3582 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003583
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003584 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3585 int32_t low_value = Low32Bits(value);
3586 int32_t high_value = High32Bits(value);
3587 Immediate low(low_value);
3588 Immediate high(high_value);
3589
3590 __ movl(eax, high);
3591 // eax <- in1.lo * in2.hi
3592 __ imull(eax, in1_lo);
3593 // in1.hi <- in1.hi * in2.lo
3594 __ imull(in1_hi, low);
3595 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3596 __ addl(in1_hi, eax);
3597 // move in2_lo to eax to prepare for double precision
3598 __ movl(eax, low);
3599 // edx:eax <- in1.lo * in2.lo
3600 __ mull(in1_lo);
3601 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3602 __ addl(in1_hi, edx);
3603 // in1.lo <- (in1.lo * in2.lo)[31:0];
3604 __ movl(in1_lo, eax);
3605 } else if (second.IsRegisterPair()) {
3606 Register in2_hi = second.AsRegisterPairHigh<Register>();
3607 Register in2_lo = second.AsRegisterPairLow<Register>();
3608
3609 __ movl(eax, in2_hi);
3610 // eax <- in1.lo * in2.hi
3611 __ imull(eax, in1_lo);
3612 // in1.hi <- in1.hi * in2.lo
3613 __ imull(in1_hi, in2_lo);
3614 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3615 __ addl(in1_hi, eax);
3616 // move in1_lo to eax to prepare for double precision
3617 __ movl(eax, in1_lo);
3618 // edx:eax <- in1.lo * in2.lo
3619 __ mull(in2_lo);
3620 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3621 __ addl(in1_hi, edx);
3622 // in1.lo <- (in1.lo * in2.lo)[31:0];
3623 __ movl(in1_lo, eax);
3624 } else {
3625 DCHECK(second.IsDoubleStackSlot()) << second;
3626 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3627 Address in2_lo(ESP, second.GetStackIndex());
3628
3629 __ movl(eax, in2_hi);
3630 // eax <- in1.lo * in2.hi
3631 __ imull(eax, in1_lo);
3632 // in1.hi <- in1.hi * in2.lo
3633 __ imull(in1_hi, in2_lo);
3634 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3635 __ addl(in1_hi, eax);
3636 // move in1_lo to eax to prepare for double precision
3637 __ movl(eax, in1_lo);
3638 // edx:eax <- in1.lo * in2.lo
3639 __ mull(in2_lo);
3640 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3641 __ addl(in1_hi, edx);
3642 // in1.lo <- (in1.lo * in2.lo)[31:0];
3643 __ movl(in1_lo, eax);
3644 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003645
3646 break;
3647 }
3648
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003649 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003650 DCHECK(first.Equals(locations->Out()));
3651 if (second.IsFpuRegister()) {
3652 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3653 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3654 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003655 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003656 __ mulss(first.AsFpuRegister<XmmRegister>(),
3657 codegen_->LiteralFloatAddress(
3658 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003659 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003660 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3661 } else {
3662 DCHECK(second.IsStackSlot());
3663 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3664 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003665 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003666 }
3667
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003668 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003669 DCHECK(first.Equals(locations->Out()));
3670 if (second.IsFpuRegister()) {
3671 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3672 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3673 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003674 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003675 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3676 codegen_->LiteralDoubleAddress(
3677 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003678 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003679 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3680 } else {
3681 DCHECK(second.IsDoubleStackSlot());
3682 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3683 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003684 break;
3685 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003686
3687 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003688 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003689 }
3690}
3691
Roland Levillain232ade02015-04-20 15:14:36 +01003692void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3693 uint32_t temp_offset,
3694 uint32_t stack_adjustment,
3695 bool is_fp,
3696 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003697 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003698 DCHECK(!is_wide);
3699 if (is_fp) {
3700 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3701 } else {
3702 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3703 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003704 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003705 DCHECK(is_wide);
3706 if (is_fp) {
3707 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3708 } else {
3709 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3710 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003711 } else {
3712 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003713 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003714 Location stack_temp = Location::StackSlot(temp_offset);
3715 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003716 if (is_fp) {
3717 __ flds(Address(ESP, temp_offset));
3718 } else {
3719 __ filds(Address(ESP, temp_offset));
3720 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003721 } else {
3722 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3723 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003724 if (is_fp) {
3725 __ fldl(Address(ESP, temp_offset));
3726 } else {
3727 __ fildl(Address(ESP, temp_offset));
3728 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003729 }
3730 }
3731}
3732
3733void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003734 DataType::Type type = rem->GetResultType();
3735 bool is_float = type == DataType::Type::kFloat32;
3736 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003737 LocationSummary* locations = rem->GetLocations();
3738 Location first = locations->InAt(0);
3739 Location second = locations->InAt(1);
3740 Location out = locations->Out();
3741
3742 // Create stack space for 2 elements.
3743 // TODO: enhance register allocator to ask for stack temporaries.
Vladimir Markodec78172020-06-19 15:31:23 +01003744 codegen_->IncreaseFrame(2 * elem_size);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003745
3746 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003747 const bool is_wide = !is_float;
Andreas Gampe3db70682018-12-26 15:12:03 -08003748 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp= */ true, is_wide);
3749 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp= */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003750
3751 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003752 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003753 __ Bind(&retry);
3754 __ fprem();
3755
3756 // Move FP status to AX.
3757 __ fstsw();
3758
3759 // And see if the argument reduction is complete. This is signaled by the
3760 // C2 FPU flag bit set to 0.
3761 __ andl(EAX, Immediate(kC2ConditionMask));
3762 __ j(kNotEqual, &retry);
3763
3764 // We have settled on the final value. Retrieve it into an XMM register.
3765 // Store FP top of stack to real stack.
3766 if (is_float) {
3767 __ fsts(Address(ESP, 0));
3768 } else {
3769 __ fstl(Address(ESP, 0));
3770 }
3771
3772 // Pop the 2 items from the FP stack.
3773 __ fucompp();
3774
3775 // Load the value from the stack into an XMM register.
3776 DCHECK(out.IsFpuRegister()) << out;
3777 if (is_float) {
3778 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3779 } else {
3780 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3781 }
3782
3783 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01003784 codegen_->DecreaseFrame(2 * elem_size);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003785}
3786
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003787
3788void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3789 DCHECK(instruction->IsDiv() || instruction->IsRem());
3790
3791 LocationSummary* locations = instruction->GetLocations();
3792 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003793 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003794
3795 Register out_register = locations->Out().AsRegister<Register>();
3796 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003797 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003798
3799 DCHECK(imm == 1 || imm == -1);
3800
3801 if (instruction->IsRem()) {
3802 __ xorl(out_register, out_register);
3803 } else {
3804 __ movl(out_register, input_register);
3805 if (imm == -1) {
3806 __ negl(out_register);
3807 }
3808 }
3809}
3810
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303811void InstructionCodeGeneratorX86::RemByPowerOfTwo(HRem* instruction) {
3812 LocationSummary* locations = instruction->GetLocations();
3813 Location second = locations->InAt(1);
3814
3815 Register out = locations->Out().AsRegister<Register>();
3816 Register numerator = locations->InAt(0).AsRegister<Register>();
3817
3818 int32_t imm = Int64FromConstant(second.GetConstant());
3819 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3820 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3821
3822 Register tmp = locations->GetTemp(0).AsRegister<Register>();
3823 NearLabel done;
3824 __ movl(out, numerator);
3825 __ andl(out, Immediate(abs_imm-1));
3826 __ j(Condition::kZero, &done);
3827 __ leal(tmp, Address(out, static_cast<int32_t>(~(abs_imm-1))));
3828 __ testl(numerator, numerator);
3829 __ cmovl(Condition::kLess, out, tmp);
3830 __ Bind(&done);
3831}
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003832
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003833void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003834 LocationSummary* locations = instruction->GetLocations();
3835
3836 Register out_register = locations->Out().AsRegister<Register>();
3837 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003838 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003839 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3840 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003841
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003842 Register num = locations->GetTemp(0).AsRegister<Register>();
3843
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003844 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003845 __ testl(input_register, input_register);
3846 __ cmovl(kGreaterEqual, num, input_register);
3847 int shift = CTZ(imm);
3848 __ sarl(num, Immediate(shift));
3849
3850 if (imm < 0) {
3851 __ negl(num);
3852 }
3853
3854 __ movl(out_register, num);
3855}
3856
3857void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3858 DCHECK(instruction->IsDiv() || instruction->IsRem());
3859
3860 LocationSummary* locations = instruction->GetLocations();
3861 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3862
3863 Register eax = locations->InAt(0).AsRegister<Register>();
3864 Register out = locations->Out().AsRegister<Register>();
3865 Register num;
3866 Register edx;
3867
3868 if (instruction->IsDiv()) {
3869 edx = locations->GetTemp(0).AsRegister<Register>();
3870 num = locations->GetTemp(1).AsRegister<Register>();
3871 } else {
3872 edx = locations->Out().AsRegister<Register>();
3873 num = locations->GetTemp(0).AsRegister<Register>();
3874 }
3875
3876 DCHECK_EQ(EAX, eax);
3877 DCHECK_EQ(EDX, edx);
3878 if (instruction->IsDiv()) {
3879 DCHECK_EQ(EAX, out);
3880 } else {
3881 DCHECK_EQ(EDX, out);
3882 }
3883
3884 int64_t magic;
3885 int shift;
Andreas Gampe3db70682018-12-26 15:12:03 -08003886 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003887
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003888 // Save the numerator.
3889 __ movl(num, eax);
3890
3891 // EAX = magic
3892 __ movl(eax, Immediate(magic));
3893
3894 // EDX:EAX = magic * numerator
3895 __ imull(num);
3896
3897 if (imm > 0 && magic < 0) {
3898 // EDX += num
3899 __ addl(edx, num);
3900 } else if (imm < 0 && magic > 0) {
3901 __ subl(edx, num);
3902 }
3903
3904 // Shift if needed.
3905 if (shift != 0) {
3906 __ sarl(edx, Immediate(shift));
3907 }
3908
3909 // EDX += 1 if EDX < 0
3910 __ movl(eax, edx);
3911 __ shrl(edx, Immediate(31));
3912 __ addl(edx, eax);
3913
3914 if (instruction->IsRem()) {
3915 __ movl(eax, num);
3916 __ imull(edx, Immediate(imm));
3917 __ subl(eax, edx);
3918 __ movl(edx, eax);
3919 } else {
3920 __ movl(eax, edx);
3921 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003922}
3923
Calin Juravlebacfec32014-11-14 15:54:36 +00003924void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3925 DCHECK(instruction->IsDiv() || instruction->IsRem());
3926
3927 LocationSummary* locations = instruction->GetLocations();
3928 Location out = locations->Out();
3929 Location first = locations->InAt(0);
3930 Location second = locations->InAt(1);
3931 bool is_div = instruction->IsDiv();
3932
3933 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003934 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003935 DCHECK_EQ(EAX, first.AsRegister<Register>());
3936 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003937
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003938 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003939 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003940
3941 if (imm == 0) {
3942 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3943 } else if (imm == 1 || imm == -1) {
3944 DivRemOneOrMinusOne(instruction);
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303945 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
3946 if (is_div) {
3947 DivByPowerOfTwo(instruction->AsDiv());
3948 } else {
3949 RemByPowerOfTwo(instruction->AsRem());
3950 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003951 } else {
3952 DCHECK(imm <= -2 || imm >= 2);
3953 GenerateDivRemWithAnyConstant(instruction);
3954 }
3955 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003956 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00003957 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003958 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003959
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003960 Register second_reg = second.AsRegister<Register>();
3961 // 0x80000000/-1 triggers an arithmetic exception!
3962 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3963 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003964
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003965 __ cmpl(second_reg, Immediate(-1));
3966 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003967
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003968 // edx:eax <- sign-extended of eax
3969 __ cdq();
3970 // eax = quotient, edx = remainder
3971 __ idivl(second_reg);
3972 __ Bind(slow_path->GetExitLabel());
3973 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003974 break;
3975 }
3976
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003977 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003978 InvokeRuntimeCallingConvention calling_convention;
3979 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3980 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3981 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3982 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3983 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3984 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3985
3986 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003987 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003988 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003989 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003990 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003991 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003992 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003993 break;
3994 }
3995
3996 default:
3997 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3998 }
3999}
4000
Calin Juravle7c4954d2014-10-28 16:57:40 +00004001void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004002 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004003 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004004 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01004005 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004006
Calin Juravle7c4954d2014-10-28 16:57:40 +00004007 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004008 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00004009 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004010 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00004011 locations->SetOut(Location::SameAsFirstInput());
4012 // Intel uses edx:eax as the dividend.
4013 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004014 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
4015 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
4016 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01004017 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004018 locations->AddTemp(Location::RequiresRegister());
4019 }
Calin Juravled0d48522014-11-04 16:40:20 +00004020 break;
4021 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004022 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004023 InvokeRuntimeCallingConvention calling_convention;
4024 locations->SetInAt(0, Location::RegisterPairLocation(
4025 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4026 locations->SetInAt(1, Location::RegisterPairLocation(
4027 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4028 // Runtime helper puts the result in EAX, EDX.
4029 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00004030 break;
4031 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004032 case DataType::Type::kFloat32:
4033 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00004034 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00004035 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
4036 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00004037 } else if (div->InputAt(1)->IsConstant()) {
4038 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00004039 } else {
4040 locations->SetInAt(1, Location::Any());
4041 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004042 locations->SetOut(Location::SameAsFirstInput());
4043 break;
4044 }
4045
4046 default:
4047 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4048 }
4049}
4050
4051void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
4052 LocationSummary* locations = div->GetLocations();
4053 Location first = locations->InAt(0);
4054 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004055
4056 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004057 case DataType::Type::kInt32:
4058 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004059 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00004060 break;
4061 }
4062
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004063 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04004064 if (second.IsFpuRegister()) {
4065 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
4066 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
4067 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00004068 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04004069 __ divss(first.AsFpuRegister<XmmRegister>(),
4070 codegen_->LiteralFloatAddress(
4071 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004072 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04004073 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
4074 } else {
4075 DCHECK(second.IsStackSlot());
4076 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
4077 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004078 break;
4079 }
4080
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004081 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04004082 if (second.IsFpuRegister()) {
4083 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
4084 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
4085 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00004086 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04004087 __ divsd(first.AsFpuRegister<XmmRegister>(),
4088 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004089 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
4090 const_area->GetBaseMethodAddress(),
4091 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04004092 } else {
4093 DCHECK(second.IsDoubleStackSlot());
4094 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
4095 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004096 break;
4097 }
4098
4099 default:
4100 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4101 }
4102}
4103
Calin Juravlebacfec32014-11-14 15:54:36 +00004104void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004105 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004106
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004107 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004108 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004109 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01004110 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00004111
Calin Juravled2ec87d2014-12-08 14:24:46 +00004112 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004113 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004114 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004115 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00004116 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004117 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
4118 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
4119 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01004120 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01004121 locations->AddTemp(Location::RequiresRegister());
4122 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004123 break;
4124 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004125 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004126 InvokeRuntimeCallingConvention calling_convention;
4127 locations->SetInAt(0, Location::RegisterPairLocation(
4128 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4129 locations->SetInAt(1, Location::RegisterPairLocation(
4130 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4131 // Runtime helper puts the result in EAX, EDX.
4132 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
4133 break;
4134 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004135 case DataType::Type::kFloat64:
4136 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05004137 locations->SetInAt(0, Location::Any());
4138 locations->SetInAt(1, Location::Any());
4139 locations->SetOut(Location::RequiresFpuRegister());
4140 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00004141 break;
4142 }
4143
4144 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004145 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004146 }
4147}
4148
4149void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004150 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00004151 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004152 case DataType::Type::kInt32:
4153 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004154 GenerateDivRemIntegral(rem);
4155 break;
4156 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004157 case DataType::Type::kFloat32:
4158 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05004159 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00004160 break;
4161 }
4162 default:
4163 LOG(FATAL) << "Unexpected rem type " << type;
4164 }
4165}
4166
Aart Bik1f8d51b2018-02-15 10:42:37 -08004167static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
4168 LocationSummary* locations = new (allocator) LocationSummary(minmax);
4169 switch (minmax->GetResultType()) {
4170 case DataType::Type::kInt32:
4171 locations->SetInAt(0, Location::RequiresRegister());
4172 locations->SetInAt(1, Location::RequiresRegister());
4173 locations->SetOut(Location::SameAsFirstInput());
4174 break;
4175 case DataType::Type::kInt64:
4176 locations->SetInAt(0, Location::RequiresRegister());
4177 locations->SetInAt(1, Location::RequiresRegister());
4178 locations->SetOut(Location::SameAsFirstInput());
4179 // Register to use to perform a long subtract to set cc.
4180 locations->AddTemp(Location::RequiresRegister());
4181 break;
4182 case DataType::Type::kFloat32:
4183 locations->SetInAt(0, Location::RequiresFpuRegister());
4184 locations->SetInAt(1, Location::RequiresFpuRegister());
4185 locations->SetOut(Location::SameAsFirstInput());
4186 locations->AddTemp(Location::RequiresRegister());
4187 break;
4188 case DataType::Type::kFloat64:
4189 locations->SetInAt(0, Location::RequiresFpuRegister());
4190 locations->SetInAt(1, Location::RequiresFpuRegister());
4191 locations->SetOut(Location::SameAsFirstInput());
4192 break;
4193 default:
4194 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
4195 }
4196}
4197
Aart Bik351df3e2018-03-07 11:54:57 -08004198void InstructionCodeGeneratorX86::GenerateMinMaxInt(LocationSummary* locations,
4199 bool is_min,
4200 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08004201 Location op1_loc = locations->InAt(0);
4202 Location op2_loc = locations->InAt(1);
4203
4204 // Shortcut for same input locations.
4205 if (op1_loc.Equals(op2_loc)) {
4206 // Can return immediately, as op1_loc == out_loc.
4207 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
4208 // a copy here.
4209 DCHECK(locations->Out().Equals(op1_loc));
4210 return;
4211 }
4212
4213 if (type == DataType::Type::kInt64) {
4214 // Need to perform a subtract to get the sign right.
4215 // op1 is already in the same location as the output.
4216 Location output = locations->Out();
4217 Register output_lo = output.AsRegisterPairLow<Register>();
4218 Register output_hi = output.AsRegisterPairHigh<Register>();
4219
4220 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
4221 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
4222
4223 // The comparison is performed by subtracting the second operand from
4224 // the first operand and then setting the status flags in the same
4225 // manner as the SUB instruction."
4226 __ cmpl(output_lo, op2_lo);
4227
4228 // Now use a temp and the borrow to finish the subtraction of op2_hi.
4229 Register temp = locations->GetTemp(0).AsRegister<Register>();
4230 __ movl(temp, output_hi);
4231 __ sbbl(temp, op2_hi);
4232
4233 // Now the condition code is correct.
4234 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
4235 __ cmovl(cond, output_lo, op2_lo);
4236 __ cmovl(cond, output_hi, op2_hi);
4237 } else {
4238 DCHECK_EQ(type, DataType::Type::kInt32);
4239 Register out = locations->Out().AsRegister<Register>();
4240 Register op2 = op2_loc.AsRegister<Register>();
4241
4242 // (out := op1)
4243 // out <=? op2
4244 // if out is min jmp done
4245 // out := op2
4246 // done:
4247
4248 __ cmpl(out, op2);
4249 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
4250 __ cmovl(cond, out, op2);
4251 }
4252}
4253
4254void InstructionCodeGeneratorX86::GenerateMinMaxFP(LocationSummary* locations,
4255 bool is_min,
4256 DataType::Type type) {
4257 Location op1_loc = locations->InAt(0);
4258 Location op2_loc = locations->InAt(1);
4259 Location out_loc = locations->Out();
4260 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
4261
4262 // Shortcut for same input locations.
4263 if (op1_loc.Equals(op2_loc)) {
4264 DCHECK(out_loc.Equals(op1_loc));
4265 return;
4266 }
4267
4268 // (out := op1)
4269 // out <=? op2
4270 // if Nan jmp Nan_label
4271 // if out is min jmp done
4272 // if op2 is min jmp op2_label
4273 // handle -0/+0
4274 // jmp done
4275 // Nan_label:
4276 // out := NaN
4277 // op2_label:
4278 // out := op2
4279 // done:
4280 //
4281 // This removes one jmp, but needs to copy one input (op1) to out.
4282 //
4283 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
4284
4285 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
4286
4287 NearLabel nan, done, op2_label;
4288 if (type == DataType::Type::kFloat64) {
4289 __ ucomisd(out, op2);
4290 } else {
4291 DCHECK_EQ(type, DataType::Type::kFloat32);
4292 __ ucomiss(out, op2);
4293 }
4294
4295 __ j(Condition::kParityEven, &nan);
4296
4297 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
4298 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
4299
4300 // Handle 0.0/-0.0.
4301 if (is_min) {
4302 if (type == DataType::Type::kFloat64) {
4303 __ orpd(out, op2);
4304 } else {
4305 __ orps(out, op2);
4306 }
4307 } else {
4308 if (type == DataType::Type::kFloat64) {
4309 __ andpd(out, op2);
4310 } else {
4311 __ andps(out, op2);
4312 }
4313 }
4314 __ jmp(&done);
4315
4316 // NaN handling.
4317 __ Bind(&nan);
4318 if (type == DataType::Type::kFloat64) {
4319 // TODO: Use a constant from the constant table (requires extra input).
4320 __ LoadLongConstant(out, kDoubleNaN);
4321 } else {
4322 Register constant = locations->GetTemp(0).AsRegister<Register>();
4323 __ movl(constant, Immediate(kFloatNaN));
4324 __ movd(out, constant);
4325 }
4326 __ jmp(&done);
4327
4328 // out := op2;
4329 __ Bind(&op2_label);
4330 if (type == DataType::Type::kFloat64) {
4331 __ movsd(out, op2);
4332 } else {
4333 __ movss(out, op2);
4334 }
4335
4336 // Done.
4337 __ Bind(&done);
4338}
4339
Aart Bik351df3e2018-03-07 11:54:57 -08004340void InstructionCodeGeneratorX86::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4341 DataType::Type type = minmax->GetResultType();
4342 switch (type) {
4343 case DataType::Type::kInt32:
4344 case DataType::Type::kInt64:
4345 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
4346 break;
4347 case DataType::Type::kFloat32:
4348 case DataType::Type::kFloat64:
4349 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
4350 break;
4351 default:
4352 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4353 }
4354}
4355
Aart Bik1f8d51b2018-02-15 10:42:37 -08004356void LocationsBuilderX86::VisitMin(HMin* min) {
4357 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4358}
4359
4360void InstructionCodeGeneratorX86::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004361 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004362}
4363
4364void LocationsBuilderX86::VisitMax(HMax* max) {
4365 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4366}
4367
4368void InstructionCodeGeneratorX86::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004369 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004370}
4371
Aart Bik3dad3412018-02-28 12:01:46 -08004372void LocationsBuilderX86::VisitAbs(HAbs* abs) {
4373 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4374 switch (abs->GetResultType()) {
4375 case DataType::Type::kInt32:
4376 locations->SetInAt(0, Location::RegisterLocation(EAX));
4377 locations->SetOut(Location::SameAsFirstInput());
4378 locations->AddTemp(Location::RegisterLocation(EDX));
4379 break;
4380 case DataType::Type::kInt64:
4381 locations->SetInAt(0, Location::RequiresRegister());
4382 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4383 locations->AddTemp(Location::RequiresRegister());
4384 break;
4385 case DataType::Type::kFloat32:
4386 locations->SetInAt(0, Location::RequiresFpuRegister());
4387 locations->SetOut(Location::SameAsFirstInput());
4388 locations->AddTemp(Location::RequiresFpuRegister());
4389 locations->AddTemp(Location::RequiresRegister());
4390 break;
4391 case DataType::Type::kFloat64:
4392 locations->SetInAt(0, Location::RequiresFpuRegister());
4393 locations->SetOut(Location::SameAsFirstInput());
4394 locations->AddTemp(Location::RequiresFpuRegister());
4395 break;
4396 default:
4397 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4398 }
4399}
4400
4401void InstructionCodeGeneratorX86::VisitAbs(HAbs* abs) {
4402 LocationSummary* locations = abs->GetLocations();
4403 switch (abs->GetResultType()) {
4404 case DataType::Type::kInt32: {
4405 Register out = locations->Out().AsRegister<Register>();
4406 DCHECK_EQ(out, EAX);
4407 Register temp = locations->GetTemp(0).AsRegister<Register>();
4408 DCHECK_EQ(temp, EDX);
4409 // Sign extend EAX into EDX.
4410 __ cdq();
4411 // XOR EAX with sign.
4412 __ xorl(EAX, EDX);
4413 // Subtract out sign to correct.
4414 __ subl(EAX, EDX);
4415 // The result is in EAX.
4416 break;
4417 }
4418 case DataType::Type::kInt64: {
4419 Location input = locations->InAt(0);
4420 Register input_lo = input.AsRegisterPairLow<Register>();
4421 Register input_hi = input.AsRegisterPairHigh<Register>();
4422 Location output = locations->Out();
4423 Register output_lo = output.AsRegisterPairLow<Register>();
4424 Register output_hi = output.AsRegisterPairHigh<Register>();
4425 Register temp = locations->GetTemp(0).AsRegister<Register>();
4426 // Compute the sign into the temporary.
4427 __ movl(temp, input_hi);
4428 __ sarl(temp, Immediate(31));
4429 // Store the sign into the output.
4430 __ movl(output_lo, temp);
4431 __ movl(output_hi, temp);
4432 // XOR the input to the output.
4433 __ xorl(output_lo, input_lo);
4434 __ xorl(output_hi, input_hi);
4435 // Subtract the sign.
4436 __ subl(output_lo, temp);
4437 __ sbbl(output_hi, temp);
4438 break;
4439 }
4440 case DataType::Type::kFloat32: {
4441 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4442 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4443 Register constant = locations->GetTemp(1).AsRegister<Register>();
4444 __ movl(constant, Immediate(INT32_C(0x7FFFFFFF)));
4445 __ movd(temp, constant);
4446 __ andps(out, temp);
4447 break;
4448 }
4449 case DataType::Type::kFloat64: {
4450 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4451 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4452 // TODO: Use a constant from the constant table (requires extra input).
4453 __ LoadLongConstant(temp, INT64_C(0x7FFFFFFFFFFFFFFF));
4454 __ andpd(out, temp);
4455 break;
4456 }
4457 default:
4458 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4459 }
4460}
4461
Calin Juravled0d48522014-11-04 16:40:20 +00004462void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004463 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004464 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004465 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004466 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004467 case DataType::Type::kInt8:
4468 case DataType::Type::kUint16:
4469 case DataType::Type::kInt16:
4470 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004471 locations->SetInAt(0, Location::Any());
4472 break;
4473 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004474 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004475 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
4476 if (!instruction->IsConstant()) {
4477 locations->AddTemp(Location::RequiresRegister());
4478 }
4479 break;
4480 }
4481 default:
4482 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4483 }
Calin Juravled0d48522014-11-04 16:40:20 +00004484}
4485
4486void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004487 SlowPathCode* slow_path =
4488 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004489 codegen_->AddSlowPath(slow_path);
4490
4491 LocationSummary* locations = instruction->GetLocations();
4492 Location value = locations->InAt(0);
4493
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004494 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004495 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004496 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004497 case DataType::Type::kInt8:
4498 case DataType::Type::kUint16:
4499 case DataType::Type::kInt16:
4500 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004501 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004502 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004503 __ j(kEqual, slow_path->GetEntryLabel());
4504 } else if (value.IsStackSlot()) {
4505 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
4506 __ j(kEqual, slow_path->GetEntryLabel());
4507 } else {
4508 DCHECK(value.IsConstant()) << value;
4509 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004510 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004511 }
4512 }
4513 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004514 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004515 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004516 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004517 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004518 __ movl(temp, value.AsRegisterPairLow<Register>());
4519 __ orl(temp, value.AsRegisterPairHigh<Register>());
4520 __ j(kEqual, slow_path->GetEntryLabel());
4521 } else {
4522 DCHECK(value.IsConstant()) << value;
4523 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4524 __ jmp(slow_path->GetEntryLabel());
4525 }
4526 }
4527 break;
4528 }
4529 default:
4530 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004531 }
Calin Juravled0d48522014-11-04 16:40:20 +00004532}
4533
Calin Juravle9aec02f2014-11-18 23:06:35 +00004534void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
4535 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4536
4537 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004538 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004539
4540 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004541 case DataType::Type::kInt32:
4542 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004543 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00004544 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00004545 // The shift count needs to be in CL or a constant.
4546 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004547 locations->SetOut(Location::SameAsFirstInput());
4548 break;
4549 }
4550 default:
4551 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4552 }
4553}
4554
4555void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
4556 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4557
4558 LocationSummary* locations = op->GetLocations();
4559 Location first = locations->InAt(0);
4560 Location second = locations->InAt(1);
4561 DCHECK(first.Equals(locations->Out()));
4562
4563 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004564 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00004565 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004566 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004567 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004568 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004569 DCHECK_EQ(ECX, second_reg);
4570 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004571 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004572 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004573 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004574 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004575 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004576 }
4577 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004578 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004579 if (shift == 0) {
4580 return;
4581 }
4582 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004583 if (op->IsShl()) {
4584 __ shll(first_reg, imm);
4585 } else if (op->IsShr()) {
4586 __ sarl(first_reg, imm);
4587 } else {
4588 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004589 }
4590 }
4591 break;
4592 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004593 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004594 if (second.IsRegister()) {
4595 Register second_reg = second.AsRegister<Register>();
4596 DCHECK_EQ(ECX, second_reg);
4597 if (op->IsShl()) {
4598 GenerateShlLong(first, second_reg);
4599 } else if (op->IsShr()) {
4600 GenerateShrLong(first, second_reg);
4601 } else {
4602 GenerateUShrLong(first, second_reg);
4603 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004604 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00004605 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004606 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004607 // Nothing to do if the shift is 0, as the input is already the output.
4608 if (shift != 0) {
4609 if (op->IsShl()) {
4610 GenerateShlLong(first, shift);
4611 } else if (op->IsShr()) {
4612 GenerateShrLong(first, shift);
4613 } else {
4614 GenerateUShrLong(first, shift);
4615 }
4616 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004617 }
4618 break;
4619 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004620 default:
4621 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4622 }
4623}
4624
Mark P Mendell73945692015-04-29 14:56:17 +00004625void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
4626 Register low = loc.AsRegisterPairLow<Register>();
4627 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04004628 if (shift == 1) {
4629 // This is just an addition.
4630 __ addl(low, low);
4631 __ adcl(high, high);
4632 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00004633 // Shift by 32 is easy. High gets low, and low gets 0.
4634 codegen_->EmitParallelMoves(
4635 loc.ToLow(),
4636 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004637 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004638 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4639 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004640 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004641 } else if (shift > 32) {
4642 // Low part becomes 0. High part is low part << (shift-32).
4643 __ movl(high, low);
4644 __ shll(high, Immediate(shift - 32));
4645 __ xorl(low, low);
4646 } else {
4647 // Between 1 and 31.
4648 __ shld(high, low, Immediate(shift));
4649 __ shll(low, Immediate(shift));
4650 }
4651}
4652
Calin Juravle9aec02f2014-11-18 23:06:35 +00004653void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004654 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004655 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4656 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4657 __ testl(shifter, Immediate(32));
4658 __ j(kEqual, &done);
4659 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4660 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4661 __ Bind(&done);
4662}
4663
Mark P Mendell73945692015-04-29 14:56:17 +00004664void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4665 Register low = loc.AsRegisterPairLow<Register>();
4666 Register high = loc.AsRegisterPairHigh<Register>();
4667 if (shift == 32) {
4668 // Need to copy the sign.
4669 DCHECK_NE(low, high);
4670 __ movl(low, high);
4671 __ sarl(high, Immediate(31));
4672 } else if (shift > 32) {
4673 DCHECK_NE(low, high);
4674 // High part becomes sign. Low part is shifted by shift - 32.
4675 __ movl(low, high);
4676 __ sarl(high, Immediate(31));
4677 __ sarl(low, Immediate(shift - 32));
4678 } else {
4679 // Between 1 and 31.
4680 __ shrd(low, high, Immediate(shift));
4681 __ sarl(high, Immediate(shift));
4682 }
4683}
4684
Calin Juravle9aec02f2014-11-18 23:06:35 +00004685void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004686 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004687 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4688 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4689 __ testl(shifter, Immediate(32));
4690 __ j(kEqual, &done);
4691 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4692 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4693 __ Bind(&done);
4694}
4695
Mark P Mendell73945692015-04-29 14:56:17 +00004696void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4697 Register low = loc.AsRegisterPairLow<Register>();
4698 Register high = loc.AsRegisterPairHigh<Register>();
4699 if (shift == 32) {
4700 // Shift by 32 is easy. Low gets high, and high gets 0.
4701 codegen_->EmitParallelMoves(
4702 loc.ToHigh(),
4703 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004704 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004705 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4706 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004707 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004708 } else if (shift > 32) {
4709 // Low part is high >> (shift - 32). High part becomes 0.
4710 __ movl(low, high);
4711 __ shrl(low, Immediate(shift - 32));
4712 __ xorl(high, high);
4713 } else {
4714 // Between 1 and 31.
4715 __ shrd(low, high, Immediate(shift));
4716 __ shrl(high, Immediate(shift));
4717 }
4718}
4719
Calin Juravle9aec02f2014-11-18 23:06:35 +00004720void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004721 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004722 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4723 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4724 __ testl(shifter, Immediate(32));
4725 __ j(kEqual, &done);
4726 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4727 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4728 __ Bind(&done);
4729}
4730
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004731void LocationsBuilderX86::VisitRor(HRor* ror) {
4732 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004733 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004734
4735 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004736 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004737 // Add the temporary needed.
4738 locations->AddTemp(Location::RequiresRegister());
4739 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004740 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004741 locations->SetInAt(0, Location::RequiresRegister());
4742 // The shift count needs to be in CL (unless it is a constant).
4743 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4744 locations->SetOut(Location::SameAsFirstInput());
4745 break;
4746 default:
4747 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4748 UNREACHABLE();
4749 }
4750}
4751
4752void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4753 LocationSummary* locations = ror->GetLocations();
4754 Location first = locations->InAt(0);
4755 Location second = locations->InAt(1);
4756
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004757 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004758 Register first_reg = first.AsRegister<Register>();
4759 if (second.IsRegister()) {
4760 Register second_reg = second.AsRegister<Register>();
4761 __ rorl(first_reg, second_reg);
4762 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004763 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004764 __ rorl(first_reg, imm);
4765 }
4766 return;
4767 }
4768
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004769 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004770 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4771 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4772 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4773 if (second.IsRegister()) {
4774 Register second_reg = second.AsRegister<Register>();
4775 DCHECK_EQ(second_reg, ECX);
4776 __ movl(temp_reg, first_reg_hi);
4777 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4778 __ shrd(first_reg_lo, temp_reg, second_reg);
4779 __ movl(temp_reg, first_reg_hi);
4780 __ testl(second_reg, Immediate(32));
4781 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4782 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4783 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004784 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004785 if (shift_amt == 0) {
4786 // Already fine.
4787 return;
4788 }
4789 if (shift_amt == 32) {
4790 // Just swap.
4791 __ movl(temp_reg, first_reg_lo);
4792 __ movl(first_reg_lo, first_reg_hi);
4793 __ movl(first_reg_hi, temp_reg);
4794 return;
4795 }
4796
4797 Immediate imm(shift_amt);
4798 // Save the constents of the low value.
4799 __ movl(temp_reg, first_reg_lo);
4800
4801 // Shift right into low, feeding bits from high.
4802 __ shrd(first_reg_lo, first_reg_hi, imm);
4803
4804 // Shift right into high, feeding bits from the original low.
4805 __ shrd(first_reg_hi, temp_reg, imm);
4806
4807 // Swap if needed.
4808 if (shift_amt > 32) {
4809 __ movl(temp_reg, first_reg_lo);
4810 __ movl(first_reg_lo, first_reg_hi);
4811 __ movl(first_reg_hi, temp_reg);
4812 }
4813 }
4814}
4815
Calin Juravle9aec02f2014-11-18 23:06:35 +00004816void LocationsBuilderX86::VisitShl(HShl* shl) {
4817 HandleShift(shl);
4818}
4819
4820void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4821 HandleShift(shl);
4822}
4823
4824void LocationsBuilderX86::VisitShr(HShr* shr) {
4825 HandleShift(shr);
4826}
4827
4828void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4829 HandleShift(shr);
4830}
4831
4832void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4833 HandleShift(ushr);
4834}
4835
4836void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4837 HandleShift(ushr);
4838}
4839
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004840void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004841 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4842 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004843 locations->SetOut(Location::RegisterLocation(EAX));
Alex Lightd109e302018-06-27 10:25:41 -07004844 InvokeRuntimeCallingConvention calling_convention;
4845 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004846}
4847
4848void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07004849 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
4850 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4851 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004852}
4853
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004854void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004855 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4856 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004857 locations->SetOut(Location::RegisterLocation(EAX));
4858 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004859 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4860 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004861}
4862
4863void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01004864 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
4865 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004866 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004867 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004868 DCHECK(!codegen_->IsLeafMethod());
4869}
4870
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004871void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004872 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004873 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004874 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4875 if (location.IsStackSlot()) {
4876 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4877 } else if (location.IsDoubleStackSlot()) {
4878 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004879 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004880 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004881}
4882
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004883void InstructionCodeGeneratorX86::VisitParameterValue(
4884 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4885}
4886
4887void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4888 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004889 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004890 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4891}
4892
4893void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004894}
4895
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004896void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4897 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004898 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004899 locations->SetInAt(0, Location::RequiresRegister());
4900 locations->SetOut(Location::RequiresRegister());
4901}
4902
4903void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4904 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004905 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004906 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004907 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004908 __ movl(locations->Out().AsRegister<Register>(),
4909 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004910 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004911 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004912 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004913 __ movl(locations->Out().AsRegister<Register>(),
4914 Address(locations->InAt(0).AsRegister<Register>(),
4915 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4916 // temp = temp->GetImtEntryAt(method_offset);
4917 __ movl(locations->Out().AsRegister<Register>(),
4918 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004919 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004920}
4921
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004922void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004923 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004924 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004925 locations->SetInAt(0, Location::RequiresRegister());
4926 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004927}
4928
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004929void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4930 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004931 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004932 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004933 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004934 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004935 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004936 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004937 break;
4938
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004939 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004940 __ notl(out.AsRegisterPairLow<Register>());
4941 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004942 break;
4943
4944 default:
4945 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4946 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004947}
4948
David Brazdil66d126e2015-04-03 16:02:44 +01004949void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4950 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004951 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004952 locations->SetInAt(0, Location::RequiresRegister());
4953 locations->SetOut(Location::SameAsFirstInput());
4954}
4955
4956void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004957 LocationSummary* locations = bool_not->GetLocations();
4958 Location in = locations->InAt(0);
4959 Location out = locations->Out();
4960 DCHECK(in.Equals(out));
4961 __ xorl(out.AsRegister<Register>(), Immediate(1));
4962}
4963
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004964void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004965 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004966 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004967 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004968 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004969 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004970 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004971 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004972 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004973 case DataType::Type::kInt32:
4974 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004975 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004976 locations->SetInAt(1, Location::Any());
4977 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4978 break;
4979 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004980 case DataType::Type::kFloat32:
4981 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004982 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004983 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4984 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4985 } else if (compare->InputAt(1)->IsConstant()) {
4986 locations->SetInAt(1, Location::RequiresFpuRegister());
4987 } else {
4988 locations->SetInAt(1, Location::Any());
4989 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004990 locations->SetOut(Location::RequiresRegister());
4991 break;
4992 }
4993 default:
4994 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4995 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004996}
4997
4998void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004999 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005000 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00005001 Location left = locations->InAt(0);
5002 Location right = locations->InAt(1);
5003
Mark Mendell0c9497d2015-08-21 09:30:05 -04005004 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08005005 Condition less_cond = kLess;
5006
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005007 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005008 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005009 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005010 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005011 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005012 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005013 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01005014 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08005015 break;
5016 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005017 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005018 Register left_low = left.AsRegisterPairLow<Register>();
5019 Register left_high = left.AsRegisterPairHigh<Register>();
5020 int32_t val_low = 0;
5021 int32_t val_high = 0;
5022 bool right_is_const = false;
5023
5024 if (right.IsConstant()) {
5025 DCHECK(right.GetConstant()->IsLongConstant());
5026 right_is_const = true;
5027 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
5028 val_low = Low32Bits(val);
5029 val_high = High32Bits(val);
5030 }
5031
Calin Juravleddb7df22014-11-25 20:56:51 +00005032 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005033 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005034 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005035 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005036 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005037 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08005038 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005039 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005040 __ j(kLess, &less); // Signed compare.
5041 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005042 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005043 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005044 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005045 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005046 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005047 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08005048 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005049 }
Aart Bika19616e2016-02-01 18:57:58 -08005050 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00005051 break;
5052 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005053 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00005054 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00005055 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08005056 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00005057 break;
5058 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005059 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00005060 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00005061 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08005062 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005063 break;
5064 }
5065 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00005066 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005067 }
Aart Bika19616e2016-02-01 18:57:58 -08005068
Calin Juravleddb7df22014-11-25 20:56:51 +00005069 __ movl(out, Immediate(0));
5070 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08005071 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00005072
5073 __ Bind(&greater);
5074 __ movl(out, Immediate(1));
5075 __ jmp(&done);
5076
5077 __ Bind(&less);
5078 __ movl(out, Immediate(-1));
5079
5080 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005081}
5082
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005083void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005084 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005085 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005086 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01005087 locations->SetInAt(i, Location::Any());
5088 }
5089 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005090}
5091
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005092void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005093 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005094}
5095
Roland Levillain7c1559a2015-12-15 10:55:36 +00005096void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00005097 /*
5098 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
5099 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
5100 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
5101 */
5102 switch (kind) {
5103 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00005104 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00005105 break;
5106 }
5107 case MemBarrierKind::kAnyStore:
5108 case MemBarrierKind::kLoadAny:
5109 case MemBarrierKind::kStoreStore: {
5110 // nop
5111 break;
5112 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05005113 case MemBarrierKind::kNTStoreStore:
5114 // Non-Temporal Store/Store needs an explicit fence.
Andreas Gampe3db70682018-12-26 15:12:03 -08005115 MemoryFence(/* non-temporal= */ true);
Mark Mendell7aa04a12016-01-27 22:39:07 -05005116 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005117 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005118}
5119
Vladimir Markodc151b22015-10-15 18:02:30 +01005120HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
5121 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01005122 ArtMethod* method ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005123 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01005124}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005125
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005126Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
5127 Register temp) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00005128 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005129 if (!invoke->GetLocations()->Intrinsified()) {
5130 return location.AsRegister<Register>();
5131 }
5132 // For intrinsics we allow any location, so it may be on the stack.
5133 if (!location.IsRegister()) {
5134 __ movl(temp, Address(ESP, location.GetStackIndex()));
5135 return temp;
5136 }
5137 // For register locations, check if the register was saved. If so, get it from the stack.
5138 // Note: There is a chance that the register was saved but not overwritten, so we could
5139 // save one load. However, since this is just an intrinsic slow path we prefer this
5140 // simple and more robust approach rather that trying to determine if that's the case.
5141 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00005142 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
5143 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
5144 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
5145 __ movl(temp, Address(ESP, stack_offset));
5146 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005147 }
5148 return location.AsRegister<Register>();
5149}
5150
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005151void CodeGeneratorX86::GenerateStaticOrDirectCall(
5152 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00005153 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
5154 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005155 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005156 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005157 uint32_t offset =
5158 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
5159 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00005160 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005161 }
Vladimir Marko58155012015-08-19 12:49:41 +00005162 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Marko86c87522020-05-11 16:55:55 +01005163 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005164 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005165 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005166 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko65979462017-05-19 17:25:12 +01005167 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
5168 temp.AsRegister<Register>());
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005169 __ leal(temp.AsRegister<Register>(),
5170 Address(base_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005171 RecordBootImageMethodPatch(invoke);
Vladimir Marko65979462017-05-19 17:25:12 +01005172 break;
5173 }
Vladimir Markob066d432018-01-03 13:14:37 +00005174 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
5175 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
5176 temp.AsRegister<Register>());
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005177 __ movl(temp.AsRegister<Register>(), Address(base_reg, kPlaceholder32BitOffset));
Vladimir Markob066d432018-01-03 13:14:37 +00005178 RecordBootImageRelRoPatch(
5179 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005180 GetBootImageOffset(invoke));
Vladimir Markob066d432018-01-03 13:14:37 +00005181 break;
5182 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005183 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005184 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
5185 temp.AsRegister<Register>());
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005186 __ movl(temp.AsRegister<Register>(), Address(base_reg, kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005187 RecordMethodBssEntryPatch(invoke);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005188 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005189 break;
5190 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005191 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
5192 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
5193 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005194 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
5195 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5196 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01005197 }
Vladimir Marko58155012015-08-19 12:49:41 +00005198 }
5199
5200 switch (invoke->GetCodePtrLocation()) {
5201 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
5202 __ call(GetFrameEntryLabel());
Vladimir Marko86c87522020-05-11 16:55:55 +01005203 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko58155012015-08-19 12:49:41 +00005204 break;
Vladimir Marko86c87522020-05-11 16:55:55 +01005205 case HInvokeStaticOrDirect::CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01005206 size_t out_frame_size =
5207 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorX86,
5208 kNativeStackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01005209 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Marko86c87522020-05-11 16:55:55 +01005210 // (callee_method + offset_of_jni_entry_point)()
5211 __ call(Address(callee_method.AsRegister<Register>(),
5212 ArtMethod::EntryPointFromJniOffset(kX86PointerSize).Int32Value()));
5213 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5214 if (out_frame_size == 0u && DataType::IsFloatingPointType(invoke->GetType())) {
5215 // Create space for conversion.
5216 out_frame_size = 8u;
Vladimir Markodec78172020-06-19 15:31:23 +01005217 IncreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01005218 }
5219 // Zero-/sign-extend or move the result when needed due to native and managed ABI mismatch.
5220 switch (invoke->GetType()) {
5221 case DataType::Type::kBool:
5222 __ movzxb(EAX, AL);
5223 break;
5224 case DataType::Type::kInt8:
5225 __ movsxb(EAX, AL);
5226 break;
5227 case DataType::Type::kUint16:
5228 __ movzxw(EAX, EAX);
5229 break;
5230 case DataType::Type::kInt16:
5231 __ movsxw(EAX, EAX);
5232 break;
5233 case DataType::Type::kFloat32:
5234 __ fstps(Address(ESP, 0));
5235 __ movss(XMM0, Address(ESP, 0));
5236 break;
5237 case DataType::Type::kFloat64:
5238 __ fstpl(Address(ESP, 0));
5239 __ movsd(XMM0, Address(ESP, 0));
5240 break;
5241 case DataType::Type::kInt32:
5242 case DataType::Type::kInt64:
5243 case DataType::Type::kVoid:
5244 break;
5245 default:
5246 DCHECK(false) << invoke->GetType();
5247 break;
5248 }
5249 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01005250 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01005251 }
5252 break;
5253 }
Vladimir Marko58155012015-08-19 12:49:41 +00005254 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5255 // (callee_method + offset_of_quick_compiled_code)()
5256 __ call(Address(callee_method.AsRegister<Register>(),
5257 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005258 kX86PointerSize).Int32Value()));
Vladimir Marko86c87522020-05-11 16:55:55 +01005259 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko58155012015-08-19 12:49:41 +00005260 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04005261 }
5262
5263 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04005264}
5265
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005266void CodeGeneratorX86::GenerateVirtualCall(
5267 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005268 Register temp = temp_in.AsRegister<Register>();
5269 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5270 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005271
5272 // Use the calling convention instead of the location of the receiver, as
5273 // intrinsics may have put the receiver in a different register. In the intrinsics
5274 // slow path, the arguments have been moved to the right place, so here we are
5275 // guaranteed that the receiver is the first register of the calling convention.
5276 InvokeDexCallingConvention calling_convention;
5277 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005278 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005279 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005280 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005281 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005282 // Instead of simply (possibly) unpoisoning `temp` here, we should
5283 // emit a read barrier for the previous class reference load.
5284 // However this is not required in practice, as this is an
5285 // intermediate/temporary reference and because the current
5286 // concurrent copying collector keeps the from-space memory
5287 // intact/accessible until the end of the marking phase (the
5288 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005289 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00005290
5291 MaybeGenerateInlineCacheCheck(invoke, temp);
5292
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005293 // temp = temp->GetMethodAt(method_offset);
5294 __ movl(temp, Address(temp, method_offset));
5295 // call temp->GetEntryPoint();
5296 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07005297 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005298 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005299}
5300
Vladimir Marko6fd16062018-06-26 11:02:04 +01005301void CodeGeneratorX86::RecordBootImageIntrinsicPatch(HX86ComputeBaseMethodAddress* method_address,
5302 uint32_t intrinsic_data) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005303 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005304 method_address, /* target_dex_file= */ nullptr, intrinsic_data);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005305 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005306}
5307
Vladimir Markob066d432018-01-03 13:14:37 +00005308void CodeGeneratorX86::RecordBootImageRelRoPatch(HX86ComputeBaseMethodAddress* method_address,
5309 uint32_t boot_image_offset) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005310 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005311 method_address, /* target_dex_file= */ nullptr, boot_image_offset);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005312 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Markob066d432018-01-03 13:14:37 +00005313}
5314
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005315void CodeGeneratorX86::RecordBootImageMethodPatch(HInvokeStaticOrDirect* invoke) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005316 HX86ComputeBaseMethodAddress* method_address =
Vladimir Marko65979462017-05-19 17:25:12 +01005317 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005318 boot_image_method_patches_.emplace_back(
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005319 method_address,
5320 invoke->GetResolvedMethodReference().dex_file,
5321 invoke->GetResolvedMethodReference().index);
Vladimir Marko65979462017-05-19 17:25:12 +01005322 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005323}
5324
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005325void CodeGeneratorX86::RecordMethodBssEntryPatch(HInvokeStaticOrDirect* invoke) {
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005326 DCHECK(IsSameDexFile(GetGraph()->GetDexFile(), *invoke->GetMethodReference().dex_file));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005327 HX86ComputeBaseMethodAddress* method_address =
5328 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005329 // Add the patch entry and bind its label at the end of the instruction.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005330 method_bss_entry_patches_.emplace_back(
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005331 method_address,
5332 invoke->GetMethodReference().dex_file,
5333 invoke->GetMethodReference().index);
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005334 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005335}
5336
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005337void CodeGeneratorX86::RecordBootImageTypePatch(HLoadClass* load_class) {
5338 HX86ComputeBaseMethodAddress* method_address =
5339 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5340 boot_image_type_patches_.emplace_back(
5341 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005342 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005343}
5344
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005345Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005346 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005347 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5348 type_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005349 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005350 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005351}
5352
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005353void CodeGeneratorX86::RecordBootImageStringPatch(HLoadString* load_string) {
5354 HX86ComputeBaseMethodAddress* method_address =
5355 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
5356 boot_image_string_patches_.emplace_back(
5357 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
5358 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01005359}
5360
Vladimir Markoaad75c62016-10-03 08:46:48 +00005361Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005362 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005363 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005364 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005365 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005366 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005367}
5368
Vladimir Markoeebb8212018-06-05 14:57:24 +01005369void CodeGeneratorX86::LoadBootImageAddress(Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01005370 uint32_t boot_image_reference,
Vladimir Markoeebb8212018-06-05 14:57:24 +01005371 HInvokeStaticOrDirect* invoke) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005372 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005373 HX86ComputeBaseMethodAddress* method_address =
5374 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5375 DCHECK(method_address != nullptr);
5376 Register method_address_reg =
5377 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005378 __ leal(reg, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005379 RecordBootImageIntrinsicPatch(method_address, boot_image_reference);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01005380 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01005381 HX86ComputeBaseMethodAddress* method_address =
5382 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5383 DCHECK(method_address != nullptr);
5384 Register method_address_reg =
5385 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005386 __ movl(reg, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005387 RecordBootImageRelRoPatch(method_address, boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01005388 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01005389 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01005390 gc::Heap* heap = Runtime::Current()->GetHeap();
5391 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01005392 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01005393 __ movl(reg, Immediate(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address))));
5394 }
5395}
5396
Vladimir Marko6fd16062018-06-26 11:02:04 +01005397void CodeGeneratorX86::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
5398 uint32_t boot_image_offset) {
5399 DCHECK(invoke->IsStatic());
5400 InvokeRuntimeCallingConvention calling_convention;
5401 Register argument = calling_convention.GetRegisterAt(0);
5402 if (GetCompilerOptions().IsBootImage()) {
5403 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
5404 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
Vladimir Marko6fd16062018-06-26 11:02:04 +01005405 HX86ComputeBaseMethodAddress* method_address =
5406 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5407 DCHECK(method_address != nullptr);
5408 Register method_address_reg =
5409 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005410 __ leal(argument, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Nicolas Geoffraye6c0f2a2020-09-07 08:30:52 +01005411 MethodReference target_method = invoke->GetResolvedMethodReference();
Vladimir Marko6fd16062018-06-26 11:02:04 +01005412 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
5413 boot_image_type_patches_.emplace_back(method_address, target_method.dex_file, type_idx.index_);
5414 __ Bind(&boot_image_type_patches_.back().label);
5415 } else {
5416 LoadBootImageAddress(argument, boot_image_offset, invoke);
5417 }
5418 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
5419 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
5420}
5421
Vladimir Markoaad75c62016-10-03 08:46:48 +00005422// The label points to the end of the "movl" or another instruction but the literal offset
5423// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
5424constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
5425
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005426template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005427inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005428 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005429 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005430 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005431 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005432 linker_patches->push_back(Factory(literal_offset,
5433 info.target_dex_file,
5434 GetMethodAddressOffset(info.method_address),
5435 info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005436 }
5437}
5438
Vladimir Marko6fd16062018-06-26 11:02:04 +01005439template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
5440linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
5441 const DexFile* target_dex_file,
5442 uint32_t pc_insn_offset,
5443 uint32_t boot_image_offset) {
5444 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5445 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005446}
5447
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005448void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005449 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005450 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01005451 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005452 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00005453 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01005454 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005455 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01005456 string_bss_entry_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01005457 boot_image_other_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005458 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01005459 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005460 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
5461 boot_image_method_patches_, linker_patches);
5462 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
5463 boot_image_type_patches_, linker_patches);
5464 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005465 boot_image_string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005466 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005467 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005468 DCHECK(boot_image_type_patches_.empty());
5469 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01005470 }
5471 if (GetCompilerOptions().IsBootImage()) {
5472 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5473 boot_image_other_patches_, linker_patches);
5474 } else {
5475 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
5476 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005477 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005478 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5479 method_bss_entry_patches_, linker_patches);
5480 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5481 type_bss_entry_patches_, linker_patches);
5482 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5483 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005484 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005485}
5486
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005487void CodeGeneratorX86::MarkGCCard(Register temp,
5488 Register card,
5489 Register object,
5490 Register value,
5491 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005492 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005493 if (value_can_be_null) {
5494 __ testl(value, value);
5495 __ j(kEqual, &is_null);
5496 }
Roland Levillainc73f0522018-08-14 15:16:50 +01005497 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07005498 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01005499 // Calculate the offset (in the card table) of the card corresponding to
5500 // `object`.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005501 __ movl(temp, object);
5502 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01005503 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
5504 // `object`'s card.
5505 //
5506 // Register `card` contains the address of the card table. Note that the card
5507 // table's base is biased during its creation so that it always starts at an
5508 // address whose least-significant byte is equal to `kCardDirty` (see
5509 // art::gc::accounting::CardTable::Create). Therefore the MOVB instruction
5510 // below writes the `kCardDirty` (byte) value into the `object`'s card
5511 // (located at `card + object >> kCardShift`).
5512 //
5513 // This dual use of the value in register `card` (1. to calculate the location
5514 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
5515 // (no need to explicitly load `kCardDirty` as an immediate value).
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00005516 __ movb(Address(temp, card, TIMES_1, 0),
5517 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005518 if (value_can_be_null) {
5519 __ Bind(&is_null);
5520 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005521}
5522
Calin Juravle52c48962014-12-16 17:02:57 +00005523void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5524 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005525
5526 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005527 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005528 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005529 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5530 kEmitCompilerReadBarrier
5531 ? LocationSummary::kCallOnSlowPath
5532 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005533 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005534 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005535 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005536 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005537
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005538 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005539 locations->SetOut(Location::RequiresFpuRegister());
5540 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005541 // The output overlaps in case of long: we don't want the low move
5542 // to overwrite the object's location. Likewise, in the case of
5543 // an object field get with read barriers enabled, we do not want
5544 // the move to overwrite the object's location, as we need it to emit
5545 // the read barrier.
5546 locations->SetOut(
5547 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005548 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005549 Location::kOutputOverlap :
5550 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005551 }
Calin Juravle52c48962014-12-16 17:02:57 +00005552
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005553 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00005554 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005555 // So we use an XMM register as a temp to achieve atomicity (first
5556 // load the temp into the XMM and then copy the XMM into the
5557 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00005558 locations->AddTemp(Location::RequiresFpuRegister());
5559 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005560}
5561
Calin Juravle52c48962014-12-16 17:02:57 +00005562void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
5563 const FieldInfo& field_info) {
5564 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005565
Calin Juravle52c48962014-12-16 17:02:57 +00005566 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005567 Location base_loc = locations->InAt(0);
5568 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005569 Location out = locations->Out();
5570 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01005571 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5572 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00005573 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5574
Vladimir Marko61b92282017-10-11 13:23:17 +01005575 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005576 case DataType::Type::kBool:
5577 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005578 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005579 break;
5580 }
5581
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005582 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005583 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005584 break;
5585 }
5586
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005587 case DataType::Type::kUint16: {
5588 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005589 break;
5590 }
5591
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005592 case DataType::Type::kInt16: {
5593 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005594 break;
5595 }
5596
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005597 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00005598 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005599 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005600
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005601 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005602 // /* HeapReference<Object> */ out = *(base + offset)
5603 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005604 // Note that a potential implicit null check is handled in this
5605 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
5606 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005607 instruction, out, base, offset, /* needs_null_check= */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005608 if (is_volatile) {
5609 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5610 }
5611 } else {
5612 __ movl(out.AsRegister<Register>(), Address(base, offset));
5613 codegen_->MaybeRecordImplicitNullCheck(instruction);
5614 if (is_volatile) {
5615 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5616 }
5617 // If read barriers are enabled, emit read barriers other than
5618 // Baker's using a slow path (and also unpoison the loaded
5619 // reference, if heap poisoning is enabled).
5620 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5621 }
5622 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005623 }
5624
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005625 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005626 if (is_volatile) {
5627 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5628 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005629 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005630 __ movd(out.AsRegisterPairLow<Register>(), temp);
5631 __ psrlq(temp, Immediate(32));
5632 __ movd(out.AsRegisterPairHigh<Register>(), temp);
5633 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005634 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00005635 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005636 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005637 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
5638 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005639 break;
5640 }
5641
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005642 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00005643 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005644 break;
5645 }
5646
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005647 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005648 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005649 break;
5650 }
5651
Aart Bik66c158e2018-01-31 12:55:04 -08005652 case DataType::Type::kUint32:
5653 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005654 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005655 LOG(FATAL) << "Unreachable type " << load_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005656 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005657 }
Calin Juravle52c48962014-12-16 17:02:57 +00005658
Vladimir Marko61b92282017-10-11 13:23:17 +01005659 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005660 // Potential implicit null checks, in the case of reference or
5661 // long fields, are handled in the previous switch statement.
5662 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005663 codegen_->MaybeRecordImplicitNullCheck(instruction);
5664 }
5665
Calin Juravle52c48962014-12-16 17:02:57 +00005666 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005667 if (load_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005668 // Memory barriers, in the case of references, are also handled
5669 // in the previous switch statement.
5670 } else {
5671 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5672 }
Roland Levillain4d027112015-07-01 15:41:14 +01005673 }
Calin Juravle52c48962014-12-16 17:02:57 +00005674}
5675
5676void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5677 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5678
5679 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005680 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00005681 locations->SetInAt(0, Location::RequiresRegister());
5682 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005683 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005684 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00005685
5686 // The register allocator does not support multiple
5687 // inputs that die at entry with one in a specific register.
5688 if (is_byte_type) {
5689 // Ensure the value is in a byte register.
5690 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005691 } else if (DataType::IsFloatingPointType(field_type)) {
5692 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05005693 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5694 locations->SetInAt(1, Location::RequiresFpuRegister());
5695 } else {
5696 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
5697 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005698 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05005699 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00005700 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005701
Calin Juravle52c48962014-12-16 17:02:57 +00005702 // 64bits value can be atomically written to an address with movsd and an XMM register.
5703 // We need two XMM registers because there's no easier way to (bit) copy a register pair
5704 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
5705 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
5706 // isolated cases when we need this it isn't worth adding the extra complexity.
5707 locations->AddTemp(Location::RequiresFpuRegister());
5708 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005709 } else {
5710 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5711
5712 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5713 // Temporary registers for the write barrier.
5714 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
5715 // Ensure the card is in a byte register.
5716 locations->AddTemp(Location::RegisterLocation(ECX));
5717 }
Calin Juravle52c48962014-12-16 17:02:57 +00005718 }
5719}
5720
5721void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005722 const FieldInfo& field_info,
5723 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005724 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5725
5726 LocationSummary* locations = instruction->GetLocations();
5727 Register base = locations->InAt(0).AsRegister<Register>();
5728 Location value = locations->InAt(1);
5729 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005730 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00005731 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005732 bool needs_write_barrier =
5733 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005734
5735 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005736 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005737 }
5738
Mark Mendell81489372015-11-04 11:30:41 -05005739 bool maybe_record_implicit_null_check_done = false;
5740
Calin Juravle52c48962014-12-16 17:02:57 +00005741 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005742 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005743 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005744 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005745 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
5746 break;
5747 }
5748
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005749 case DataType::Type::kUint16:
5750 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05005751 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005752 __ movw(Address(base, offset),
5753 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05005754 } else {
5755 __ movw(Address(base, offset), value.AsRegister<Register>());
5756 }
Calin Juravle52c48962014-12-16 17:02:57 +00005757 break;
5758 }
5759
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005760 case DataType::Type::kInt32:
5761 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01005762 if (kPoisonHeapReferences && needs_write_barrier) {
5763 // Note that in the case where `value` is a null reference,
5764 // we do not enter this block, as the reference does not
5765 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005766 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01005767 Register temp = locations->GetTemp(0).AsRegister<Register>();
5768 __ movl(temp, value.AsRegister<Register>());
5769 __ PoisonHeapReference(temp);
5770 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05005771 } else if (value.IsConstant()) {
5772 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5773 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005774 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005775 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01005776 __ movl(Address(base, offset), value.AsRegister<Register>());
5777 }
Calin Juravle52c48962014-12-16 17:02:57 +00005778 break;
5779 }
5780
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005781 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005782 if (is_volatile) {
5783 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5784 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5785 __ movd(temp1, value.AsRegisterPairLow<Register>());
5786 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5787 __ punpckldq(temp1, temp2);
5788 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005789 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005790 } else if (value.IsConstant()) {
5791 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5792 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5793 codegen_->MaybeRecordImplicitNullCheck(instruction);
5794 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005795 } else {
5796 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005797 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005798 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5799 }
Mark Mendell81489372015-11-04 11:30:41 -05005800 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005801 break;
5802 }
5803
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005804 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05005805 if (value.IsConstant()) {
5806 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5807 __ movl(Address(base, offset), Immediate(v));
5808 } else {
5809 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5810 }
Calin Juravle52c48962014-12-16 17:02:57 +00005811 break;
5812 }
5813
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005814 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005815 if (value.IsConstant()) {
Andra Danciuc992e422020-09-16 08:12:02 +00005816 DCHECK(!is_volatile);
Mark Mendell81489372015-11-04 11:30:41 -05005817 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5818 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5819 codegen_->MaybeRecordImplicitNullCheck(instruction);
5820 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5821 maybe_record_implicit_null_check_done = true;
5822 } else {
5823 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5824 }
Calin Juravle52c48962014-12-16 17:02:57 +00005825 break;
5826 }
5827
Aart Bik66c158e2018-01-31 12:55:04 -08005828 case DataType::Type::kUint32:
5829 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005830 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005831 LOG(FATAL) << "Unreachable type " << field_type;
5832 UNREACHABLE();
5833 }
5834
Mark Mendell81489372015-11-04 11:30:41 -05005835 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005836 codegen_->MaybeRecordImplicitNullCheck(instruction);
5837 }
5838
Roland Levillain4d027112015-07-01 15:41:14 +01005839 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005840 Register temp = locations->GetTemp(0).AsRegister<Register>();
5841 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005842 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005843 }
5844
Calin Juravle52c48962014-12-16 17:02:57 +00005845 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005846 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005847 }
5848}
5849
5850void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5851 HandleFieldGet(instruction, instruction->GetFieldInfo());
5852}
5853
5854void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5855 HandleFieldGet(instruction, instruction->GetFieldInfo());
5856}
5857
5858void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5859 HandleFieldSet(instruction, instruction->GetFieldInfo());
5860}
5861
5862void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005863 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005864}
5865
5866void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5867 HandleFieldSet(instruction, instruction->GetFieldInfo());
5868}
5869
5870void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005871 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005872}
5873
5874void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5875 HandleFieldGet(instruction, instruction->GetFieldInfo());
5876}
5877
5878void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5879 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005880}
5881
Vladimir Marko552a1342017-10-31 10:56:47 +00005882void LocationsBuilderX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5883 codegen_->CreateStringBuilderAppendLocations(instruction, Location::RegisterLocation(EAX));
5884}
5885
5886void InstructionCodeGeneratorX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5887 __ movl(EAX, Immediate(instruction->GetFormat()->GetValue()));
5888 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
5889}
5890
Calin Juravlee460d1d2015-09-29 04:52:17 +01005891void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5892 HUnresolvedInstanceFieldGet* instruction) {
5893 FieldAccessCallingConventionX86 calling_convention;
5894 codegen_->CreateUnresolvedFieldLocationSummary(
5895 instruction, instruction->GetFieldType(), calling_convention);
5896}
5897
5898void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5899 HUnresolvedInstanceFieldGet* instruction) {
5900 FieldAccessCallingConventionX86 calling_convention;
5901 codegen_->GenerateUnresolvedFieldAccess(instruction,
5902 instruction->GetFieldType(),
5903 instruction->GetFieldIndex(),
5904 instruction->GetDexPc(),
5905 calling_convention);
5906}
5907
5908void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5909 HUnresolvedInstanceFieldSet* instruction) {
5910 FieldAccessCallingConventionX86 calling_convention;
5911 codegen_->CreateUnresolvedFieldLocationSummary(
5912 instruction, instruction->GetFieldType(), calling_convention);
5913}
5914
5915void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5916 HUnresolvedInstanceFieldSet* instruction) {
5917 FieldAccessCallingConventionX86 calling_convention;
5918 codegen_->GenerateUnresolvedFieldAccess(instruction,
5919 instruction->GetFieldType(),
5920 instruction->GetFieldIndex(),
5921 instruction->GetDexPc(),
5922 calling_convention);
5923}
5924
5925void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5926 HUnresolvedStaticFieldGet* instruction) {
5927 FieldAccessCallingConventionX86 calling_convention;
5928 codegen_->CreateUnresolvedFieldLocationSummary(
5929 instruction, instruction->GetFieldType(), calling_convention);
5930}
5931
5932void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5933 HUnresolvedStaticFieldGet* instruction) {
5934 FieldAccessCallingConventionX86 calling_convention;
5935 codegen_->GenerateUnresolvedFieldAccess(instruction,
5936 instruction->GetFieldType(),
5937 instruction->GetFieldIndex(),
5938 instruction->GetDexPc(),
5939 calling_convention);
5940}
5941
5942void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5943 HUnresolvedStaticFieldSet* instruction) {
5944 FieldAccessCallingConventionX86 calling_convention;
5945 codegen_->CreateUnresolvedFieldLocationSummary(
5946 instruction, instruction->GetFieldType(), calling_convention);
5947}
5948
5949void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5950 HUnresolvedStaticFieldSet* instruction) {
5951 FieldAccessCallingConventionX86 calling_convention;
5952 codegen_->GenerateUnresolvedFieldAccess(instruction,
5953 instruction->GetFieldType(),
5954 instruction->GetFieldIndex(),
5955 instruction->GetDexPc(),
5956 calling_convention);
5957}
5958
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005959void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005960 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5961 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5962 ? Location::RequiresRegister()
5963 : Location::Any();
5964 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005965}
5966
Calin Juravle2ae48182016-03-16 14:05:09 +00005967void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5968 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005969 return;
5970 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005971 LocationSummary* locations = instruction->GetLocations();
5972 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005973
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005974 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005975 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005976}
5977
Calin Juravle2ae48182016-03-16 14:05:09 +00005978void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005979 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005980 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005981
5982 LocationSummary* locations = instruction->GetLocations();
5983 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005984
5985 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005986 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005987 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005988 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005989 } else {
5990 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005991 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005992 __ jmp(slow_path->GetEntryLabel());
5993 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005994 }
5995 __ j(kEqual, slow_path->GetEntryLabel());
5996}
5997
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005998void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005999 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006000}
6001
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006002void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006003 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006004 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006005 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006006 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
6007 object_array_get_with_read_barrier
6008 ? LocationSummary::kCallOnSlowPath
6009 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01006010 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006011 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006012 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006013 locations->SetInAt(0, Location::RequiresRegister());
6014 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006015 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01006016 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6017 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006018 // The output overlaps in case of long: we don't want the low move
6019 // to overwrite the array's location. Likewise, in the case of an
6020 // object array get with read barriers enabled, we do not want the
6021 // move to overwrite the array's location, as we need it to emit
6022 // the read barrier.
6023 locations->SetOut(
6024 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006025 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
6026 ? Location::kOutputOverlap
6027 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01006028 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006029}
6030
6031void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
6032 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006033 Location obj_loc = locations->InAt(0);
6034 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006035 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006036 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006037 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006038
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006039 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00006040 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006041 case DataType::Type::kBool:
6042 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006043 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006044 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006045 break;
6046 }
6047
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006048 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006049 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006050 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006051 break;
6052 }
6053
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006054 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006055 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07006056 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6057 // Branch cases into compressed and uncompressed for each index's type.
6058 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
6059 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00006060 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006061 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006062 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6063 "Expecting 0=compressed, 1=uncompressed");
6064 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07006065 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
6066 __ jmp(&done);
6067 __ Bind(&not_compressed);
6068 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
6069 __ Bind(&done);
6070 } else {
6071 // Common case for charAt of array of char or when string compression's
6072 // feature is turned off.
6073 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
6074 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006075 break;
6076 }
6077
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006078 case DataType::Type::kInt16: {
6079 Register out = out_loc.AsRegister<Register>();
6080 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
6081 break;
6082 }
6083
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006084 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006085 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006086 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006087 break;
6088 }
6089
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006090 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006091 static_assert(
6092 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6093 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00006094 // /* HeapReference<Object> */ out =
6095 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6096 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006097 // Note that a potential implicit null check is handled in this
6098 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
6099 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08006100 instruction, out_loc, obj, data_offset, index, /* needs_null_check= */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006101 } else {
6102 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006103 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
6104 codegen_->MaybeRecordImplicitNullCheck(instruction);
6105 // If read barriers are enabled, emit read barriers other than
6106 // Baker's using a slow path (and also unpoison the loaded
6107 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00006108 if (index.IsConstant()) {
6109 uint32_t offset =
6110 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006111 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6112 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006113 codegen_->MaybeGenerateReadBarrierSlow(
6114 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6115 }
6116 }
6117 break;
6118 }
6119
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006120 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006121 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006122 __ movl(out_loc.AsRegisterPairLow<Register>(),
6123 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
6124 codegen_->MaybeRecordImplicitNullCheck(instruction);
6125 __ movl(out_loc.AsRegisterPairHigh<Register>(),
6126 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006127 break;
6128 }
6129
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006130 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006131 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006132 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006133 break;
6134 }
6135
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006136 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006137 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006138 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006139 break;
6140 }
6141
Aart Bik66c158e2018-01-31 12:55:04 -08006142 case DataType::Type::kUint32:
6143 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006144 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00006145 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07006146 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006147 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006148
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006149 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006150 // Potential implicit null checks, in the case of reference or
6151 // long arrays, are handled in the previous switch statement.
6152 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00006153 codegen_->MaybeRecordImplicitNullCheck(instruction);
6154 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006155}
6156
6157void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006158 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006159
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006160 bool needs_write_barrier =
6161 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006162 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006163
Vladimir Markoca6fff82017-10-03 14:49:14 +01006164 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01006165 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006166 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006167
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006168 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006169 // We need the inputs to be different than the output in case of long operation.
6170 // In case of a byte operation, the register allocator does not support multiple
6171 // inputs that die at entry with one in a specific register.
6172 locations->SetInAt(0, Location::RequiresRegister());
6173 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6174 if (is_byte_type) {
6175 // Ensure the value is in a byte register.
6176 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006177 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05006178 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006179 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006180 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
6181 }
6182 if (needs_write_barrier) {
6183 // Temporary registers for the write barrier.
6184 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6185 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00006186 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006187 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006188}
6189
6190void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
6191 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006192 Location array_loc = locations->InAt(0);
6193 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006194 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006195 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006196 DataType::Type value_type = instruction->GetComponentType();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006197 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006198 bool needs_write_barrier =
6199 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006200
6201 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006202 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006203 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006204 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006205 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006206 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006207 if (value.IsRegister()) {
6208 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006209 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01006210 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006211 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006212 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006213 break;
6214 }
6215
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006216 case DataType::Type::kUint16:
6217 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006218 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006219 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006220 if (value.IsRegister()) {
6221 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006222 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01006223 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006224 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006225 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006226 break;
6227 }
6228
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006229 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006230 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006231 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006232
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006233 if (!value.IsRegister()) {
6234 // Just setting null.
6235 DCHECK(instruction->InputAt(2)->IsNullConstant());
6236 DCHECK(value.IsConstant()) << value;
6237 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00006238 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006239 DCHECK(!needs_write_barrier);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006240 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006241 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006242 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006243
6244 DCHECK(needs_write_barrier);
6245 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01006246 Location temp_loc = locations->GetTemp(0);
6247 Register temp = temp_loc.AsRegister<Register>();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006248
6249 bool can_value_be_null = instruction->GetValueCanBeNull();
6250 NearLabel do_store;
6251 if (can_value_be_null) {
6252 __ testl(register_value, register_value);
6253 __ j(kEqual, &do_store);
6254 }
6255
6256 SlowPathCode* slow_path = nullptr;
6257 if (needs_type_check) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006258 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006259 codegen_->AddSlowPath(slow_path);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006260
6261 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6262 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6263 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006264
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006265 // Note that when Baker read barriers are enabled, the type
6266 // checks are performed without read barriers. This is fine,
6267 // even in the case where a class object is in the from-space
6268 // after the flip, as a comparison involving such a type would
6269 // not produce a false positive; it may of course produce a
6270 // false negative, in which case we would take the ArraySet
6271 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01006272
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006273 // /* HeapReference<Class> */ temp = array->klass_
6274 __ movl(temp, Address(array, class_offset));
6275 codegen_->MaybeRecordImplicitNullCheck(instruction);
6276 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01006277
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006278 // /* HeapReference<Class> */ temp = temp->component_type_
6279 __ movl(temp, Address(temp, component_offset));
6280 // If heap poisoning is enabled, no need to unpoison `temp`
6281 // nor the object reference in `register_value->klass`, as
6282 // we are comparing two poisoned references.
6283 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01006284
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006285 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006286 NearLabel do_put;
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006287 __ j(kEqual, &do_put);
6288 // If heap poisoning is enabled, the `temp` reference has
6289 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006290 __ MaybeUnpoisonHeapReference(temp);
6291
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006292 // If heap poisoning is enabled, no need to unpoison the
6293 // heap reference loaded below, as it is only used for a
6294 // comparison with null.
6295 __ cmpl(Address(temp, super_offset), Immediate(0));
6296 __ j(kNotEqual, slow_path->GetEntryLabel());
6297 __ Bind(&do_put);
6298 } else {
6299 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006300 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006301 }
6302
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006303 Register card = locations->GetTemp(1).AsRegister<Register>();
6304 codegen_->MarkGCCard(
6305 temp, card, array, value.AsRegister<Register>(), /* value_can_be_null= */ false);
6306
6307 if (can_value_be_null) {
6308 DCHECK(do_store.IsLinked());
6309 __ Bind(&do_store);
6310 }
6311
6312 Register source = register_value;
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006313 if (kPoisonHeapReferences) {
6314 __ movl(temp, register_value);
6315 __ PoisonHeapReference(temp);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006316 source = temp;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006317 }
6318
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006319 __ movl(address, source);
6320
6321 if (can_value_be_null || !needs_type_check) {
6322 codegen_->MaybeRecordImplicitNullCheck(instruction);
6323 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006324
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006325 if (slow_path != nullptr) {
6326 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006327 }
6328
6329 break;
6330 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006331
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006332 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006333 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006334 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006335 if (value.IsRegister()) {
6336 __ movl(address, value.AsRegister<Register>());
6337 } else {
6338 DCHECK(value.IsConstant()) << value;
6339 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
6340 __ movl(address, Immediate(v));
6341 }
6342 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006343 break;
6344 }
6345
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006346 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006347 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006348 if (value.IsRegisterPair()) {
6349 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6350 value.AsRegisterPairLow<Register>());
6351 codegen_->MaybeRecordImplicitNullCheck(instruction);
6352 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6353 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006354 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006355 DCHECK(value.IsConstant());
6356 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
6357 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6358 Immediate(Low32Bits(val)));
6359 codegen_->MaybeRecordImplicitNullCheck(instruction);
6360 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6361 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006362 }
6363 break;
6364 }
6365
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006366 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006367 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006368 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006369 if (value.IsFpuRegister()) {
6370 __ movss(address, value.AsFpuRegister<XmmRegister>());
6371 } else {
6372 DCHECK(value.IsConstant());
6373 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
6374 __ movl(address, Immediate(v));
6375 }
6376 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006377 break;
6378 }
6379
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006380 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006381 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006382 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006383 if (value.IsFpuRegister()) {
6384 __ movsd(address, value.AsFpuRegister<XmmRegister>());
6385 } else {
6386 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006387 Address address_hi =
6388 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05006389 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
6390 __ movl(address, Immediate(Low32Bits(v)));
6391 codegen_->MaybeRecordImplicitNullCheck(instruction);
6392 __ movl(address_hi, Immediate(High32Bits(v)));
6393 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006394 break;
6395 }
6396
Aart Bik66c158e2018-01-31 12:55:04 -08006397 case DataType::Type::kUint32:
6398 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006399 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006400 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07006401 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006402 }
6403}
6404
6405void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006406 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006407 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04006408 if (!instruction->IsEmittedAtUseSite()) {
6409 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6410 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006411}
6412
6413void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04006414 if (instruction->IsEmittedAtUseSite()) {
6415 return;
6416 }
6417
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006418 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006419 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006420 Register obj = locations->InAt(0).AsRegister<Register>();
6421 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006422 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00006423 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07006424 // Mask out most significant bit in case the array is String's array of char.
6425 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006426 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006427 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006428}
6429
6430void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006431 RegisterSet caller_saves = RegisterSet::Empty();
6432 InvokeRuntimeCallingConvention calling_convention;
6433 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6434 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6435 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05006436 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04006437 HInstruction* length = instruction->InputAt(1);
6438 if (!length->IsEmittedAtUseSite()) {
6439 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6440 }
jessicahandojo4877b792016-09-08 19:49:13 -07006441 // Need register to see array's length.
6442 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6443 locations->AddTemp(Location::RequiresRegister());
6444 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006445}
6446
6447void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07006448 const bool is_string_compressed_char_at =
6449 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006450 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05006451 Location index_loc = locations->InAt(0);
6452 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006453 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006454 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006455
Mark Mendell99dbd682015-04-22 16:18:52 -04006456 if (length_loc.IsConstant()) {
6457 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
6458 if (index_loc.IsConstant()) {
6459 // BCE will remove the bounds check if we are guarenteed to pass.
6460 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6461 if (index < 0 || index >= length) {
6462 codegen_->AddSlowPath(slow_path);
6463 __ jmp(slow_path->GetEntryLabel());
6464 } else {
6465 // Some optimization after BCE may have generated this, and we should not
6466 // generate a bounds check if it is a valid range.
6467 }
6468 return;
6469 }
6470
6471 // We have to reverse the jump condition because the length is the constant.
6472 Register index_reg = index_loc.AsRegister<Register>();
6473 __ cmpl(index_reg, Immediate(length));
6474 codegen_->AddSlowPath(slow_path);
6475 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006476 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04006477 HInstruction* array_length = instruction->InputAt(1);
6478 if (array_length->IsEmittedAtUseSite()) {
6479 // Address the length field in the array.
6480 DCHECK(array_length->IsArrayLength());
6481 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
6482 Location array_loc = array_length->GetLocations()->InAt(0);
6483 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07006484 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006485 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
6486 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07006487 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
6488 __ movl(length_reg, array_len);
6489 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006490 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006491 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04006492 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006493 // Checking bounds for general case:
6494 // Array of char or string's array with feature compression off.
6495 if (index_loc.IsConstant()) {
6496 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6497 __ cmpl(array_len, Immediate(value));
6498 } else {
6499 __ cmpl(array_len, index_loc.AsRegister<Register>());
6500 }
6501 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04006502 }
Mark Mendell99dbd682015-04-22 16:18:52 -04006503 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006504 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04006505 }
6506 codegen_->AddSlowPath(slow_path);
6507 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006508 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006509}
6510
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006511void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006512 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006513}
6514
6515void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006516 if (instruction->GetNext()->IsSuspendCheck() &&
6517 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6518 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6519 // The back edge will generate the suspend check.
6520 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6521 }
6522
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006523 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6524}
6525
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006526void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006527 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6528 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07006529 // In suspend check slow path, usually there are no caller-save registers at all.
6530 // If SIMD instructions are present, however, we force spilling all live SIMD
6531 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07006532 locations->SetCustomSlowPathCallerSaves(
6533 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006534}
6535
6536void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006537 HBasicBlock* block = instruction->GetBlock();
6538 if (block->GetLoopInformation() != nullptr) {
6539 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6540 // The back edge will generate the suspend check.
6541 return;
6542 }
6543 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6544 // The goto will generate the suspend check.
6545 return;
6546 }
6547 GenerateSuspendCheck(instruction, nullptr);
6548}
6549
6550void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
6551 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006552 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006553 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
6554 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006555 slow_path =
6556 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006557 instruction->SetSlowPath(slow_path);
6558 codegen_->AddSlowPath(slow_path);
6559 if (successor != nullptr) {
6560 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006561 }
6562 } else {
6563 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6564 }
6565
Andreas Gampe542451c2016-07-26 09:02:02 -07006566 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006567 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006568 if (successor == nullptr) {
6569 __ j(kNotEqual, slow_path->GetEntryLabel());
6570 __ Bind(slow_path->GetReturnLabel());
6571 } else {
6572 __ j(kEqual, codegen_->GetLabelOf(successor));
6573 __ jmp(slow_path->GetEntryLabel());
6574 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006575}
6576
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006577X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
6578 return codegen_->GetAssembler();
6579}
6580
Aart Bikcfe50bb2017-12-12 14:54:12 -08006581void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006582 ScratchRegisterScope ensure_scratch(
6583 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6584 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6585 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05006586
Aart Bikcfe50bb2017-12-12 14:54:12 -08006587 // Now that temp register is available (possibly spilled), move blocks of memory.
6588 for (int i = 0; i < number_of_words; i++) {
6589 __ movl(temp_reg, Address(ESP, src + stack_offset));
6590 __ movl(Address(ESP, dst + stack_offset), temp_reg);
6591 stack_offset += kX86WordSize;
6592 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006593}
6594
6595void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006596 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006597 Location source = move->GetSource();
6598 Location destination = move->GetDestination();
6599
6600 if (source.IsRegister()) {
6601 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006602 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006603 } else if (destination.IsFpuRegister()) {
6604 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006605 } else {
6606 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006607 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006608 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006609 } else if (source.IsRegisterPair()) {
Vladimir Marko86c87522020-05-11 16:55:55 +01006610 if (destination.IsRegisterPair()) {
6611 __ movl(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
6612 DCHECK_NE(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
6613 __ movl(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
6614 } else if (destination.IsFpuRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006615 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Vladimir Markodec78172020-06-19 15:31:23 +01006616 // Push the 2 source registers to the stack.
Vladimir Marko86c87522020-05-11 16:55:55 +01006617 __ pushl(source.AsRegisterPairHigh<Register>());
6618 __ cfi().AdjustCFAOffset(elem_size);
6619 __ pushl(source.AsRegisterPairLow<Register>());
6620 __ cfi().AdjustCFAOffset(elem_size);
6621 // Load the destination register.
David Brazdil74eb1b22015-12-14 11:44:01 +00006622 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
6623 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01006624 codegen_->DecreaseFrame(2 * elem_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01006625 } else {
6626 DCHECK(destination.IsDoubleStackSlot());
6627 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
6628 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
6629 source.AsRegisterPairHigh<Register>());
6630 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006631 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006632 if (destination.IsRegister()) {
6633 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
6634 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006635 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006636 } else if (destination.IsRegisterPair()) {
Vladimir Marko86c87522020-05-11 16:55:55 +01006637 size_t elem_size = DataType::Size(DataType::Type::kInt32);
6638 // Create stack space for 2 elements.
Vladimir Markodec78172020-06-19 15:31:23 +01006639 codegen_->IncreaseFrame(2 * elem_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01006640 // Store the source register.
6641 __ movsd(Address(ESP, 0), source.AsFpuRegister<XmmRegister>());
6642 // And pop the values into destination registers.
6643 __ popl(destination.AsRegisterPairLow<Register>());
6644 __ cfi().AdjustCFAOffset(-elem_size);
6645 __ popl(destination.AsRegisterPairHigh<Register>());
6646 __ cfi().AdjustCFAOffset(-elem_size);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006647 } else if (destination.IsStackSlot()) {
6648 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006649 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006650 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006651 } else {
6652 DCHECK(destination.IsSIMDStackSlot());
6653 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05006654 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006655 } else if (source.IsStackSlot()) {
6656 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006657 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006658 } else if (destination.IsFpuRegister()) {
6659 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006660 } else {
6661 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08006662 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006663 }
6664 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006665 if (destination.IsRegisterPair()) {
6666 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
6667 __ movl(destination.AsRegisterPairHigh<Register>(),
6668 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
6669 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006670 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6671 } else {
6672 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006673 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006674 }
Aart Bik5576f372017-03-23 16:17:37 -07006675 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006676 if (destination.IsFpuRegister()) {
6677 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6678 } else {
6679 DCHECK(destination.IsSIMDStackSlot());
6680 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6681 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006682 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006683 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006684 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006685 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006686 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006687 if (value == 0) {
6688 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
6689 } else {
6690 __ movl(destination.AsRegister<Register>(), Immediate(value));
6691 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006692 } else {
6693 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05006694 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006695 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006696 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006697 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006698 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006699 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006700 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006701 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6702 if (value == 0) {
6703 // Easy handling of 0.0.
6704 __ xorps(dest, dest);
6705 } else {
6706 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006707 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6708 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
6709 __ movl(temp, Immediate(value));
6710 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006711 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006712 } else {
6713 DCHECK(destination.IsStackSlot()) << destination;
6714 __ movl(Address(ESP, destination.GetStackIndex()), imm);
6715 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006716 } else if (constant->IsLongConstant()) {
6717 int64_t value = constant->AsLongConstant()->GetValue();
6718 int32_t low_value = Low32Bits(value);
6719 int32_t high_value = High32Bits(value);
6720 Immediate low(low_value);
6721 Immediate high(high_value);
6722 if (destination.IsDoubleStackSlot()) {
6723 __ movl(Address(ESP, destination.GetStackIndex()), low);
6724 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6725 } else {
6726 __ movl(destination.AsRegisterPairLow<Register>(), low);
6727 __ movl(destination.AsRegisterPairHigh<Register>(), high);
6728 }
6729 } else {
6730 DCHECK(constant->IsDoubleConstant());
6731 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006732 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006733 int32_t low_value = Low32Bits(value);
6734 int32_t high_value = High32Bits(value);
6735 Immediate low(low_value);
6736 Immediate high(high_value);
6737 if (destination.IsFpuRegister()) {
6738 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6739 if (value == 0) {
6740 // Easy handling of 0.0.
6741 __ xorpd(dest, dest);
6742 } else {
6743 __ pushl(high);
Vladimir Marko86c87522020-05-11 16:55:55 +01006744 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006745 __ pushl(low);
Vladimir Marko86c87522020-05-11 16:55:55 +01006746 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006747 __ movsd(dest, Address(ESP, 0));
Vladimir Markodec78172020-06-19 15:31:23 +01006748 codegen_->DecreaseFrame(8);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006749 }
6750 } else {
6751 DCHECK(destination.IsDoubleStackSlot()) << destination;
6752 __ movl(Address(ESP, destination.GetStackIndex()), low);
6753 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6754 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006755 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006756 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006757 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006758 }
6759}
6760
Mark Mendella5c19ce2015-04-01 12:51:05 -04006761void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006762 Register suggested_scratch = reg == EAX ? EBX : EAX;
6763 ScratchRegisterScope ensure_scratch(
6764 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
6765
6766 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6767 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
6768 __ movl(Address(ESP, mem + stack_offset), reg);
6769 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006770}
6771
Mark Mendell7c8d0092015-01-26 11:21:33 -05006772void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006773 ScratchRegisterScope ensure_scratch(
6774 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6775
6776 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6777 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6778 __ movl(temp_reg, Address(ESP, mem + stack_offset));
6779 __ movss(Address(ESP, mem + stack_offset), reg);
6780 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006781}
6782
Aart Bikcfe50bb2017-12-12 14:54:12 -08006783void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
6784 size_t extra_slot = 4 * kX86WordSize;
Vladimir Markodec78172020-06-19 15:31:23 +01006785 codegen_->IncreaseFrame(extra_slot);
Aart Bikcfe50bb2017-12-12 14:54:12 -08006786 __ movups(Address(ESP, 0), XmmRegister(reg));
6787 ExchangeMemory(0, mem + extra_slot, 4);
6788 __ movups(XmmRegister(reg), Address(ESP, 0));
Vladimir Markodec78172020-06-19 15:31:23 +01006789 codegen_->DecreaseFrame(extra_slot);
Aart Bikcfe50bb2017-12-12 14:54:12 -08006790}
6791
6792void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006793 ScratchRegisterScope ensure_scratch1(
6794 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006795
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006796 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
6797 ScratchRegisterScope ensure_scratch2(
6798 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006799
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006800 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
6801 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006802
6803 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
6804 for (int i = 0; i < number_of_words; i++) {
6805 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
6806 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
6807 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
6808 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
6809 stack_offset += kX86WordSize;
6810 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006811}
6812
6813void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006814 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006815 Location source = move->GetSource();
6816 Location destination = move->GetDestination();
6817
6818 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04006819 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
6820 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
6821 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
6822 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
6823 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006824 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006825 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006826 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006827 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006828 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006829 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006830 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
6831 // Use XOR Swap algorithm to avoid a temporary.
6832 DCHECK_NE(source.reg(), destination.reg());
6833 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6834 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
6835 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6836 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
6837 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6838 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
6839 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006840 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
6841 // Take advantage of the 16 bytes in the XMM register.
6842 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
6843 Address stack(ESP, destination.GetStackIndex());
6844 // Load the double into the high doubleword.
6845 __ movhpd(reg, stack);
6846
6847 // Store the low double into the destination.
6848 __ movsd(stack, reg);
6849
6850 // Move the high double to the low double.
6851 __ psrldq(reg, Immediate(8));
6852 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6853 // Take advantage of the 16 bytes in the XMM register.
6854 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6855 Address stack(ESP, source.GetStackIndex());
6856 // Load the double into the high doubleword.
6857 __ movhpd(reg, stack);
6858
6859 // Store the low double into the destination.
6860 __ movsd(stack, reg);
6861
6862 // Move the high double to the low double.
6863 __ psrldq(reg, Immediate(8));
6864 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006865 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
6866 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
6867 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6868 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
6869 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6870 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
6871 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006872 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006873 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006874 }
6875}
6876
6877void ParallelMoveResolverX86::SpillScratch(int reg) {
6878 __ pushl(static_cast<Register>(reg));
6879}
6880
6881void ParallelMoveResolverX86::RestoreScratch(int reg) {
6882 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006883}
6884
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006885HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6886 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006887 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006888 case HLoadClass::LoadKind::kInvalid:
6889 LOG(FATAL) << "UNREACHABLE";
6890 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006891 case HLoadClass::LoadKind::kReferrersClass:
6892 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006893 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006894 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006895 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01006896 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006897 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006898 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006899 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01006900 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006901 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006902 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006903 break;
6904 }
6905 return desired_class_load_kind;
6906}
6907
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006908void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006909 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006910 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006911 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006912 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006913 cls,
6914 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006915 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006916 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006917 return;
6918 }
Vladimir Marko41559982017-01-06 14:04:23 +00006919 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006920
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006921 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6922 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006923 ? LocationSummary::kCallOnSlowPath
6924 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006925 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006926 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006927 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006928 }
6929
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006930 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006931 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006932 load_kind == HLoadClass::LoadKind::kBootImageRelRo ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006933 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006934 locations->SetInAt(0, Location::RequiresRegister());
6935 }
6936 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006937 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6938 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6939 // Rely on the type resolution and/or initialization to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006940 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006941 } else {
6942 // For non-Baker read barrier we have a temp-clobbering call.
6943 }
6944 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006945}
6946
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006947Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006948 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006949 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006950 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006951 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006952 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006953 PatchInfo<Label>* info = &jit_class_patches_.back();
6954 return &info->label;
6955}
6956
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006957// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6958// move.
6959void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006960 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006961 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006962 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006963 return;
6964 }
Vladimir Marko41559982017-01-06 14:04:23 +00006965 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006966
Vladimir Marko41559982017-01-06 14:04:23 +00006967 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006968 Location out_loc = locations->Out();
6969 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006970
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006971 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006972 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6973 ? kWithoutReadBarrier
6974 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006975 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006976 case HLoadClass::LoadKind::kReferrersClass: {
6977 DCHECK(!cls->CanCallRuntime());
6978 DCHECK(!cls->MustGenerateClinitCheck());
6979 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6980 Register current_method = locations->InAt(0).AsRegister<Register>();
6981 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006982 cls,
6983 out_loc,
6984 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Andreas Gampe3db70682018-12-26 15:12:03 -08006985 /* fixup_label= */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006986 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006987 break;
6988 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006989 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01006990 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
6991 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006992 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006993 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00006994 __ leal(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006995 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006996 break;
6997 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006998 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006999 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7000 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007001 __ movl(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007002 codegen_->RecordBootImageRelRoPatch(cls->InputAt(0)->AsX86ComputeBaseMethodAddress(),
7003 codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007004 break;
7005 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007006 case HLoadClass::LoadKind::kBssEntry: {
7007 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007008 Address address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007009 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
7010 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007011 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007012 generate_null_check = true;
7013 break;
7014 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007015 case HLoadClass::LoadKind::kJitBootImageAddress: {
7016 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
7017 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
7018 DCHECK_NE(address, 0u);
7019 __ movl(out, Immediate(address));
7020 break;
7021 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007022 case HLoadClass::LoadKind::kJitTableAddress: {
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007023 Address address = Address::Absolute(CodeGeneratorX86::kPlaceholder32BitOffset);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007024 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007025 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007026 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00007027 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007028 break;
7029 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007030 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007031 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007032 LOG(FATAL) << "UNREACHABLE";
7033 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007034 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007035
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007036 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7037 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007038 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007039 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007040
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007041 if (generate_null_check) {
7042 __ testl(out, out);
7043 __ j(kEqual, slow_path->GetEntryLabel());
7044 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007045
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007046 if (cls->MustGenerateClinitCheck()) {
7047 GenerateClassInitializationCheck(slow_path, out);
7048 } else {
7049 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007050 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007051 }
7052}
7053
Orion Hodsondbaa5c72018-05-10 08:22:46 +01007054void LocationsBuilderX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7055 InvokeRuntimeCallingConvention calling_convention;
7056 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7057 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
7058}
7059
7060void InstructionCodeGeneratorX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
7061 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
7062}
7063
Orion Hodson18259d72018-04-12 11:18:23 +01007064void LocationsBuilderX86::VisitLoadMethodType(HLoadMethodType* load) {
7065 InvokeRuntimeCallingConvention calling_convention;
7066 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7067 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
7068}
7069
7070void InstructionCodeGeneratorX86::VisitLoadMethodType(HLoadMethodType* load) {
7071 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
7072}
7073
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007074void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
7075 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007076 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007077 locations->SetInAt(0, Location::RequiresRegister());
7078 if (check->HasUses()) {
7079 locations->SetOut(Location::SameAsFirstInput());
7080 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007081 // Rely on the type initialization to save everything we need.
7082 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007083}
7084
7085void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007086 // We assume the class to not be null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01007087 SlowPathCode* slow_path =
7088 new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(check->GetLoadClass(), check);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007089 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00007090 GenerateClassInitializationCheck(slow_path,
7091 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007092}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007093
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007094void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07007095 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00007096 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
7097 const size_t status_byte_offset =
7098 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
Vladimir Markobf121912019-06-04 13:49:05 +01007099 constexpr uint32_t shifted_visibly_initialized_value =
7100 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << (status_lsb_position % kBitsPerByte);
Vladimir Markodc682aa2018-01-04 18:42:57 +00007101
Vladimir Markobf121912019-06-04 13:49:05 +01007102 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_visibly_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00007103 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007104 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007105}
7106
Vladimir Marko175e7862018-03-27 09:03:13 +00007107void InstructionCodeGeneratorX86::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
7108 Register temp) {
7109 uint32_t path_to_root = check->GetBitstringPathToRoot();
7110 uint32_t mask = check->GetBitstringMask();
7111 DCHECK(IsPowerOfTwo(mask + 1));
7112 size_t mask_bits = WhichPowerOf2(mask + 1);
7113
7114 if (mask_bits == 16u) {
7115 // Compare the bitstring in memory.
7116 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
7117 } else {
7118 // /* uint32_t */ temp = temp->status_
7119 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
7120 // Compare the bitstring bits using SUB.
7121 __ subl(temp, Immediate(path_to_root));
7122 // Shift out bits that do not contribute to the comparison.
7123 __ shll(temp, Immediate(32u - mask_bits));
7124 }
7125}
7126
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007127HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
7128 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007129 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007130 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007131 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007132 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01007133 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007134 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007135 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007136 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01007137 DCHECK(GetCompilerOptions().IsJitCompiler());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007138 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007139 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007140 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007141 }
7142 return desired_string_load_kind;
7143}
7144
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007145void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007146 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007147 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007148 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007149 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007150 load_kind == HLoadString::LoadKind::kBootImageRelRo ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00007151 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007152 locations->SetInAt(0, Location::RequiresRegister());
7153 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007154 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007155 locations->SetOut(Location::RegisterLocation(EAX));
7156 } else {
7157 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007158 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7159 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007160 // Rely on the pResolveString to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007161 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007162 } else {
7163 // For non-Baker read barrier we have a temp-clobbering call.
7164 }
7165 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007166 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007167}
7168
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007169Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01007170 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007171 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007172 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007173 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007174 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007175 PatchInfo<Label>* info = &jit_string_patches_.back();
7176 return &info->label;
7177}
7178
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007179// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7180// move.
7181void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01007182 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007183 Location out_loc = locations->Out();
7184 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007185
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007186 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007187 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007188 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7189 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007190 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007191 __ leal(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007192 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007193 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007194 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007195 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007196 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7197 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007198 __ movl(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007199 codegen_->RecordBootImageRelRoPatch(load->InputAt(0)->AsX86ComputeBaseMethodAddress(),
7200 codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007201 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007202 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007203 case HLoadString::LoadKind::kBssEntry: {
7204 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007205 Address address = Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007206 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007207 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007208 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007209 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007210 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007211 codegen_->AddSlowPath(slow_path);
7212 __ testl(out, out);
7213 __ j(kEqual, slow_path->GetEntryLabel());
7214 __ Bind(slow_path->GetExitLabel());
7215 return;
7216 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007217 case HLoadString::LoadKind::kJitBootImageAddress: {
7218 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
7219 DCHECK_NE(address, 0u);
7220 __ movl(out, Immediate(address));
7221 return;
7222 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007223 case HLoadString::LoadKind::kJitTableAddress: {
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007224 Address address = Address::Absolute(CodeGeneratorX86::kPlaceholder32BitOffset);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007225 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007226 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007227 // /* GcRoot<mirror::String> */ out = *address
7228 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
7229 return;
7230 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007231 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007232 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007233 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007234
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007235 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007236 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007237 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007238 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007239 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7240 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007241}
7242
David Brazdilcb1c0552015-08-04 16:22:25 +01007243static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007244 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01007245}
7246
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007247void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
7248 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007249 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007250 locations->SetOut(Location::RequiresRegister());
7251}
7252
7253void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01007254 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
7255}
7256
7257void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007258 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01007259}
7260
7261void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7262 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007263}
7264
7265void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007266 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7267 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007268 InvokeRuntimeCallingConvention calling_convention;
7269 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7270}
7271
7272void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007273 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007274 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007275}
7276
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007277// Temp is used for read barrier.
7278static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7279 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00007280 !kUseBakerReadBarrier &&
7281 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00007282 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007283 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7284 return 1;
7285 }
7286 return 0;
7287}
7288
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007289// Interface case has 2 temps, one for holding the number of interfaces, one for the current
7290// interface pointer, the current interface is compared in memory.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007291// The other checks have one temp for loading the object's class.
7292static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007293 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007294 return 2;
7295 }
7296 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007297}
7298
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007299void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007300 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007301 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01007302 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007303 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007304 case TypeCheckKind::kExactCheck:
7305 case TypeCheckKind::kAbstractClassCheck:
7306 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007307 case TypeCheckKind::kArrayObjectCheck: {
7308 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7309 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7310 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007311 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007312 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007313 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007314 case TypeCheckKind::kUnresolvedCheck:
7315 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007316 call_kind = LocationSummary::kCallOnSlowPath;
7317 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007318 case TypeCheckKind::kBitstringCheck:
7319 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007320 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007321
Vladimir Markoca6fff82017-10-03 14:49:14 +01007322 LocationSummary* locations =
7323 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01007324 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007325 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007326 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007327 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007328 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7329 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7330 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7331 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7332 } else {
7333 locations->SetInAt(1, Location::Any());
7334 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007335 // Note that TypeCheckSlowPathX86 uses this "out" register too.
7336 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007337 // When read barriers are enabled, we need a temporary register for some cases.
7338 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007339}
7340
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007341void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007342 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007343 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007344 Location obj_loc = locations->InAt(0);
7345 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007346 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007347 Location out_loc = locations->Out();
7348 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007349 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7350 DCHECK_LE(num_temps, 1u);
7351 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007352 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007353 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7354 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7355 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07007356 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007357 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007358
7359 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007360 // Avoid null check if we know obj is not null.
7361 if (instruction->MustDoNullCheck()) {
7362 __ testl(obj, obj);
7363 __ j(kEqual, &zero);
7364 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007365
Roland Levillain7c1559a2015-12-15 10:55:36 +00007366 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007367 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007368 ReadBarrierOption read_barrier_option =
7369 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007370 // /* HeapReference<Class> */ out = obj->klass_
7371 GenerateReferenceLoadTwoRegisters(instruction,
7372 out_loc,
7373 obj_loc,
7374 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007375 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007376 if (cls.IsRegister()) {
7377 __ cmpl(out, cls.AsRegister<Register>());
7378 } else {
7379 DCHECK(cls.IsStackSlot()) << cls;
7380 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7381 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007382
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007383 // Classes must be equal for the instanceof to succeed.
7384 __ j(kNotEqual, &zero);
7385 __ movl(out, Immediate(1));
7386 __ jmp(&done);
7387 break;
7388 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007389
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007390 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007391 ReadBarrierOption read_barrier_option =
7392 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007393 // /* HeapReference<Class> */ out = obj->klass_
7394 GenerateReferenceLoadTwoRegisters(instruction,
7395 out_loc,
7396 obj_loc,
7397 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007398 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007399 // If the class is abstract, we eagerly fetch the super class of the
7400 // object to avoid doing a comparison we know will fail.
7401 NearLabel loop;
7402 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007403 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007404 GenerateReferenceLoadOneRegister(instruction,
7405 out_loc,
7406 super_offset,
7407 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007408 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007409 __ testl(out, out);
7410 // If `out` is null, we use it for the result, and jump to `done`.
7411 __ j(kEqual, &done);
7412 if (cls.IsRegister()) {
7413 __ cmpl(out, cls.AsRegister<Register>());
7414 } else {
7415 DCHECK(cls.IsStackSlot()) << cls;
7416 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7417 }
7418 __ j(kNotEqual, &loop);
7419 __ movl(out, Immediate(1));
7420 if (zero.IsLinked()) {
7421 __ jmp(&done);
7422 }
7423 break;
7424 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007425
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007426 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007427 ReadBarrierOption read_barrier_option =
7428 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007429 // /* HeapReference<Class> */ out = obj->klass_
7430 GenerateReferenceLoadTwoRegisters(instruction,
7431 out_loc,
7432 obj_loc,
7433 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007434 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007435 // Walk over the class hierarchy to find a match.
7436 NearLabel loop, success;
7437 __ Bind(&loop);
7438 if (cls.IsRegister()) {
7439 __ cmpl(out, cls.AsRegister<Register>());
7440 } else {
7441 DCHECK(cls.IsStackSlot()) << cls;
7442 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7443 }
7444 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007445 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007446 GenerateReferenceLoadOneRegister(instruction,
7447 out_loc,
7448 super_offset,
7449 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007450 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007451 __ testl(out, out);
7452 __ j(kNotEqual, &loop);
7453 // If `out` is null, we use it for the result, and jump to `done`.
7454 __ jmp(&done);
7455 __ Bind(&success);
7456 __ movl(out, Immediate(1));
7457 if (zero.IsLinked()) {
7458 __ jmp(&done);
7459 }
7460 break;
7461 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007462
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007463 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007464 ReadBarrierOption read_barrier_option =
7465 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007466 // /* HeapReference<Class> */ out = obj->klass_
7467 GenerateReferenceLoadTwoRegisters(instruction,
7468 out_loc,
7469 obj_loc,
7470 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007471 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007472 // Do an exact check.
7473 NearLabel exact_check;
7474 if (cls.IsRegister()) {
7475 __ cmpl(out, cls.AsRegister<Register>());
7476 } else {
7477 DCHECK(cls.IsStackSlot()) << cls;
7478 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7479 }
7480 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007481 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007482 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007483 GenerateReferenceLoadOneRegister(instruction,
7484 out_loc,
7485 component_offset,
7486 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007487 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007488 __ testl(out, out);
7489 // If `out` is null, we use it for the result, and jump to `done`.
7490 __ j(kEqual, &done);
7491 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
7492 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007493 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007494 __ movl(out, Immediate(1));
7495 __ jmp(&done);
7496 break;
7497 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007498
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007499 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007500 // No read barrier since the slow path will retry upon failure.
7501 // /* HeapReference<Class> */ out = obj->klass_
7502 GenerateReferenceLoadTwoRegisters(instruction,
7503 out_loc,
7504 obj_loc,
7505 class_offset,
7506 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007507 if (cls.IsRegister()) {
7508 __ cmpl(out, cls.AsRegister<Register>());
7509 } else {
7510 DCHECK(cls.IsStackSlot()) << cls;
7511 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7512 }
7513 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007514 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007515 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007516 codegen_->AddSlowPath(slow_path);
7517 __ j(kNotEqual, slow_path->GetEntryLabel());
7518 __ movl(out, Immediate(1));
7519 if (zero.IsLinked()) {
7520 __ jmp(&done);
7521 }
7522 break;
7523 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007524
Calin Juravle98893e12015-10-02 21:05:03 +01007525 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007526 case TypeCheckKind::kInterfaceCheck: {
7527 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007528 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00007529 // cases.
7530 //
7531 // We cannot directly call the InstanceofNonTrivial runtime
7532 // entry point without resorting to a type checking slow path
7533 // here (i.e. by calling InvokeRuntime directly), as it would
7534 // require to assign fixed registers for the inputs of this
7535 // HInstanceOf instruction (following the runtime calling
7536 // convention), which might be cluttered by the potential first
7537 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007538 //
7539 // TODO: Introduce a new runtime entry point taking the object
7540 // to test (instead of its class) as argument, and let it deal
7541 // with the read barrier issues. This will let us refactor this
7542 // case of the `switch` code as it was previously (with a direct
7543 // call to the runtime not using a type checking slow path).
7544 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007545 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007546 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007547 instruction, /* is_fatal= */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007548 codegen_->AddSlowPath(slow_path);
7549 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007550 if (zero.IsLinked()) {
7551 __ jmp(&done);
7552 }
7553 break;
7554 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007555
7556 case TypeCheckKind::kBitstringCheck: {
7557 // /* HeapReference<Class> */ temp = obj->klass_
7558 GenerateReferenceLoadTwoRegisters(instruction,
7559 out_loc,
7560 obj_loc,
7561 class_offset,
7562 kWithoutReadBarrier);
7563
7564 GenerateBitstringTypeCheckCompare(instruction, out);
7565 __ j(kNotEqual, &zero);
7566 __ movl(out, Immediate(1));
7567 __ jmp(&done);
7568 break;
7569 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007570 }
7571
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007572 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007573 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007574 __ xorl(out, out);
7575 }
7576
7577 if (done.IsLinked()) {
7578 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007579 }
7580
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007581 if (slow_path != nullptr) {
7582 __ Bind(slow_path->GetExitLabel());
7583 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007584}
7585
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007586void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007587 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007588 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007589 LocationSummary* locations =
7590 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007591 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007592 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7593 // Require a register for the interface check since there is a loop that compares the class to
7594 // a memory address.
7595 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007596 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7597 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7598 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7599 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007600 } else {
7601 locations->SetInAt(1, Location::Any());
7602 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007603 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007604 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
7605}
7606
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007607void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007608 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007609 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007610 Location obj_loc = locations->InAt(0);
7611 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007612 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007613 Location temp_loc = locations->GetTemp(0);
7614 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007615 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7616 DCHECK_GE(num_temps, 1u);
7617 DCHECK_LE(num_temps, 2u);
7618 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7619 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7620 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7621 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7622 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7623 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7624 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7625 const uint32_t object_array_data_offset =
7626 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007627
Vladimir Marko87584542017-12-12 17:47:52 +00007628 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007629 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007630 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7631 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007632 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007633
Roland Levillain0d5a2812015-11-13 10:07:31 +00007634 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007635 // Avoid null check if we know obj is not null.
7636 if (instruction->MustDoNullCheck()) {
7637 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007638 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007639 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007640
Roland Levillain0d5a2812015-11-13 10:07:31 +00007641 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007642 case TypeCheckKind::kExactCheck:
7643 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007644 // /* HeapReference<Class> */ temp = obj->klass_
7645 GenerateReferenceLoadTwoRegisters(instruction,
7646 temp_loc,
7647 obj_loc,
7648 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007649 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007650
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007651 if (cls.IsRegister()) {
7652 __ cmpl(temp, cls.AsRegister<Register>());
7653 } else {
7654 DCHECK(cls.IsStackSlot()) << cls;
7655 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7656 }
7657 // Jump to slow path for throwing the exception or doing a
7658 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007659 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007660 break;
7661 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007662
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007663 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007664 // /* HeapReference<Class> */ temp = obj->klass_
7665 GenerateReferenceLoadTwoRegisters(instruction,
7666 temp_loc,
7667 obj_loc,
7668 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007669 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007670
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007671 // If the class is abstract, we eagerly fetch the super class of the
7672 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007673 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007674 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007675 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007676 GenerateReferenceLoadOneRegister(instruction,
7677 temp_loc,
7678 super_offset,
7679 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007680 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007681
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007682 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7683 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007684 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007685 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007686
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007687 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007688 if (cls.IsRegister()) {
7689 __ cmpl(temp, cls.AsRegister<Register>());
7690 } else {
7691 DCHECK(cls.IsStackSlot()) << cls;
7692 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7693 }
7694 __ j(kNotEqual, &loop);
7695 break;
7696 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007697
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007698 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007699 // /* HeapReference<Class> */ temp = obj->klass_
7700 GenerateReferenceLoadTwoRegisters(instruction,
7701 temp_loc,
7702 obj_loc,
7703 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007704 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007705
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007706 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007707 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007708 __ Bind(&loop);
7709 if (cls.IsRegister()) {
7710 __ cmpl(temp, cls.AsRegister<Register>());
7711 } else {
7712 DCHECK(cls.IsStackSlot()) << cls;
7713 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7714 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007715 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007716
Roland Levillain0d5a2812015-11-13 10:07:31 +00007717 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007718 GenerateReferenceLoadOneRegister(instruction,
7719 temp_loc,
7720 super_offset,
7721 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007722 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007723
7724 // If the class reference currently in `temp` is not null, jump
7725 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007726 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007727 __ j(kNotZero, &loop);
7728 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007729 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007730 break;
7731 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007732
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007733 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007734 // /* HeapReference<Class> */ temp = obj->klass_
7735 GenerateReferenceLoadTwoRegisters(instruction,
7736 temp_loc,
7737 obj_loc,
7738 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007739 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007740
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007741 // Do an exact check.
7742 if (cls.IsRegister()) {
7743 __ cmpl(temp, cls.AsRegister<Register>());
7744 } else {
7745 DCHECK(cls.IsStackSlot()) << cls;
7746 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7747 }
7748 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007749
7750 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007751 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007752 GenerateReferenceLoadOneRegister(instruction,
7753 temp_loc,
7754 component_offset,
7755 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007756 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007757
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007758 // If the component type is null (i.e. the object not an array), jump to the slow path to
7759 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007760 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007761 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007762
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007763 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007764 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007765 break;
7766 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007767
Calin Juravle98893e12015-10-02 21:05:03 +01007768 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007769 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007770 // We cannot directly call the CheckCast runtime entry point
7771 // without resorting to a type checking slow path here (i.e. by
7772 // calling InvokeRuntime directly), as it would require to
7773 // assign fixed registers for the inputs of this HInstanceOf
7774 // instruction (following the runtime calling convention), which
7775 // might be cluttered by the potential first read barrier
7776 // emission at the beginning of this method.
7777 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007778 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007779
7780 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007781 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
7782 // We can not get false positives by doing this.
7783 // /* HeapReference<Class> */ temp = obj->klass_
7784 GenerateReferenceLoadTwoRegisters(instruction,
7785 temp_loc,
7786 obj_loc,
7787 class_offset,
7788 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007789
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007790 // /* HeapReference<Class> */ temp = temp->iftable_
7791 GenerateReferenceLoadTwoRegisters(instruction,
7792 temp_loc,
7793 temp_loc,
7794 iftable_offset,
7795 kWithoutReadBarrier);
7796 // Iftable is never null.
7797 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
7798 // Maybe poison the `cls` for direct comparison with memory.
7799 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
7800 // Loop through the iftable and check if any class matches.
7801 NearLabel start_loop;
7802 __ Bind(&start_loop);
7803 // Need to subtract first to handle the empty array case.
7804 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
7805 __ j(kNegative, type_check_slow_path->GetEntryLabel());
7806 // Go to next interface if the classes do not match.
7807 __ cmpl(cls.AsRegister<Register>(),
7808 CodeGeneratorX86::ArrayAddress(temp,
7809 maybe_temp2_loc,
7810 TIMES_4,
7811 object_array_data_offset));
7812 __ j(kNotEqual, &start_loop);
7813 // If `cls` was poisoned above, unpoison it.
7814 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007815 break;
7816 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007817
7818 case TypeCheckKind::kBitstringCheck: {
7819 // /* HeapReference<Class> */ temp = obj->klass_
7820 GenerateReferenceLoadTwoRegisters(instruction,
7821 temp_loc,
7822 obj_loc,
7823 class_offset,
7824 kWithoutReadBarrier);
7825
7826 GenerateBitstringTypeCheckCompare(instruction, temp);
7827 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
7828 break;
7829 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007830 }
7831 __ Bind(&done);
7832
Roland Levillain0d5a2812015-11-13 10:07:31 +00007833 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007834}
7835
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007836void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007837 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7838 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007839 InvokeRuntimeCallingConvention calling_convention;
7840 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7841}
7842
7843void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007844 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
7845 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01007846 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01007847 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007848 if (instruction->IsEnter()) {
7849 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7850 } else {
7851 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7852 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007853}
7854
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307855void LocationsBuilderX86::VisitX86AndNot(HX86AndNot* instruction) {
7856 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7857 DCHECK(DataType::IsIntOrLongType(instruction->GetType())) << instruction->GetType();
7858 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7859 locations->SetInAt(0, Location::RequiresRegister());
7860 locations->SetInAt(1, Location::RequiresRegister());
7861 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7862}
7863
7864void InstructionCodeGeneratorX86::VisitX86AndNot(HX86AndNot* instruction) {
7865 LocationSummary* locations = instruction->GetLocations();
7866 Location first = locations->InAt(0);
7867 Location second = locations->InAt(1);
7868 Location dest = locations->Out();
7869 if (instruction->GetResultType() == DataType::Type::kInt32) {
7870 __ andn(dest.AsRegister<Register>(),
7871 first.AsRegister<Register>(),
7872 second.AsRegister<Register>());
7873 } else {
7874 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
7875 __ andn(dest.AsRegisterPairLow<Register>(),
7876 first.AsRegisterPairLow<Register>(),
7877 second.AsRegisterPairLow<Register>());
7878 __ andn(dest.AsRegisterPairHigh<Register>(),
7879 first.AsRegisterPairHigh<Register>(),
7880 second.AsRegisterPairHigh<Register>());
7881 }
7882}
7883
7884void LocationsBuilderX86::VisitX86MaskOrResetLeastSetBit(HX86MaskOrResetLeastSetBit* instruction) {
7885 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7886 DCHECK(instruction->GetType() == DataType::Type::kInt32) << instruction->GetType();
7887 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7888 locations->SetInAt(0, Location::RequiresRegister());
7889 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7890}
7891
7892void InstructionCodeGeneratorX86::VisitX86MaskOrResetLeastSetBit(
7893 HX86MaskOrResetLeastSetBit* instruction) {
7894 LocationSummary* locations = instruction->GetLocations();
7895 Location src = locations->InAt(0);
7896 Location dest = locations->Out();
7897 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
7898 switch (instruction->GetOpKind()) {
7899 case HInstruction::kAnd:
7900 __ blsr(dest.AsRegister<Register>(), src.AsRegister<Register>());
7901 break;
7902 case HInstruction::kXor:
7903 __ blsmsk(dest.AsRegister<Register>(), src.AsRegister<Register>());
7904 break;
7905 default:
7906 LOG(FATAL) << "Unreachable";
7907 }
7908}
7909
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007910void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
7911void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
7912void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
7913
7914void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7915 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007916 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007917 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7918 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007919 locations->SetInAt(0, Location::RequiresRegister());
7920 locations->SetInAt(1, Location::Any());
7921 locations->SetOut(Location::SameAsFirstInput());
7922}
7923
7924void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
7925 HandleBitwiseOperation(instruction);
7926}
7927
7928void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
7929 HandleBitwiseOperation(instruction);
7930}
7931
7932void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
7933 HandleBitwiseOperation(instruction);
7934}
7935
7936void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7937 LocationSummary* locations = instruction->GetLocations();
7938 Location first = locations->InAt(0);
7939 Location second = locations->InAt(1);
7940 DCHECK(first.Equals(locations->Out()));
7941
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007942 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007943 if (second.IsRegister()) {
7944 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007945 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007946 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007947 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007948 } else {
7949 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007950 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007951 }
7952 } else if (second.IsConstant()) {
7953 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007954 __ andl(first.AsRegister<Register>(),
7955 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007956 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007957 __ orl(first.AsRegister<Register>(),
7958 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007959 } else {
7960 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00007961 __ xorl(first.AsRegister<Register>(),
7962 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007963 }
7964 } else {
7965 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007966 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007967 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007968 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007969 } else {
7970 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007971 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007972 }
7973 }
7974 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007975 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007976 if (second.IsRegisterPair()) {
7977 if (instruction->IsAnd()) {
7978 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7979 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7980 } else if (instruction->IsOr()) {
7981 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7982 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7983 } else {
7984 DCHECK(instruction->IsXor());
7985 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7986 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7987 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007988 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007989 if (instruction->IsAnd()) {
7990 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7991 __ andl(first.AsRegisterPairHigh<Register>(),
7992 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7993 } else if (instruction->IsOr()) {
7994 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7995 __ orl(first.AsRegisterPairHigh<Register>(),
7996 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7997 } else {
7998 DCHECK(instruction->IsXor());
7999 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
8000 __ xorl(first.AsRegisterPairHigh<Register>(),
8001 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
8002 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008003 } else {
8004 DCHECK(second.IsConstant()) << second;
8005 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008006 int32_t low_value = Low32Bits(value);
8007 int32_t high_value = High32Bits(value);
8008 Immediate low(low_value);
8009 Immediate high(high_value);
8010 Register first_low = first.AsRegisterPairLow<Register>();
8011 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008012 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008013 if (low_value == 0) {
8014 __ xorl(first_low, first_low);
8015 } else if (low_value != -1) {
8016 __ andl(first_low, low);
8017 }
8018 if (high_value == 0) {
8019 __ xorl(first_high, first_high);
8020 } else if (high_value != -1) {
8021 __ andl(first_high, high);
8022 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008023 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008024 if (low_value != 0) {
8025 __ orl(first_low, low);
8026 }
8027 if (high_value != 0) {
8028 __ orl(first_high, high);
8029 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008030 } else {
8031 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04008032 if (low_value != 0) {
8033 __ xorl(first_low, low);
8034 }
8035 if (high_value != 0) {
8036 __ xorl(first_high, high);
8037 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00008038 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008039 }
8040 }
8041}
8042
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008043void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
8044 HInstruction* instruction,
8045 Location out,
8046 uint32_t offset,
8047 Location maybe_temp,
8048 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008049 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008050 if (read_barrier_option == kWithReadBarrier) {
8051 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008052 if (kUseBakerReadBarrier) {
8053 // Load with fast path based Baker's read barrier.
8054 // /* HeapReference<Object> */ out = *(out + offset)
8055 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008056 instruction, out, out_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008057 } else {
8058 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008059 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00008060 // in the following move operation, as we will need it for the
8061 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00008062 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008063 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008064 // /* HeapReference<Object> */ out = *(out + offset)
8065 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008066 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008067 }
8068 } else {
8069 // Plain load with no read barrier.
8070 // /* HeapReference<Object> */ out = *(out + offset)
8071 __ movl(out_reg, Address(out_reg, offset));
8072 __ MaybeUnpoisonHeapReference(out_reg);
8073 }
8074}
8075
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008076void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
8077 HInstruction* instruction,
8078 Location out,
8079 Location obj,
8080 uint32_t offset,
8081 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008082 Register out_reg = out.AsRegister<Register>();
8083 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008084 if (read_barrier_option == kWithReadBarrier) {
8085 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008086 if (kUseBakerReadBarrier) {
8087 // Load with fast path based Baker's read barrier.
8088 // /* HeapReference<Object> */ out = *(obj + offset)
8089 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08008090 instruction, out, obj_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008091 } else {
8092 // Load with slow path based read barrier.
8093 // /* HeapReference<Object> */ out = *(obj + offset)
8094 __ movl(out_reg, Address(obj_reg, offset));
8095 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8096 }
8097 } else {
8098 // Plain load with no read barrier.
8099 // /* HeapReference<Object> */ out = *(obj + offset)
8100 __ movl(out_reg, Address(obj_reg, offset));
8101 __ MaybeUnpoisonHeapReference(out_reg);
8102 }
8103}
8104
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008105void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
8106 HInstruction* instruction,
8107 Location root,
8108 const Address& address,
8109 Label* fixup_label,
8110 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008111 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008112 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07008113 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008114 if (kUseBakerReadBarrier) {
8115 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
8116 // Baker's read barrier are used:
8117 //
Roland Levillaind966ce72017-02-09 16:20:14 +00008118 // root = obj.field;
8119 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8120 // if (temp != null) {
8121 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00008122 // }
8123
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008124 // /* GcRoot<mirror::Object> */ root = *address
8125 __ movl(root_reg, address);
8126 if (fixup_label != nullptr) {
8127 __ Bind(fixup_label);
8128 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008129 static_assert(
8130 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8131 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8132 "have different sizes.");
8133 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8134 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8135 "have different sizes.");
8136
Vladimir Marko953437b2016-08-24 08:30:46 +00008137 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008138 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008139 instruction, root, /* unpoison_ref_before_marking= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008140 codegen_->AddSlowPath(slow_path);
8141
Roland Levillaind966ce72017-02-09 16:20:14 +00008142 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
8143 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01008144 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00008145 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
8146 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008147 __ j(kNotEqual, slow_path->GetEntryLabel());
8148 __ Bind(slow_path->GetExitLabel());
8149 } else {
8150 // GC root loaded through a slow path for read barriers other
8151 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008152 // /* GcRoot<mirror::Object>* */ root = address
8153 __ leal(root_reg, address);
8154 if (fixup_label != nullptr) {
8155 __ Bind(fixup_label);
8156 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008157 // /* mirror::Object* */ root = root->Read()
8158 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8159 }
8160 } else {
8161 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008162 // /* GcRoot<mirror::Object> */ root = *address
8163 __ movl(root_reg, address);
8164 if (fixup_label != nullptr) {
8165 __ Bind(fixup_label);
8166 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00008167 // Note that GC roots are not affected by heap poisoning, thus we
8168 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008169 }
8170}
8171
8172void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8173 Location ref,
8174 Register obj,
8175 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00008176 bool needs_null_check) {
8177 DCHECK(kEmitCompilerReadBarrier);
8178 DCHECK(kUseBakerReadBarrier);
8179
8180 // /* HeapReference<Object> */ ref = *(obj + offset)
8181 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00008182 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008183}
8184
8185void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8186 Location ref,
8187 Register obj,
8188 uint32_t data_offset,
8189 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00008190 bool needs_null_check) {
8191 DCHECK(kEmitCompilerReadBarrier);
8192 DCHECK(kUseBakerReadBarrier);
8193
Roland Levillain3d312422016-06-23 13:53:42 +01008194 static_assert(
8195 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8196 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00008197 // /* HeapReference<Object> */ ref =
8198 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008199 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00008200 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008201}
8202
8203void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8204 Location ref,
8205 Register obj,
8206 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008207 bool needs_null_check,
8208 bool always_update_field,
8209 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008210 DCHECK(kEmitCompilerReadBarrier);
8211 DCHECK(kUseBakerReadBarrier);
8212
8213 // In slow path based read barriers, the read barrier call is
8214 // inserted after the original load. However, in fast path based
8215 // Baker's read barriers, we need to perform the load of
8216 // mirror::Object::monitor_ *before* the original reference load.
8217 // This load-load ordering is required by the read barrier.
8218 // The fast path/slow path (for Baker's algorithm) should look like:
8219 //
8220 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8221 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8222 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008223 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00008224 // if (is_gray) {
8225 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
8226 // }
8227 //
8228 // Note: the original implementation in ReadBarrier::Barrier is
8229 // slightly more complex as:
8230 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00008231 // the high-bits of rb_state, which are expected to be all zeroes
8232 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
8233 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008234 // - it performs additional checks that we do not do here for
8235 // performance reasons.
8236
8237 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00008238 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
8239
Vladimir Marko953437b2016-08-24 08:30:46 +00008240 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01008241 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008242 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00008243 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
8244 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
8245 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
8246
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008247 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00008248 // ref = ReadBarrier::Mark(ref);
8249 // At this point, just do the "if" and make sure that flags are preserved until the branch.
8250 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00008251 if (needs_null_check) {
8252 MaybeRecordImplicitNullCheck(instruction);
8253 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008254
8255 // Load fence to prevent load-load reordering.
8256 // Note that this is a no-op, thanks to the x86 memory model.
8257 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
8258
8259 // The actual reference load.
8260 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00008261 __ movl(ref_reg, src); // Flags are unaffected.
8262
8263 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
8264 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008265 SlowPathCode* slow_path;
8266 if (always_update_field) {
8267 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01008268 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008269 instruction, ref, obj, src, /* unpoison_ref_before_marking= */ true, *temp);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008270 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008271 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008272 instruction, ref, /* unpoison_ref_before_marking= */ true);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008273 }
Vladimir Marko953437b2016-08-24 08:30:46 +00008274 AddSlowPath(slow_path);
8275
8276 // We have done the "if" of the gray bit check above, now branch based on the flags.
8277 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00008278
8279 // Object* ref = ref_addr->AsMirrorPtr()
8280 __ MaybeUnpoisonHeapReference(ref_reg);
8281
Roland Levillain7c1559a2015-12-15 10:55:36 +00008282 __ Bind(slow_path->GetExitLabel());
8283}
8284
8285void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
8286 Location out,
8287 Location ref,
8288 Location obj,
8289 uint32_t offset,
8290 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008291 DCHECK(kEmitCompilerReadBarrier);
8292
Roland Levillain7c1559a2015-12-15 10:55:36 +00008293 // Insert a slow path based read barrier *after* the reference load.
8294 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008295 // If heap poisoning is enabled, the unpoisoning of the loaded
8296 // reference will be carried out by the runtime within the slow
8297 // path.
8298 //
8299 // Note that `ref` currently does not get unpoisoned (when heap
8300 // poisoning is enabled), which is alright as the `ref` argument is
8301 // not used by the artReadBarrierSlow entry point.
8302 //
8303 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008304 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00008305 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
8306 AddSlowPath(slow_path);
8307
Roland Levillain0d5a2812015-11-13 10:07:31 +00008308 __ jmp(slow_path->GetEntryLabel());
8309 __ Bind(slow_path->GetExitLabel());
8310}
8311
Roland Levillain7c1559a2015-12-15 10:55:36 +00008312void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
8313 Location out,
8314 Location ref,
8315 Location obj,
8316 uint32_t offset,
8317 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008318 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008319 // Baker's read barriers shall be handled by the fast path
8320 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
8321 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008322 // If heap poisoning is enabled, unpoisoning will be taken care of
8323 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008324 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008325 } else if (kPoisonHeapReferences) {
8326 __ UnpoisonHeapReference(out.AsRegister<Register>());
8327 }
8328}
8329
Roland Levillain7c1559a2015-12-15 10:55:36 +00008330void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8331 Location out,
8332 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008333 DCHECK(kEmitCompilerReadBarrier);
8334
Roland Levillain7c1559a2015-12-15 10:55:36 +00008335 // Insert a slow path based read barrier *after* the GC root load.
8336 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008337 // Note that GC roots are not affected by heap poisoning, so we do
8338 // not need to do anything special for this here.
8339 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008340 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008341 AddSlowPath(slow_path);
8342
Roland Levillain0d5a2812015-11-13 10:07:31 +00008343 __ jmp(slow_path->GetEntryLabel());
8344 __ Bind(slow_path->GetExitLabel());
8345}
8346
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008347void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008348 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008349 LOG(FATAL) << "Unreachable";
8350}
8351
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008352void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008353 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008354 LOG(FATAL) << "Unreachable";
8355}
8356
Mark Mendellfe57faa2015-09-18 09:26:15 -04008357// Simple implementation of packed switch - generate cascaded compare/jumps.
8358void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8359 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008360 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04008361 locations->SetInAt(0, Location::RequiresRegister());
8362}
8363
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008364void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
8365 int32_t lower_bound,
8366 uint32_t num_entries,
8367 HBasicBlock* switch_block,
8368 HBasicBlock* default_block) {
8369 // Figure out the correct compare values and jump conditions.
8370 // Handle the first compare/branch as a special case because it might
8371 // jump to the default case.
8372 DCHECK_GT(num_entries, 2u);
8373 Condition first_condition;
8374 uint32_t index;
8375 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
8376 if (lower_bound != 0) {
8377 first_condition = kLess;
8378 __ cmpl(value_reg, Immediate(lower_bound));
8379 __ j(first_condition, codegen_->GetLabelOf(default_block));
8380 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008381
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008382 index = 1;
8383 } else {
8384 // Handle all the compare/jumps below.
8385 first_condition = kBelow;
8386 index = 0;
8387 }
8388
8389 // Handle the rest of the compare/jumps.
8390 for (; index + 1 < num_entries; index += 2) {
8391 int32_t compare_to_value = lower_bound + index + 1;
8392 __ cmpl(value_reg, Immediate(compare_to_value));
8393 // Jump to successors[index] if value < case_value[index].
8394 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
8395 // Jump to successors[index + 1] if value == case_value[index + 1].
8396 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
8397 }
8398
8399 if (index != num_entries) {
8400 // There are an odd number of entries. Handle the last one.
8401 DCHECK_EQ(index + 1, num_entries);
8402 __ cmpl(value_reg, Immediate(lower_bound + index));
8403 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008404 }
8405
8406 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008407 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
8408 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008409 }
8410}
8411
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008412void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8413 int32_t lower_bound = switch_instr->GetStartValue();
8414 uint32_t num_entries = switch_instr->GetNumEntries();
8415 LocationSummary* locations = switch_instr->GetLocations();
8416 Register value_reg = locations->InAt(0).AsRegister<Register>();
8417
8418 GenPackedSwitchWithCompares(value_reg,
8419 lower_bound,
8420 num_entries,
8421 switch_instr->GetBlock(),
8422 switch_instr->GetDefaultBlock());
8423}
8424
Mark Mendell805b3b52015-09-18 14:10:29 -04008425void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8426 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008427 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04008428 locations->SetInAt(0, Location::RequiresRegister());
8429
8430 // Constant area pointer.
8431 locations->SetInAt(1, Location::RequiresRegister());
8432
8433 // And the temporary we need.
8434 locations->AddTemp(Location::RequiresRegister());
8435}
8436
8437void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8438 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008439 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04008440 LocationSummary* locations = switch_instr->GetLocations();
8441 Register value_reg = locations->InAt(0).AsRegister<Register>();
8442 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8443
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008444 if (num_entries <= kPackedSwitchJumpTableThreshold) {
8445 GenPackedSwitchWithCompares(value_reg,
8446 lower_bound,
8447 num_entries,
8448 switch_instr->GetBlock(),
8449 default_block);
8450 return;
8451 }
8452
Mark Mendell805b3b52015-09-18 14:10:29 -04008453 // Optimizing has a jump area.
8454 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
8455 Register constant_area = locations->InAt(1).AsRegister<Register>();
8456
8457 // Remove the bias, if needed.
8458 if (lower_bound != 0) {
8459 __ leal(temp_reg, Address(value_reg, -lower_bound));
8460 value_reg = temp_reg;
8461 }
8462
8463 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008464 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04008465 __ cmpl(value_reg, Immediate(num_entries - 1));
8466 __ j(kAbove, codegen_->GetLabelOf(default_block));
8467
8468 // We are in the range of the table.
8469 // Load (target-constant_area) from the jump table, indexing by the value.
8470 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
8471
8472 // Compute the actual target address by adding in constant_area.
8473 __ addl(temp_reg, constant_area);
8474
8475 // And jump.
8476 __ jmp(temp_reg);
8477}
8478
Mark Mendell0616ae02015-04-17 12:49:27 -04008479void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
8480 HX86ComputeBaseMethodAddress* insn) {
8481 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008482 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008483 locations->SetOut(Location::RequiresRegister());
8484}
8485
8486void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
8487 HX86ComputeBaseMethodAddress* insn) {
8488 LocationSummary* locations = insn->GetLocations();
8489 Register reg = locations->Out().AsRegister<Register>();
8490
8491 // Generate call to next instruction.
8492 Label next_instruction;
8493 __ call(&next_instruction);
8494 __ Bind(&next_instruction);
8495
8496 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008497 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04008498
8499 // Grab the return address off the stack.
8500 __ popl(reg);
8501}
8502
8503void LocationsBuilderX86::VisitX86LoadFromConstantTable(
8504 HX86LoadFromConstantTable* insn) {
8505 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008506 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008507
8508 locations->SetInAt(0, Location::RequiresRegister());
8509 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
8510
8511 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00008512 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008513 return;
8514 }
8515
8516 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008517 case DataType::Type::kFloat32:
8518 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008519 locations->SetOut(Location::RequiresFpuRegister());
8520 break;
8521
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008522 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008523 locations->SetOut(Location::RequiresRegister());
8524 break;
8525
8526 default:
8527 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8528 }
8529}
8530
8531void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00008532 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008533 return;
8534 }
8535
8536 LocationSummary* locations = insn->GetLocations();
8537 Location out = locations->Out();
8538 Register const_area = locations->InAt(0).AsRegister<Register>();
8539 HConstant *value = insn->GetConstant();
8540
8541 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008542 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008543 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008544 codegen_->LiteralFloatAddress(
8545 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008546 break;
8547
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008548 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008549 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008550 codegen_->LiteralDoubleAddress(
8551 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008552 break;
8553
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008554 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008555 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008556 codegen_->LiteralInt32Address(
8557 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008558 break;
8559
8560 default:
8561 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8562 }
8563}
8564
Mark Mendell0616ae02015-04-17 12:49:27 -04008565/**
8566 * Class to handle late fixup of offsets into constant area.
8567 */
Vladimir Marko5233f932015-09-29 19:01:15 +01008568class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04008569 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008570 RIPFixup(CodeGeneratorX86& codegen,
8571 HX86ComputeBaseMethodAddress* base_method_address,
8572 size_t offset)
8573 : codegen_(&codegen),
8574 base_method_address_(base_method_address),
8575 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008576
8577 protected:
8578 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
8579
8580 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008581 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008582
8583 private:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008584 void Process(const MemoryRegion& region, int pos) override {
Mark Mendell0616ae02015-04-17 12:49:27 -04008585 // Patch the correct offset for the instruction. The place to patch is the
8586 // last 4 bytes of the instruction.
8587 // The value to patch is the distance from the offset in the constant area
8588 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04008589 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008590 int32_t relative_position =
8591 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04008592
8593 // Patch in the right value.
8594 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
8595 }
8596
Mark Mendell0616ae02015-04-17 12:49:27 -04008597 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04008598 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008599};
8600
Mark Mendell805b3b52015-09-18 14:10:29 -04008601/**
8602 * Class to handle late fixup of offsets to a jump table that will be created in the
8603 * constant area.
8604 */
8605class JumpTableRIPFixup : public RIPFixup {
8606 public:
8607 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008608 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
8609 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008610
8611 void CreateJumpTable() {
8612 X86Assembler* assembler = codegen_->GetAssembler();
8613
8614 // Ensure that the reference to the jump table has the correct offset.
8615 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
8616 SetOffset(offset_in_constant_table);
8617
8618 // The label values in the jump table are computed relative to the
8619 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008620 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04008621
8622 // Populate the jump table with the correct values for the jump table.
8623 int32_t num_entries = switch_instr_->GetNumEntries();
8624 HBasicBlock* block = switch_instr_->GetBlock();
8625 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
8626 // The value that we want is the target offset - the position of the table.
8627 for (int32_t i = 0; i < num_entries; i++) {
8628 HBasicBlock* b = successors[i];
8629 Label* l = codegen_->GetLabelOf(b);
8630 DCHECK(l->IsBound());
8631 int32_t offset_to_block = l->Position() - relative_offset;
8632 assembler->AppendInt32(offset_to_block);
8633 }
8634 }
8635
8636 private:
8637 const HX86PackedSwitch* switch_instr_;
8638};
8639
8640void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
8641 // Generate the constant area if needed.
8642 X86Assembler* assembler = GetAssembler();
jaishank20d1c942019-03-08 15:08:17 +05308643
Mark Mendell805b3b52015-09-18 14:10:29 -04008644 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
8645 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
8646 // byte values.
8647 assembler->Align(4, 0);
8648 constant_area_start_ = assembler->CodeSize();
8649
8650 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008651 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04008652 jump_table->CreateJumpTable();
8653 }
8654
8655 // And now add the constant area to the generated code.
8656 assembler->AddConstantArea();
8657 }
8658
8659 // And finish up.
8660 CodeGenerator::Finalize(allocator);
8661}
8662
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008663Address CodeGeneratorX86::LiteralDoubleAddress(double v,
8664 HX86ComputeBaseMethodAddress* method_base,
8665 Register reg) {
8666 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008667 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008668 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008669}
8670
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008671Address CodeGeneratorX86::LiteralFloatAddress(float v,
8672 HX86ComputeBaseMethodAddress* method_base,
8673 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008674 AssemblerFixup* fixup =
8675 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008676 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008677}
8678
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008679Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
8680 HX86ComputeBaseMethodAddress* method_base,
8681 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008682 AssemblerFixup* fixup =
8683 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008684 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008685}
8686
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008687Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
8688 HX86ComputeBaseMethodAddress* method_base,
8689 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008690 AssemblerFixup* fixup =
8691 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008692 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008693}
8694
Aart Bika19616e2016-02-01 18:57:58 -08008695void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
8696 if (value == 0) {
8697 __ xorl(dest, dest);
8698 } else {
8699 __ movl(dest, Immediate(value));
8700 }
8701}
8702
8703void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
8704 if (value == 0) {
8705 __ testl(dest, dest);
8706 } else {
8707 __ cmpl(dest, Immediate(value));
8708 }
8709}
8710
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008711void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
8712 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07008713 GenerateIntCompare(lhs_reg, rhs);
8714}
8715
8716void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008717 if (rhs.IsConstant()) {
8718 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07008719 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008720 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07008721 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008722 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07008723 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008724 }
8725}
8726
8727Address CodeGeneratorX86::ArrayAddress(Register obj,
8728 Location index,
8729 ScaleFactor scale,
8730 uint32_t data_offset) {
8731 return index.IsConstant() ?
8732 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
8733 Address(obj, index.AsRegister<Register>(), scale, data_offset);
8734}
8735
Mark Mendell805b3b52015-09-18 14:10:29 -04008736Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
8737 Register reg,
8738 Register value) {
8739 // Create a fixup to be used to create and address the jump table.
8740 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008741 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04008742
8743 // We have to populate the jump tables.
8744 fixups_to_jump_tables_.push_back(table_fixup);
8745
8746 // We want a scaled address, as we are extracting the correct offset from the table.
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008747 return Address(reg, value, TIMES_4, kPlaceholder32BitOffset, table_fixup);
Mark Mendell805b3b52015-09-18 14:10:29 -04008748}
8749
Andreas Gampe85b62f22015-09-09 13:15:38 -07008750// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008751void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07008752 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008753 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008754 return;
8755 }
8756
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008757 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008758
8759 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
8760 if (target.Equals(return_loc)) {
8761 return;
8762 }
8763
8764 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8765 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008766 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008767 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008768 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
8769 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008770 GetMoveResolver()->EmitNativeCode(&parallel_move);
8771 } else {
8772 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01008773 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07008774 parallel_move.AddMove(return_loc, target, type, nullptr);
8775 GetMoveResolver()->EmitNativeCode(&parallel_move);
8776 }
8777}
8778
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008779void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
8780 const uint8_t* roots_data,
8781 const PatchInfo<Label>& info,
8782 uint64_t index_in_table) const {
8783 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
8784 uintptr_t address =
8785 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
Andreas Gampec55bb392018-09-21 00:02:02 +00008786 using unaligned_uint32_t __attribute__((__aligned__(1))) = uint32_t;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008787 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
8788 dchecked_integral_cast<uint32_t>(address);
8789}
8790
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008791void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8792 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008793 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008794 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008795 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008796 }
8797
8798 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008799 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008800 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008801 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008802 }
8803}
8804
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008805void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8806 ATTRIBUTE_UNUSED) {
8807 LOG(FATAL) << "Unreachable";
8808}
8809
8810void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8811 ATTRIBUTE_UNUSED) {
8812 LOG(FATAL) << "Unreachable";
8813}
8814
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05308815bool LocationsBuilderX86::CpuHasAvxFeatureFlag() {
8816 return codegen_->GetInstructionSetFeatures().HasAVX();
8817}
8818bool LocationsBuilderX86::CpuHasAvx2FeatureFlag() {
8819 return codegen_->GetInstructionSetFeatures().HasAVX2();
8820}
8821bool InstructionCodeGeneratorX86::CpuHasAvxFeatureFlag() {
8822 return codegen_->GetInstructionSetFeatures().HasAVX();
8823}
8824bool InstructionCodeGeneratorX86::CpuHasAvx2FeatureFlag() {
8825 return codegen_->GetInstructionSetFeatures().HasAVX2();
8826}
8827
Roland Levillain4d027112015-07-01 15:41:14 +01008828#undef __
8829
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008830} // namespace x86
8831} // namespace art