blob: 04123cf70e1a8086a549194c3b53084ff6fb32db [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Vladimir Marko86c87522020-05-11 16:55:55 +010019#include "arch/x86/jni_frame_x86.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000020#include "art_method-inl.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010021#include "class_table.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010022#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000023#include "compiled_method.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010024#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000025#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010026#include "gc/accounting/card_table.h"
Vladimir Markoeebb8212018-06-05 14:57:24 +010027#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070028#include "heap_poisoning.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040029#include "intrinsics.h"
30#include "intrinsics_x86.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000031#include "jit/profiling_info.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010032#include "linker/linker_patch.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070033#include "lock_word.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070034#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070035#include "mirror/class-inl.h"
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +000036#include "scoped_thread_state_change-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010037#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000038#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010039#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010041#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000042
Vladimir Marko0a516052019-10-14 13:00:44 +000043namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010044
Roland Levillain0d5a2812015-11-13 10:07:31 +000045template<class MirrorType>
46class GcRoot;
47
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000048namespace x86 {
49
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010050static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010051static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050052static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010053
Mark Mendell24f2dfa2015-01-14 19:51:45 -050054static constexpr int kC2ConditionMask = 0x400;
55
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000056static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000057
Aart Bik1f8d51b2018-02-15 10:42:37 -080058static constexpr int64_t kDoubleNaN = INT64_C(0x7FF8000000000000);
59static constexpr int32_t kFloatNaN = INT32_C(0x7FC00000);
60
Vladimir Marko3232dbb2018-07-25 15:42:46 +010061static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
62 InvokeRuntimeCallingConvention calling_convention;
63 RegisterSet caller_saves = RegisterSet::Empty();
64 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
65 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
66 // that the the kPrimNot result register is the same as the first argument register.
67 return caller_saves;
68}
69
Roland Levillain7cbd27f2016-08-11 23:53:33 +010070// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
71#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070072#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86PointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010073
Andreas Gampe85b62f22015-09-09 13:15:38 -070074class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010075 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000076 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010078 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +010079 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010080 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000081 if (instruction_->CanThrowIntoCatchBlock()) {
82 // Live registers will be restored in the catch block if caught.
83 SaveLiveRegisters(codegen, instruction_->GetLocations());
84 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010085 x86_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexandre Rames8158f282015-08-07 10:26:17 +010086 instruction_,
87 instruction_->GetDexPc(),
88 this);
Roland Levillain888d0672015-11-23 18:53:50 +000089 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010090 }
91
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010092 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +010093
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010094 const char* GetDescription() const override { return "NullCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +010095
Nicolas Geoffraye5038322014-07-04 09:41:32 +010096 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010097 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
98};
99
Andreas Gampe85b62f22015-09-09 13:15:38 -0700100class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000101 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000102 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000103
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100104 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +0100105 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +0000106 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100107 x86_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000108 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000109 }
110
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100111 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100112
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100113 const char* GetDescription() const override { return "DivZeroCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100114
Calin Juravled0d48522014-11-04 16:40:20 +0000115 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000116 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
117};
118
Andreas Gampe85b62f22015-09-09 13:15:38 -0700119class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000120 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000121 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
122 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000123
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100124 void EmitNativeCode(CodeGenerator* codegen) override {
Calin Juravled0d48522014-11-04 16:40:20 +0000125 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000126 if (is_div_) {
127 __ negl(reg_);
128 } else {
129 __ movl(reg_, Immediate(0));
130 }
Calin Juravled0d48522014-11-04 16:40:20 +0000131 __ jmp(GetExitLabel());
132 }
133
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100134 const char* GetDescription() const override { return "DivRemMinusOneSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100135
Calin Juravled0d48522014-11-04 16:40:20 +0000136 private:
137 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000138 bool is_div_;
139 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000140};
141
Andreas Gampe85b62f22015-09-09 13:15:38 -0700142class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100143 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000144 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100145
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100146 void EmitNativeCode(CodeGenerator* codegen) override {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100147 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100148 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100149 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000150 // We're moving two locations to locations that could overlap, so we need a parallel
151 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000152 if (instruction_->CanThrowIntoCatchBlock()) {
153 // Live registers will be restored in the catch block if caught.
154 SaveLiveRegisters(codegen, instruction_->GetLocations());
155 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400156
157 // Are we using an array length from memory?
158 HInstruction* array_length = instruction_->InputAt(1);
159 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100160 InvokeRuntimeCallingConvention calling_convention;
Mark Mendellee8d9712016-07-12 11:13:15 -0400161 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
162 // Load the array length into our temporary.
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100163 HArrayLength* length = array_length->AsArrayLength();
164 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(length);
Mark Mendellee8d9712016-07-12 11:13:15 -0400165 Location array_loc = array_length->GetLocations()->InAt(0);
166 Address array_len(array_loc.AsRegister<Register>(), len_offset);
167 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
168 // Check for conflicts with index.
169 if (length_loc.Equals(locations->InAt(0))) {
170 // We know we aren't using parameter 2.
171 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
172 }
173 __ movl(length_loc.AsRegister<Register>(), array_len);
Nicolas Geoffray0aff3a82017-10-13 13:12:36 +0100174 if (mirror::kUseStringCompression && length->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100175 __ shrl(length_loc.AsRegister<Register>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700176 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400177 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000178 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100179 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000180 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100181 DataType::Type::kInt32,
Mark Mendellee8d9712016-07-12 11:13:15 -0400182 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100183 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100184 DataType::Type::kInt32);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100185 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
186 ? kQuickThrowStringBounds
187 : kQuickThrowArrayBounds;
188 x86_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100189 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000190 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100191 }
192
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100193 bool IsFatal() const override { return true; }
Alexandre Rames8158f282015-08-07 10:26:17 +0100194
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100195 const char* GetDescription() const override { return "BoundsCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100196
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100197 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100198 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
199};
200
Andreas Gampe85b62f22015-09-09 13:15:38 -0700201class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000202 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000203 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000204 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000205
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100206 void EmitNativeCode(CodeGenerator* codegen) override {
Aart Bikb13c65b2017-03-21 20:14:07 -0700207 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100208 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000209 __ Bind(GetEntryLabel());
Aart Bik24b905f2017-04-06 09:59:06 -0700210 SaveLiveRegisters(codegen, locations); // Only saves full width XMM for SIMD.
Serban Constantinescuba45db02016-07-12 22:53:02 +0100211 x86_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000212 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Aart Bik24b905f2017-04-06 09:59:06 -0700213 RestoreLiveRegisters(codegen, locations); // Only restores full width XMM for SIMD.
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100214 if (successor_ == nullptr) {
215 __ jmp(GetReturnLabel());
216 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100217 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100218 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000219 }
220
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100221 Label* GetReturnLabel() {
222 DCHECK(successor_ == nullptr);
223 return &return_label_;
224 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000225
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100226 HBasicBlock* GetSuccessor() const {
227 return successor_;
228 }
229
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100230 const char* GetDescription() const override { return "SuspendCheckSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100231
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000232 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100233 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000234 Label return_label_;
235
236 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
237};
238
Vladimir Markoaad75c62016-10-03 08:46:48 +0000239class LoadStringSlowPathX86 : public SlowPathCode {
240 public:
241 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
242
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100243 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Markoaad75c62016-10-03 08:46:48 +0000244 LocationSummary* locations = instruction_->GetLocations();
245 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
246
247 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
248 __ Bind(GetEntryLabel());
249 SaveLiveRegisters(codegen, locations);
250
251 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000252 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
253 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index.index_));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000254 x86_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
255 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
256 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
257 RestoreLiveRegisters(codegen, locations);
258
Vladimir Markoaad75c62016-10-03 08:46:48 +0000259 __ jmp(GetExitLabel());
260 }
261
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100262 const char* GetDescription() const override { return "LoadStringSlowPathX86"; }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000263
264 private:
265 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
266};
267
Andreas Gampe85b62f22015-09-09 13:15:38 -0700268class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000269 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100270 LoadClassSlowPathX86(HLoadClass* cls, HInstruction* at)
271 : SlowPathCode(at), cls_(cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000272 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100273 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000274 }
275
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100276 void EmitNativeCode(CodeGenerator* codegen) override {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000277 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100278 Location out = locations->Out();
279 const uint32_t dex_pc = instruction_->GetDexPc();
280 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
281 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
282
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000283 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
284 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000285 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000286
287 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100288 if (must_resolve_type) {
289 DCHECK(IsSameDexFile(cls_->GetDexFile(), x86_codegen->GetGraph()->GetDexFile()));
290 dex::TypeIndex type_index = cls_->GetTypeIndex();
291 __ movl(calling_convention.GetRegisterAt(0), Immediate(type_index.index_));
Vladimir Marko9d479252018-07-24 11:35:20 +0100292 x86_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
293 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100294 // If we also must_do_clinit, the resolved type is now in the correct register.
295 } else {
296 DCHECK(must_do_clinit);
297 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
298 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), source);
299 }
300 if (must_do_clinit) {
301 x86_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
302 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Roland Levillain888d0672015-11-23 18:53:50 +0000303 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000304
305 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000306 if (out.IsValid()) {
307 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
308 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000309 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000310 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000311 __ jmp(GetExitLabel());
312 }
313
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100314 const char* GetDescription() const override { return "LoadClassSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100315
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000316 private:
317 // The class this slow path will load.
318 HLoadClass* const cls_;
319
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000320 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
321};
322
Andreas Gampe85b62f22015-09-09 13:15:38 -0700323class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000325 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000326 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000327
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100328 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000329 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000330 DCHECK(instruction_->IsCheckCast()
331 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000332
333 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
334 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000335
Vladimir Markoe619f6c2017-12-12 16:00:01 +0000336 if (kPoisonHeapReferences &&
337 instruction_->IsCheckCast() &&
338 instruction_->AsCheckCast()->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) {
339 // First, unpoison the `cls` reference that was poisoned for direct memory comparison.
340 __ UnpoisonHeapReference(locations->InAt(1).AsRegister<Register>());
341 }
342
Vladimir Marko87584542017-12-12 17:47:52 +0000343 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000344 SaveLiveRegisters(codegen, locations);
345 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000346
347 // We're moving two locations to locations that could overlap, so we need a parallel
348 // move resolver.
349 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800350 x86_codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800351 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100352 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800353 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800354 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100355 DataType::Type::kReference);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000356 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100357 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100358 instruction_,
359 instruction_->GetDexPc(),
360 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800361 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000362 } else {
363 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800364 x86_codegen->InvokeRuntime(kQuickCheckInstanceOf,
365 instruction_,
366 instruction_->GetDexPc(),
367 this);
368 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000369 }
370
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000371 if (!is_fatal_) {
372 if (instruction_->IsInstanceOf()) {
373 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
374 }
375 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000376
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000377 __ jmp(GetExitLabel());
378 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000379 }
380
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100381 const char* GetDescription() const override { return "TypeCheckSlowPathX86"; }
382 bool IsFatal() const override { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100383
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000384 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000385 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000386
387 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
388};
389
Andreas Gampe85b62f22015-09-09 13:15:38 -0700390class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700391 public:
Aart Bik42249c32016-01-07 15:33:50 -0800392 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000393 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700394
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100395 void EmitNativeCode(CodeGenerator* codegen) override {
Alexandre Rames8158f282015-08-07 10:26:17 +0100396 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700397 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100398 LocationSummary* locations = instruction_->GetLocations();
399 SaveLiveRegisters(codegen, locations);
400 InvokeRuntimeCallingConvention calling_convention;
401 x86_codegen->Load32BitValue(
402 calling_convention.GetRegisterAt(0),
403 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100404 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100405 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700406 }
407
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100408 const char* GetDescription() const override { return "DeoptimizationSlowPathX86"; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100409
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700410 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700411 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
412};
413
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100414class ArraySetSlowPathX86 : public SlowPathCode {
415 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000416 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100417
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100418 void EmitNativeCode(CodeGenerator* codegen) override {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100419 LocationSummary* locations = instruction_->GetLocations();
420 __ Bind(GetEntryLabel());
421 SaveLiveRegisters(codegen, locations);
422
423 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100424 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100425 parallel_move.AddMove(
426 locations->InAt(0),
427 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100428 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100429 nullptr);
430 parallel_move.AddMove(
431 locations->InAt(1),
432 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100433 DataType::Type::kInt32,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100434 nullptr);
435 parallel_move.AddMove(
436 locations->InAt(2),
437 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100438 DataType::Type::kReference,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100439 nullptr);
440 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
441
442 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100443 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000444 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100445 RestoreLiveRegisters(codegen, locations);
446 __ jmp(GetExitLabel());
447 }
448
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100449 const char* GetDescription() const override { return "ArraySetSlowPathX86"; }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100450
451 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100452 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
453};
454
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100455// Slow path marking an object reference `ref` during a read
456// barrier. The field `obj.field` in the object `obj` holding this
457// reference does not get updated by this slow path after marking (see
458// ReadBarrierMarkAndUpdateFieldSlowPathX86 below for that).
459//
460// This means that after the execution of this slow path, `ref` will
461// always be up-to-date, but `obj.field` may not; i.e., after the
462// flip, `ref` will be a to-space reference, but `obj.field` will
463// probably still be a from-space reference (unless it gets updated by
464// another thread, or if another thread installed another object
465// reference (different from `ref`) in `obj.field`).
Roland Levillain7c1559a2015-12-15 10:55:36 +0000466class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
467 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100468 ReadBarrierMarkSlowPathX86(HInstruction* instruction,
469 Location ref,
470 bool unpoison_ref_before_marking)
471 : SlowPathCode(instruction),
472 ref_(ref),
473 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000474 DCHECK(kEmitCompilerReadBarrier);
475 }
476
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100477 const char* GetDescription() const override { return "ReadBarrierMarkSlowPathX86"; }
Roland Levillain7c1559a2015-12-15 10:55:36 +0000478
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100479 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000480 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100481 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000482 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100483 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000484 DCHECK(instruction_->IsInstanceFieldGet() ||
485 instruction_->IsStaticFieldGet() ||
486 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100487 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000488 instruction_->IsLoadClass() ||
489 instruction_->IsLoadString() ||
490 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100491 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100492 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
493 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000494 << "Unexpected instruction in read barrier marking slow path: "
495 << instruction_->DebugName();
496
497 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100498 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000499 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100500 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000501 }
Roland Levillain4359e612016-07-20 11:32:19 +0100502 // No need to save live registers; it's taken care of by the
503 // entrypoint. Also, there is no need to update the stack mask,
504 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000505 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100506 DCHECK_NE(ref_reg, ESP);
507 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100508 // "Compact" slow path, saving two moves.
509 //
510 // Instead of using the standard runtime calling convention (input
511 // and output in EAX):
512 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100513 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100514 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100515 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100516 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100517 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100518 // of a dedicated entrypoint:
519 //
520 // rX <- ReadBarrierMarkRegX(rX)
521 //
Roland Levillain97c46462017-05-11 14:04:03 +0100522 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100523 // This runtime call does not require a stack map.
524 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000525 __ jmp(GetExitLabel());
526 }
527
528 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100529 // The location (register) of the marked object reference.
530 const Location ref_;
531 // Should the reference in `ref_` be unpoisoned prior to marking it?
532 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000533
534 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
535};
536
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100537// Slow path marking an object reference `ref` during a read barrier,
538// and if needed, atomically updating the field `obj.field` in the
539// object `obj` holding this reference after marking (contrary to
540// ReadBarrierMarkSlowPathX86 above, which never tries to update
541// `obj.field`).
542//
543// This means that after the execution of this slow path, both `ref`
544// and `obj.field` will be up-to-date; i.e., after the flip, both will
545// hold the same to-space reference (unless another thread installed
546// another object reference (different from `ref`) in `obj.field`).
547class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
548 public:
549 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
550 Location ref,
551 Register obj,
552 const Address& field_addr,
553 bool unpoison_ref_before_marking,
554 Register temp)
555 : SlowPathCode(instruction),
556 ref_(ref),
557 obj_(obj),
558 field_addr_(field_addr),
559 unpoison_ref_before_marking_(unpoison_ref_before_marking),
560 temp_(temp) {
561 DCHECK(kEmitCompilerReadBarrier);
562 }
563
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100564 const char* GetDescription() const override { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100565
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100566 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100567 LocationSummary* locations = instruction_->GetLocations();
568 Register ref_reg = ref_.AsRegister<Register>();
569 DCHECK(locations->CanCall());
570 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
571 // This slow path is only used by the UnsafeCASObject intrinsic.
572 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
573 << "Unexpected instruction in read barrier marking and field updating slow path: "
574 << instruction_->DebugName();
575 DCHECK(instruction_->GetLocations()->Intrinsified());
576 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
577
578 __ Bind(GetEntryLabel());
579 if (unpoison_ref_before_marking_) {
580 // Object* ref = ref_addr->AsMirrorPtr()
581 __ MaybeUnpoisonHeapReference(ref_reg);
582 }
583
584 // Save the old (unpoisoned) reference.
585 __ movl(temp_, ref_reg);
586
587 // No need to save live registers; it's taken care of by the
588 // entrypoint. Also, there is no need to update the stack mask,
589 // as this runtime call will not trigger a garbage collection.
590 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
591 DCHECK_NE(ref_reg, ESP);
592 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
593 // "Compact" slow path, saving two moves.
594 //
595 // Instead of using the standard runtime calling convention (input
596 // and output in EAX):
597 //
598 // EAX <- ref
599 // EAX <- ReadBarrierMark(EAX)
600 // ref <- EAX
601 //
602 // we just use rX (the register containing `ref`) as input and output
603 // of a dedicated entrypoint:
604 //
605 // rX <- ReadBarrierMarkRegX(rX)
606 //
Roland Levillain97c46462017-05-11 14:04:03 +0100607 int32_t entry_point_offset = Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100608 // This runtime call does not require a stack map.
609 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
610
611 // If the new reference is different from the old reference,
612 // update the field in the holder (`*field_addr`).
613 //
614 // Note that this field could also hold a different object, if
615 // another thread had concurrently changed it. In that case, the
616 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
617 // operation below would abort the CAS, leaving the field as-is.
618 NearLabel done;
619 __ cmpl(temp_, ref_reg);
620 __ j(kEqual, &done);
621
622 // Update the the holder's field atomically. This may fail if
623 // mutator updates before us, but it's OK. This is achieved
624 // using a strong compare-and-set (CAS) operation with relaxed
625 // memory synchronization ordering, where the expected value is
626 // the old reference and the desired value is the new reference.
627 // This operation is implemented with a 32-bit LOCK CMPXLCHG
628 // instruction, which requires the expected value (the old
629 // reference) to be in EAX. Save EAX beforehand, and move the
630 // expected value (stored in `temp_`) into EAX.
631 __ pushl(EAX);
632 __ movl(EAX, temp_);
633
634 // Convenience aliases.
635 Register base = obj_;
636 Register expected = EAX;
637 Register value = ref_reg;
638
639 bool base_equals_value = (base == value);
640 if (kPoisonHeapReferences) {
641 if (base_equals_value) {
642 // If `base` and `value` are the same register location, move
643 // `value` to a temporary register. This way, poisoning
644 // `value` won't invalidate `base`.
645 value = temp_;
646 __ movl(value, base);
647 }
648
649 // Check that the register allocator did not assign the location
650 // of `expected` (EAX) to `value` nor to `base`, so that heap
651 // poisoning (when enabled) works as intended below.
652 // - If `value` were equal to `expected`, both references would
653 // be poisoned twice, meaning they would not be poisoned at
654 // all, as heap poisoning uses address negation.
655 // - If `base` were equal to `expected`, poisoning `expected`
656 // would invalidate `base`.
657 DCHECK_NE(value, expected);
658 DCHECK_NE(base, expected);
659
660 __ PoisonHeapReference(expected);
661 __ PoisonHeapReference(value);
662 }
663
664 __ LockCmpxchgl(field_addr_, value);
665
666 // If heap poisoning is enabled, we need to unpoison the values
667 // that were poisoned earlier.
668 if (kPoisonHeapReferences) {
669 if (base_equals_value) {
670 // `value` has been moved to a temporary register, no need
671 // to unpoison it.
672 } else {
673 __ UnpoisonHeapReference(value);
674 }
675 // No need to unpoison `expected` (EAX), as it is be overwritten below.
676 }
677
678 // Restore EAX.
679 __ popl(EAX);
680
681 __ Bind(&done);
682 __ jmp(GetExitLabel());
683 }
684
685 private:
686 // The location (register) of the marked object reference.
687 const Location ref_;
688 // The register containing the object holding the marked object reference field.
689 const Register obj_;
690 // The address of the marked reference field. The base of this address must be `obj_`.
691 const Address field_addr_;
692
693 // Should the reference in `ref_` be unpoisoned prior to marking it?
694 const bool unpoison_ref_before_marking_;
695
696 const Register temp_;
697
698 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
699};
700
Roland Levillain0d5a2812015-11-13 10:07:31 +0000701// Slow path generating a read barrier for a heap reference.
702class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
703 public:
704 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
705 Location out,
706 Location ref,
707 Location obj,
708 uint32_t offset,
709 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000710 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000711 out_(out),
712 ref_(ref),
713 obj_(obj),
714 offset_(offset),
715 index_(index) {
716 DCHECK(kEmitCompilerReadBarrier);
717 // If `obj` is equal to `out` or `ref`, it means the initial object
718 // has been overwritten by (or after) the heap object reference load
719 // to be instrumented, e.g.:
720 //
721 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000722 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000723 //
724 // In that case, we have lost the information about the original
725 // object, and the emitted read barrier cannot work properly.
726 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
727 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
728 }
729
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100730 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000731 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
732 LocationSummary* locations = instruction_->GetLocations();
733 Register reg_out = out_.AsRegister<Register>();
734 DCHECK(locations->CanCall());
735 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100736 DCHECK(instruction_->IsInstanceFieldGet() ||
737 instruction_->IsStaticFieldGet() ||
738 instruction_->IsArrayGet() ||
739 instruction_->IsInstanceOf() ||
740 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -0700741 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000742 << "Unexpected instruction in read barrier for heap reference slow path: "
743 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000744
745 __ Bind(GetEntryLabel());
746 SaveLiveRegisters(codegen, locations);
747
748 // We may have to change the index's value, but as `index_` is a
749 // constant member (like other "inputs" of this slow path),
750 // introduce a copy of it, `index`.
751 Location index = index_;
752 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100753 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000754 if (instruction_->IsArrayGet()) {
755 // Compute the actual memory offset and store it in `index`.
756 Register index_reg = index_.AsRegister<Register>();
757 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
758 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
759 // We are about to change the value of `index_reg` (see the
760 // calls to art::x86::X86Assembler::shll and
761 // art::x86::X86Assembler::AddImmediate below), but it has
762 // not been saved by the previous call to
763 // art::SlowPathCode::SaveLiveRegisters, as it is a
764 // callee-save register --
765 // art::SlowPathCode::SaveLiveRegisters does not consider
766 // callee-save registers, as it has been designed with the
767 // assumption that callee-save registers are supposed to be
768 // handled by the called function. So, as a callee-save
769 // register, `index_reg` _would_ eventually be saved onto
770 // the stack, but it would be too late: we would have
771 // changed its value earlier. Therefore, we manually save
772 // it here into another freely available register,
773 // `free_reg`, chosen of course among the caller-save
774 // registers (as a callee-save `free_reg` register would
775 // exhibit the same problem).
776 //
777 // Note we could have requested a temporary register from
778 // the register allocator instead; but we prefer not to, as
779 // this is a slow path, and we know we can find a
780 // caller-save register that is available.
781 Register free_reg = FindAvailableCallerSaveRegister(codegen);
782 __ movl(free_reg, index_reg);
783 index_reg = free_reg;
784 index = Location::RegisterLocation(index_reg);
785 } else {
786 // The initial register stored in `index_` has already been
787 // saved in the call to art::SlowPathCode::SaveLiveRegisters
788 // (as it is not a callee-save register), so we can freely
789 // use it.
790 }
791 // Shifting the index value contained in `index_reg` by the scale
792 // factor (2) cannot overflow in practice, as the runtime is
793 // unable to allocate object arrays with a size larger than
794 // 2^26 - 1 (that is, 2^28 - 4 bytes).
795 __ shll(index_reg, Immediate(TIMES_4));
796 static_assert(
797 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
798 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
799 __ AddImmediate(index_reg, Immediate(offset_));
800 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100801 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
802 // intrinsics, `index_` is not shifted by a scale factor of 2
803 // (as in the case of ArrayGet), as it is actually an offset
804 // to an object field within an object.
805 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000806 DCHECK(instruction_->GetLocations()->Intrinsified());
807 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
808 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
809 << instruction_->AsInvoke()->GetIntrinsic();
810 DCHECK_EQ(offset_, 0U);
811 DCHECK(index_.IsRegisterPair());
812 // UnsafeGet's offset location is a register pair, the low
813 // part contains the correct offset.
814 index = index_.ToLow();
815 }
816 }
817
818 // We're moving two or three locations to locations that could
819 // overlap, so we need a parallel move resolver.
820 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100821 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Roland Levillain0d5a2812015-11-13 10:07:31 +0000822 parallel_move.AddMove(ref_,
823 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100824 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000825 nullptr);
826 parallel_move.AddMove(obj_,
827 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100828 DataType::Type::kReference,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000829 nullptr);
830 if (index.IsValid()) {
831 parallel_move.AddMove(index,
832 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100833 DataType::Type::kInt32,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000834 nullptr);
835 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
836 } else {
837 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
838 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
839 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100840 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000841 CheckEntrypointTypes<
842 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
843 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
844
845 RestoreLiveRegisters(codegen, locations);
846 __ jmp(GetExitLabel());
847 }
848
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100849 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathX86"; }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000850
851 private:
852 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
853 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
854 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
855 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
856 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
857 return static_cast<Register>(i);
858 }
859 }
860 // We shall never fail to find a free caller-save register, as
861 // there are more than two core caller-save registers on x86
862 // (meaning it is possible to find one which is different from
863 // `ref` and `obj`).
864 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
865 LOG(FATAL) << "Could not find a free caller-save register";
866 UNREACHABLE();
867 }
868
Roland Levillain0d5a2812015-11-13 10:07:31 +0000869 const Location out_;
870 const Location ref_;
871 const Location obj_;
872 const uint32_t offset_;
873 // An additional location containing an index to an array.
874 // Only used for HArrayGet and the UnsafeGetObject &
875 // UnsafeGetObjectVolatile intrinsics.
876 const Location index_;
877
878 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
879};
880
881// Slow path generating a read barrier for a GC root.
882class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
883 public:
884 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000885 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000886 DCHECK(kEmitCompilerReadBarrier);
887 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000888
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100889 void EmitNativeCode(CodeGenerator* codegen) override {
Roland Levillain0d5a2812015-11-13 10:07:31 +0000890 LocationSummary* locations = instruction_->GetLocations();
891 Register reg_out = out_.AsRegister<Register>();
892 DCHECK(locations->CanCall());
893 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000894 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
895 << "Unexpected instruction in read barrier for GC root slow path: "
896 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000897
898 __ Bind(GetEntryLabel());
899 SaveLiveRegisters(codegen, locations);
900
901 InvokeRuntimeCallingConvention calling_convention;
902 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
903 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100904 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000905 instruction_,
906 instruction_->GetDexPc(),
907 this);
908 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
909 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
910
911 RestoreLiveRegisters(codegen, locations);
912 __ jmp(GetExitLabel());
913 }
914
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100915 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathX86"; }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000916
917 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000918 const Location out_;
919 const Location root_;
920
921 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
922};
923
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100924#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100925// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
926#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100927
Aart Bike9f37602015-10-09 11:15:55 -0700928inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700929 switch (cond) {
930 case kCondEQ: return kEqual;
931 case kCondNE: return kNotEqual;
932 case kCondLT: return kLess;
933 case kCondLE: return kLessEqual;
934 case kCondGT: return kGreater;
935 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700936 case kCondB: return kBelow;
937 case kCondBE: return kBelowEqual;
938 case kCondA: return kAbove;
939 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700940 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100941 LOG(FATAL) << "Unreachable";
942 UNREACHABLE();
943}
944
Aart Bike9f37602015-10-09 11:15:55 -0700945// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100946inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
947 switch (cond) {
948 case kCondEQ: return kEqual;
949 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700950 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100951 case kCondLT: return kBelow;
952 case kCondLE: return kBelowEqual;
953 case kCondGT: return kAbove;
954 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700955 // Unsigned remain unchanged.
956 case kCondB: return kBelow;
957 case kCondBE: return kBelowEqual;
958 case kCondA: return kAbove;
959 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100960 }
961 LOG(FATAL) << "Unreachable";
962 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700963}
964
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100965void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100966 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100967}
968
969void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100970 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100971}
972
Vladimir Markoa0431112018-06-25 09:32:54 +0100973const X86InstructionSetFeatures& CodeGeneratorX86::GetInstructionSetFeatures() const {
974 return *GetCompilerOptions().GetInstructionSetFeatures()->AsX86InstructionSetFeatures();
975}
976
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100977size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
978 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
979 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100980}
981
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100982size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
983 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
984 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100985}
986
Mark Mendell7c8d0092015-01-26 11:21:33 -0500987size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700988 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700989 __ movups(Address(ESP, stack_index), XmmRegister(reg_id));
Aart Bikb13c65b2017-03-21 20:14:07 -0700990 } else {
991 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
992 }
Artem Serov6a0b6572019-07-26 20:38:37 +0100993 return GetSlowPathFPWidth();
Mark Mendell7c8d0092015-01-26 11:21:33 -0500994}
995
996size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Aart Bikb13c65b2017-03-21 20:14:07 -0700997 if (GetGraph()->HasSIMD()) {
Aart Bik5576f372017-03-23 16:17:37 -0700998 __ movups(XmmRegister(reg_id), Address(ESP, stack_index));
Aart Bikb13c65b2017-03-21 20:14:07 -0700999 } else {
1000 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
1001 }
Artem Serov6a0b6572019-07-26 20:38:37 +01001002 return GetSlowPathFPWidth();
Mark Mendell7c8d0092015-01-26 11:21:33 -05001003}
1004
Calin Juravle175dc732015-08-25 15:42:32 +01001005void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
1006 HInstruction* instruction,
1007 uint32_t dex_pc,
1008 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001009 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001010 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
1011 if (EntrypointRequiresStackMap(entrypoint)) {
1012 RecordPcInfo(instruction, dex_pc, slow_path);
1013 }
Alexandre Rames8158f282015-08-07 10:26:17 +01001014}
1015
Roland Levillaindec8f632016-07-22 17:10:06 +01001016void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1017 HInstruction* instruction,
1018 SlowPathCode* slow_path) {
1019 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +01001020 GenerateInvokeRuntime(entry_point_offset);
1021}
1022
1023void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001024 __ fs()->call(Address::Absolute(entry_point_offset));
1025}
1026
Mark Mendellfb8d2792015-03-31 22:16:59 -04001027CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +00001028 const CompilerOptions& compiler_options,
1029 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -05001030 : CodeGenerator(graph,
1031 kNumberOfCpuRegisters,
1032 kNumberOfXmmRegisters,
1033 kNumberOfRegisterPairs,
1034 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1035 arraysize(kCoreCalleeSaves))
1036 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001037 0,
1038 compiler_options,
1039 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001040 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001041 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001042 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001043 move_resolver_(graph->GetAllocator(), this),
1044 assembler_(graph->GetAllocator()),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001045 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1046 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1047 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1048 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001049 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001050 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko2d06e022019-07-08 15:45:19 +01001051 boot_image_other_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001052 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1053 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001054 constant_area_start_(-1),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001055 fixups_to_jump_tables_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001056 method_address_offset_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001057 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001058 // Use a fake return address register to mimic Quick.
1059 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001060}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001061
David Brazdil58282f42016-01-14 12:45:10 +00001062void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001063 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001064 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001065}
1066
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001067InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001068 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001069 assembler_(codegen->GetAssembler()),
1070 codegen_(codegen) {}
1071
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001072static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001073 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001074}
1075
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001076void CodeGeneratorX86::MaybeIncrementHotness(bool is_frame_entry) {
1077 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
1078 Register reg = EAX;
1079 if (is_frame_entry) {
1080 reg = kMethodRegisterArgument;
1081 } else {
1082 __ pushl(EAX);
Vladimir Markodec78172020-06-19 15:31:23 +01001083 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001084 __ movl(EAX, Address(ESP, kX86WordSize));
1085 }
1086 NearLabel overflow;
1087 __ cmpw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()),
1088 Immediate(ArtMethod::MaxCounter()));
1089 __ j(kEqual, &overflow);
1090 __ addw(Address(reg, ArtMethod::HotnessCountOffset().Int32Value()),
1091 Immediate(1));
1092 __ Bind(&overflow);
1093 if (!is_frame_entry) {
1094 __ popl(EAX);
Vladimir Markodec78172020-06-19 15:31:23 +01001095 __ cfi().AdjustCFAOffset(-4);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001096 }
1097 }
1098
1099 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
1100 ScopedObjectAccess soa(Thread::Current());
1101 ProfilingInfo* info = GetGraph()->GetArtMethod()->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001102 if (info != nullptr) {
1103 uint32_t address = reinterpret_cast32<uint32_t>(info);
1104 NearLabel done;
1105 if (HasEmptyFrame()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001106 CHECK(is_frame_entry);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001107 // Alignment
Vladimir Markodec78172020-06-19 15:31:23 +01001108 IncreaseFrame(8);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001109 // We need a temporary. The stub also expects the method at bottom of stack.
1110 __ pushl(EAX);
1111 __ cfi().AdjustCFAOffset(4);
1112 __ movl(EAX, Immediate(address));
1113 __ addw(Address(EAX, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()),
1114 Immediate(1));
1115 __ j(kCarryClear, &done);
1116 GenerateInvokeRuntime(
1117 GetThreadOffset<kX86PointerSize>(kQuickCompileOptimized).Int32Value());
1118 __ Bind(&done);
1119 // We don't strictly require to restore EAX, but this makes the generated
1120 // code easier to reason about.
1121 __ popl(EAX);
1122 __ cfi().AdjustCFAOffset(-4);
Vladimir Markodec78172020-06-19 15:31:23 +01001123 DecreaseFrame(8);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00001124 } else {
1125 if (!RequiresCurrentMethod()) {
1126 CHECK(is_frame_entry);
1127 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1128 }
1129 // We need a temporary.
1130 __ pushl(EAX);
1131 __ cfi().AdjustCFAOffset(4);
1132 __ movl(EAX, Immediate(address));
1133 __ addw(Address(EAX, ProfilingInfo::BaselineHotnessCountOffset().Int32Value()),
1134 Immediate(1));
1135 __ popl(EAX); // Put stack as expected before exiting or calling stub.
1136 __ cfi().AdjustCFAOffset(-4);
1137 __ j(kCarryClear, &done);
1138 GenerateInvokeRuntime(
1139 GetThreadOffset<kX86PointerSize>(kQuickCompileOptimized).Int32Value());
1140 __ Bind(&done);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001141 }
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001142 }
1143 }
1144}
1145
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001146void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001147 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001148 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001149 bool skip_overflow_check =
1150 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001151 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001152
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001153 if (!skip_overflow_check) {
Vladimir Marko33bff252017-11-01 14:35:42 +00001154 size_t reserved_bytes = GetStackOverflowReservedBytes(InstructionSet::kX86);
1155 __ testl(EAX, Address(ESP, -static_cast<int32_t>(reserved_bytes)));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001156 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001157 }
1158
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001159 if (!HasEmptyFrame()) {
1160 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1161 Register reg = kCoreCalleeSaves[i];
1162 if (allocated_registers_.ContainsCoreRegister(reg)) {
1163 __ pushl(reg);
1164 __ cfi().AdjustCFAOffset(kX86WordSize);
1165 __ cfi().RelOffset(DWARFReg(reg), 0);
1166 }
1167 }
Mark Mendell5f874182015-03-04 15:42:45 -05001168
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001169 int adjust = GetFrameSize() - FrameEntrySpillSize();
Vladimir Markodec78172020-06-19 15:31:23 +01001170 IncreaseFrame(adjust);
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001171 // Save the current method if we need it. Note that we do not
1172 // do this in HCurrentMethod, as the instruction might have been removed
1173 // in the SSA graph.
1174 if (RequiresCurrentMethod()) {
1175 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1176 }
1177
1178 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1179 // Initialize should_deoptimize flag to 0.
1180 __ movl(Address(ESP, GetStackOffsetOfShouldDeoptimizeFlag()), Immediate(0));
Mark Mendell5f874182015-03-04 15:42:45 -05001181 }
1182 }
1183
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001184 MaybeIncrementHotness(/* is_frame_entry= */ true);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001185}
1186
1187void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001188 __ cfi().RememberState();
1189 if (!HasEmptyFrame()) {
1190 int adjust = GetFrameSize() - FrameEntrySpillSize();
Vladimir Markodec78172020-06-19 15:31:23 +01001191 DecreaseFrame(adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001192
David Srbeckyc34dc932015-04-12 09:27:43 +01001193 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1194 Register reg = kCoreCalleeSaves[i];
1195 if (allocated_registers_.ContainsCoreRegister(reg)) {
1196 __ popl(reg);
1197 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1198 __ cfi().Restore(DWARFReg(reg));
1199 }
Mark Mendell5f874182015-03-04 15:42:45 -05001200 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001201 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001202 __ ret();
1203 __ cfi().RestoreState();
1204 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001205}
1206
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001207void CodeGeneratorX86::Bind(HBasicBlock* block) {
1208 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001209}
1210
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001211Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001212 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001213 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001214 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001215 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001216 case DataType::Type::kInt8:
1217 case DataType::Type::kUint16:
1218 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -08001219 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001220 case DataType::Type::kInt32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001221 return Location::RegisterLocation(EAX);
1222
Aart Bik66c158e2018-01-31 12:55:04 -08001223 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001224 case DataType::Type::kInt64:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001225 return Location::RegisterPairLocation(EAX, EDX);
1226
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001227 case DataType::Type::kVoid:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001228 return Location::NoLocation();
1229
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001230 case DataType::Type::kFloat64:
1231 case DataType::Type::kFloat32:
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001232 return Location::FpuRegisterLocation(XMM0);
1233 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001234
1235 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001236}
1237
1238Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1239 return Location::RegisterLocation(kMethodRegisterArgument);
1240}
1241
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001242Location InvokeDexCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001243 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001244 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001245 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001246 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001247 case DataType::Type::kInt8:
1248 case DataType::Type::kUint16:
1249 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001250 case DataType::Type::kInt32: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001251 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001252 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001253 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001254 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001255 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001256 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001257 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001258 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001259
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001260 case DataType::Type::kInt64: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001261 uint32_t index = gp_index_;
1262 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001263 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001264 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001265 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1266 calling_convention.GetRegisterPairAt(index));
1267 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001268 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001269 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1270 }
1271 }
1272
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001273 case DataType::Type::kFloat32: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001274 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001275 stack_index_++;
1276 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1277 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1278 } else {
1279 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1280 }
1281 }
1282
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001283 case DataType::Type::kFloat64: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001284 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001285 stack_index_ += 2;
1286 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1287 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1288 } else {
1289 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001290 }
1291 }
1292
Aart Bik66c158e2018-01-31 12:55:04 -08001293 case DataType::Type::kUint32:
1294 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001295 case DataType::Type::kVoid:
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001296 LOG(FATAL) << "Unexpected parameter type " << type;
Elliott Hughesc1896c92018-11-29 11:33:18 -08001297 UNREACHABLE();
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001298 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001299 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001300}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001301
Vladimir Marko86c87522020-05-11 16:55:55 +01001302Location CriticalNativeCallingConventionVisitorX86::GetNextLocation(DataType::Type type) {
1303 DCHECK_NE(type, DataType::Type::kReference);
1304
1305 Location location;
1306 if (DataType::Is64BitType(type)) {
1307 location = Location::DoubleStackSlot(stack_offset_);
1308 stack_offset_ += 2 * kFramePointerSize;
1309 } else {
1310 location = Location::StackSlot(stack_offset_);
1311 stack_offset_ += kFramePointerSize;
1312 }
1313 if (for_register_allocation_) {
1314 location = Location::Any();
1315 }
1316 return location;
1317}
1318
1319Location CriticalNativeCallingConventionVisitorX86::GetReturnLocation(DataType::Type type) const {
1320 // We perform conversion to the managed ABI return register after the call if needed.
1321 InvokeDexCallingConventionVisitorX86 dex_calling_convention;
1322 return dex_calling_convention.GetReturnLocation(type);
1323}
1324
1325Location CriticalNativeCallingConventionVisitorX86::GetMethodLocation() const {
1326 // Pass the method in the hidden argument EAX.
1327 return Location::RegisterLocation(EAX);
1328}
1329
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001330void CodeGeneratorX86::Move32(Location destination, Location source) {
1331 if (source.Equals(destination)) {
1332 return;
1333 }
1334 if (destination.IsRegister()) {
1335 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001336 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001337 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001338 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001339 } else {
1340 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001341 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001342 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001343 } else if (destination.IsFpuRegister()) {
1344 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001345 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001346 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001347 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001348 } else {
1349 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001350 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001351 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001352 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001353 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001354 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001355 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001356 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001357 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001358 } else if (source.IsConstant()) {
1359 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001360 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001361 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001362 } else {
1363 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001364 __ pushl(Address(ESP, source.GetStackIndex()));
1365 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001366 }
1367 }
1368}
1369
1370void CodeGeneratorX86::Move64(Location destination, Location source) {
1371 if (source.Equals(destination)) {
1372 return;
1373 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001374 if (destination.IsRegisterPair()) {
1375 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001376 EmitParallelMoves(
1377 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1378 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001379 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001380 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001381 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001382 DataType::Type::kInt32);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001383 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001384 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1385 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1386 __ psrlq(src_reg, Immediate(32));
1387 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001388 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001389 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001390 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001391 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1392 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001393 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1394 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001395 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001396 if (source.IsFpuRegister()) {
1397 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1398 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001399 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001400 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001401 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Vladimir Markodec78172020-06-19 15:31:23 +01001402 // Push the 2 source registers to the stack.
1403 __ pushl(source.AsRegisterPairHigh<Register>());
1404 __ cfi().AdjustCFAOffset(elem_size);
1405 __ pushl(source.AsRegisterPairLow<Register>());
1406 __ cfi().AdjustCFAOffset(elem_size);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001407 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1408 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01001409 DecreaseFrame(2 * elem_size);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001410 } else {
1411 LOG(FATAL) << "Unimplemented";
1412 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001413 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001414 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001415 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001416 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001417 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001418 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001419 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001420 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001421 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001422 } else if (source.IsConstant()) {
1423 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001424 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1425 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001426 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001427 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1428 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001429 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001430 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001431 EmitParallelMoves(
1432 Location::StackSlot(source.GetStackIndex()),
1433 Location::StackSlot(destination.GetStackIndex()),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001434 DataType::Type::kInt32,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001435 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001436 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001437 DataType::Type::kInt32);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001438 }
1439 }
1440}
1441
Calin Juravle175dc732015-08-25 15:42:32 +01001442void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1443 DCHECK(location.IsRegister());
1444 __ movl(location.AsRegister<Register>(), Immediate(value));
1445}
1446
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001447void CodeGeneratorX86::MoveLocation(Location dst, Location src, DataType::Type dst_type) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001448 HParallelMove move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001449 if (dst_type == DataType::Type::kInt64 && !src.IsConstant() && !src.IsFpuRegister()) {
1450 move.AddMove(src.ToLow(), dst.ToLow(), DataType::Type::kInt32, nullptr);
1451 move.AddMove(src.ToHigh(), dst.ToHigh(), DataType::Type::kInt32, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001452 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001453 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001454 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001455 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001456}
1457
1458void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1459 if (location.IsRegister()) {
1460 locations->AddTemp(location);
1461 } else if (location.IsRegisterPair()) {
1462 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1463 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1464 } else {
1465 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1466 }
1467}
1468
David Brazdilfc6a86a2015-06-26 10:33:45 +00001469void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08001470 if (successor->IsExitBlock()) {
1471 DCHECK(got->GetPrevious()->AlwaysThrows());
1472 return; // no code needed
1473 }
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001474
1475 HBasicBlock* block = got->GetBlock();
1476 HInstruction* previous = got->GetPrevious();
1477
1478 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001479 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffraya59af8a2019-11-27 17:42:32 +00001480 codegen_->MaybeIncrementHotness(/* is_frame_entry= */ false);
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001481 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1482 return;
1483 }
1484
1485 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1486 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1487 }
1488 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001489 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001490 }
1491}
1492
David Brazdilfc6a86a2015-06-26 10:33:45 +00001493void LocationsBuilderX86::VisitGoto(HGoto* got) {
1494 got->SetLocations(nullptr);
1495}
1496
1497void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1498 HandleGoto(got, got->GetSuccessor());
1499}
1500
1501void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1502 try_boundary->SetLocations(nullptr);
1503}
1504
1505void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1506 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1507 if (!successor->IsExitBlock()) {
1508 HandleGoto(try_boundary, successor);
1509 }
1510}
1511
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001512void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001513 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001514}
1515
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001516void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001517}
1518
Mark Mendell152408f2015-12-31 12:28:50 -05001519template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001520void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001521 LabelType* true_label,
1522 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001523 if (cond->IsFPConditionTrueIfNaN()) {
1524 __ j(kUnordered, true_label);
1525 } else if (cond->IsFPConditionFalseIfNaN()) {
1526 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001527 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001528 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001529}
1530
Mark Mendell152408f2015-12-31 12:28:50 -05001531template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001532void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001533 LabelType* true_label,
1534 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001535 LocationSummary* locations = cond->GetLocations();
1536 Location left = locations->InAt(0);
1537 Location right = locations->InAt(1);
1538 IfCondition if_cond = cond->GetCondition();
1539
Mark Mendellc4701932015-04-10 13:18:51 -04001540 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001541 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001542 IfCondition true_high_cond = if_cond;
1543 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001544 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001545
1546 // Set the conditions for the test, remembering that == needs to be
1547 // decided using the low words.
1548 switch (if_cond) {
1549 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001550 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001551 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001552 break;
1553 case kCondLT:
1554 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001555 break;
1556 case kCondLE:
1557 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001558 break;
1559 case kCondGT:
1560 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001561 break;
1562 case kCondGE:
1563 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001564 break;
Aart Bike9f37602015-10-09 11:15:55 -07001565 case kCondB:
1566 false_high_cond = kCondA;
1567 break;
1568 case kCondBE:
1569 true_high_cond = kCondB;
1570 break;
1571 case kCondA:
1572 false_high_cond = kCondB;
1573 break;
1574 case kCondAE:
1575 true_high_cond = kCondA;
1576 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001577 }
1578
1579 if (right.IsConstant()) {
1580 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001581 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001582 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001583
Aart Bika19616e2016-02-01 18:57:58 -08001584 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001585 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001586 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001587 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001588 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001589 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001590 __ j(X86Condition(true_high_cond), true_label);
1591 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001592 }
1593 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001594 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001595 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001596 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001597 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001598
1599 __ cmpl(left_high, right_high);
1600 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001601 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001602 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001603 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001604 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001605 __ j(X86Condition(true_high_cond), true_label);
1606 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001607 }
1608 // Must be equal high, so compare the lows.
1609 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001610 } else {
1611 DCHECK(right.IsDoubleStackSlot());
1612 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1613 if (if_cond == kCondNE) {
1614 __ j(X86Condition(true_high_cond), true_label);
1615 } else if (if_cond == kCondEQ) {
1616 __ j(X86Condition(false_high_cond), false_label);
1617 } else {
1618 __ j(X86Condition(true_high_cond), true_label);
1619 __ j(X86Condition(false_high_cond), false_label);
1620 }
1621 // Must be equal high, so compare the lows.
1622 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001623 }
1624 // The last comparison might be unsigned.
1625 __ j(final_condition, true_label);
1626}
1627
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001628void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1629 Location rhs,
1630 HInstruction* insn,
1631 bool is_double) {
1632 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1633 if (is_double) {
1634 if (rhs.IsFpuRegister()) {
1635 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1636 } else if (const_area != nullptr) {
1637 DCHECK(const_area->IsEmittedAtUseSite());
1638 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1639 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001640 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1641 const_area->GetBaseMethodAddress(),
1642 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001643 } else {
1644 DCHECK(rhs.IsDoubleStackSlot());
1645 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1646 }
1647 } else {
1648 if (rhs.IsFpuRegister()) {
1649 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1650 } else if (const_area != nullptr) {
1651 DCHECK(const_area->IsEmittedAtUseSite());
1652 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1653 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00001654 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1655 const_area->GetBaseMethodAddress(),
1656 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001657 } else {
1658 DCHECK(rhs.IsStackSlot());
1659 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1660 }
1661 }
1662}
1663
Mark Mendell152408f2015-12-31 12:28:50 -05001664template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001665void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001666 LabelType* true_target_in,
1667 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001668 // Generated branching requires both targets to be explicit. If either of the
1669 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001670 LabelType fallthrough_target;
1671 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1672 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001673
Mark Mendellc4701932015-04-10 13:18:51 -04001674 LocationSummary* locations = condition->GetLocations();
1675 Location left = locations->InAt(0);
1676 Location right = locations->InAt(1);
1677
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001678 DataType::Type type = condition->InputAt(0)->GetType();
Mark Mendellc4701932015-04-10 13:18:51 -04001679 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001680 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04001681 GenerateLongComparesAndJumps(condition, true_target, false_target);
1682 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001683 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001684 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001685 GenerateFPJumps(condition, true_target, false_target);
1686 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001687 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001688 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001689 GenerateFPJumps(condition, true_target, false_target);
1690 break;
1691 default:
1692 LOG(FATAL) << "Unexpected compare type " << type;
1693 }
1694
David Brazdil0debae72015-11-12 18:37:00 +00001695 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001696 __ jmp(false_target);
1697 }
David Brazdil0debae72015-11-12 18:37:00 +00001698
1699 if (fallthrough_target.IsLinked()) {
1700 __ Bind(&fallthrough_target);
1701 }
Mark Mendellc4701932015-04-10 13:18:51 -04001702}
1703
David Brazdil0debae72015-11-12 18:37:00 +00001704static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1705 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1706 // are set only strictly before `branch`. We can't use the eflags on long/FP
1707 // conditions if they are materialized due to the complex branching.
1708 return cond->IsCondition() &&
1709 cond->GetNext() == branch &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001710 cond->InputAt(0)->GetType() != DataType::Type::kInt64 &&
1711 !DataType::IsFloatingPointType(cond->InputAt(0)->GetType());
David Brazdil0debae72015-11-12 18:37:00 +00001712}
1713
Mark Mendell152408f2015-12-31 12:28:50 -05001714template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001715void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001716 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001717 LabelType* true_target,
1718 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001719 HInstruction* cond = instruction->InputAt(condition_input_index);
1720
1721 if (true_target == nullptr && false_target == nullptr) {
1722 // Nothing to do. The code always falls through.
1723 return;
1724 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001725 // Constant condition, statically compared against "true" (integer value 1).
1726 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001727 if (true_target != nullptr) {
1728 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001729 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001730 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001731 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001732 if (false_target != nullptr) {
1733 __ jmp(false_target);
1734 }
1735 }
1736 return;
1737 }
1738
1739 // The following code generates these patterns:
1740 // (1) true_target == nullptr && false_target != nullptr
1741 // - opposite condition true => branch to false_target
1742 // (2) true_target != nullptr && false_target == nullptr
1743 // - condition true => branch to true_target
1744 // (3) true_target != nullptr && false_target != nullptr
1745 // - condition true => branch to true_target
1746 // - branch to false_target
1747 if (IsBooleanValueOrMaterializedCondition(cond)) {
1748 if (AreEflagsSetFrom(cond, instruction)) {
1749 if (true_target == nullptr) {
1750 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1751 } else {
1752 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1753 }
1754 } else {
1755 // Materialized condition, compare against 0.
1756 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1757 if (lhs.IsRegister()) {
1758 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1759 } else {
1760 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1761 }
1762 if (true_target == nullptr) {
1763 __ j(kEqual, false_target);
1764 } else {
1765 __ j(kNotEqual, true_target);
1766 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001767 }
1768 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001769 // Condition has not been materialized, use its inputs as the comparison and
1770 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001771 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001772
1773 // If this is a long or FP comparison that has been folded into
1774 // the HCondition, generate the comparison directly.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001775 DataType::Type type = condition->InputAt(0)->GetType();
1776 if (type == DataType::Type::kInt64 || DataType::IsFloatingPointType(type)) {
David Brazdil0debae72015-11-12 18:37:00 +00001777 GenerateCompareTestAndBranch(condition, true_target, false_target);
1778 return;
1779 }
1780
1781 Location lhs = condition->GetLocations()->InAt(0);
1782 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001783 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001784 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001785 if (true_target == nullptr) {
1786 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1787 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001788 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001789 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001790 }
David Brazdil0debae72015-11-12 18:37:00 +00001791
1792 // If neither branch falls through (case 3), the conditional branch to `true_target`
1793 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1794 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001795 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001796 }
1797}
1798
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001799void LocationsBuilderX86::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001800 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00001801 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001802 locations->SetInAt(0, Location::Any());
1803 }
1804}
1805
1806void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001807 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1808 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1809 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1810 nullptr : codegen_->GetLabelOf(true_successor);
1811 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1812 nullptr : codegen_->GetLabelOf(false_successor);
Andreas Gampe3db70682018-12-26 15:12:03 -08001813 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001814}
1815
1816void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001817 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001818 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01001819 InvokeRuntimeCallingConvention calling_convention;
1820 RegisterSet caller_saves = RegisterSet::Empty();
1821 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
1822 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00001823 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001824 locations->SetInAt(0, Location::Any());
1825 }
1826}
1827
1828void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001829 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001830 GenerateTestAndBranch<Label>(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08001831 /* condition_input_index= */ 0,
David Brazdil74eb1b22015-12-14 11:44:01 +00001832 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08001833 /* false_target= */ nullptr);
David Brazdil74eb1b22015-12-14 11:44:01 +00001834}
1835
Mingyao Yang063fc772016-08-02 11:02:54 -07001836void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001837 LocationSummary* locations = new (GetGraph()->GetAllocator())
Mingyao Yang063fc772016-08-02 11:02:54 -07001838 LocationSummary(flag, LocationSummary::kNoCall);
1839 locations->SetOut(Location::RequiresRegister());
1840}
1841
1842void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1843 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1844 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1845}
1846
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001847static bool SelectCanUseCMOV(HSelect* select) {
1848 // There are no conditional move instructions for XMMs.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001849 if (DataType::IsFloatingPointType(select->GetType())) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001850 return false;
1851 }
1852
1853 // A FP condition doesn't generate the single CC that we need.
1854 // In 32 bit mode, a long condition doesn't generate a single CC either.
1855 HInstruction* condition = select->GetCondition();
1856 if (condition->IsCondition()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001857 DataType::Type compare_type = condition->InputAt(0)->GetType();
1858 if (compare_type == DataType::Type::kInt64 ||
1859 DataType::IsFloatingPointType(compare_type)) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001860 return false;
1861 }
1862 }
1863
1864 // We can generate a CMOV for this Select.
1865 return true;
1866}
1867
David Brazdil74eb1b22015-12-14 11:44:01 +00001868void LocationsBuilderX86::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001869 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001870 if (DataType::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001871 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001872 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001873 } else {
1874 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001875 if (SelectCanUseCMOV(select)) {
1876 if (select->InputAt(1)->IsConstant()) {
1877 // Cmov can't handle a constant value.
1878 locations->SetInAt(1, Location::RequiresRegister());
1879 } else {
1880 locations->SetInAt(1, Location::Any());
1881 }
1882 } else {
1883 locations->SetInAt(1, Location::Any());
1884 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001885 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001886 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1887 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001888 }
1889 locations->SetOut(Location::SameAsFirstInput());
1890}
1891
1892void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1893 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001894 DCHECK(locations->InAt(0).Equals(locations->Out()));
1895 if (SelectCanUseCMOV(select)) {
1896 // If both the condition and the source types are integer, we can generate
1897 // a CMOV to implement Select.
1898
1899 HInstruction* select_condition = select->GetCondition();
1900 Condition cond = kNotEqual;
1901
1902 // Figure out how to test the 'condition'.
1903 if (select_condition->IsCondition()) {
1904 HCondition* condition = select_condition->AsCondition();
1905 if (!condition->IsEmittedAtUseSite()) {
1906 // This was a previously materialized condition.
1907 // Can we use the existing condition code?
1908 if (AreEflagsSetFrom(condition, select)) {
1909 // Materialization was the previous instruction. Condition codes are right.
1910 cond = X86Condition(condition->GetCondition());
1911 } else {
1912 // No, we have to recreate the condition code.
1913 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1914 __ testl(cond_reg, cond_reg);
1915 }
1916 } else {
1917 // We can't handle FP or long here.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001918 DCHECK_NE(condition->InputAt(0)->GetType(), DataType::Type::kInt64);
1919 DCHECK(!DataType::IsFloatingPointType(condition->InputAt(0)->GetType()));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001920 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001921 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001922 cond = X86Condition(condition->GetCondition());
1923 }
1924 } else {
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001925 // Must be a Boolean condition, which needs to be compared to 0.
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001926 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1927 __ testl(cond_reg, cond_reg);
1928 }
1929
1930 // If the condition is true, overwrite the output, which already contains false.
1931 Location false_loc = locations->InAt(0);
1932 Location true_loc = locations->InAt(1);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001933 if (select->GetType() == DataType::Type::kInt64) {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001934 // 64 bit conditional move.
1935 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1936 Register false_low = false_loc.AsRegisterPairLow<Register>();
1937 if (true_loc.IsRegisterPair()) {
1938 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1939 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1940 } else {
1941 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1942 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1943 }
1944 } else {
1945 // 32 bit conditional move.
1946 Register false_reg = false_loc.AsRegister<Register>();
1947 if (true_loc.IsRegister()) {
1948 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1949 } else {
1950 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1951 }
1952 }
1953 } else {
1954 NearLabel false_target;
1955 GenerateTestAndBranch<NearLabel>(
Andreas Gampe3db70682018-12-26 15:12:03 -08001956 select, /* condition_input_index= */ 2, /* true_target= */ nullptr, &false_target);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001957 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1958 __ Bind(&false_target);
1959 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001960}
1961
David Srbecky0cf44932015-12-09 14:09:59 +00001962void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001963 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00001964}
1965
David Srbeckyd28f4a02016-03-14 17:14:24 +00001966void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1967 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001968}
1969
Vladimir Markodec78172020-06-19 15:31:23 +01001970void CodeGeneratorX86::IncreaseFrame(size_t adjustment) {
1971 __ subl(ESP, Immediate(adjustment));
1972 __ cfi().AdjustCFAOffset(adjustment);
1973}
1974
1975void CodeGeneratorX86::DecreaseFrame(size_t adjustment) {
1976 __ addl(ESP, Immediate(adjustment));
1977 __ cfi().AdjustCFAOffset(-adjustment);
1978}
1979
David Srbeckyc7098ff2016-02-09 14:30:11 +00001980void CodeGeneratorX86::GenerateNop() {
1981 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001982}
1983
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001984void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001985 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001986 new (GetGraph()->GetAllocator()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001987 // Handle the long/FP comparisons made in instruction simplification.
1988 switch (cond->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001989 case DataType::Type::kInt64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001990 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001991 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001992 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001993 locations->SetOut(Location::RequiresRegister());
1994 }
1995 break;
1996 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001997 case DataType::Type::kFloat32:
1998 case DataType::Type::kFloat64: {
Mark Mendellc4701932015-04-10 13:18:51 -04001999 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002000 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
2001 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
2002 } else if (cond->InputAt(1)->IsConstant()) {
2003 locations->SetInAt(1, Location::RequiresFpuRegister());
2004 } else {
2005 locations->SetInAt(1, Location::Any());
2006 }
David Brazdilb3e773e2016-01-26 11:28:37 +00002007 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002008 locations->SetOut(Location::RequiresRegister());
2009 }
2010 break;
2011 }
2012 default:
2013 locations->SetInAt(0, Location::RequiresRegister());
2014 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00002015 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002016 // We need a byte register.
2017 locations->SetOut(Location::RegisterLocation(ECX));
2018 }
2019 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002020 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002021}
2022
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002023void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002024 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04002025 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002026 }
Mark Mendellc4701932015-04-10 13:18:51 -04002027
2028 LocationSummary* locations = cond->GetLocations();
2029 Location lhs = locations->InAt(0);
2030 Location rhs = locations->InAt(1);
2031 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05002032 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002033
2034 switch (cond->InputAt(0)->GetType()) {
2035 default: {
2036 // Integer case.
2037
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01002038 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04002039 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01002040 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07002041 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04002042 return;
2043 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002044 case DataType::Type::kInt64:
Mark Mendellc4701932015-04-10 13:18:51 -04002045 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2046 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002047 case DataType::Type::kFloat32:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002048 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04002049 GenerateFPJumps(cond, &true_label, &false_label);
2050 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002051 case DataType::Type::kFloat64:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002052 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04002053 GenerateFPJumps(cond, &true_label, &false_label);
2054 break;
2055 }
2056
2057 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002058 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04002059
Roland Levillain4fa13f62015-07-06 18:11:54 +01002060 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04002061 __ Bind(&false_label);
2062 __ xorl(reg, reg);
2063 __ jmp(&done_label);
2064
Roland Levillain4fa13f62015-07-06 18:11:54 +01002065 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04002066 __ Bind(&true_label);
2067 __ movl(reg, Immediate(1));
2068 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002069}
2070
2071void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002072 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002073}
2074
2075void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002076 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002077}
2078
2079void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002080 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002081}
2082
2083void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002084 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002085}
2086
2087void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002088 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002089}
2090
2091void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002092 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002093}
2094
2095void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002096 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002097}
2098
2099void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002100 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002101}
2102
2103void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002104 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002105}
2106
2107void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002108 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002109}
2110
2111void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002112 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002113}
2114
2115void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002116 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002117}
2118
Aart Bike9f37602015-10-09 11:15:55 -07002119void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002120 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002121}
2122
2123void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002124 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002125}
2126
2127void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002128 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002129}
2130
2131void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002132 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002133}
2134
2135void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002136 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002137}
2138
2139void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002140 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002141}
2142
2143void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002144 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002145}
2146
2147void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002148 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002149}
2150
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002151void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002152 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002153 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002154 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002155}
2156
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002157void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002158 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002159}
2160
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002161void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2162 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002163 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002164 locations->SetOut(Location::ConstantLocation(constant));
2165}
2166
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002167void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002168 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002169}
2170
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002171void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002172 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002173 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002174 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002175}
2176
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002177void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002178 // Will be generated at use site.
2179}
2180
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002181void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2182 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002183 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002184 locations->SetOut(Location::ConstantLocation(constant));
2185}
2186
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002187void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002188 // Will be generated at use site.
2189}
2190
2191void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2192 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002193 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002194 locations->SetOut(Location::ConstantLocation(constant));
2195}
2196
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002197void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002198 // Will be generated at use site.
2199}
2200
Igor Murashkind01745e2017-04-05 16:40:31 -07002201void LocationsBuilderX86::VisitConstructorFence(HConstructorFence* constructor_fence) {
2202 constructor_fence->SetLocations(nullptr);
2203}
2204
2205void InstructionCodeGeneratorX86::VisitConstructorFence(
2206 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
2207 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
2208}
2209
Calin Juravle27df7582015-04-17 19:12:31 +01002210void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2211 memory_barrier->SetLocations(nullptr);
2212}
2213
2214void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002215 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002216}
2217
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002218void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002219 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002220}
2221
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002222void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002223 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002224}
2225
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002226void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002227 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002228 new (GetGraph()->GetAllocator()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002229 switch (ret->InputAt(0)->GetType()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002230 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002231 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002232 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002233 case DataType::Type::kInt8:
2234 case DataType::Type::kUint16:
2235 case DataType::Type::kInt16:
2236 case DataType::Type::kInt32:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002237 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002238 break;
2239
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002240 case DataType::Type::kInt64:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002241 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002242 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002243 break;
2244
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002245 case DataType::Type::kFloat32:
2246 case DataType::Type::kFloat64:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002247 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002248 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002249 break;
2250
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002251 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002252 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002253 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002254}
2255
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002256void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002257 switch (ret->InputAt(0)->GetType()) {
2258 case DataType::Type::kReference:
2259 case DataType::Type::kBool:
2260 case DataType::Type::kUint8:
2261 case DataType::Type::kInt8:
2262 case DataType::Type::kUint16:
2263 case DataType::Type::kInt16:
2264 case DataType::Type::kInt32:
2265 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
2266 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002267
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002268 case DataType::Type::kInt64:
2269 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2270 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
2271 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002272
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002273 case DataType::Type::kFloat32:
2274 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2275 if (GetGraph()->IsCompilingOsr()) {
2276 // To simplify callers of an OSR method, we put the return value in both
2277 // floating point and core registers.
2278 __ movd(EAX, XMM0);
2279 }
2280 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002281
Nicolas Geoffray57cacb72019-12-08 22:07:08 +00002282 case DataType::Type::kFloat64:
2283 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
2284 if (GetGraph()->IsCompilingOsr()) {
2285 // To simplify callers of an OSR method, we put the return value in both
2286 // floating point and core registers.
2287 __ movd(EAX, XMM0);
2288 // Use XMM1 as temporary register to not clobber XMM0.
2289 __ movaps(XMM1, XMM0);
2290 __ psrlq(XMM1, Immediate(32));
2291 __ movd(EDX, XMM1);
2292 }
2293 break;
2294
2295 default:
2296 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002297 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002298 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002299}
2300
Calin Juravle175dc732015-08-25 15:42:32 +01002301void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2302 // The trampoline uses the same calling convention as dex calling conventions,
2303 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2304 // the method_idx.
2305 HandleInvoke(invoke);
2306}
2307
2308void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2309 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2310}
2311
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002312void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002313 // Explicit clinit checks triggered by static invokes must have been pruned by
2314 // art::PrepareForRegisterAllocation.
2315 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002316
Mark Mendellfb8d2792015-03-31 22:16:59 -04002317 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002318 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01002319 if (invoke->GetLocations()->CanCall() &&
2320 invoke->HasPcRelativeMethodLoadKind() &&
2321 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).IsInvalid()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002322 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002323 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002324 return;
2325 }
2326
Vladimir Marko86c87522020-05-11 16:55:55 +01002327 if (invoke->GetCodePtrLocation() == HInvokeStaticOrDirect::CodePtrLocation::kCallCriticalNative) {
2328 CriticalNativeCallingConventionVisitorX86 calling_convention_visitor(
2329 /*for_register_allocation=*/ true);
2330 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
2331 } else {
2332 HandleInvoke(invoke);
2333 }
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002334
Vladimir Marko86c87522020-05-11 16:55:55 +01002335 // For PC-relative load kinds the invoke has an extra input, the PC-relative address base.
Vladimir Marko65979462017-05-19 17:25:12 +01002336 if (invoke->HasPcRelativeMethodLoadKind()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002337 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002338 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002339}
2340
Mark Mendell09ed1a32015-03-25 08:30:06 -04002341static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2342 if (invoke->GetLocations()->Intrinsified()) {
2343 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2344 intrinsic.Dispatch(invoke);
2345 return true;
2346 }
2347 return false;
2348}
2349
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002350void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002351 // Explicit clinit checks triggered by static invokes must have been pruned by
2352 // art::PrepareForRegisterAllocation.
2353 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002354
Mark Mendell09ed1a32015-03-25 08:30:06 -04002355 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2356 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002357 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002358
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002359 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002360 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002361 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002362}
2363
2364void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002365 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2366 if (intrinsic.TryDispatch(invoke)) {
2367 return;
2368 }
2369
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002370 HandleInvoke(invoke);
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002371
2372 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002373 // Add one temporary for inline cache update.
2374 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2375 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002376}
2377
2378void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002379 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002380 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002381}
2382
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002383void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002384 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2385 return;
2386 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002387
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002388 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002389 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002390}
2391
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002392void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002393 // This call to HandleInvoke allocates a temporary (core) register
2394 // which is also used to transfer the hidden argument from FP to
2395 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002396 HandleInvoke(invoke);
2397 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002398 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002399
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002400 if (GetGraph()->IsCompilingBaseline() && !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002401 // Add one temporary for inline cache update.
2402 invoke->GetLocations()->AddTemp(Location::RegisterLocation(EBP));
2403 }
2404}
2405
2406void CodeGeneratorX86::MaybeGenerateInlineCacheCheck(HInstruction* instruction, Register klass) {
2407 DCHECK_EQ(EAX, klass);
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002408 // We know the destination of an intrinsic, so no need to record inline
2409 // caches (also the intrinsic location builder doesn't request an additional
2410 // temporary).
2411 if (!instruction->GetLocations()->Intrinsified() &&
Nicolas Geoffray9b5271e2019-12-04 14:39:46 +00002412 GetGraph()->IsCompilingBaseline() &&
Nicolas Geoffray17a39ba2019-11-27 20:57:48 +00002413 !Runtime::Current()->IsAotCompiler()) {
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002414 DCHECK(!instruction->GetEnvironment()->IsFromInlinedInvoke());
2415 ScopedObjectAccess soa(Thread::Current());
2416 ProfilingInfo* info = GetGraph()->GetArtMethod()->GetProfilingInfo(kRuntimePointerSize);
Nicolas Geoffray796aa2c2019-12-17 10:20:05 +00002417 if (info != nullptr) {
2418 InlineCache* cache = info->GetInlineCache(instruction->GetDexPc());
2419 uint32_t address = reinterpret_cast32<uint32_t>(cache);
2420 if (kIsDebugBuild) {
2421 uint32_t temp_index = instruction->GetLocations()->GetTempCount() - 1u;
2422 CHECK_EQ(EBP, instruction->GetLocations()->GetTemp(temp_index).AsRegister<Register>());
2423 }
2424 Register temp = EBP;
2425 NearLabel done;
2426 __ movl(temp, Immediate(address));
2427 // Fast path for a monomorphic cache.
2428 __ cmpl(klass, Address(temp, InlineCache::ClassesOffset().Int32Value()));
2429 __ j(kEqual, &done);
2430 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(kQuickUpdateInlineCache).Int32Value());
2431 __ Bind(&done);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002432 }
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002433 }
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002434}
2435
2436void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2437 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002438 LocationSummary* locations = invoke->GetLocations();
2439 Register temp = locations->GetTemp(0).AsRegister<Register>();
2440 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002441 Location receiver = locations->InAt(0);
2442 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2443
Roland Levillain0d5a2812015-11-13 10:07:31 +00002444 // Set the hidden argument. This is safe to do this here, as XMM7
2445 // won't be modified thereafter, before the `call` instruction.
2446 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002447 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002448 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002449
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002450 if (receiver.IsStackSlot()) {
2451 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002452 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002453 __ movl(temp, Address(temp, class_offset));
2454 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002455 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002456 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002457 }
Roland Levillain4d027112015-07-01 15:41:14 +01002458 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002459 // Instead of simply (possibly) unpoisoning `temp` here, we should
2460 // emit a read barrier for the previous class reference load.
2461 // However this is not required in practice, as this is an
2462 // intermediate/temporary reference and because the current
2463 // concurrent copying collector keeps the from-space memory
2464 // intact/accessible until the end of the marking phase (the
2465 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002466 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00002467
2468 codegen_->MaybeGenerateInlineCacheCheck(invoke, temp);
2469
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002470 // temp = temp->GetAddressOfIMT()
2471 __ movl(temp,
2472 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002473 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002474 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002475 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002476 __ movl(temp, Address(temp, method_offset));
2477 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002478 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002479 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002480
2481 DCHECK(!codegen_->IsLeafMethod());
2482 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2483}
2484
Orion Hodsonac141392017-01-13 11:53:47 +00002485void LocationsBuilderX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00002486 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2487 if (intrinsic.TryDispatch(invoke)) {
2488 return;
2489 }
Orion Hodsonac141392017-01-13 11:53:47 +00002490 HandleInvoke(invoke);
2491}
2492
2493void InstructionCodeGeneratorX86::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
Andra Danciua0130e82020-07-23 12:34:56 +00002494 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2495 return;
2496 }
Orion Hodsonac141392017-01-13 11:53:47 +00002497 codegen_->GenerateInvokePolymorphicCall(invoke);
2498}
2499
Orion Hodson4c8e12e2018-05-18 08:33:20 +01002500void LocationsBuilderX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2501 HandleInvoke(invoke);
2502}
2503
2504void InstructionCodeGeneratorX86::VisitInvokeCustom(HInvokeCustom* invoke) {
2505 codegen_->GenerateInvokeCustomCall(invoke);
2506}
2507
Roland Levillain88cb1752014-10-20 16:36:47 +01002508void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2509 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002510 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Roland Levillain88cb1752014-10-20 16:36:47 +01002511 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002512 case DataType::Type::kInt32:
2513 case DataType::Type::kInt64:
Roland Levillain88cb1752014-10-20 16:36:47 +01002514 locations->SetInAt(0, Location::RequiresRegister());
2515 locations->SetOut(Location::SameAsFirstInput());
2516 break;
2517
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002518 case DataType::Type::kFloat32:
Roland Levillain5368c212014-11-27 15:03:41 +00002519 locations->SetInAt(0, Location::RequiresFpuRegister());
2520 locations->SetOut(Location::SameAsFirstInput());
2521 locations->AddTemp(Location::RequiresRegister());
2522 locations->AddTemp(Location::RequiresFpuRegister());
2523 break;
2524
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002525 case DataType::Type::kFloat64:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002526 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002527 locations->SetOut(Location::SameAsFirstInput());
2528 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002529 break;
2530
2531 default:
2532 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2533 }
2534}
2535
2536void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2537 LocationSummary* locations = neg->GetLocations();
2538 Location out = locations->Out();
2539 Location in = locations->InAt(0);
2540 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002541 case DataType::Type::kInt32:
Roland Levillain88cb1752014-10-20 16:36:47 +01002542 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002543 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002544 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002545 break;
2546
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002547 case DataType::Type::kInt64:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002548 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002549 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002550 __ negl(out.AsRegisterPairLow<Register>());
2551 // Negation is similar to subtraction from zero. The least
2552 // significant byte triggers a borrow when it is different from
2553 // zero; to take it into account, add 1 to the most significant
2554 // byte if the carry flag (CF) is set to 1 after the first NEGL
2555 // operation.
2556 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2557 __ negl(out.AsRegisterPairHigh<Register>());
2558 break;
2559
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002560 case DataType::Type::kFloat32: {
Roland Levillain5368c212014-11-27 15:03:41 +00002561 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002562 Register constant = locations->GetTemp(0).AsRegister<Register>();
2563 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002564 // Implement float negation with an exclusive or with value
2565 // 0x80000000 (mask for bit 31, representing the sign of a
2566 // single-precision floating-point number).
2567 __ movl(constant, Immediate(INT32_C(0x80000000)));
2568 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002569 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002570 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002571 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002572
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002573 case DataType::Type::kFloat64: {
Roland Levillain5368c212014-11-27 15:03:41 +00002574 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002575 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002576 // Implement double negation with an exclusive or with value
2577 // 0x8000000000000000 (mask for bit 63, representing the sign of
2578 // a double-precision floating-point number).
2579 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002580 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002581 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002582 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002583
2584 default:
2585 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2586 }
2587}
2588
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002589void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2590 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002591 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002592 DCHECK(DataType::IsFloatingPointType(neg->GetType()));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002593 locations->SetInAt(0, Location::RequiresFpuRegister());
2594 locations->SetInAt(1, Location::RequiresRegister());
2595 locations->SetOut(Location::SameAsFirstInput());
2596 locations->AddTemp(Location::RequiresFpuRegister());
2597}
2598
2599void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2600 LocationSummary* locations = neg->GetLocations();
2601 Location out = locations->Out();
2602 DCHECK(locations->InAt(0).Equals(out));
2603
2604 Register constant_area = locations->InAt(1).AsRegister<Register>();
2605 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002606 if (neg->GetType() == DataType::Type::kFloat32) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002607 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000),
2608 neg->GetBaseMethodAddress(),
2609 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002610 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2611 } else {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00002612 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000),
2613 neg->GetBaseMethodAddress(),
2614 constant_area));
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002615 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2616 }
2617}
2618
Roland Levillaindff1f282014-11-05 14:15:05 +00002619void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002620 DataType::Type result_type = conversion->GetResultType();
2621 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002622 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2623 << input_type << " -> " << result_type;
Roland Levillain624279f2014-12-04 11:54:28 +00002624
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002625 // The float-to-long and double-to-long type conversions rely on a
2626 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002627 LocationSummary::CallKind call_kind =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002628 ((input_type == DataType::Type::kFloat32 || input_type == DataType::Type::kFloat64)
2629 && result_type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002630 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002631 : LocationSummary::kNoCall;
2632 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002633 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Roland Levillain624279f2014-12-04 11:54:28 +00002634
Roland Levillaindff1f282014-11-05 14:15:05 +00002635 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002636 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002637 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002638 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002639 case DataType::Type::kUint8:
2640 case DataType::Type::kInt8:
2641 case DataType::Type::kUint16:
2642 case DataType::Type::kInt16:
2643 case DataType::Type::kInt32:
2644 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2645 // Make the output overlap to please the register allocator. This greatly simplifies
2646 // the validation of the linear scan implementation
2647 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2648 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002649 case DataType::Type::kInt64: {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002650 HInstruction* input = conversion->InputAt(0);
2651 Location input_location = input->IsConstant()
2652 ? Location::ConstantLocation(input->AsConstant())
2653 : Location::RegisterPairLocation(EAX, EDX);
2654 locations->SetInAt(0, input_location);
2655 // Make the output overlap to please the register allocator. This greatly simplifies
2656 // the validation of the linear scan implementation
2657 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2658 break;
2659 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002660
2661 default:
2662 LOG(FATAL) << "Unexpected type conversion from " << input_type
2663 << " to " << result_type;
2664 }
2665 break;
2666
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002667 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002668 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002669 DCHECK(DataType::IsIntegralType(input_type)) << input_type;
2670 locations->SetInAt(0, Location::Any());
2671 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain01a8d712014-11-14 16:27:39 +00002672 break;
2673
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002674 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002675 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002676 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002677 locations->SetInAt(0, Location::Any());
2678 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2679 break;
2680
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002681 case DataType::Type::kFloat32:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002682 locations->SetInAt(0, Location::RequiresFpuRegister());
2683 locations->SetOut(Location::RequiresRegister());
2684 locations->AddTemp(Location::RequiresFpuRegister());
2685 break;
2686
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002687 case DataType::Type::kFloat64:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002688 locations->SetInAt(0, Location::RequiresFpuRegister());
2689 locations->SetOut(Location::RequiresRegister());
2690 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002691 break;
2692
2693 default:
2694 LOG(FATAL) << "Unexpected type conversion from " << input_type
2695 << " to " << result_type;
2696 }
2697 break;
2698
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002699 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002700 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002701 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002702 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002703 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002704 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002705 case DataType::Type::kInt16:
2706 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00002707 locations->SetInAt(0, Location::RegisterLocation(EAX));
2708 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2709 break;
2710
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002711 case DataType::Type::kFloat32:
2712 case DataType::Type::kFloat64: {
Vladimir Marko949c91f2015-01-27 10:48:44 +00002713 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002714 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2715 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2716
Vladimir Marko949c91f2015-01-27 10:48:44 +00002717 // The runtime helper puts the result in EAX, EDX.
2718 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002719 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002720 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002721
2722 default:
2723 LOG(FATAL) << "Unexpected type conversion from " << input_type
2724 << " to " << result_type;
2725 }
2726 break;
2727
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002728 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00002729 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002730 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002731 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002732 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002733 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002734 case DataType::Type::kInt16:
2735 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002736 locations->SetInAt(0, Location::RequiresRegister());
2737 locations->SetOut(Location::RequiresFpuRegister());
2738 break;
2739
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002740 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002741 locations->SetInAt(0, Location::Any());
2742 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002743 break;
2744
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002745 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002746 locations->SetInAt(0, Location::RequiresFpuRegister());
2747 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002748 break;
2749
2750 default:
2751 LOG(FATAL) << "Unexpected type conversion from " << input_type
2752 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08002753 }
Roland Levillaincff13742014-11-17 14:32:17 +00002754 break;
2755
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002756 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00002757 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002758 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002759 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002760 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002761 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002762 case DataType::Type::kInt16:
2763 case DataType::Type::kInt32:
Roland Levillaincff13742014-11-17 14:32:17 +00002764 locations->SetInAt(0, Location::RequiresRegister());
2765 locations->SetOut(Location::RequiresFpuRegister());
2766 break;
2767
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002768 case DataType::Type::kInt64:
Roland Levillain232ade02015-04-20 15:14:36 +01002769 locations->SetInAt(0, Location::Any());
2770 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002771 break;
2772
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002773 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002774 locations->SetInAt(0, Location::RequiresFpuRegister());
2775 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002776 break;
2777
2778 default:
2779 LOG(FATAL) << "Unexpected type conversion from " << input_type
2780 << " to " << result_type;
2781 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002782 break;
2783
2784 default:
2785 LOG(FATAL) << "Unexpected type conversion from " << input_type
2786 << " to " << result_type;
2787 }
2788}
2789
2790void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2791 LocationSummary* locations = conversion->GetLocations();
2792 Location out = locations->Out();
2793 Location in = locations->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002794 DataType::Type result_type = conversion->GetResultType();
2795 DataType::Type input_type = conversion->GetInputType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002796 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
2797 << input_type << " -> " << result_type;
Roland Levillaindff1f282014-11-05 14:15:05 +00002798 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002799 case DataType::Type::kUint8:
2800 switch (input_type) {
2801 case DataType::Type::kInt8:
2802 case DataType::Type::kUint16:
2803 case DataType::Type::kInt16:
2804 case DataType::Type::kInt32:
2805 if (in.IsRegister()) {
2806 __ movzxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2807 } else {
2808 DCHECK(in.GetConstant()->IsIntConstant());
2809 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2810 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2811 }
2812 break;
2813 case DataType::Type::kInt64:
2814 if (in.IsRegisterPair()) {
2815 __ movzxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2816 } else {
2817 DCHECK(in.GetConstant()->IsLongConstant());
2818 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2819 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint8_t>(value)));
2820 }
2821 break;
2822
2823 default:
2824 LOG(FATAL) << "Unexpected type conversion from " << input_type
2825 << " to " << result_type;
2826 }
2827 break;
2828
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002829 case DataType::Type::kInt8:
Roland Levillain51d3fc42014-11-13 14:11:42 +00002830 switch (input_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002831 case DataType::Type::kUint8:
2832 case DataType::Type::kUint16:
2833 case DataType::Type::kInt16:
2834 case DataType::Type::kInt32:
2835 if (in.IsRegister()) {
2836 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
2837 } else {
2838 DCHECK(in.GetConstant()->IsIntConstant());
2839 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2840 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2841 }
2842 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002843 case DataType::Type::kInt64:
Vladimir Markob52bbde2016-02-12 12:06:05 +00002844 if (in.IsRegisterPair()) {
2845 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2846 } else {
2847 DCHECK(in.GetConstant()->IsLongConstant());
2848 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2849 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2850 }
2851 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002852
2853 default:
2854 LOG(FATAL) << "Unexpected type conversion from " << input_type
2855 << " to " << result_type;
2856 }
2857 break;
2858
2859 case DataType::Type::kUint16:
2860 switch (input_type) {
2861 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002862 case DataType::Type::kInt16:
2863 case DataType::Type::kInt32:
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002864 if (in.IsRegister()) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002865 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
2866 } else if (in.IsStackSlot()) {
2867 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002868 } else {
2869 DCHECK(in.GetConstant()->IsIntConstant());
2870 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002871 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2872 }
2873 break;
2874 case DataType::Type::kInt64:
2875 if (in.IsRegisterPair()) {
2876 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2877 } else if (in.IsDoubleStackSlot()) {
2878 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2879 } else {
2880 DCHECK(in.GetConstant()->IsLongConstant());
2881 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2882 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002883 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002884 break;
2885
2886 default:
2887 LOG(FATAL) << "Unexpected type conversion from " << input_type
2888 << " to " << result_type;
2889 }
2890 break;
2891
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002892 case DataType::Type::kInt16:
Roland Levillain01a8d712014-11-14 16:27:39 +00002893 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002894 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002895 case DataType::Type::kInt32:
Roland Levillain01a8d712014-11-14 16:27:39 +00002896 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002897 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002898 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002899 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002900 } else {
2901 DCHECK(in.GetConstant()->IsIntConstant());
2902 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002903 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002904 }
2905 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002906 case DataType::Type::kInt64:
2907 if (in.IsRegisterPair()) {
2908 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2909 } else if (in.IsDoubleStackSlot()) {
2910 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2911 } else {
2912 DCHECK(in.GetConstant()->IsLongConstant());
2913 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2914 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2915 }
2916 break;
Roland Levillain01a8d712014-11-14 16:27:39 +00002917
2918 default:
2919 LOG(FATAL) << "Unexpected type conversion from " << input_type
2920 << " to " << result_type;
2921 }
2922 break;
2923
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002924 case DataType::Type::kInt32:
Roland Levillain946e1432014-11-11 17:35:19 +00002925 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002926 case DataType::Type::kInt64:
Roland Levillain946e1432014-11-11 17:35:19 +00002927 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002928 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002929 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002930 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002931 } else {
2932 DCHECK(in.IsConstant());
2933 DCHECK(in.GetConstant()->IsLongConstant());
2934 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002935 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002936 }
2937 break;
2938
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002939 case DataType::Type::kFloat32: {
Roland Levillain3f8f9362014-12-02 17:45:01 +00002940 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2941 Register output = out.AsRegister<Register>();
2942 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002943 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002944
2945 __ movl(output, Immediate(kPrimIntMax));
2946 // temp = int-to-float(output)
2947 __ cvtsi2ss(temp, output);
2948 // if input >= temp goto done
2949 __ comiss(input, temp);
2950 __ j(kAboveEqual, &done);
2951 // if input == NaN goto nan
2952 __ j(kUnordered, &nan);
2953 // output = float-to-int-truncate(input)
2954 __ cvttss2si(output, input);
2955 __ jmp(&done);
2956 __ Bind(&nan);
2957 // output = 0
2958 __ xorl(output, output);
2959 __ Bind(&done);
2960 break;
2961 }
2962
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002963 case DataType::Type::kFloat64: {
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002964 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2965 Register output = out.AsRegister<Register>();
2966 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002967 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002968
2969 __ movl(output, Immediate(kPrimIntMax));
2970 // temp = int-to-double(output)
2971 __ cvtsi2sd(temp, output);
2972 // if input >= temp goto done
2973 __ comisd(input, temp);
2974 __ j(kAboveEqual, &done);
2975 // if input == NaN goto nan
2976 __ j(kUnordered, &nan);
2977 // output = double-to-int-truncate(input)
2978 __ cvttsd2si(output, input);
2979 __ jmp(&done);
2980 __ Bind(&nan);
2981 // output = 0
2982 __ xorl(output, output);
2983 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002984 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002985 }
Roland Levillain946e1432014-11-11 17:35:19 +00002986
2987 default:
2988 LOG(FATAL) << "Unexpected type conversion from " << input_type
2989 << " to " << result_type;
2990 }
2991 break;
2992
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002993 case DataType::Type::kInt64:
Roland Levillaindff1f282014-11-05 14:15:05 +00002994 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002995 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002996 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002997 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002998 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002999 case DataType::Type::kInt16:
3000 case DataType::Type::kInt32:
Roland Levillaindff1f282014-11-05 14:15:05 +00003001 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
3002 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00003003 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00003004 __ cdq();
3005 break;
3006
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003007 case DataType::Type::kFloat32:
Serban Constantinescuba45db02016-07-12 22:53:02 +01003008 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003009 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00003010 break;
3011
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003012 case DataType::Type::kFloat64:
Serban Constantinescuba45db02016-07-12 22:53:02 +01003013 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003014 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00003015 break;
3016
3017 default:
3018 LOG(FATAL) << "Unexpected type conversion from " << input_type
3019 << " to " << result_type;
3020 }
3021 break;
3022
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003023 case DataType::Type::kFloat32:
Roland Levillaincff13742014-11-17 14:32:17 +00003024 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003025 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003026 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003027 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003028 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003029 case DataType::Type::kInt16:
3030 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003031 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003032 break;
3033
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003034 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003035 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00003036
Roland Levillain232ade02015-04-20 15:14:36 +01003037 // Create stack space for the call to
3038 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
3039 // TODO: enhance register allocator to ask for stack temporaries.
3040 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003041 adjustment = DataType::Size(DataType::Type::kInt64);
Vladimir Markodec78172020-06-19 15:31:23 +01003042 codegen_->IncreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003043 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003044
Roland Levillain232ade02015-04-20 15:14:36 +01003045 // Load the value to the FP stack, using temporaries if needed.
3046 PushOntoFPStack(in, 0, adjustment, false, true);
3047
3048 if (out.IsStackSlot()) {
3049 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
3050 } else {
3051 __ fstps(Address(ESP, 0));
3052 Location stack_temp = Location::StackSlot(0);
3053 codegen_->Move32(out, stack_temp);
3054 }
3055
3056 // Remove the temporary stack space we allocated.
3057 if (adjustment != 0) {
Vladimir Markodec78172020-06-19 15:31:23 +01003058 codegen_->DecreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003059 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003060 break;
3061 }
3062
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003063 case DataType::Type::kFloat64:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003064 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003065 break;
3066
3067 default:
3068 LOG(FATAL) << "Unexpected type conversion from " << input_type
3069 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003070 }
Roland Levillaincff13742014-11-17 14:32:17 +00003071 break;
3072
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003073 case DataType::Type::kFloat64:
Roland Levillaincff13742014-11-17 14:32:17 +00003074 switch (input_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003075 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003076 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003077 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003078 case DataType::Type::kUint16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003079 case DataType::Type::kInt16:
3080 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003081 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003082 break;
3083
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003084 case DataType::Type::kInt64: {
Roland Levillain232ade02015-04-20 15:14:36 +01003085 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00003086
Roland Levillain232ade02015-04-20 15:14:36 +01003087 // Create stack space for the call to
3088 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
3089 // TODO: enhance register allocator to ask for stack temporaries.
3090 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003091 adjustment = DataType::Size(DataType::Type::kInt64);
Vladimir Markodec78172020-06-19 15:31:23 +01003092 codegen_->IncreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003093 }
3094
3095 // Load the value to the FP stack, using temporaries if needed.
3096 PushOntoFPStack(in, 0, adjustment, false, true);
3097
3098 if (out.IsDoubleStackSlot()) {
3099 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
3100 } else {
3101 __ fstpl(Address(ESP, 0));
3102 Location stack_temp = Location::DoubleStackSlot(0);
3103 codegen_->Move64(out, stack_temp);
3104 }
3105
3106 // Remove the temporary stack space we allocated.
3107 if (adjustment != 0) {
Vladimir Markodec78172020-06-19 15:31:23 +01003108 codegen_->DecreaseFrame(adjustment);
Roland Levillain232ade02015-04-20 15:14:36 +01003109 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00003110 break;
3111 }
3112
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003113 case DataType::Type::kFloat32:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003114 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003115 break;
3116
3117 default:
3118 LOG(FATAL) << "Unexpected type conversion from " << input_type
3119 << " to " << result_type;
Igor Murashkin2ffb7032017-11-08 13:35:21 -08003120 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003121 break;
3122
3123 default:
3124 LOG(FATAL) << "Unexpected type conversion from " << input_type
3125 << " to " << result_type;
3126 }
3127}
3128
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003129void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003130 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003131 new (GetGraph()->GetAllocator()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003132 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003133 case DataType::Type::kInt32: {
Mark Mendell09b84632015-02-13 17:48:38 -05003134 locations->SetInAt(0, Location::RequiresRegister());
3135 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
3136 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3137 break;
3138 }
3139
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003140 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003141 locations->SetInAt(0, Location::RequiresRegister());
3142 locations->SetInAt(1, Location::Any());
3143 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003144 break;
3145 }
3146
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003147 case DataType::Type::kFloat32:
3148 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003149 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003150 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3151 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003152 } else if (add->InputAt(1)->IsConstant()) {
3153 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003154 } else {
3155 locations->SetInAt(1, Location::Any());
3156 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003157 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003158 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003159 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003160
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003161 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003162 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Elliott Hughesc1896c92018-11-29 11:33:18 -08003163 UNREACHABLE();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003164 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003165}
3166
3167void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
3168 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003169 Location first = locations->InAt(0);
3170 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05003171 Location out = locations->Out();
3172
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003173 switch (add->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003174 case DataType::Type::kInt32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003175 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003176 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3177 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04003178 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
3179 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05003180 } else {
3181 __ leal(out.AsRegister<Register>(), Address(
3182 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
3183 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003184 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05003185 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
3186 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
3187 __ addl(out.AsRegister<Register>(), Immediate(value));
3188 } else {
3189 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
3190 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003191 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05003192 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003193 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003194 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003195 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003196 }
3197
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003198 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003199 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003200 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3201 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003202 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003203 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
3204 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003205 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003206 } else {
3207 DCHECK(second.IsConstant()) << second;
3208 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3209 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3210 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003211 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003212 break;
3213 }
3214
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003215 case DataType::Type::kFloat32: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003216 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003217 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003218 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3219 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003220 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003221 __ addss(first.AsFpuRegister<XmmRegister>(),
3222 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003223 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3224 const_area->GetBaseMethodAddress(),
3225 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003226 } else {
3227 DCHECK(second.IsStackSlot());
3228 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003229 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003230 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003231 }
3232
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003233 case DataType::Type::kFloat64: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003234 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003235 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003236 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3237 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003238 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003239 __ addsd(first.AsFpuRegister<XmmRegister>(),
3240 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003241 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3242 const_area->GetBaseMethodAddress(),
3243 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003244 } else {
3245 DCHECK(second.IsDoubleStackSlot());
3246 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003247 }
3248 break;
3249 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003250
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003251 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003252 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003253 }
3254}
3255
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003256void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003257 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003258 new (GetGraph()->GetAllocator()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003259 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003260 case DataType::Type::kInt32:
3261 case DataType::Type::kInt64: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003262 locations->SetInAt(0, Location::RequiresRegister());
3263 locations->SetInAt(1, Location::Any());
3264 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003265 break;
3266 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003267 case DataType::Type::kFloat32:
3268 case DataType::Type::kFloat64: {
Calin Juravle11351682014-10-23 15:38:15 +01003269 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003270 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3271 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003272 } else if (sub->InputAt(1)->IsConstant()) {
3273 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003274 } else {
3275 locations->SetInAt(1, Location::Any());
3276 }
Calin Juravle11351682014-10-23 15:38:15 +01003277 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003278 break;
Calin Juravle11351682014-10-23 15:38:15 +01003279 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003280
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003281 default:
Calin Juravle11351682014-10-23 15:38:15 +01003282 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003283 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003284}
3285
3286void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3287 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003288 Location first = locations->InAt(0);
3289 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003290 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003291 switch (sub->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003292 case DataType::Type::kInt32: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003293 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003294 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003295 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003296 __ subl(first.AsRegister<Register>(),
3297 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003298 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003299 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003300 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003301 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003302 }
3303
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003304 case DataType::Type::kInt64: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003305 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003306 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3307 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003308 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003309 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003310 __ sbbl(first.AsRegisterPairHigh<Register>(),
3311 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003312 } else {
3313 DCHECK(second.IsConstant()) << second;
3314 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3315 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3316 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003317 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003318 break;
3319 }
3320
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003321 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003322 if (second.IsFpuRegister()) {
3323 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3324 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3325 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003326 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003327 __ subss(first.AsFpuRegister<XmmRegister>(),
3328 codegen_->LiteralFloatAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003329 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3330 const_area->GetBaseMethodAddress(),
3331 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003332 } else {
3333 DCHECK(second.IsStackSlot());
3334 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3335 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003336 break;
Calin Juravle11351682014-10-23 15:38:15 +01003337 }
3338
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003339 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003340 if (second.IsFpuRegister()) {
3341 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3342 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3343 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003344 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003345 __ subsd(first.AsFpuRegister<XmmRegister>(),
3346 codegen_->LiteralDoubleAddress(
3347 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003348 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003349 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3350 } else {
3351 DCHECK(second.IsDoubleStackSlot());
3352 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3353 }
Calin Juravle11351682014-10-23 15:38:15 +01003354 break;
3355 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003356
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003357 default:
Calin Juravle11351682014-10-23 15:38:15 +01003358 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003359 }
3360}
3361
Calin Juravle34bacdf2014-10-07 20:23:36 +01003362void LocationsBuilderX86::VisitMul(HMul* mul) {
3363 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003364 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003365 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003366 case DataType::Type::kInt32:
Calin Juravle34bacdf2014-10-07 20:23:36 +01003367 locations->SetInAt(0, Location::RequiresRegister());
3368 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003369 if (mul->InputAt(1)->IsIntConstant()) {
3370 // Can use 3 operand multiply.
3371 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3372 } else {
3373 locations->SetOut(Location::SameAsFirstInput());
3374 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003375 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003376 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003377 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003378 locations->SetInAt(1, Location::Any());
3379 locations->SetOut(Location::SameAsFirstInput());
3380 // Needed for imul on 32bits with 64bits output.
3381 locations->AddTemp(Location::RegisterLocation(EAX));
3382 locations->AddTemp(Location::RegisterLocation(EDX));
3383 break;
3384 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003385 case DataType::Type::kFloat32:
3386 case DataType::Type::kFloat64: {
Calin Juravleb5bfa962014-10-21 18:02:24 +01003387 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003388 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3389 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003390 } else if (mul->InputAt(1)->IsConstant()) {
3391 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003392 } else {
3393 locations->SetInAt(1, Location::Any());
3394 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003395 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003396 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003397 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003398
3399 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003400 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003401 }
3402}
3403
3404void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3405 LocationSummary* locations = mul->GetLocations();
3406 Location first = locations->InAt(0);
3407 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003408 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003409
3410 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003411 case DataType::Type::kInt32:
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003412 // The constant may have ended up in a register, so test explicitly to avoid
3413 // problems where the output may not be the same as the first operand.
3414 if (mul->InputAt(1)->IsIntConstant()) {
3415 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3416 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3417 } else if (second.IsRegister()) {
3418 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003419 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003420 } else {
3421 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003422 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003423 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003424 }
3425 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003426
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003427 case DataType::Type::kInt64: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003428 Register in1_hi = first.AsRegisterPairHigh<Register>();
3429 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003430 Register eax = locations->GetTemp(0).AsRegister<Register>();
3431 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003432
3433 DCHECK_EQ(EAX, eax);
3434 DCHECK_EQ(EDX, edx);
3435
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003436 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003437 // output: in1
3438 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3439 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3440 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003441 if (second.IsConstant()) {
3442 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003443
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003444 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3445 int32_t low_value = Low32Bits(value);
3446 int32_t high_value = High32Bits(value);
3447 Immediate low(low_value);
3448 Immediate high(high_value);
3449
3450 __ movl(eax, high);
3451 // eax <- in1.lo * in2.hi
3452 __ imull(eax, in1_lo);
3453 // in1.hi <- in1.hi * in2.lo
3454 __ imull(in1_hi, low);
3455 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3456 __ addl(in1_hi, eax);
3457 // move in2_lo to eax to prepare for double precision
3458 __ movl(eax, low);
3459 // edx:eax <- in1.lo * in2.lo
3460 __ mull(in1_lo);
3461 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3462 __ addl(in1_hi, edx);
3463 // in1.lo <- (in1.lo * in2.lo)[31:0];
3464 __ movl(in1_lo, eax);
3465 } else if (second.IsRegisterPair()) {
3466 Register in2_hi = second.AsRegisterPairHigh<Register>();
3467 Register in2_lo = second.AsRegisterPairLow<Register>();
3468
3469 __ movl(eax, in2_hi);
3470 // eax <- in1.lo * in2.hi
3471 __ imull(eax, in1_lo);
3472 // in1.hi <- in1.hi * in2.lo
3473 __ imull(in1_hi, in2_lo);
3474 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3475 __ addl(in1_hi, eax);
3476 // move in1_lo to eax to prepare for double precision
3477 __ movl(eax, in1_lo);
3478 // edx:eax <- in1.lo * in2.lo
3479 __ mull(in2_lo);
3480 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3481 __ addl(in1_hi, edx);
3482 // in1.lo <- (in1.lo * in2.lo)[31:0];
3483 __ movl(in1_lo, eax);
3484 } else {
3485 DCHECK(second.IsDoubleStackSlot()) << second;
3486 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3487 Address in2_lo(ESP, second.GetStackIndex());
3488
3489 __ movl(eax, in2_hi);
3490 // eax <- in1.lo * in2.hi
3491 __ imull(eax, in1_lo);
3492 // in1.hi <- in1.hi * in2.lo
3493 __ imull(in1_hi, in2_lo);
3494 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3495 __ addl(in1_hi, eax);
3496 // move in1_lo to eax to prepare for double precision
3497 __ movl(eax, in1_lo);
3498 // edx:eax <- in1.lo * in2.lo
3499 __ mull(in2_lo);
3500 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3501 __ addl(in1_hi, edx);
3502 // in1.lo <- (in1.lo * in2.lo)[31:0];
3503 __ movl(in1_lo, eax);
3504 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003505
3506 break;
3507 }
3508
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003509 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003510 DCHECK(first.Equals(locations->Out()));
3511 if (second.IsFpuRegister()) {
3512 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3513 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3514 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003515 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003516 __ mulss(first.AsFpuRegister<XmmRegister>(),
3517 codegen_->LiteralFloatAddress(
3518 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003519 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003520 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3521 } else {
3522 DCHECK(second.IsStackSlot());
3523 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3524 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003525 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003526 }
3527
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003528 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003529 DCHECK(first.Equals(locations->Out()));
3530 if (second.IsFpuRegister()) {
3531 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3532 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3533 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003534 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003535 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3536 codegen_->LiteralDoubleAddress(
3537 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003538 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003539 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3540 } else {
3541 DCHECK(second.IsDoubleStackSlot());
3542 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3543 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003544 break;
3545 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003546
3547 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003548 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003549 }
3550}
3551
Roland Levillain232ade02015-04-20 15:14:36 +01003552void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3553 uint32_t temp_offset,
3554 uint32_t stack_adjustment,
3555 bool is_fp,
3556 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003557 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003558 DCHECK(!is_wide);
3559 if (is_fp) {
3560 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3561 } else {
3562 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3563 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003564 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003565 DCHECK(is_wide);
3566 if (is_fp) {
3567 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3568 } else {
3569 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3570 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003571 } else {
3572 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003573 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003574 Location stack_temp = Location::StackSlot(temp_offset);
3575 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003576 if (is_fp) {
3577 __ flds(Address(ESP, temp_offset));
3578 } else {
3579 __ filds(Address(ESP, temp_offset));
3580 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003581 } else {
3582 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3583 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003584 if (is_fp) {
3585 __ fldl(Address(ESP, temp_offset));
3586 } else {
3587 __ fildl(Address(ESP, temp_offset));
3588 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003589 }
3590 }
3591}
3592
3593void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003594 DataType::Type type = rem->GetResultType();
3595 bool is_float = type == DataType::Type::kFloat32;
3596 size_t elem_size = DataType::Size(type);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003597 LocationSummary* locations = rem->GetLocations();
3598 Location first = locations->InAt(0);
3599 Location second = locations->InAt(1);
3600 Location out = locations->Out();
3601
3602 // Create stack space for 2 elements.
3603 // TODO: enhance register allocator to ask for stack temporaries.
Vladimir Markodec78172020-06-19 15:31:23 +01003604 codegen_->IncreaseFrame(2 * elem_size);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003605
3606 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003607 const bool is_wide = !is_float;
Andreas Gampe3db70682018-12-26 15:12:03 -08003608 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp= */ true, is_wide);
3609 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp= */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003610
3611 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003612 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003613 __ Bind(&retry);
3614 __ fprem();
3615
3616 // Move FP status to AX.
3617 __ fstsw();
3618
3619 // And see if the argument reduction is complete. This is signaled by the
3620 // C2 FPU flag bit set to 0.
3621 __ andl(EAX, Immediate(kC2ConditionMask));
3622 __ j(kNotEqual, &retry);
3623
3624 // We have settled on the final value. Retrieve it into an XMM register.
3625 // Store FP top of stack to real stack.
3626 if (is_float) {
3627 __ fsts(Address(ESP, 0));
3628 } else {
3629 __ fstl(Address(ESP, 0));
3630 }
3631
3632 // Pop the 2 items from the FP stack.
3633 __ fucompp();
3634
3635 // Load the value from the stack into an XMM register.
3636 DCHECK(out.IsFpuRegister()) << out;
3637 if (is_float) {
3638 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3639 } else {
3640 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3641 }
3642
3643 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01003644 codegen_->DecreaseFrame(2 * elem_size);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003645}
3646
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003647
3648void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3649 DCHECK(instruction->IsDiv() || instruction->IsRem());
3650
3651 LocationSummary* locations = instruction->GetLocations();
3652 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003653 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003654
3655 Register out_register = locations->Out().AsRegister<Register>();
3656 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003657 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003658
3659 DCHECK(imm == 1 || imm == -1);
3660
3661 if (instruction->IsRem()) {
3662 __ xorl(out_register, out_register);
3663 } else {
3664 __ movl(out_register, input_register);
3665 if (imm == -1) {
3666 __ negl(out_register);
3667 }
3668 }
3669}
3670
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303671void InstructionCodeGeneratorX86::RemByPowerOfTwo(HRem* instruction) {
3672 LocationSummary* locations = instruction->GetLocations();
3673 Location second = locations->InAt(1);
3674
3675 Register out = locations->Out().AsRegister<Register>();
3676 Register numerator = locations->InAt(0).AsRegister<Register>();
3677
3678 int32_t imm = Int64FromConstant(second.GetConstant());
3679 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3680 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3681
3682 Register tmp = locations->GetTemp(0).AsRegister<Register>();
3683 NearLabel done;
3684 __ movl(out, numerator);
3685 __ andl(out, Immediate(abs_imm-1));
3686 __ j(Condition::kZero, &done);
3687 __ leal(tmp, Address(out, static_cast<int32_t>(~(abs_imm-1))));
3688 __ testl(numerator, numerator);
3689 __ cmovl(Condition::kLess, out, tmp);
3690 __ Bind(&done);
3691}
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003692
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003693void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003694 LocationSummary* locations = instruction->GetLocations();
3695
3696 Register out_register = locations->Out().AsRegister<Register>();
3697 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003698 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003699 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3700 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003701
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003702 Register num = locations->GetTemp(0).AsRegister<Register>();
3703
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003704 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003705 __ testl(input_register, input_register);
3706 __ cmovl(kGreaterEqual, num, input_register);
3707 int shift = CTZ(imm);
3708 __ sarl(num, Immediate(shift));
3709
3710 if (imm < 0) {
3711 __ negl(num);
3712 }
3713
3714 __ movl(out_register, num);
3715}
3716
3717void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3718 DCHECK(instruction->IsDiv() || instruction->IsRem());
3719
3720 LocationSummary* locations = instruction->GetLocations();
3721 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3722
3723 Register eax = locations->InAt(0).AsRegister<Register>();
3724 Register out = locations->Out().AsRegister<Register>();
3725 Register num;
3726 Register edx;
3727
3728 if (instruction->IsDiv()) {
3729 edx = locations->GetTemp(0).AsRegister<Register>();
3730 num = locations->GetTemp(1).AsRegister<Register>();
3731 } else {
3732 edx = locations->Out().AsRegister<Register>();
3733 num = locations->GetTemp(0).AsRegister<Register>();
3734 }
3735
3736 DCHECK_EQ(EAX, eax);
3737 DCHECK_EQ(EDX, edx);
3738 if (instruction->IsDiv()) {
3739 DCHECK_EQ(EAX, out);
3740 } else {
3741 DCHECK_EQ(EDX, out);
3742 }
3743
3744 int64_t magic;
3745 int shift;
Andreas Gampe3db70682018-12-26 15:12:03 -08003746 CalculateMagicAndShiftForDivRem(imm, /* is_long= */ false, &magic, &shift);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003747
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003748 // Save the numerator.
3749 __ movl(num, eax);
3750
3751 // EAX = magic
3752 __ movl(eax, Immediate(magic));
3753
3754 // EDX:EAX = magic * numerator
3755 __ imull(num);
3756
3757 if (imm > 0 && magic < 0) {
3758 // EDX += num
3759 __ addl(edx, num);
3760 } else if (imm < 0 && magic > 0) {
3761 __ subl(edx, num);
3762 }
3763
3764 // Shift if needed.
3765 if (shift != 0) {
3766 __ sarl(edx, Immediate(shift));
3767 }
3768
3769 // EDX += 1 if EDX < 0
3770 __ movl(eax, edx);
3771 __ shrl(edx, Immediate(31));
3772 __ addl(edx, eax);
3773
3774 if (instruction->IsRem()) {
3775 __ movl(eax, num);
3776 __ imull(edx, Immediate(imm));
3777 __ subl(eax, edx);
3778 __ movl(edx, eax);
3779 } else {
3780 __ movl(eax, edx);
3781 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003782}
3783
Calin Juravlebacfec32014-11-14 15:54:36 +00003784void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3785 DCHECK(instruction->IsDiv() || instruction->IsRem());
3786
3787 LocationSummary* locations = instruction->GetLocations();
3788 Location out = locations->Out();
3789 Location first = locations->InAt(0);
3790 Location second = locations->InAt(1);
3791 bool is_div = instruction->IsDiv();
3792
3793 switch (instruction->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003794 case DataType::Type::kInt32: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003795 DCHECK_EQ(EAX, first.AsRegister<Register>());
3796 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003797
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003798 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003799 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003800
3801 if (imm == 0) {
3802 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3803 } else if (imm == 1 || imm == -1) {
3804 DivRemOneOrMinusOne(instruction);
Shalini Salomi Bodapatia66784b2018-11-06 13:05:44 +05303805 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
3806 if (is_div) {
3807 DivByPowerOfTwo(instruction->AsDiv());
3808 } else {
3809 RemByPowerOfTwo(instruction->AsRem());
3810 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003811 } else {
3812 DCHECK(imm <= -2 || imm >= 2);
3813 GenerateDivRemWithAnyConstant(instruction);
3814 }
3815 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003816 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) DivRemMinusOneSlowPathX86(
David Srbecky9cd6d372016-02-09 15:24:47 +00003817 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003818 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003819
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003820 Register second_reg = second.AsRegister<Register>();
3821 // 0x80000000/-1 triggers an arithmetic exception!
3822 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3823 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003824
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003825 __ cmpl(second_reg, Immediate(-1));
3826 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003827
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003828 // edx:eax <- sign-extended of eax
3829 __ cdq();
3830 // eax = quotient, edx = remainder
3831 __ idivl(second_reg);
3832 __ Bind(slow_path->GetExitLabel());
3833 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003834 break;
3835 }
3836
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003837 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003838 InvokeRuntimeCallingConvention calling_convention;
3839 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3840 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3841 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3842 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3843 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3844 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3845
3846 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003847 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003848 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003849 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003850 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003851 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003852 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003853 break;
3854 }
3855
3856 default:
3857 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3858 }
3859}
3860
Calin Juravle7c4954d2014-10-28 16:57:40 +00003861void LocationsBuilderX86::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003862 LocationSummary::CallKind call_kind = (div->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003863 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003864 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003865 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003866
Calin Juravle7c4954d2014-10-28 16:57:40 +00003867 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003868 case DataType::Type::kInt32: {
Calin Juravled0d48522014-11-04 16:40:20 +00003869 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003870 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003871 locations->SetOut(Location::SameAsFirstInput());
3872 // Intel uses edx:eax as the dividend.
3873 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003874 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3875 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3876 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003877 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003878 locations->AddTemp(Location::RequiresRegister());
3879 }
Calin Juravled0d48522014-11-04 16:40:20 +00003880 break;
3881 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003882 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003883 InvokeRuntimeCallingConvention calling_convention;
3884 locations->SetInAt(0, Location::RegisterPairLocation(
3885 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3886 locations->SetInAt(1, Location::RegisterPairLocation(
3887 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3888 // Runtime helper puts the result in EAX, EDX.
3889 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003890 break;
3891 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003892 case DataType::Type::kFloat32:
3893 case DataType::Type::kFloat64: {
Calin Juravle7c4954d2014-10-28 16:57:40 +00003894 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003895 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3896 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003897 } else if (div->InputAt(1)->IsConstant()) {
3898 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003899 } else {
3900 locations->SetInAt(1, Location::Any());
3901 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003902 locations->SetOut(Location::SameAsFirstInput());
3903 break;
3904 }
3905
3906 default:
3907 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3908 }
3909}
3910
3911void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3912 LocationSummary* locations = div->GetLocations();
3913 Location first = locations->InAt(0);
3914 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003915
3916 switch (div->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003917 case DataType::Type::kInt32:
3918 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003919 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003920 break;
3921 }
3922
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003923 case DataType::Type::kFloat32: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003924 if (second.IsFpuRegister()) {
3925 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3926 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3927 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003928 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003929 __ divss(first.AsFpuRegister<XmmRegister>(),
3930 codegen_->LiteralFloatAddress(
3931 const_area->GetConstant()->AsFloatConstant()->GetValue(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003932 const_area->GetBaseMethodAddress(),
Mark Mendell0616ae02015-04-17 12:49:27 -04003933 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3934 } else {
3935 DCHECK(second.IsStackSlot());
3936 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3937 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003938 break;
3939 }
3940
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003941 case DataType::Type::kFloat64: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003942 if (second.IsFpuRegister()) {
3943 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3944 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3945 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003946 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003947 __ divsd(first.AsFpuRegister<XmmRegister>(),
3948 codegen_->LiteralDoubleAddress(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00003949 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3950 const_area->GetBaseMethodAddress(),
3951 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
Mark Mendell0616ae02015-04-17 12:49:27 -04003952 } else {
3953 DCHECK(second.IsDoubleStackSlot());
3954 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3955 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003956 break;
3957 }
3958
3959 default:
3960 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3961 }
3962}
3963
Calin Juravlebacfec32014-11-14 15:54:36 +00003964void LocationsBuilderX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003965 DataType::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003966
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003967 LocationSummary::CallKind call_kind = (rem->GetResultType() == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003968 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003969 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01003970 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003971
Calin Juravled2ec87d2014-12-08 14:24:46 +00003972 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003973 case DataType::Type::kInt32: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003974 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003975 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003976 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003977 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3978 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3979 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003980 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003981 locations->AddTemp(Location::RequiresRegister());
3982 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003983 break;
3984 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003985 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003986 InvokeRuntimeCallingConvention calling_convention;
3987 locations->SetInAt(0, Location::RegisterPairLocation(
3988 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3989 locations->SetInAt(1, Location::RegisterPairLocation(
3990 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3991 // Runtime helper puts the result in EAX, EDX.
3992 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3993 break;
3994 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003995 case DataType::Type::kFloat64:
3996 case DataType::Type::kFloat32: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003997 locations->SetInAt(0, Location::Any());
3998 locations->SetInAt(1, Location::Any());
3999 locations->SetOut(Location::RequiresFpuRegister());
4000 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00004001 break;
4002 }
4003
4004 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004005 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004006 }
4007}
4008
4009void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004010 DataType::Type type = rem->GetResultType();
Calin Juravlebacfec32014-11-14 15:54:36 +00004011 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004012 case DataType::Type::kInt32:
4013 case DataType::Type::kInt64: {
Calin Juravlebacfec32014-11-14 15:54:36 +00004014 GenerateDivRemIntegral(rem);
4015 break;
4016 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004017 case DataType::Type::kFloat32:
4018 case DataType::Type::kFloat64: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05004019 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00004020 break;
4021 }
4022 default:
4023 LOG(FATAL) << "Unexpected rem type " << type;
4024 }
4025}
4026
Aart Bik1f8d51b2018-02-15 10:42:37 -08004027static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
4028 LocationSummary* locations = new (allocator) LocationSummary(minmax);
4029 switch (minmax->GetResultType()) {
4030 case DataType::Type::kInt32:
4031 locations->SetInAt(0, Location::RequiresRegister());
4032 locations->SetInAt(1, Location::RequiresRegister());
4033 locations->SetOut(Location::SameAsFirstInput());
4034 break;
4035 case DataType::Type::kInt64:
4036 locations->SetInAt(0, Location::RequiresRegister());
4037 locations->SetInAt(1, Location::RequiresRegister());
4038 locations->SetOut(Location::SameAsFirstInput());
4039 // Register to use to perform a long subtract to set cc.
4040 locations->AddTemp(Location::RequiresRegister());
4041 break;
4042 case DataType::Type::kFloat32:
4043 locations->SetInAt(0, Location::RequiresFpuRegister());
4044 locations->SetInAt(1, Location::RequiresFpuRegister());
4045 locations->SetOut(Location::SameAsFirstInput());
4046 locations->AddTemp(Location::RequiresRegister());
4047 break;
4048 case DataType::Type::kFloat64:
4049 locations->SetInAt(0, Location::RequiresFpuRegister());
4050 locations->SetInAt(1, Location::RequiresFpuRegister());
4051 locations->SetOut(Location::SameAsFirstInput());
4052 break;
4053 default:
4054 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
4055 }
4056}
4057
Aart Bik351df3e2018-03-07 11:54:57 -08004058void InstructionCodeGeneratorX86::GenerateMinMaxInt(LocationSummary* locations,
4059 bool is_min,
4060 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08004061 Location op1_loc = locations->InAt(0);
4062 Location op2_loc = locations->InAt(1);
4063
4064 // Shortcut for same input locations.
4065 if (op1_loc.Equals(op2_loc)) {
4066 // Can return immediately, as op1_loc == out_loc.
4067 // Note: if we ever support separate registers, e.g., output into memory, we need to check for
4068 // a copy here.
4069 DCHECK(locations->Out().Equals(op1_loc));
4070 return;
4071 }
4072
4073 if (type == DataType::Type::kInt64) {
4074 // Need to perform a subtract to get the sign right.
4075 // op1 is already in the same location as the output.
4076 Location output = locations->Out();
4077 Register output_lo = output.AsRegisterPairLow<Register>();
4078 Register output_hi = output.AsRegisterPairHigh<Register>();
4079
4080 Register op2_lo = op2_loc.AsRegisterPairLow<Register>();
4081 Register op2_hi = op2_loc.AsRegisterPairHigh<Register>();
4082
4083 // The comparison is performed by subtracting the second operand from
4084 // the first operand and then setting the status flags in the same
4085 // manner as the SUB instruction."
4086 __ cmpl(output_lo, op2_lo);
4087
4088 // Now use a temp and the borrow to finish the subtraction of op2_hi.
4089 Register temp = locations->GetTemp(0).AsRegister<Register>();
4090 __ movl(temp, output_hi);
4091 __ sbbl(temp, op2_hi);
4092
4093 // Now the condition code is correct.
4094 Condition cond = is_min ? Condition::kGreaterEqual : Condition::kLess;
4095 __ cmovl(cond, output_lo, op2_lo);
4096 __ cmovl(cond, output_hi, op2_hi);
4097 } else {
4098 DCHECK_EQ(type, DataType::Type::kInt32);
4099 Register out = locations->Out().AsRegister<Register>();
4100 Register op2 = op2_loc.AsRegister<Register>();
4101
4102 // (out := op1)
4103 // out <=? op2
4104 // if out is min jmp done
4105 // out := op2
4106 // done:
4107
4108 __ cmpl(out, op2);
4109 Condition cond = is_min ? Condition::kGreater : Condition::kLess;
4110 __ cmovl(cond, out, op2);
4111 }
4112}
4113
4114void InstructionCodeGeneratorX86::GenerateMinMaxFP(LocationSummary* locations,
4115 bool is_min,
4116 DataType::Type type) {
4117 Location op1_loc = locations->InAt(0);
4118 Location op2_loc = locations->InAt(1);
4119 Location out_loc = locations->Out();
4120 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
4121
4122 // Shortcut for same input locations.
4123 if (op1_loc.Equals(op2_loc)) {
4124 DCHECK(out_loc.Equals(op1_loc));
4125 return;
4126 }
4127
4128 // (out := op1)
4129 // out <=? op2
4130 // if Nan jmp Nan_label
4131 // if out is min jmp done
4132 // if op2 is min jmp op2_label
4133 // handle -0/+0
4134 // jmp done
4135 // Nan_label:
4136 // out := NaN
4137 // op2_label:
4138 // out := op2
4139 // done:
4140 //
4141 // This removes one jmp, but needs to copy one input (op1) to out.
4142 //
4143 // TODO: This is straight from Quick (except literal pool). Make NaN an out-of-line slowpath?
4144
4145 XmmRegister op2 = op2_loc.AsFpuRegister<XmmRegister>();
4146
4147 NearLabel nan, done, op2_label;
4148 if (type == DataType::Type::kFloat64) {
4149 __ ucomisd(out, op2);
4150 } else {
4151 DCHECK_EQ(type, DataType::Type::kFloat32);
4152 __ ucomiss(out, op2);
4153 }
4154
4155 __ j(Condition::kParityEven, &nan);
4156
4157 __ j(is_min ? Condition::kAbove : Condition::kBelow, &op2_label);
4158 __ j(is_min ? Condition::kBelow : Condition::kAbove, &done);
4159
4160 // Handle 0.0/-0.0.
4161 if (is_min) {
4162 if (type == DataType::Type::kFloat64) {
4163 __ orpd(out, op2);
4164 } else {
4165 __ orps(out, op2);
4166 }
4167 } else {
4168 if (type == DataType::Type::kFloat64) {
4169 __ andpd(out, op2);
4170 } else {
4171 __ andps(out, op2);
4172 }
4173 }
4174 __ jmp(&done);
4175
4176 // NaN handling.
4177 __ Bind(&nan);
4178 if (type == DataType::Type::kFloat64) {
4179 // TODO: Use a constant from the constant table (requires extra input).
4180 __ LoadLongConstant(out, kDoubleNaN);
4181 } else {
4182 Register constant = locations->GetTemp(0).AsRegister<Register>();
4183 __ movl(constant, Immediate(kFloatNaN));
4184 __ movd(out, constant);
4185 }
4186 __ jmp(&done);
4187
4188 // out := op2;
4189 __ Bind(&op2_label);
4190 if (type == DataType::Type::kFloat64) {
4191 __ movsd(out, op2);
4192 } else {
4193 __ movss(out, op2);
4194 }
4195
4196 // Done.
4197 __ Bind(&done);
4198}
4199
Aart Bik351df3e2018-03-07 11:54:57 -08004200void InstructionCodeGeneratorX86::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
4201 DataType::Type type = minmax->GetResultType();
4202 switch (type) {
4203 case DataType::Type::kInt32:
4204 case DataType::Type::kInt64:
4205 GenerateMinMaxInt(minmax->GetLocations(), is_min, type);
4206 break;
4207 case DataType::Type::kFloat32:
4208 case DataType::Type::kFloat64:
4209 GenerateMinMaxFP(minmax->GetLocations(), is_min, type);
4210 break;
4211 default:
4212 LOG(FATAL) << "Unexpected type for HMinMax " << type;
4213 }
4214}
4215
Aart Bik1f8d51b2018-02-15 10:42:37 -08004216void LocationsBuilderX86::VisitMin(HMin* min) {
4217 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
4218}
4219
4220void InstructionCodeGeneratorX86::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08004221 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004222}
4223
4224void LocationsBuilderX86::VisitMax(HMax* max) {
4225 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
4226}
4227
4228void InstructionCodeGeneratorX86::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08004229 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08004230}
4231
Aart Bik3dad3412018-02-28 12:01:46 -08004232void LocationsBuilderX86::VisitAbs(HAbs* abs) {
4233 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
4234 switch (abs->GetResultType()) {
4235 case DataType::Type::kInt32:
4236 locations->SetInAt(0, Location::RegisterLocation(EAX));
4237 locations->SetOut(Location::SameAsFirstInput());
4238 locations->AddTemp(Location::RegisterLocation(EDX));
4239 break;
4240 case DataType::Type::kInt64:
4241 locations->SetInAt(0, Location::RequiresRegister());
4242 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4243 locations->AddTemp(Location::RequiresRegister());
4244 break;
4245 case DataType::Type::kFloat32:
4246 locations->SetInAt(0, Location::RequiresFpuRegister());
4247 locations->SetOut(Location::SameAsFirstInput());
4248 locations->AddTemp(Location::RequiresFpuRegister());
4249 locations->AddTemp(Location::RequiresRegister());
4250 break;
4251 case DataType::Type::kFloat64:
4252 locations->SetInAt(0, Location::RequiresFpuRegister());
4253 locations->SetOut(Location::SameAsFirstInput());
4254 locations->AddTemp(Location::RequiresFpuRegister());
4255 break;
4256 default:
4257 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4258 }
4259}
4260
4261void InstructionCodeGeneratorX86::VisitAbs(HAbs* abs) {
4262 LocationSummary* locations = abs->GetLocations();
4263 switch (abs->GetResultType()) {
4264 case DataType::Type::kInt32: {
4265 Register out = locations->Out().AsRegister<Register>();
4266 DCHECK_EQ(out, EAX);
4267 Register temp = locations->GetTemp(0).AsRegister<Register>();
4268 DCHECK_EQ(temp, EDX);
4269 // Sign extend EAX into EDX.
4270 __ cdq();
4271 // XOR EAX with sign.
4272 __ xorl(EAX, EDX);
4273 // Subtract out sign to correct.
4274 __ subl(EAX, EDX);
4275 // The result is in EAX.
4276 break;
4277 }
4278 case DataType::Type::kInt64: {
4279 Location input = locations->InAt(0);
4280 Register input_lo = input.AsRegisterPairLow<Register>();
4281 Register input_hi = input.AsRegisterPairHigh<Register>();
4282 Location output = locations->Out();
4283 Register output_lo = output.AsRegisterPairLow<Register>();
4284 Register output_hi = output.AsRegisterPairHigh<Register>();
4285 Register temp = locations->GetTemp(0).AsRegister<Register>();
4286 // Compute the sign into the temporary.
4287 __ movl(temp, input_hi);
4288 __ sarl(temp, Immediate(31));
4289 // Store the sign into the output.
4290 __ movl(output_lo, temp);
4291 __ movl(output_hi, temp);
4292 // XOR the input to the output.
4293 __ xorl(output_lo, input_lo);
4294 __ xorl(output_hi, input_hi);
4295 // Subtract the sign.
4296 __ subl(output_lo, temp);
4297 __ sbbl(output_hi, temp);
4298 break;
4299 }
4300 case DataType::Type::kFloat32: {
4301 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4302 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4303 Register constant = locations->GetTemp(1).AsRegister<Register>();
4304 __ movl(constant, Immediate(INT32_C(0x7FFFFFFF)));
4305 __ movd(temp, constant);
4306 __ andps(out, temp);
4307 break;
4308 }
4309 case DataType::Type::kFloat64: {
4310 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4311 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4312 // TODO: Use a constant from the constant table (requires extra input).
4313 __ LoadLongConstant(temp, INT64_C(0x7FFFFFFFFFFFFFFF));
4314 __ andpd(out, temp);
4315 break;
4316 }
4317 default:
4318 LOG(FATAL) << "Unexpected type for HAbs " << abs->GetResultType();
4319 }
4320}
4321
Calin Juravled0d48522014-11-04 16:40:20 +00004322void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004323 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004324 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004325 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004326 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004327 case DataType::Type::kInt8:
4328 case DataType::Type::kUint16:
4329 case DataType::Type::kInt16:
4330 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004331 locations->SetInAt(0, Location::Any());
4332 break;
4333 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004334 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004335 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
4336 if (!instruction->IsConstant()) {
4337 locations->AddTemp(Location::RequiresRegister());
4338 }
4339 break;
4340 }
4341 default:
4342 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4343 }
Calin Juravled0d48522014-11-04 16:40:20 +00004344}
4345
4346void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01004347 SlowPathCode* slow_path =
4348 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004349 codegen_->AddSlowPath(slow_path);
4350
4351 LocationSummary* locations = instruction->GetLocations();
4352 Location value = locations->InAt(0);
4353
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004354 switch (instruction->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004355 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004356 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004357 case DataType::Type::kInt8:
4358 case DataType::Type::kUint16:
4359 case DataType::Type::kInt16:
4360 case DataType::Type::kInt32: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004361 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004362 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004363 __ j(kEqual, slow_path->GetEntryLabel());
4364 } else if (value.IsStackSlot()) {
4365 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
4366 __ j(kEqual, slow_path->GetEntryLabel());
4367 } else {
4368 DCHECK(value.IsConstant()) << value;
4369 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01004370 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004371 }
4372 }
4373 break;
Calin Juravled0d48522014-11-04 16:40:20 +00004374 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004375 case DataType::Type::kInt64: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004376 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004377 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004378 __ movl(temp, value.AsRegisterPairLow<Register>());
4379 __ orl(temp, value.AsRegisterPairHigh<Register>());
4380 __ j(kEqual, slow_path->GetEntryLabel());
4381 } else {
4382 DCHECK(value.IsConstant()) << value;
4383 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4384 __ jmp(slow_path->GetEntryLabel());
4385 }
4386 }
4387 break;
4388 }
4389 default:
4390 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00004391 }
Calin Juravled0d48522014-11-04 16:40:20 +00004392}
4393
Calin Juravle9aec02f2014-11-18 23:06:35 +00004394void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
4395 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4396
4397 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004398 new (GetGraph()->GetAllocator()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004399
4400 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004401 case DataType::Type::kInt32:
4402 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004403 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00004404 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00004405 // The shift count needs to be in CL or a constant.
4406 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004407 locations->SetOut(Location::SameAsFirstInput());
4408 break;
4409 }
4410 default:
4411 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4412 }
4413}
4414
4415void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
4416 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4417
4418 LocationSummary* locations = op->GetLocations();
4419 Location first = locations->InAt(0);
4420 Location second = locations->InAt(1);
4421 DCHECK(first.Equals(locations->Out()));
4422
4423 switch (op->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004424 case DataType::Type::kInt32: {
Mark P Mendell73945692015-04-29 14:56:17 +00004425 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004426 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004427 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004428 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004429 DCHECK_EQ(ECX, second_reg);
4430 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004431 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004432 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004433 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004434 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004435 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004436 }
4437 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004438 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004439 if (shift == 0) {
4440 return;
4441 }
4442 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004443 if (op->IsShl()) {
4444 __ shll(first_reg, imm);
4445 } else if (op->IsShr()) {
4446 __ sarl(first_reg, imm);
4447 } else {
4448 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004449 }
4450 }
4451 break;
4452 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004453 case DataType::Type::kInt64: {
Mark P Mendell73945692015-04-29 14:56:17 +00004454 if (second.IsRegister()) {
4455 Register second_reg = second.AsRegister<Register>();
4456 DCHECK_EQ(ECX, second_reg);
4457 if (op->IsShl()) {
4458 GenerateShlLong(first, second_reg);
4459 } else if (op->IsShr()) {
4460 GenerateShrLong(first, second_reg);
4461 } else {
4462 GenerateUShrLong(first, second_reg);
4463 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004464 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00004465 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004466 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00004467 // Nothing to do if the shift is 0, as the input is already the output.
4468 if (shift != 0) {
4469 if (op->IsShl()) {
4470 GenerateShlLong(first, shift);
4471 } else if (op->IsShr()) {
4472 GenerateShrLong(first, shift);
4473 } else {
4474 GenerateUShrLong(first, shift);
4475 }
4476 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00004477 }
4478 break;
4479 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004480 default:
4481 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
4482 }
4483}
4484
Mark P Mendell73945692015-04-29 14:56:17 +00004485void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
4486 Register low = loc.AsRegisterPairLow<Register>();
4487 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04004488 if (shift == 1) {
4489 // This is just an addition.
4490 __ addl(low, low);
4491 __ adcl(high, high);
4492 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00004493 // Shift by 32 is easy. High gets low, and low gets 0.
4494 codegen_->EmitParallelMoves(
4495 loc.ToLow(),
4496 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004497 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004498 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4499 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004500 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004501 } else if (shift > 32) {
4502 // Low part becomes 0. High part is low part << (shift-32).
4503 __ movl(high, low);
4504 __ shll(high, Immediate(shift - 32));
4505 __ xorl(low, low);
4506 } else {
4507 // Between 1 and 31.
4508 __ shld(high, low, Immediate(shift));
4509 __ shll(low, Immediate(shift));
4510 }
4511}
4512
Calin Juravle9aec02f2014-11-18 23:06:35 +00004513void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004514 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004515 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
4516 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
4517 __ testl(shifter, Immediate(32));
4518 __ j(kEqual, &done);
4519 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
4520 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
4521 __ Bind(&done);
4522}
4523
Mark P Mendell73945692015-04-29 14:56:17 +00004524void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
4525 Register low = loc.AsRegisterPairLow<Register>();
4526 Register high = loc.AsRegisterPairHigh<Register>();
4527 if (shift == 32) {
4528 // Need to copy the sign.
4529 DCHECK_NE(low, high);
4530 __ movl(low, high);
4531 __ sarl(high, Immediate(31));
4532 } else if (shift > 32) {
4533 DCHECK_NE(low, high);
4534 // High part becomes sign. Low part is shifted by shift - 32.
4535 __ movl(low, high);
4536 __ sarl(high, Immediate(31));
4537 __ sarl(low, Immediate(shift - 32));
4538 } else {
4539 // Between 1 and 31.
4540 __ shrd(low, high, Immediate(shift));
4541 __ sarl(high, Immediate(shift));
4542 }
4543}
4544
Calin Juravle9aec02f2014-11-18 23:06:35 +00004545void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004546 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004547 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4548 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
4549 __ testl(shifter, Immediate(32));
4550 __ j(kEqual, &done);
4551 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4552 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
4553 __ Bind(&done);
4554}
4555
Mark P Mendell73945692015-04-29 14:56:17 +00004556void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4557 Register low = loc.AsRegisterPairLow<Register>();
4558 Register high = loc.AsRegisterPairHigh<Register>();
4559 if (shift == 32) {
4560 // Shift by 32 is easy. Low gets high, and high gets 0.
4561 codegen_->EmitParallelMoves(
4562 loc.ToHigh(),
4563 loc.ToLow(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004564 DataType::Type::kInt32,
Mark P Mendell73945692015-04-29 14:56:17 +00004565 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4566 loc.ToHigh(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004567 DataType::Type::kInt32);
Mark P Mendell73945692015-04-29 14:56:17 +00004568 } else if (shift > 32) {
4569 // Low part is high >> (shift - 32). High part becomes 0.
4570 __ movl(low, high);
4571 __ shrl(low, Immediate(shift - 32));
4572 __ xorl(high, high);
4573 } else {
4574 // Between 1 and 31.
4575 __ shrd(low, high, Immediate(shift));
4576 __ shrl(high, Immediate(shift));
4577 }
4578}
4579
Calin Juravle9aec02f2014-11-18 23:06:35 +00004580void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004581 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004582 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4583 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4584 __ testl(shifter, Immediate(32));
4585 __ j(kEqual, &done);
4586 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4587 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4588 __ Bind(&done);
4589}
4590
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004591void LocationsBuilderX86::VisitRor(HRor* ror) {
4592 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004593 new (GetGraph()->GetAllocator()) LocationSummary(ror, LocationSummary::kNoCall);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004594
4595 switch (ror->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004596 case DataType::Type::kInt64:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004597 // Add the temporary needed.
4598 locations->AddTemp(Location::RequiresRegister());
4599 FALLTHROUGH_INTENDED;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004600 case DataType::Type::kInt32:
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004601 locations->SetInAt(0, Location::RequiresRegister());
4602 // The shift count needs to be in CL (unless it is a constant).
4603 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4604 locations->SetOut(Location::SameAsFirstInput());
4605 break;
4606 default:
4607 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4608 UNREACHABLE();
4609 }
4610}
4611
4612void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4613 LocationSummary* locations = ror->GetLocations();
4614 Location first = locations->InAt(0);
4615 Location second = locations->InAt(1);
4616
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004617 if (ror->GetResultType() == DataType::Type::kInt32) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004618 Register first_reg = first.AsRegister<Register>();
4619 if (second.IsRegister()) {
4620 Register second_reg = second.AsRegister<Register>();
4621 __ rorl(first_reg, second_reg);
4622 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004623 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004624 __ rorl(first_reg, imm);
4625 }
4626 return;
4627 }
4628
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004629 DCHECK_EQ(ror->GetResultType(), DataType::Type::kInt64);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004630 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4631 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4632 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4633 if (second.IsRegister()) {
4634 Register second_reg = second.AsRegister<Register>();
4635 DCHECK_EQ(second_reg, ECX);
4636 __ movl(temp_reg, first_reg_hi);
4637 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4638 __ shrd(first_reg_lo, temp_reg, second_reg);
4639 __ movl(temp_reg, first_reg_hi);
4640 __ testl(second_reg, Immediate(32));
4641 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4642 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4643 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004644 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004645 if (shift_amt == 0) {
4646 // Already fine.
4647 return;
4648 }
4649 if (shift_amt == 32) {
4650 // Just swap.
4651 __ movl(temp_reg, first_reg_lo);
4652 __ movl(first_reg_lo, first_reg_hi);
4653 __ movl(first_reg_hi, temp_reg);
4654 return;
4655 }
4656
4657 Immediate imm(shift_amt);
4658 // Save the constents of the low value.
4659 __ movl(temp_reg, first_reg_lo);
4660
4661 // Shift right into low, feeding bits from high.
4662 __ shrd(first_reg_lo, first_reg_hi, imm);
4663
4664 // Shift right into high, feeding bits from the original low.
4665 __ shrd(first_reg_hi, temp_reg, imm);
4666
4667 // Swap if needed.
4668 if (shift_amt > 32) {
4669 __ movl(temp_reg, first_reg_lo);
4670 __ movl(first_reg_lo, first_reg_hi);
4671 __ movl(first_reg_hi, temp_reg);
4672 }
4673 }
4674}
4675
Calin Juravle9aec02f2014-11-18 23:06:35 +00004676void LocationsBuilderX86::VisitShl(HShl* shl) {
4677 HandleShift(shl);
4678}
4679
4680void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4681 HandleShift(shl);
4682}
4683
4684void LocationsBuilderX86::VisitShr(HShr* shr) {
4685 HandleShift(shr);
4686}
4687
4688void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4689 HandleShift(shr);
4690}
4691
4692void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4693 HandleShift(ushr);
4694}
4695
4696void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4697 HandleShift(ushr);
4698}
4699
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004700void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004701 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4702 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004703 locations->SetOut(Location::RegisterLocation(EAX));
Alex Lightd109e302018-06-27 10:25:41 -07004704 InvokeRuntimeCallingConvention calling_convention;
4705 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004706}
4707
4708void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07004709 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
4710 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
4711 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004712}
4713
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004714void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004715 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
4716 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004717 locations->SetOut(Location::RegisterLocation(EAX));
4718 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004719 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4720 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004721}
4722
4723void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01004724 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
4725 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004726 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004727 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004728 DCHECK(!codegen_->IsLeafMethod());
4729}
4730
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004731void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004732 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004733 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004734 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4735 if (location.IsStackSlot()) {
4736 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4737 } else if (location.IsDoubleStackSlot()) {
4738 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004739 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004740 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004741}
4742
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004743void InstructionCodeGeneratorX86::VisitParameterValue(
4744 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4745}
4746
4747void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4748 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004749 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004750 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4751}
4752
4753void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004754}
4755
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004756void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4757 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004758 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004759 locations->SetInAt(0, Location::RequiresRegister());
4760 locations->SetOut(Location::RequiresRegister());
4761}
4762
4763void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4764 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004765 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004766 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004767 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004768 __ movl(locations->Out().AsRegister<Register>(),
4769 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004770 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004771 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004772 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004773 __ movl(locations->Out().AsRegister<Register>(),
4774 Address(locations->InAt(0).AsRegister<Register>(),
4775 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4776 // temp = temp->GetImtEntryAt(method_offset);
4777 __ movl(locations->Out().AsRegister<Register>(),
4778 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004779 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004780}
4781
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004782void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004783 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004784 new (GetGraph()->GetAllocator()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004785 locations->SetInAt(0, Location::RequiresRegister());
4786 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004787}
4788
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004789void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4790 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004791 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004792 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004793 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004794 switch (not_->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004795 case DataType::Type::kInt32:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004796 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004797 break;
4798
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004799 case DataType::Type::kInt64:
Roland Levillain70566432014-10-24 16:20:17 +01004800 __ notl(out.AsRegisterPairLow<Register>());
4801 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004802 break;
4803
4804 default:
4805 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4806 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004807}
4808
David Brazdil66d126e2015-04-03 16:02:44 +01004809void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4810 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004811 new (GetGraph()->GetAllocator()) LocationSummary(bool_not, LocationSummary::kNoCall);
David Brazdil66d126e2015-04-03 16:02:44 +01004812 locations->SetInAt(0, Location::RequiresRegister());
4813 locations->SetOut(Location::SameAsFirstInput());
4814}
4815
4816void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004817 LocationSummary* locations = bool_not->GetLocations();
4818 Location in = locations->InAt(0);
4819 Location out = locations->Out();
4820 DCHECK(in.Equals(out));
4821 __ xorl(out.AsRegister<Register>(), Immediate(1));
4822}
4823
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004824void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004825 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004826 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004827 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004828 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004829 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004830 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004831 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004832 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004833 case DataType::Type::kInt32:
4834 case DataType::Type::kInt64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004835 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004836 locations->SetInAt(1, Location::Any());
4837 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4838 break;
4839 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004840 case DataType::Type::kFloat32:
4841 case DataType::Type::kFloat64: {
Calin Juravleddb7df22014-11-25 20:56:51 +00004842 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004843 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4844 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4845 } else if (compare->InputAt(1)->IsConstant()) {
4846 locations->SetInAt(1, Location::RequiresFpuRegister());
4847 } else {
4848 locations->SetInAt(1, Location::Any());
4849 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004850 locations->SetOut(Location::RequiresRegister());
4851 break;
4852 }
4853 default:
4854 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4855 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004856}
4857
4858void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004859 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004860 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004861 Location left = locations->InAt(0);
4862 Location right = locations->InAt(1);
4863
Mark Mendell0c9497d2015-08-21 09:30:05 -04004864 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004865 Condition less_cond = kLess;
4866
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004867 switch (compare->InputAt(0)->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004868 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004869 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004870 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004871 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004872 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004873 case DataType::Type::kInt32: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004874 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004875 break;
4876 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004877 case DataType::Type::kInt64: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004878 Register left_low = left.AsRegisterPairLow<Register>();
4879 Register left_high = left.AsRegisterPairHigh<Register>();
4880 int32_t val_low = 0;
4881 int32_t val_high = 0;
4882 bool right_is_const = false;
4883
4884 if (right.IsConstant()) {
4885 DCHECK(right.GetConstant()->IsLongConstant());
4886 right_is_const = true;
4887 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4888 val_low = Low32Bits(val);
4889 val_high = High32Bits(val);
4890 }
4891
Calin Juravleddb7df22014-11-25 20:56:51 +00004892 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004893 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004894 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004895 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004896 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004897 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004898 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004899 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004900 __ j(kLess, &less); // Signed compare.
4901 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004902 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004903 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004904 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004905 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004906 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004907 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004908 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004909 }
Aart Bika19616e2016-02-01 18:57:58 -08004910 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004911 break;
4912 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004913 case DataType::Type::kFloat32: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004914 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004915 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004916 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004917 break;
4918 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004919 case DataType::Type::kFloat64: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004920 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004921 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004922 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004923 break;
4924 }
4925 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004926 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004927 }
Aart Bika19616e2016-02-01 18:57:58 -08004928
Calin Juravleddb7df22014-11-25 20:56:51 +00004929 __ movl(out, Immediate(0));
4930 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004931 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004932
4933 __ Bind(&greater);
4934 __ movl(out, Immediate(1));
4935 __ jmp(&done);
4936
4937 __ Bind(&less);
4938 __ movl(out, Immediate(-1));
4939
4940 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004941}
4942
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004943void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004944 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004945 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004946 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004947 locations->SetInAt(i, Location::Any());
4948 }
4949 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004950}
4951
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004952void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004953 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004954}
4955
Roland Levillain7c1559a2015-12-15 10:55:36 +00004956void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004957 /*
4958 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4959 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4960 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4961 */
4962 switch (kind) {
4963 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004964 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004965 break;
4966 }
4967 case MemBarrierKind::kAnyStore:
4968 case MemBarrierKind::kLoadAny:
4969 case MemBarrierKind::kStoreStore: {
4970 // nop
4971 break;
4972 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004973 case MemBarrierKind::kNTStoreStore:
4974 // Non-Temporal Store/Store needs an explicit fence.
Andreas Gampe3db70682018-12-26 15:12:03 -08004975 MemoryFence(/* non-temporal= */ true);
Mark Mendell7aa04a12016-01-27 22:39:07 -05004976 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004977 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004978}
4979
Vladimir Markodc151b22015-10-15 18:02:30 +01004980HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4981 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01004982 ArtMethod* method ATTRIBUTE_UNUSED) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00004983 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004984}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004985
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004986Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4987 Register temp) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004988 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004989 if (!invoke->GetLocations()->Intrinsified()) {
4990 return location.AsRegister<Register>();
4991 }
4992 // For intrinsics we allow any location, so it may be on the stack.
4993 if (!location.IsRegister()) {
4994 __ movl(temp, Address(ESP, location.GetStackIndex()));
4995 return temp;
4996 }
4997 // For register locations, check if the register was saved. If so, get it from the stack.
4998 // Note: There is a chance that the register was saved but not overwritten, so we could
4999 // save one load. However, since this is just an intrinsic slow path we prefer this
5000 // simple and more robust approach rather that trying to determine if that's the case.
5001 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Marko4ee8e292017-06-02 15:39:30 +00005002 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
5003 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
5004 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
5005 __ movl(temp, Address(ESP, stack_offset));
5006 return temp;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005007 }
5008 return location.AsRegister<Register>();
5009}
5010
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005011void CodeGeneratorX86::GenerateStaticOrDirectCall(
5012 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00005013 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
5014 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005015 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00005016 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005017 uint32_t offset =
5018 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
5019 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00005020 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01005021 }
Vladimir Marko58155012015-08-19 12:49:41 +00005022 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Marko86c87522020-05-11 16:55:55 +01005023 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00005024 break;
Vladimir Marko65979462017-05-19 17:25:12 +01005025 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01005026 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko65979462017-05-19 17:25:12 +01005027 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
5028 temp.AsRegister<Register>());
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005029 __ leal(temp.AsRegister<Register>(),
5030 Address(base_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005031 RecordBootImageMethodPatch(invoke);
Vladimir Marko65979462017-05-19 17:25:12 +01005032 break;
5033 }
Vladimir Markob066d432018-01-03 13:14:37 +00005034 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
5035 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
5036 temp.AsRegister<Register>());
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005037 __ movl(temp.AsRegister<Register>(), Address(base_reg, kPlaceholder32BitOffset));
Vladimir Markob066d432018-01-03 13:14:37 +00005038 RecordBootImageRelRoPatch(
5039 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress(),
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005040 GetBootImageOffset(invoke));
Vladimir Markob066d432018-01-03 13:14:37 +00005041 break;
5042 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005043 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005044 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
5045 temp.AsRegister<Register>());
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005046 __ movl(temp.AsRegister<Register>(), Address(base_reg, kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005047 RecordMethodBssEntryPatch(invoke);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01005048 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005049 break;
5050 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01005051 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
5052 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
5053 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005054 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
5055 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
5056 return; // No code pointer retrieval; the runtime performs the call directly.
Vladimir Marko9b688a02015-05-06 14:12:42 +01005057 }
Vladimir Marko58155012015-08-19 12:49:41 +00005058 }
5059
5060 switch (invoke->GetCodePtrLocation()) {
5061 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
5062 __ call(GetFrameEntryLabel());
Vladimir Marko86c87522020-05-11 16:55:55 +01005063 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko58155012015-08-19 12:49:41 +00005064 break;
Vladimir Marko86c87522020-05-11 16:55:55 +01005065 case HInvokeStaticOrDirect::CodePtrLocation::kCallCriticalNative: {
Vladimir Marko86c87522020-05-11 16:55:55 +01005066 size_t out_frame_size =
5067 PrepareCriticalNativeCall<CriticalNativeCallingConventionVisitorX86,
5068 kNativeStackAlignment,
Vladimir Markodec78172020-06-19 15:31:23 +01005069 GetCriticalNativeDirectCallFrameSize>(invoke);
Vladimir Marko86c87522020-05-11 16:55:55 +01005070 // (callee_method + offset_of_jni_entry_point)()
5071 __ call(Address(callee_method.AsRegister<Register>(),
5072 ArtMethod::EntryPointFromJniOffset(kX86PointerSize).Int32Value()));
5073 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
5074 if (out_frame_size == 0u && DataType::IsFloatingPointType(invoke->GetType())) {
5075 // Create space for conversion.
5076 out_frame_size = 8u;
Vladimir Markodec78172020-06-19 15:31:23 +01005077 IncreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01005078 }
5079 // Zero-/sign-extend or move the result when needed due to native and managed ABI mismatch.
5080 switch (invoke->GetType()) {
5081 case DataType::Type::kBool:
5082 __ movzxb(EAX, AL);
5083 break;
5084 case DataType::Type::kInt8:
5085 __ movsxb(EAX, AL);
5086 break;
5087 case DataType::Type::kUint16:
5088 __ movzxw(EAX, EAX);
5089 break;
5090 case DataType::Type::kInt16:
5091 __ movsxw(EAX, EAX);
5092 break;
5093 case DataType::Type::kFloat32:
5094 __ fstps(Address(ESP, 0));
5095 __ movss(XMM0, Address(ESP, 0));
5096 break;
5097 case DataType::Type::kFloat64:
5098 __ fstpl(Address(ESP, 0));
5099 __ movsd(XMM0, Address(ESP, 0));
5100 break;
5101 case DataType::Type::kInt32:
5102 case DataType::Type::kInt64:
5103 case DataType::Type::kVoid:
5104 break;
5105 default:
5106 DCHECK(false) << invoke->GetType();
5107 break;
5108 }
5109 if (out_frame_size != 0u) {
Vladimir Markodec78172020-06-19 15:31:23 +01005110 DecreaseFrame(out_frame_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01005111 }
5112 break;
5113 }
Vladimir Marko58155012015-08-19 12:49:41 +00005114 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
5115 // (callee_method + offset_of_quick_compiled_code)()
5116 __ call(Address(callee_method.AsRegister<Register>(),
5117 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005118 kX86PointerSize).Int32Value()));
Vladimir Marko86c87522020-05-11 16:55:55 +01005119 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Vladimir Marko58155012015-08-19 12:49:41 +00005120 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04005121 }
5122
5123 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04005124}
5125
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005126void CodeGeneratorX86::GenerateVirtualCall(
5127 HInvokeVirtual* invoke, Location temp_in, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005128 Register temp = temp_in.AsRegister<Register>();
5129 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
5130 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005131
5132 // Use the calling convention instead of the location of the receiver, as
5133 // intrinsics may have put the receiver in a different register. In the intrinsics
5134 // slow path, the arguments have been moved to the right place, so here we are
5135 // guaranteed that the receiver is the first register of the calling convention.
5136 InvokeDexCallingConvention calling_convention;
5137 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005138 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005139 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00005140 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005141 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005142 // Instead of simply (possibly) unpoisoning `temp` here, we should
5143 // emit a read barrier for the previous class reference load.
5144 // However this is not required in practice, as this is an
5145 // intermediate/temporary reference and because the current
5146 // concurrent copying collector keeps the from-space memory
5147 // intact/accessible until the end of the marking phase (the
5148 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005149 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffraye2a3aa92019-11-25 17:52:58 +00005150
5151 MaybeGenerateInlineCacheCheck(invoke, temp);
5152
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005153 // temp = temp->GetMethodAt(method_offset);
5154 __ movl(temp, Address(temp, method_offset));
5155 // call temp->GetEntryPoint();
5156 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07005157 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Vladimir Markoe7197bf2017-06-02 17:00:23 +01005158 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00005159}
5160
Vladimir Marko6fd16062018-06-26 11:02:04 +01005161void CodeGeneratorX86::RecordBootImageIntrinsicPatch(HX86ComputeBaseMethodAddress* method_address,
5162 uint32_t intrinsic_data) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005163 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005164 method_address, /* target_dex_file= */ nullptr, intrinsic_data);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005165 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Marko6fd16062018-06-26 11:02:04 +01005166}
5167
Vladimir Markob066d432018-01-03 13:14:37 +00005168void CodeGeneratorX86::RecordBootImageRelRoPatch(HX86ComputeBaseMethodAddress* method_address,
5169 uint32_t boot_image_offset) {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005170 boot_image_other_patches_.emplace_back(
Andreas Gampe3db70682018-12-26 15:12:03 -08005171 method_address, /* target_dex_file= */ nullptr, boot_image_offset);
Vladimir Marko2d06e022019-07-08 15:45:19 +01005172 __ Bind(&boot_image_other_patches_.back().label);
Vladimir Markob066d432018-01-03 13:14:37 +00005173}
5174
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005175void CodeGeneratorX86::RecordBootImageMethodPatch(HInvokeStaticOrDirect* invoke) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005176 HX86ComputeBaseMethodAddress* method_address =
Vladimir Marko65979462017-05-19 17:25:12 +01005177 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005178 boot_image_method_patches_.emplace_back(
5179 method_address, invoke->GetTargetMethod().dex_file, invoke->GetTargetMethod().index);
Vladimir Marko65979462017-05-19 17:25:12 +01005180 __ Bind(&boot_image_method_patches_.back().label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005181}
5182
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005183void CodeGeneratorX86::RecordMethodBssEntryPatch(HInvokeStaticOrDirect* invoke) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005184 HX86ComputeBaseMethodAddress* method_address =
5185 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005186 // Add the patch entry and bind its label at the end of the instruction.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005187 method_bss_entry_patches_.emplace_back(
5188 method_address, &GetGraph()->GetDexFile(), invoke->GetDexMethodIndex());
5189 __ Bind(&method_bss_entry_patches_.back().label);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005190}
5191
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005192void CodeGeneratorX86::RecordBootImageTypePatch(HLoadClass* load_class) {
5193 HX86ComputeBaseMethodAddress* method_address =
5194 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5195 boot_image_type_patches_.emplace_back(
5196 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005197 __ Bind(&boot_image_type_patches_.back().label);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005198}
5199
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005200Label* CodeGeneratorX86::NewTypeBssEntryPatch(HLoadClass* load_class) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005201 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005202 load_class->InputAt(0)->AsX86ComputeBaseMethodAddress();
5203 type_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005204 method_address, &load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005205 return &type_bss_entry_patches_.back().label;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005206}
5207
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005208void CodeGeneratorX86::RecordBootImageStringPatch(HLoadString* load_string) {
5209 HX86ComputeBaseMethodAddress* method_address =
5210 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
5211 boot_image_string_patches_.emplace_back(
5212 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
5213 __ Bind(&boot_image_string_patches_.back().label);
Vladimir Marko65979462017-05-19 17:25:12 +01005214}
5215
Vladimir Markoaad75c62016-10-03 08:46:48 +00005216Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005217 HX86ComputeBaseMethodAddress* method_address =
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005218 load_string->InputAt(0)->AsX86ComputeBaseMethodAddress();
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005219 string_bss_entry_patches_.emplace_back(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005220 method_address, &load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01005221 return &string_bss_entry_patches_.back().label;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005222}
5223
Vladimir Markoeebb8212018-06-05 14:57:24 +01005224void CodeGeneratorX86::LoadBootImageAddress(Register reg,
Vladimir Marko6fd16062018-06-26 11:02:04 +01005225 uint32_t boot_image_reference,
Vladimir Markoeebb8212018-06-05 14:57:24 +01005226 HInvokeStaticOrDirect* invoke) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005227 if (GetCompilerOptions().IsBootImage()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01005228 HX86ComputeBaseMethodAddress* method_address =
5229 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5230 DCHECK(method_address != nullptr);
5231 Register method_address_reg =
5232 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005233 __ leal(reg, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005234 RecordBootImageIntrinsicPatch(method_address, boot_image_reference);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01005235 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Markoeebb8212018-06-05 14:57:24 +01005236 HX86ComputeBaseMethodAddress* method_address =
5237 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5238 DCHECK(method_address != nullptr);
5239 Register method_address_reg =
5240 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005241 __ movl(reg, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005242 RecordBootImageRelRoPatch(method_address, boot_image_reference);
Vladimir Markoeebb8212018-06-05 14:57:24 +01005243 } else {
Vladimir Marko695348f2020-05-19 14:42:02 +01005244 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markoeebb8212018-06-05 14:57:24 +01005245 gc::Heap* heap = Runtime::Current()->GetHeap();
5246 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01005247 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01005248 __ movl(reg, Immediate(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address))));
5249 }
5250}
5251
Vladimir Marko6fd16062018-06-26 11:02:04 +01005252void CodeGeneratorX86::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
5253 uint32_t boot_image_offset) {
5254 DCHECK(invoke->IsStatic());
5255 InvokeRuntimeCallingConvention calling_convention;
5256 Register argument = calling_convention.GetRegisterAt(0);
5257 if (GetCompilerOptions().IsBootImage()) {
5258 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
5259 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
Vladimir Marko6fd16062018-06-26 11:02:04 +01005260 HX86ComputeBaseMethodAddress* method_address =
5261 invoke->InputAt(invoke->GetSpecialInputIndex())->AsX86ComputeBaseMethodAddress();
5262 DCHECK(method_address != nullptr);
5263 Register method_address_reg =
5264 invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex()).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00005265 __ leal(argument, Address(method_address_reg, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko6fd16062018-06-26 11:02:04 +01005266 MethodReference target_method = invoke->GetTargetMethod();
5267 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
5268 boot_image_type_patches_.emplace_back(method_address, target_method.dex_file, type_idx.index_);
5269 __ Bind(&boot_image_type_patches_.back().label);
5270 } else {
5271 LoadBootImageAddress(argument, boot_image_offset, invoke);
5272 }
5273 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
5274 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
5275}
5276
Vladimir Markoaad75c62016-10-03 08:46:48 +00005277// The label points to the end of the "movl" or another instruction but the literal offset
5278// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
5279constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
5280
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005281template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00005282inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005283 const ArenaDeque<X86PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005284 ArenaVector<linker::LinkerPatch>* linker_patches) {
Nicolas Geoffray133719e2017-01-22 15:44:39 +00005285 for (const X86PcRelativePatchInfo& info : infos) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005286 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005287 linker_patches->push_back(Factory(literal_offset,
5288 info.target_dex_file,
5289 GetMethodAddressOffset(info.method_address),
5290 info.offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00005291 }
5292}
5293
Vladimir Marko6fd16062018-06-26 11:02:04 +01005294template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
5295linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
5296 const DexFile* target_dex_file,
5297 uint32_t pc_insn_offset,
5298 uint32_t boot_image_offset) {
5299 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
5300 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00005301}
5302
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005303void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Marko58155012015-08-19 12:49:41 +00005304 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005305 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01005306 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01005307 method_bss_entry_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00005308 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01005309 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005310 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01005311 string_bss_entry_patches_.size() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01005312 boot_image_other_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00005313 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01005314 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005315 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
5316 boot_image_method_patches_, linker_patches);
5317 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
5318 boot_image_type_patches_, linker_patches);
5319 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00005320 boot_image_string_patches_, linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005321 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01005322 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00005323 DCHECK(boot_image_type_patches_.empty());
5324 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01005325 }
5326 if (GetCompilerOptions().IsBootImage()) {
5327 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
5328 boot_image_other_patches_, linker_patches);
5329 } else {
5330 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
5331 boot_image_other_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005332 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01005333 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
5334 method_bss_entry_patches_, linker_patches);
5335 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
5336 type_bss_entry_patches_, linker_patches);
5337 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
5338 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00005339 DCHECK_EQ(size, linker_patches->size());
Vladimir Marko58155012015-08-19 12:49:41 +00005340}
5341
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005342void CodeGeneratorX86::MarkGCCard(Register temp,
5343 Register card,
5344 Register object,
5345 Register value,
5346 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04005347 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005348 if (value_can_be_null) {
5349 __ testl(value, value);
5350 __ j(kEqual, &is_null);
5351 }
Roland Levillainc73f0522018-08-14 15:16:50 +01005352 // Load the address of the card table into `card`.
Andreas Gampe542451c2016-07-26 09:02:02 -07005353 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Roland Levillainc73f0522018-08-14 15:16:50 +01005354 // Calculate the offset (in the card table) of the card corresponding to
5355 // `object`.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005356 __ movl(temp, object);
5357 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Roland Levillainc73f0522018-08-14 15:16:50 +01005358 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
5359 // `object`'s card.
5360 //
5361 // Register `card` contains the address of the card table. Note that the card
5362 // table's base is biased during its creation so that it always starts at an
5363 // address whose least-significant byte is equal to `kCardDirty` (see
5364 // art::gc::accounting::CardTable::Create). Therefore the MOVB instruction
5365 // below writes the `kCardDirty` (byte) value into the `object`'s card
5366 // (located at `card + object >> kCardShift`).
5367 //
5368 // This dual use of the value in register `card` (1. to calculate the location
5369 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
5370 // (no need to explicitly load `kCardDirty` as an immediate value).
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00005371 __ movb(Address(temp, card, TIMES_1, 0),
5372 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005373 if (value_can_be_null) {
5374 __ Bind(&is_null);
5375 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005376}
5377
Calin Juravle52c48962014-12-16 17:02:57 +00005378void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5379 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005380
5381 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005382 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005383 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005384 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5385 kEmitCompilerReadBarrier
5386 ? LocationSummary::kCallOnSlowPath
5387 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005388 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005389 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005390 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005391 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005392
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005393 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005394 locations->SetOut(Location::RequiresFpuRegister());
5395 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005396 // The output overlaps in case of long: we don't want the low move
5397 // to overwrite the object's location. Likewise, in the case of
5398 // an object field get with read barriers enabled, we do not want
5399 // the move to overwrite the object's location, as we need it to emit
5400 // the read barrier.
5401 locations->SetOut(
5402 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005403 (object_field_get_with_read_barrier || instruction->GetType() == DataType::Type::kInt64) ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005404 Location::kOutputOverlap :
5405 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005406 }
Calin Juravle52c48962014-12-16 17:02:57 +00005407
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005408 if (field_info.IsVolatile() && (field_info.GetFieldType() == DataType::Type::kInt64)) {
Calin Juravle52c48962014-12-16 17:02:57 +00005409 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005410 // So we use an XMM register as a temp to achieve atomicity (first
5411 // load the temp into the XMM and then copy the XMM into the
5412 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00005413 locations->AddTemp(Location::RequiresFpuRegister());
5414 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005415}
5416
Calin Juravle52c48962014-12-16 17:02:57 +00005417void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
5418 const FieldInfo& field_info) {
5419 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005420
Calin Juravle52c48962014-12-16 17:02:57 +00005421 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005422 Location base_loc = locations->InAt(0);
5423 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005424 Location out = locations->Out();
5425 bool is_volatile = field_info.IsVolatile();
Vladimir Marko61b92282017-10-11 13:23:17 +01005426 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
5427 DataType::Type load_type = instruction->GetType();
Calin Juravle52c48962014-12-16 17:02:57 +00005428 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5429
Vladimir Marko61b92282017-10-11 13:23:17 +01005430 switch (load_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005431 case DataType::Type::kBool:
5432 case DataType::Type::kUint8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005433 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005434 break;
5435 }
5436
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005437 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005438 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005439 break;
5440 }
5441
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005442 case DataType::Type::kUint16: {
5443 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005444 break;
5445 }
5446
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005447 case DataType::Type::kInt16: {
5448 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005449 break;
5450 }
5451
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005452 case DataType::Type::kInt32:
Calin Juravle52c48962014-12-16 17:02:57 +00005453 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005454 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005455
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005456 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005457 // /* HeapReference<Object> */ out = *(base + offset)
5458 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005459 // Note that a potential implicit null check is handled in this
5460 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
5461 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005462 instruction, out, base, offset, /* needs_null_check= */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005463 if (is_volatile) {
5464 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5465 }
5466 } else {
5467 __ movl(out.AsRegister<Register>(), Address(base, offset));
5468 codegen_->MaybeRecordImplicitNullCheck(instruction);
5469 if (is_volatile) {
5470 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5471 }
5472 // If read barriers are enabled, emit read barriers other than
5473 // Baker's using a slow path (and also unpoison the loaded
5474 // reference, if heap poisoning is enabled).
5475 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5476 }
5477 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005478 }
5479
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005480 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005481 if (is_volatile) {
5482 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5483 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005484 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005485 __ movd(out.AsRegisterPairLow<Register>(), temp);
5486 __ psrlq(temp, Immediate(32));
5487 __ movd(out.AsRegisterPairHigh<Register>(), temp);
5488 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005489 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00005490 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005491 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005492 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
5493 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005494 break;
5495 }
5496
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005497 case DataType::Type::kFloat32: {
Calin Juravle52c48962014-12-16 17:02:57 +00005498 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005499 break;
5500 }
5501
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005502 case DataType::Type::kFloat64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005503 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005504 break;
5505 }
5506
Aart Bik66c158e2018-01-31 12:55:04 -08005507 case DataType::Type::kUint32:
5508 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005509 case DataType::Type::kVoid:
Vladimir Marko61b92282017-10-11 13:23:17 +01005510 LOG(FATAL) << "Unreachable type " << load_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005511 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005512 }
Calin Juravle52c48962014-12-16 17:02:57 +00005513
Vladimir Marko61b92282017-10-11 13:23:17 +01005514 if (load_type == DataType::Type::kReference || load_type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005515 // Potential implicit null checks, in the case of reference or
5516 // long fields, are handled in the previous switch statement.
5517 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005518 codegen_->MaybeRecordImplicitNullCheck(instruction);
5519 }
5520
Calin Juravle52c48962014-12-16 17:02:57 +00005521 if (is_volatile) {
Vladimir Marko61b92282017-10-11 13:23:17 +01005522 if (load_type == DataType::Type::kReference) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005523 // Memory barriers, in the case of references, are also handled
5524 // in the previous switch statement.
5525 } else {
5526 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5527 }
Roland Levillain4d027112015-07-01 15:41:14 +01005528 }
Calin Juravle52c48962014-12-16 17:02:57 +00005529}
5530
5531void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5532 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5533
5534 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005535 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Calin Juravle52c48962014-12-16 17:02:57 +00005536 locations->SetInAt(0, Location::RequiresRegister());
5537 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005538 DataType::Type field_type = field_info.GetFieldType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005539 bool is_byte_type = DataType::Size(field_type) == 1u;
Calin Juravle52c48962014-12-16 17:02:57 +00005540
5541 // The register allocator does not support multiple
5542 // inputs that die at entry with one in a specific register.
5543 if (is_byte_type) {
5544 // Ensure the value is in a byte register.
5545 locations->SetInAt(1, Location::RegisterLocation(EAX));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005546 } else if (DataType::IsFloatingPointType(field_type)) {
5547 if (is_volatile && field_type == DataType::Type::kFloat64) {
Mark Mendell81489372015-11-04 11:30:41 -05005548 // In order to satisfy the semantics of volatile, this must be a single instruction store.
5549 locations->SetInAt(1, Location::RequiresFpuRegister());
5550 } else {
5551 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
5552 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005553 } else if (is_volatile && field_type == DataType::Type::kInt64) {
Mark Mendell81489372015-11-04 11:30:41 -05005554 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00005555 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005556
Calin Juravle52c48962014-12-16 17:02:57 +00005557 // 64bits value can be atomically written to an address with movsd and an XMM register.
5558 // We need two XMM registers because there's no easier way to (bit) copy a register pair
5559 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
5560 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
5561 // isolated cases when we need this it isn't worth adding the extra complexity.
5562 locations->AddTemp(Location::RequiresFpuRegister());
5563 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05005564 } else {
5565 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5566
5567 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5568 // Temporary registers for the write barrier.
5569 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
5570 // Ensure the card is in a byte register.
5571 locations->AddTemp(Location::RegisterLocation(ECX));
5572 }
Calin Juravle52c48962014-12-16 17:02:57 +00005573 }
5574}
5575
5576void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005577 const FieldInfo& field_info,
5578 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005579 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5580
5581 LocationSummary* locations = instruction->GetLocations();
5582 Register base = locations->InAt(0).AsRegister<Register>();
5583 Location value = locations->InAt(1);
5584 bool is_volatile = field_info.IsVolatile();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005585 DataType::Type field_type = field_info.GetFieldType();
Calin Juravle52c48962014-12-16 17:02:57 +00005586 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005587 bool needs_write_barrier =
5588 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005589
5590 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005591 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005592 }
5593
Mark Mendell81489372015-11-04 11:30:41 -05005594 bool maybe_record_implicit_null_check_done = false;
5595
Calin Juravle52c48962014-12-16 17:02:57 +00005596 switch (field_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005597 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005598 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005599 case DataType::Type::kInt8: {
Calin Juravle52c48962014-12-16 17:02:57 +00005600 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
5601 break;
5602 }
5603
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005604 case DataType::Type::kUint16:
5605 case DataType::Type::kInt16: {
Mark Mendell81489372015-11-04 11:30:41 -05005606 if (value.IsConstant()) {
Nicolas Geoffray78612082017-07-24 14:18:53 +01005607 __ movw(Address(base, offset),
5608 Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Mark Mendell81489372015-11-04 11:30:41 -05005609 } else {
5610 __ movw(Address(base, offset), value.AsRegister<Register>());
5611 }
Calin Juravle52c48962014-12-16 17:02:57 +00005612 break;
5613 }
5614
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005615 case DataType::Type::kInt32:
5616 case DataType::Type::kReference: {
Roland Levillain4d027112015-07-01 15:41:14 +01005617 if (kPoisonHeapReferences && needs_write_barrier) {
5618 // Note that in the case where `value` is a null reference,
5619 // we do not enter this block, as the reference does not
5620 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005621 DCHECK_EQ(field_type, DataType::Type::kReference);
Roland Levillain4d027112015-07-01 15:41:14 +01005622 Register temp = locations->GetTemp(0).AsRegister<Register>();
5623 __ movl(temp, value.AsRegister<Register>());
5624 __ PoisonHeapReference(temp);
5625 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05005626 } else if (value.IsConstant()) {
5627 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5628 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01005629 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00005630 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01005631 __ movl(Address(base, offset), value.AsRegister<Register>());
5632 }
Calin Juravle52c48962014-12-16 17:02:57 +00005633 break;
5634 }
5635
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005636 case DataType::Type::kInt64: {
Calin Juravle52c48962014-12-16 17:02:57 +00005637 if (is_volatile) {
5638 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
5639 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
5640 __ movd(temp1, value.AsRegisterPairLow<Register>());
5641 __ movd(temp2, value.AsRegisterPairHigh<Register>());
5642 __ punpckldq(temp1, temp2);
5643 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00005644 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05005645 } else if (value.IsConstant()) {
5646 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5647 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5648 codegen_->MaybeRecordImplicitNullCheck(instruction);
5649 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00005650 } else {
5651 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005652 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005653 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5654 }
Mark Mendell81489372015-11-04 11:30:41 -05005655 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005656 break;
5657 }
5658
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005659 case DataType::Type::kFloat32: {
Mark Mendell81489372015-11-04 11:30:41 -05005660 if (value.IsConstant()) {
5661 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5662 __ movl(Address(base, offset), Immediate(v));
5663 } else {
5664 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5665 }
Calin Juravle52c48962014-12-16 17:02:57 +00005666 break;
5667 }
5668
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005669 case DataType::Type::kFloat64: {
Mark Mendell81489372015-11-04 11:30:41 -05005670 if (value.IsConstant()) {
5671 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5672 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5673 codegen_->MaybeRecordImplicitNullCheck(instruction);
5674 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5675 maybe_record_implicit_null_check_done = true;
5676 } else {
5677 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5678 }
Calin Juravle52c48962014-12-16 17:02:57 +00005679 break;
5680 }
5681
Aart Bik66c158e2018-01-31 12:55:04 -08005682 case DataType::Type::kUint32:
5683 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005684 case DataType::Type::kVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005685 LOG(FATAL) << "Unreachable type " << field_type;
5686 UNREACHABLE();
5687 }
5688
Mark Mendell81489372015-11-04 11:30:41 -05005689 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005690 codegen_->MaybeRecordImplicitNullCheck(instruction);
5691 }
5692
Roland Levillain4d027112015-07-01 15:41:14 +01005693 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005694 Register temp = locations->GetTemp(0).AsRegister<Register>();
5695 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005696 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005697 }
5698
Calin Juravle52c48962014-12-16 17:02:57 +00005699 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005700 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005701 }
5702}
5703
5704void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5705 HandleFieldGet(instruction, instruction->GetFieldInfo());
5706}
5707
5708void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5709 HandleFieldGet(instruction, instruction->GetFieldInfo());
5710}
5711
5712void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5713 HandleFieldSet(instruction, instruction->GetFieldInfo());
5714}
5715
5716void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005717 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005718}
5719
5720void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5721 HandleFieldSet(instruction, instruction->GetFieldInfo());
5722}
5723
5724void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005725 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005726}
5727
5728void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5729 HandleFieldGet(instruction, instruction->GetFieldInfo());
5730}
5731
5732void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5733 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005734}
5735
Vladimir Marko552a1342017-10-31 10:56:47 +00005736void LocationsBuilderX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5737 codegen_->CreateStringBuilderAppendLocations(instruction, Location::RegisterLocation(EAX));
5738}
5739
5740void InstructionCodeGeneratorX86::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
5741 __ movl(EAX, Immediate(instruction->GetFormat()->GetValue()));
5742 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
5743}
5744
Calin Juravlee460d1d2015-09-29 04:52:17 +01005745void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5746 HUnresolvedInstanceFieldGet* instruction) {
5747 FieldAccessCallingConventionX86 calling_convention;
5748 codegen_->CreateUnresolvedFieldLocationSummary(
5749 instruction, instruction->GetFieldType(), calling_convention);
5750}
5751
5752void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5753 HUnresolvedInstanceFieldGet* instruction) {
5754 FieldAccessCallingConventionX86 calling_convention;
5755 codegen_->GenerateUnresolvedFieldAccess(instruction,
5756 instruction->GetFieldType(),
5757 instruction->GetFieldIndex(),
5758 instruction->GetDexPc(),
5759 calling_convention);
5760}
5761
5762void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5763 HUnresolvedInstanceFieldSet* instruction) {
5764 FieldAccessCallingConventionX86 calling_convention;
5765 codegen_->CreateUnresolvedFieldLocationSummary(
5766 instruction, instruction->GetFieldType(), calling_convention);
5767}
5768
5769void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5770 HUnresolvedInstanceFieldSet* instruction) {
5771 FieldAccessCallingConventionX86 calling_convention;
5772 codegen_->GenerateUnresolvedFieldAccess(instruction,
5773 instruction->GetFieldType(),
5774 instruction->GetFieldIndex(),
5775 instruction->GetDexPc(),
5776 calling_convention);
5777}
5778
5779void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5780 HUnresolvedStaticFieldGet* instruction) {
5781 FieldAccessCallingConventionX86 calling_convention;
5782 codegen_->CreateUnresolvedFieldLocationSummary(
5783 instruction, instruction->GetFieldType(), calling_convention);
5784}
5785
5786void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5787 HUnresolvedStaticFieldGet* instruction) {
5788 FieldAccessCallingConventionX86 calling_convention;
5789 codegen_->GenerateUnresolvedFieldAccess(instruction,
5790 instruction->GetFieldType(),
5791 instruction->GetFieldIndex(),
5792 instruction->GetDexPc(),
5793 calling_convention);
5794}
5795
5796void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5797 HUnresolvedStaticFieldSet* instruction) {
5798 FieldAccessCallingConventionX86 calling_convention;
5799 codegen_->CreateUnresolvedFieldLocationSummary(
5800 instruction, instruction->GetFieldType(), calling_convention);
5801}
5802
5803void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5804 HUnresolvedStaticFieldSet* instruction) {
5805 FieldAccessCallingConventionX86 calling_convention;
5806 codegen_->GenerateUnresolvedFieldAccess(instruction,
5807 instruction->GetFieldType(),
5808 instruction->GetFieldIndex(),
5809 instruction->GetDexPc(),
5810 calling_convention);
5811}
5812
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005813void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005814 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5815 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5816 ? Location::RequiresRegister()
5817 : Location::Any();
5818 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005819}
5820
Calin Juravle2ae48182016-03-16 14:05:09 +00005821void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5822 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005823 return;
5824 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005825 LocationSummary* locations = instruction->GetLocations();
5826 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005827
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005828 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005829 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005830}
5831
Calin Juravle2ae48182016-03-16 14:05:09 +00005832void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01005833 SlowPathCode* slow_path = new (GetScopedAllocator()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005834 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005835
5836 LocationSummary* locations = instruction->GetLocations();
5837 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005838
5839 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005840 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005841 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005842 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005843 } else {
5844 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005845 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005846 __ jmp(slow_path->GetEntryLabel());
5847 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005848 }
5849 __ j(kEqual, slow_path->GetEntryLabel());
5850}
5851
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005852void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005853 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005854}
5855
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005856void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005857 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005858 kEmitCompilerReadBarrier && (instruction->GetType() == DataType::Type::kReference);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005859 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01005860 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
5861 object_array_get_with_read_barrier
5862 ? LocationSummary::kCallOnSlowPath
5863 : LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005864 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005865 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005866 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005867 locations->SetInAt(0, Location::RequiresRegister());
5868 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005869 if (DataType::IsFloatingPointType(instruction->GetType())) {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005870 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5871 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005872 // The output overlaps in case of long: we don't want the low move
5873 // to overwrite the array's location. Likewise, in the case of an
5874 // object array get with read barriers enabled, we do not want the
5875 // move to overwrite the array's location, as we need it to emit
5876 // the read barrier.
5877 locations->SetOut(
5878 Location::RequiresRegister(),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005879 (instruction->GetType() == DataType::Type::kInt64 || object_array_get_with_read_barrier)
5880 ? Location::kOutputOverlap
5881 : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005882 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005883}
5884
5885void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5886 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005887 Location obj_loc = locations->InAt(0);
5888 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005889 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005890 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005891 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005892
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005893 DataType::Type type = instruction->GetType();
Calin Juravle77520bc2015-01-12 18:45:46 +00005894 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005895 case DataType::Type::kBool:
5896 case DataType::Type::kUint8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005897 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005898 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005899 break;
5900 }
5901
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005902 case DataType::Type::kInt8: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005903 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005904 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005905 break;
5906 }
5907
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005908 case DataType::Type::kUint16: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005909 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005910 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5911 // Branch cases into compressed and uncompressed for each index's type.
5912 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5913 NearLabel done, not_compressed;
Vladimir Marko3c89d422017-02-17 11:30:23 +00005914 __ testb(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005915 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005916 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5917 "Expecting 0=compressed, 1=uncompressed");
5918 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005919 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5920 __ jmp(&done);
5921 __ Bind(&not_compressed);
5922 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5923 __ Bind(&done);
5924 } else {
5925 // Common case for charAt of array of char or when string compression's
5926 // feature is turned off.
5927 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5928 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005929 break;
5930 }
5931
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01005932 case DataType::Type::kInt16: {
5933 Register out = out_loc.AsRegister<Register>();
5934 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5935 break;
5936 }
5937
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005938 case DataType::Type::kInt32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005939 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005940 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005941 break;
5942 }
5943
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005944 case DataType::Type::kReference: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005945 static_assert(
5946 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5947 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005948 // /* HeapReference<Object> */ out =
5949 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5950 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005951 // Note that a potential implicit null check is handled in this
5952 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5953 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08005954 instruction, out_loc, obj, data_offset, index, /* needs_null_check= */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005955 } else {
5956 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005957 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5958 codegen_->MaybeRecordImplicitNullCheck(instruction);
5959 // If read barriers are enabled, emit read barriers other than
5960 // Baker's using a slow path (and also unpoison the loaded
5961 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005962 if (index.IsConstant()) {
5963 uint32_t offset =
5964 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005965 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5966 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005967 codegen_->MaybeGenerateReadBarrierSlow(
5968 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5969 }
5970 }
5971 break;
5972 }
5973
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005974 case DataType::Type::kInt64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005975 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005976 __ movl(out_loc.AsRegisterPairLow<Register>(),
5977 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5978 codegen_->MaybeRecordImplicitNullCheck(instruction);
5979 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5980 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005981 break;
5982 }
5983
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005984 case DataType::Type::kFloat32: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005985 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005986 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005987 break;
5988 }
5989
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005990 case DataType::Type::kFloat64: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005991 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005992 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005993 break;
5994 }
5995
Aart Bik66c158e2018-01-31 12:55:04 -08005996 case DataType::Type::kUint32:
5997 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005998 case DataType::Type::kVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005999 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07006000 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006001 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006002
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006003 if (type == DataType::Type::kReference || type == DataType::Type::kInt64) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006004 // Potential implicit null checks, in the case of reference or
6005 // long arrays, are handled in the previous switch statement.
6006 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00006007 codegen_->MaybeRecordImplicitNullCheck(instruction);
6008 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006009}
6010
6011void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006012 DataType::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006013
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006014 bool needs_write_barrier =
6015 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006016 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006017
Vladimir Markoca6fff82017-10-03 14:49:14 +01006018 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Nicolas Geoffray39468442014-09-02 15:17:15 +01006019 instruction,
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006020 needs_type_check ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006021
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006022 bool is_byte_type = DataType::Size(value_type) == 1u;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006023 // We need the inputs to be different than the output in case of long operation.
6024 // In case of a byte operation, the register allocator does not support multiple
6025 // inputs that die at entry with one in a specific register.
6026 locations->SetInAt(0, Location::RequiresRegister());
6027 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6028 if (is_byte_type) {
6029 // Ensure the value is in a byte register.
6030 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006031 } else if (DataType::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05006032 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006033 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006034 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
6035 }
6036 if (needs_write_barrier) {
6037 // Temporary registers for the write barrier.
6038 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
6039 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00006040 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006041 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006042}
6043
6044void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
6045 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006046 Location array_loc = locations->InAt(0);
6047 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006048 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01006049 Location value = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006050 DataType::Type value_type = instruction->GetComponentType();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006051 bool needs_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006052 bool needs_write_barrier =
6053 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006054
6055 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006056 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006057 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006058 case DataType::Type::kInt8: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006059 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006060 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006061 if (value.IsRegister()) {
6062 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006063 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01006064 __ movb(address, Immediate(CodeGenerator::GetInt8ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006065 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006066 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006067 break;
6068 }
6069
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006070 case DataType::Type::kUint16:
6071 case DataType::Type::kInt16: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006072 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006073 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006074 if (value.IsRegister()) {
6075 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006076 } else {
Nicolas Geoffray78612082017-07-24 14:18:53 +01006077 __ movw(address, Immediate(CodeGenerator::GetInt16ValueOf(value.GetConstant())));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006078 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006079 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006080 break;
6081 }
6082
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006083 case DataType::Type::kReference: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006084 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006085 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006086
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006087 if (!value.IsRegister()) {
6088 // Just setting null.
6089 DCHECK(instruction->InputAt(2)->IsNullConstant());
6090 DCHECK(value.IsConstant()) << value;
6091 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00006092 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006093 DCHECK(!needs_write_barrier);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006094 DCHECK(!needs_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006095 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006096 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006097
6098 DCHECK(needs_write_barrier);
6099 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01006100 Location temp_loc = locations->GetTemp(0);
6101 Register temp = temp_loc.AsRegister<Register>();
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006102
6103 bool can_value_be_null = instruction->GetValueCanBeNull();
6104 NearLabel do_store;
6105 if (can_value_be_null) {
6106 __ testl(register_value, register_value);
6107 __ j(kEqual, &do_store);
6108 }
6109
6110 SlowPathCode* slow_path = nullptr;
6111 if (needs_type_check) {
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006112 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathX86(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006113 codegen_->AddSlowPath(slow_path);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006114
6115 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6116 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6117 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006118
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006119 // Note that when Baker read barriers are enabled, the type
6120 // checks are performed without read barriers. This is fine,
6121 // even in the case where a class object is in the from-space
6122 // after the flip, as a comparison involving such a type would
6123 // not produce a false positive; it may of course produce a
6124 // false negative, in which case we would take the ArraySet
6125 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01006126
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006127 // /* HeapReference<Class> */ temp = array->klass_
6128 __ movl(temp, Address(array, class_offset));
6129 codegen_->MaybeRecordImplicitNullCheck(instruction);
6130 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01006131
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006132 // /* HeapReference<Class> */ temp = temp->component_type_
6133 __ movl(temp, Address(temp, component_offset));
6134 // If heap poisoning is enabled, no need to unpoison `temp`
6135 // nor the object reference in `register_value->klass`, as
6136 // we are comparing two poisoned references.
6137 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01006138
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006139 if (instruction->StaticTypeOfArrayIsObjectArray()) {
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006140 NearLabel do_put;
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006141 __ j(kEqual, &do_put);
6142 // If heap poisoning is enabled, the `temp` reference has
6143 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006144 __ MaybeUnpoisonHeapReference(temp);
6145
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006146 // If heap poisoning is enabled, no need to unpoison the
6147 // heap reference loaded below, as it is only used for a
6148 // comparison with null.
6149 __ cmpl(Address(temp, super_offset), Immediate(0));
6150 __ j(kNotEqual, slow_path->GetEntryLabel());
6151 __ Bind(&do_put);
6152 } else {
6153 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006154 }
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006155 }
6156
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006157 Register card = locations->GetTemp(1).AsRegister<Register>();
6158 codegen_->MarkGCCard(
6159 temp, card, array, value.AsRegister<Register>(), /* value_can_be_null= */ false);
6160
6161 if (can_value_be_null) {
6162 DCHECK(do_store.IsLinked());
6163 __ Bind(&do_store);
6164 }
6165
6166 Register source = register_value;
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006167 if (kPoisonHeapReferences) {
6168 __ movl(temp, register_value);
6169 __ PoisonHeapReference(temp);
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006170 source = temp;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006171 }
6172
Vladimir Marko8fa839c2019-05-16 12:50:47 +00006173 __ movl(address, source);
6174
6175 if (can_value_be_null || !needs_type_check) {
6176 codegen_->MaybeRecordImplicitNullCheck(instruction);
6177 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006178
Vladimir Marko0dda8c82019-05-16 12:47:40 +00006179 if (slow_path != nullptr) {
6180 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006181 }
6182
6183 break;
6184 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006185
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006186 case DataType::Type::kInt32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006187 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006188 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006189 if (value.IsRegister()) {
6190 __ movl(address, value.AsRegister<Register>());
6191 } else {
6192 DCHECK(value.IsConstant()) << value;
6193 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
6194 __ movl(address, Immediate(v));
6195 }
6196 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006197 break;
6198 }
6199
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006200 case DataType::Type::kInt64: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006201 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006202 if (value.IsRegisterPair()) {
6203 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6204 value.AsRegisterPairLow<Register>());
6205 codegen_->MaybeRecordImplicitNullCheck(instruction);
6206 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6207 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006208 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006209 DCHECK(value.IsConstant());
6210 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
6211 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
6212 Immediate(Low32Bits(val)));
6213 codegen_->MaybeRecordImplicitNullCheck(instruction);
6214 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
6215 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006216 }
6217 break;
6218 }
6219
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006220 case DataType::Type::kFloat32: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006221 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006222 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006223 if (value.IsFpuRegister()) {
6224 __ movss(address, value.AsFpuRegister<XmmRegister>());
6225 } else {
6226 DCHECK(value.IsConstant());
6227 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
6228 __ movl(address, Immediate(v));
6229 }
6230 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006231 break;
6232 }
6233
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006234 case DataType::Type::kFloat64: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006235 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006236 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05006237 if (value.IsFpuRegister()) {
6238 __ movsd(address, value.AsFpuRegister<XmmRegister>());
6239 } else {
6240 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006241 Address address_hi =
6242 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05006243 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
6244 __ movl(address, Immediate(Low32Bits(v)));
6245 codegen_->MaybeRecordImplicitNullCheck(instruction);
6246 __ movl(address_hi, Immediate(High32Bits(v)));
6247 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006248 break;
6249 }
6250
Aart Bik66c158e2018-01-31 12:55:04 -08006251 case DataType::Type::kUint32:
6252 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006253 case DataType::Type::kVoid:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006254 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07006255 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006256 }
6257}
6258
6259void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006260 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006261 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04006262 if (!instruction->IsEmittedAtUseSite()) {
6263 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6264 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006265}
6266
6267void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04006268 if (instruction->IsEmittedAtUseSite()) {
6269 return;
6270 }
6271
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006272 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006273 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006274 Register obj = locations->InAt(0).AsRegister<Register>();
6275 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006276 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00006277 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07006278 // Mask out most significant bit in case the array is String's array of char.
6279 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006280 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006281 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006282}
6283
6284void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006285 RegisterSet caller_saves = RegisterSet::Empty();
6286 InvokeRuntimeCallingConvention calling_convention;
6287 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6288 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6289 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05006290 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04006291 HInstruction* length = instruction->InputAt(1);
6292 if (!length->IsEmittedAtUseSite()) {
6293 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6294 }
jessicahandojo4877b792016-09-08 19:49:13 -07006295 // Need register to see array's length.
6296 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6297 locations->AddTemp(Location::RequiresRegister());
6298 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006299}
6300
6301void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07006302 const bool is_string_compressed_char_at =
6303 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006304 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05006305 Location index_loc = locations->InAt(0);
6306 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07006307 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006308 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006309
Mark Mendell99dbd682015-04-22 16:18:52 -04006310 if (length_loc.IsConstant()) {
6311 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
6312 if (index_loc.IsConstant()) {
6313 // BCE will remove the bounds check if we are guarenteed to pass.
6314 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6315 if (index < 0 || index >= length) {
6316 codegen_->AddSlowPath(slow_path);
6317 __ jmp(slow_path->GetEntryLabel());
6318 } else {
6319 // Some optimization after BCE may have generated this, and we should not
6320 // generate a bounds check if it is a valid range.
6321 }
6322 return;
6323 }
6324
6325 // We have to reverse the jump condition because the length is the constant.
6326 Register index_reg = index_loc.AsRegister<Register>();
6327 __ cmpl(index_reg, Immediate(length));
6328 codegen_->AddSlowPath(slow_path);
6329 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006330 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04006331 HInstruction* array_length = instruction->InputAt(1);
6332 if (array_length->IsEmittedAtUseSite()) {
6333 // Address the length field in the array.
6334 DCHECK(array_length->IsArrayLength());
6335 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
6336 Location array_loc = array_length->GetLocations()->InAt(0);
6337 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07006338 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006339 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
6340 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07006341 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
6342 __ movl(length_reg, array_len);
6343 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006344 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07006345 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04006346 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07006347 // Checking bounds for general case:
6348 // Array of char or string's array with feature compression off.
6349 if (index_loc.IsConstant()) {
6350 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
6351 __ cmpl(array_len, Immediate(value));
6352 } else {
6353 __ cmpl(array_len, index_loc.AsRegister<Register>());
6354 }
6355 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04006356 }
Mark Mendell99dbd682015-04-22 16:18:52 -04006357 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01006358 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04006359 }
6360 codegen_->AddSlowPath(slow_path);
6361 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05006362 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006363}
6364
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006365void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006366 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006367}
6368
6369void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01006370 if (instruction->GetNext()->IsSuspendCheck() &&
6371 instruction->GetBlock()->GetLoopInformation() != nullptr) {
6372 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
6373 // The back edge will generate the suspend check.
6374 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
6375 }
6376
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006377 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6378}
6379
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006380void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006381 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
6382 instruction, LocationSummary::kCallOnSlowPath);
Aart Bikb13c65b2017-03-21 20:14:07 -07006383 // In suspend check slow path, usually there are no caller-save registers at all.
6384 // If SIMD instructions are present, however, we force spilling all live SIMD
6385 // registers in full width (since the runtime only saves/restores lower part).
Aart Bik5576f372017-03-23 16:17:37 -07006386 locations->SetCustomSlowPathCallerSaves(
6387 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006388}
6389
6390void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006391 HBasicBlock* block = instruction->GetBlock();
6392 if (block->GetLoopInformation() != nullptr) {
6393 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6394 // The back edge will generate the suspend check.
6395 return;
6396 }
6397 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6398 // The goto will generate the suspend check.
6399 return;
6400 }
6401 GenerateSuspendCheck(instruction, nullptr);
6402}
6403
6404void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
6405 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006406 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006407 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
6408 if (slow_path == nullptr) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006409 slow_path =
6410 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathX86(instruction, successor);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006411 instruction->SetSlowPath(slow_path);
6412 codegen_->AddSlowPath(slow_path);
6413 if (successor != nullptr) {
6414 DCHECK(successor->IsLoopHeader());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006415 }
6416 } else {
6417 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6418 }
6419
Andreas Gampe542451c2016-07-26 09:02:02 -07006420 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00006421 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006422 if (successor == nullptr) {
6423 __ j(kNotEqual, slow_path->GetEntryLabel());
6424 __ Bind(slow_path->GetReturnLabel());
6425 } else {
6426 __ j(kEqual, codegen_->GetLabelOf(successor));
6427 __ jmp(slow_path->GetEntryLabel());
6428 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006429}
6430
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006431X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
6432 return codegen_->GetAssembler();
6433}
6434
Aart Bikcfe50bb2017-12-12 14:54:12 -08006435void ParallelMoveResolverX86::MoveMemoryToMemory(int dst, int src, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006436 ScratchRegisterScope ensure_scratch(
6437 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6438 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6439 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
Mark Mendell7c8d0092015-01-26 11:21:33 -05006440
Aart Bikcfe50bb2017-12-12 14:54:12 -08006441 // Now that temp register is available (possibly spilled), move blocks of memory.
6442 for (int i = 0; i < number_of_words; i++) {
6443 __ movl(temp_reg, Address(ESP, src + stack_offset));
6444 __ movl(Address(ESP, dst + stack_offset), temp_reg);
6445 stack_offset += kX86WordSize;
6446 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006447}
6448
6449void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006450 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006451 Location source = move->GetSource();
6452 Location destination = move->GetDestination();
6453
6454 if (source.IsRegister()) {
6455 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006456 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006457 } else if (destination.IsFpuRegister()) {
6458 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006459 } else {
6460 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006461 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006462 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006463 } else if (source.IsRegisterPair()) {
Vladimir Marko86c87522020-05-11 16:55:55 +01006464 if (destination.IsRegisterPair()) {
6465 __ movl(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
6466 DCHECK_NE(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
6467 __ movl(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
6468 } else if (destination.IsFpuRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006469 size_t elem_size = DataType::Size(DataType::Type::kInt32);
Vladimir Markodec78172020-06-19 15:31:23 +01006470 // Push the 2 source registers to the stack.
Vladimir Marko86c87522020-05-11 16:55:55 +01006471 __ pushl(source.AsRegisterPairHigh<Register>());
6472 __ cfi().AdjustCFAOffset(elem_size);
6473 __ pushl(source.AsRegisterPairLow<Register>());
6474 __ cfi().AdjustCFAOffset(elem_size);
6475 // Load the destination register.
David Brazdil74eb1b22015-12-14 11:44:01 +00006476 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
6477 // And remove the temporary stack space we allocated.
Vladimir Markodec78172020-06-19 15:31:23 +01006478 codegen_->DecreaseFrame(2 * elem_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01006479 } else {
6480 DCHECK(destination.IsDoubleStackSlot());
6481 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
6482 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
6483 source.AsRegisterPairHigh<Register>());
6484 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006485 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006486 if (destination.IsRegister()) {
6487 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
6488 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006489 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006490 } else if (destination.IsRegisterPair()) {
Vladimir Marko86c87522020-05-11 16:55:55 +01006491 size_t elem_size = DataType::Size(DataType::Type::kInt32);
6492 // Create stack space for 2 elements.
Vladimir Markodec78172020-06-19 15:31:23 +01006493 codegen_->IncreaseFrame(2 * elem_size);
Vladimir Marko86c87522020-05-11 16:55:55 +01006494 // Store the source register.
6495 __ movsd(Address(ESP, 0), source.AsFpuRegister<XmmRegister>());
6496 // And pop the values into destination registers.
6497 __ popl(destination.AsRegisterPairLow<Register>());
6498 __ cfi().AdjustCFAOffset(-elem_size);
6499 __ popl(destination.AsRegisterPairHigh<Register>());
6500 __ cfi().AdjustCFAOffset(-elem_size);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006501 } else if (destination.IsStackSlot()) {
6502 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006503 } else if (destination.IsDoubleStackSlot()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006504 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Aart Bik5576f372017-03-23 16:17:37 -07006505 } else {
6506 DCHECK(destination.IsSIMDStackSlot());
6507 __ movups(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05006508 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006509 } else if (source.IsStackSlot()) {
6510 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006511 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006512 } else if (destination.IsFpuRegister()) {
6513 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006514 } else {
6515 DCHECK(destination.IsStackSlot());
Aart Bikcfe50bb2017-12-12 14:54:12 -08006516 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006517 }
6518 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006519 if (destination.IsRegisterPair()) {
6520 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
6521 __ movl(destination.AsRegisterPairHigh<Register>(),
6522 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
6523 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006524 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6525 } else {
6526 DCHECK(destination.IsDoubleStackSlot()) << destination;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006527 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006528 }
Aart Bik5576f372017-03-23 16:17:37 -07006529 } else if (source.IsSIMDStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006530 if (destination.IsFpuRegister()) {
6531 __ movups(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
6532 } else {
6533 DCHECK(destination.IsSIMDStackSlot());
6534 MoveMemoryToMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6535 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006536 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006537 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006538 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006539 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006540 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05006541 if (value == 0) {
6542 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
6543 } else {
6544 __ movl(destination.AsRegister<Register>(), Immediate(value));
6545 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006546 } else {
6547 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05006548 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05006549 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006550 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006551 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006552 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006553 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006554 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006555 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6556 if (value == 0) {
6557 // Easy handling of 0.0.
6558 __ xorps(dest, dest);
6559 } else {
6560 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006561 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6562 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
6563 __ movl(temp, Immediate(value));
6564 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006565 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05006566 } else {
6567 DCHECK(destination.IsStackSlot()) << destination;
6568 __ movl(Address(ESP, destination.GetStackIndex()), imm);
6569 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006570 } else if (constant->IsLongConstant()) {
6571 int64_t value = constant->AsLongConstant()->GetValue();
6572 int32_t low_value = Low32Bits(value);
6573 int32_t high_value = High32Bits(value);
6574 Immediate low(low_value);
6575 Immediate high(high_value);
6576 if (destination.IsDoubleStackSlot()) {
6577 __ movl(Address(ESP, destination.GetStackIndex()), low);
6578 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6579 } else {
6580 __ movl(destination.AsRegisterPairLow<Register>(), low);
6581 __ movl(destination.AsRegisterPairHigh<Register>(), high);
6582 }
6583 } else {
6584 DCHECK(constant->IsDoubleConstant());
6585 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00006586 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006587 int32_t low_value = Low32Bits(value);
6588 int32_t high_value = High32Bits(value);
6589 Immediate low(low_value);
6590 Immediate high(high_value);
6591 if (destination.IsFpuRegister()) {
6592 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
6593 if (value == 0) {
6594 // Easy handling of 0.0.
6595 __ xorpd(dest, dest);
6596 } else {
6597 __ pushl(high);
Vladimir Marko86c87522020-05-11 16:55:55 +01006598 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006599 __ pushl(low);
Vladimir Marko86c87522020-05-11 16:55:55 +01006600 __ cfi().AdjustCFAOffset(4);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006601 __ movsd(dest, Address(ESP, 0));
Vladimir Markodec78172020-06-19 15:31:23 +01006602 codegen_->DecreaseFrame(8);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006603 }
6604 } else {
6605 DCHECK(destination.IsDoubleStackSlot()) << destination;
6606 __ movl(Address(ESP, destination.GetStackIndex()), low);
6607 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
6608 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006609 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006610 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00006611 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006612 }
6613}
6614
Mark Mendella5c19ce2015-04-01 12:51:05 -04006615void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006616 Register suggested_scratch = reg == EAX ? EBX : EAX;
6617 ScratchRegisterScope ensure_scratch(
6618 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
6619
6620 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6621 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
6622 __ movl(Address(ESP, mem + stack_offset), reg);
6623 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006624}
6625
Mark Mendell7c8d0092015-01-26 11:21:33 -05006626void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006627 ScratchRegisterScope ensure_scratch(
6628 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
6629
6630 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
6631 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
6632 __ movl(temp_reg, Address(ESP, mem + stack_offset));
6633 __ movss(Address(ESP, mem + stack_offset), reg);
6634 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006635}
6636
Aart Bikcfe50bb2017-12-12 14:54:12 -08006637void ParallelMoveResolverX86::Exchange128(XmmRegister reg, int mem) {
6638 size_t extra_slot = 4 * kX86WordSize;
Vladimir Markodec78172020-06-19 15:31:23 +01006639 codegen_->IncreaseFrame(extra_slot);
Aart Bikcfe50bb2017-12-12 14:54:12 -08006640 __ movups(Address(ESP, 0), XmmRegister(reg));
6641 ExchangeMemory(0, mem + extra_slot, 4);
6642 __ movups(XmmRegister(reg), Address(ESP, 0));
Vladimir Markodec78172020-06-19 15:31:23 +01006643 codegen_->DecreaseFrame(extra_slot);
Aart Bikcfe50bb2017-12-12 14:54:12 -08006644}
6645
6646void ParallelMoveResolverX86::ExchangeMemory(int mem1, int mem2, int number_of_words) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006647 ScratchRegisterScope ensure_scratch1(
6648 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006649
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006650 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
6651 ScratchRegisterScope ensure_scratch2(
6652 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006653
Guillaume Sancheze14590b2015-04-15 18:57:27 +00006654 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
6655 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
Aart Bikcfe50bb2017-12-12 14:54:12 -08006656
6657 // Now that temp registers are available (possibly spilled), exchange blocks of memory.
6658 for (int i = 0; i < number_of_words; i++) {
6659 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
6660 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
6661 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
6662 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
6663 stack_offset += kX86WordSize;
6664 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006665}
6666
6667void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006668 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006669 Location source = move->GetSource();
6670 Location destination = move->GetDestination();
6671
6672 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04006673 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
6674 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
6675 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
6676 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
6677 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006678 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006679 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006680 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006681 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006682 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006683 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 1);
Mark Mendell7c8d0092015-01-26 11:21:33 -05006684 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
6685 // Use XOR Swap algorithm to avoid a temporary.
6686 DCHECK_NE(source.reg(), destination.reg());
6687 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6688 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
6689 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
6690 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
6691 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6692 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
6693 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006694 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
6695 // Take advantage of the 16 bytes in the XMM register.
6696 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
6697 Address stack(ESP, destination.GetStackIndex());
6698 // Load the double into the high doubleword.
6699 __ movhpd(reg, stack);
6700
6701 // Store the low double into the destination.
6702 __ movsd(stack, reg);
6703
6704 // Move the high double to the low double.
6705 __ psrldq(reg, Immediate(8));
6706 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
6707 // Take advantage of the 16 bytes in the XMM register.
6708 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
6709 Address stack(ESP, source.GetStackIndex());
6710 // Load the double into the high doubleword.
6711 __ movhpd(reg, stack);
6712
6713 // Store the low double into the destination.
6714 __ movsd(stack, reg);
6715
6716 // Move the high double to the low double.
6717 __ psrldq(reg, Immediate(8));
6718 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
Aart Bikcfe50bb2017-12-12 14:54:12 -08006719 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 2);
6720 } else if (source.IsSIMDStackSlot() && destination.IsSIMDStackSlot()) {
6721 ExchangeMemory(destination.GetStackIndex(), source.GetStackIndex(), 4);
6722 } else if (source.IsFpuRegister() && destination.IsSIMDStackSlot()) {
6723 Exchange128(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
6724 } else if (destination.IsFpuRegister() && source.IsSIMDStackSlot()) {
6725 Exchange128(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006726 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006727 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006728 }
6729}
6730
6731void ParallelMoveResolverX86::SpillScratch(int reg) {
6732 __ pushl(static_cast<Register>(reg));
6733}
6734
6735void ParallelMoveResolverX86::RestoreScratch(int reg) {
6736 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006737}
6738
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006739HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6740 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006741 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006742 case HLoadClass::LoadKind::kInvalid:
6743 LOG(FATAL) << "UNREACHABLE";
6744 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006745 case HLoadClass::LoadKind::kReferrersClass:
6746 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006747 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006748 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006749 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01006750 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006751 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006752 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006753 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01006754 DCHECK(GetCompilerOptions().IsJitCompiler());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006755 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006756 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006757 break;
6758 }
6759 return desired_class_load_kind;
6760}
6761
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006762void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006763 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006764 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006765 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006766 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006767 cls,
6768 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006769 Location::RegisterLocation(EAX));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006770 DCHECK_EQ(calling_convention.GetRegisterAt(0), EAX);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006771 return;
6772 }
Vladimir Marko41559982017-01-06 14:04:23 +00006773 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006774
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006775 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6776 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006777 ? LocationSummary::kCallOnSlowPath
6778 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006779 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006780 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006781 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006782 }
6783
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006784 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006785 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006786 load_kind == HLoadClass::LoadKind::kBootImageRelRo ||
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006787 load_kind == HLoadClass::LoadKind::kBssEntry) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006788 locations->SetInAt(0, Location::RequiresRegister());
6789 }
6790 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006791 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6792 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6793 // Rely on the type resolution and/or initialization to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006794 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006795 } else {
6796 // For non-Baker read barrier we have a temp-clobbering call.
6797 }
6798 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006799}
6800
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006801Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01006802 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006803 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01006804 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006805 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006806 jit_class_patches_.emplace_back(&dex_file, type_index.index_);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006807 PatchInfo<Label>* info = &jit_class_patches_.back();
6808 return &info->label;
6809}
6810
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006811// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6812// move.
6813void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006814 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006815 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00006816 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006817 return;
6818 }
Vladimir Marko41559982017-01-06 14:04:23 +00006819 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006820
Vladimir Marko41559982017-01-06 14:04:23 +00006821 LocationSummary* locations = cls->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006822 Location out_loc = locations->Out();
6823 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006824
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006825 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006826 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6827 ? kWithoutReadBarrier
6828 : kCompilerReadBarrierOption;
Vladimir Marko41559982017-01-06 14:04:23 +00006829 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006830 case HLoadClass::LoadKind::kReferrersClass: {
6831 DCHECK(!cls->CanCallRuntime());
6832 DCHECK(!cls->MustGenerateClinitCheck());
6833 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6834 Register current_method = locations->InAt(0).AsRegister<Register>();
6835 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006836 cls,
6837 out_loc,
6838 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Andreas Gampe3db70682018-12-26 15:12:03 -08006839 /* fixup_label= */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006840 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006841 break;
6842 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006843 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01006844 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
6845 codegen_->GetCompilerOptions().IsBootImageExtension());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006846 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006847 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00006848 __ leal(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00006849 codegen_->RecordBootImageTypePatch(cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006850 break;
6851 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006852 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006853 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
6854 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00006855 __ movl(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006856 codegen_->RecordBootImageRelRoPatch(cls->InputAt(0)->AsX86ComputeBaseMethodAddress(),
6857 codegen_->GetBootImageOffset(cls));
Vladimir Marko94ec2db2017-09-06 17:21:03 +01006858 break;
6859 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006860 case HLoadClass::LoadKind::kBssEntry: {
6861 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00006862 Address address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006863 Label* fixup_label = codegen_->NewTypeBssEntryPatch(cls);
6864 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01006865 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006866 generate_null_check = true;
6867 break;
6868 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006869 case HLoadClass::LoadKind::kJitBootImageAddress: {
6870 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
6871 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
6872 DCHECK_NE(address, 0u);
6873 __ movl(out, Immediate(address));
6874 break;
6875 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006876 case HLoadClass::LoadKind::kJitTableAddress: {
Vladimir Marko4ef451a2020-07-23 09:54:27 +00006877 Address address = Address::Absolute(CodeGeneratorX86::kPlaceholder32BitOffset);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006878 Label* fixup_label = codegen_->NewJitRootClassPatch(
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006879 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetClass());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006880 // /* GcRoot<mirror::Class> */ out = *address
Vladimir Markoea4c1262017-02-06 19:59:33 +00006881 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006882 break;
6883 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006884 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006885 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006886 LOG(FATAL) << "UNREACHABLE";
6887 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006888 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006889
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006890 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6891 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006892 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(cls, cls);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006893 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006894
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006895 if (generate_null_check) {
6896 __ testl(out, out);
6897 __ j(kEqual, slow_path->GetEntryLabel());
6898 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006899
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006900 if (cls->MustGenerateClinitCheck()) {
6901 GenerateClassInitializationCheck(slow_path, out);
6902 } else {
6903 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006904 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006905 }
6906}
6907
Orion Hodsondbaa5c72018-05-10 08:22:46 +01006908void LocationsBuilderX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6909 InvokeRuntimeCallingConvention calling_convention;
6910 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6911 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, location, location);
6912}
6913
6914void InstructionCodeGeneratorX86::VisitLoadMethodHandle(HLoadMethodHandle* load) {
6915 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
6916}
6917
Orion Hodson18259d72018-04-12 11:18:23 +01006918void LocationsBuilderX86::VisitLoadMethodType(HLoadMethodType* load) {
6919 InvokeRuntimeCallingConvention calling_convention;
6920 Location location = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
6921 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, location, location);
6922}
6923
6924void InstructionCodeGeneratorX86::VisitLoadMethodType(HLoadMethodType* load) {
6925 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
6926}
6927
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006928void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6929 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01006930 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006931 locations->SetInAt(0, Location::RequiresRegister());
6932 if (check->HasUses()) {
6933 locations->SetOut(Location::SameAsFirstInput());
6934 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01006935 // Rely on the type initialization to save everything we need.
6936 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006937}
6938
6939void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006940 // We assume the class to not be null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01006941 SlowPathCode* slow_path =
6942 new (codegen_->GetScopedAllocator()) LoadClassSlowPathX86(check->GetLoadClass(), check);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006943 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006944 GenerateClassInitializationCheck(slow_path,
6945 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006946}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006947
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006948void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006949 SlowPathCode* slow_path, Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00006950 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
6951 const size_t status_byte_offset =
6952 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
Vladimir Markobf121912019-06-04 13:49:05 +01006953 constexpr uint32_t shifted_visibly_initialized_value =
6954 enum_cast<uint32_t>(ClassStatus::kVisiblyInitialized) << (status_lsb_position % kBitsPerByte);
Vladimir Markodc682aa2018-01-04 18:42:57 +00006955
Vladimir Markobf121912019-06-04 13:49:05 +01006956 __ cmpb(Address(class_reg, status_byte_offset), Immediate(shifted_visibly_initialized_value));
Vladimir Marko2c64a832018-01-04 11:31:56 +00006957 __ j(kBelow, slow_path->GetEntryLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006958 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006959}
6960
Vladimir Marko175e7862018-03-27 09:03:13 +00006961void InstructionCodeGeneratorX86::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
6962 Register temp) {
6963 uint32_t path_to_root = check->GetBitstringPathToRoot();
6964 uint32_t mask = check->GetBitstringMask();
6965 DCHECK(IsPowerOfTwo(mask + 1));
6966 size_t mask_bits = WhichPowerOf2(mask + 1);
6967
6968 if (mask_bits == 16u) {
6969 // Compare the bitstring in memory.
6970 __ cmpw(Address(temp, mirror::Class::StatusOffset()), Immediate(path_to_root));
6971 } else {
6972 // /* uint32_t */ temp = temp->status_
6973 __ movl(temp, Address(temp, mirror::Class::StatusOffset()));
6974 // Compare the bitstring bits using SUB.
6975 __ subl(temp, Immediate(path_to_root));
6976 // Shift out bits that do not contribute to the comparison.
6977 __ shll(temp, Immediate(32u - mask_bits));
6978 }
6979}
6980
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006981HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6982 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006983 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006984 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00006985 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006986 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko695348f2020-05-19 14:42:02 +01006987 DCHECK(!GetCompilerOptions().IsJitCompiler());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006988 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01006989 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006990 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Marko695348f2020-05-19 14:42:02 +01006991 DCHECK(GetCompilerOptions().IsJitCompiler());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006992 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01006993 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006994 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006995 }
6996 return desired_string_load_kind;
6997}
6998
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006999void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007000 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007001 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007002 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007003 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007004 load_kind == HLoadString::LoadKind::kBootImageRelRo ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00007005 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007006 locations->SetInAt(0, Location::RequiresRegister());
7007 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007008 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007009 locations->SetOut(Location::RegisterLocation(EAX));
7010 } else {
7011 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007012 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7013 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007014 // Rely on the pResolveString to save everything.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01007015 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007016 } else {
7017 // For non-Baker read barrier we have a temp-clobbering call.
7018 }
7019 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007020 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007021}
7022
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007023Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01007024 dex::StringIndex string_index,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007025 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007026 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007027 // Add a patch entry and return the label.
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007028 jit_string_patches_.emplace_back(&dex_file, string_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007029 PatchInfo<Label>* info = &jit_string_patches_.back();
7030 return &info->label;
7031}
7032
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007033// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7034// move.
7035void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01007036 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007037 Location out_loc = locations->Out();
7038 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007039
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007040 switch (load->GetLoadKind()) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007041 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01007042 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
7043 codegen_->GetCompilerOptions().IsBootImageExtension());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007044 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007045 __ leal(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007046 codegen_->RecordBootImageStringPatch(load);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007047 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007048 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007049 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007050 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7051 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007052 __ movl(out, Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset));
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007053 codegen_->RecordBootImageRelRoPatch(load->InputAt(0)->AsX86ComputeBaseMethodAddress(),
7054 codegen_->GetBootImageOffset(load));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007055 return;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007056 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007057 case HLoadString::LoadKind::kBssEntry: {
7058 Register method_address = locations->InAt(0).AsRegister<Register>();
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007059 Address address = Address(method_address, CodeGeneratorX86::kPlaceholder32BitOffset);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007060 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007061 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007062 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markod5fd5c32019-07-02 14:46:32 +01007063 // No need for memory fence, thanks to the x86 memory model.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007064 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) LoadStringSlowPathX86(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007065 codegen_->AddSlowPath(slow_path);
7066 __ testl(out, out);
7067 __ j(kEqual, slow_path->GetEntryLabel());
7068 __ Bind(slow_path->GetExitLabel());
7069 return;
7070 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007071 case HLoadString::LoadKind::kJitBootImageAddress: {
7072 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
7073 DCHECK_NE(address, 0u);
7074 __ movl(out, Immediate(address));
7075 return;
7076 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007077 case HLoadString::LoadKind::kJitTableAddress: {
Vladimir Marko4ef451a2020-07-23 09:54:27 +00007078 Address address = Address::Absolute(CodeGeneratorX86::kPlaceholder32BitOffset);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007079 Label* fixup_label = codegen_->NewJitRootStringPatch(
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007080 load->GetDexFile(), load->GetStringIndex(), load->GetString());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007081 // /* GcRoot<mirror::String> */ out = *address
7082 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
7083 return;
7084 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007085 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007086 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007087 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007088
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007089 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007090 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007091 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007092 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07007093 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7094 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007095}
7096
David Brazdilcb1c0552015-08-04 16:22:25 +01007097static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007098 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01007099}
7100
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007101void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
7102 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007103 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007104 locations->SetOut(Location::RequiresRegister());
7105}
7106
7107void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01007108 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
7109}
7110
7111void LocationsBuilderX86::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007112 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
David Brazdilcb1c0552015-08-04 16:22:25 +01007113}
7114
7115void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7116 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007117}
7118
7119void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007120 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7121 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007122 InvokeRuntimeCallingConvention calling_convention;
7123 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7124}
7125
7126void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007127 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007128 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007129}
7130
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007131// Temp is used for read barrier.
7132static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7133 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00007134 !kUseBakerReadBarrier &&
7135 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00007136 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007137 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7138 return 1;
7139 }
7140 return 0;
7141}
7142
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007143// Interface case has 2 temps, one for holding the number of interfaces, one for the current
7144// interface pointer, the current interface is compared in memory.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007145// The other checks have one temp for loading the object's class.
7146static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007147 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007148 return 2;
7149 }
7150 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007151}
7152
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007153void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007154 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007155 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01007156 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007157 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007158 case TypeCheckKind::kExactCheck:
7159 case TypeCheckKind::kAbstractClassCheck:
7160 case TypeCheckKind::kClassHierarchyCheck:
Vladimir Marko87584542017-12-12 17:47:52 +00007161 case TypeCheckKind::kArrayObjectCheck: {
7162 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7163 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7164 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007165 break;
Vladimir Marko87584542017-12-12 17:47:52 +00007166 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007167 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007168 case TypeCheckKind::kUnresolvedCheck:
7169 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007170 call_kind = LocationSummary::kCallOnSlowPath;
7171 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007172 case TypeCheckKind::kBitstringCheck:
7173 break;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007174 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007175
Vladimir Markoca6fff82017-10-03 14:49:14 +01007176 LocationSummary* locations =
7177 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01007178 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007179 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007180 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007181 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007182 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7183 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7184 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7185 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7186 } else {
7187 locations->SetInAt(1, Location::Any());
7188 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007189 // Note that TypeCheckSlowPathX86 uses this "out" register too.
7190 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007191 // When read barriers are enabled, we need a temporary register for some cases.
7192 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007193}
7194
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007195void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007196 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007197 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007198 Location obj_loc = locations->InAt(0);
7199 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007200 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007201 Location out_loc = locations->Out();
7202 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007203 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7204 DCHECK_LE(num_temps, 1u);
7205 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007206 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007207 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7208 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7209 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07007210 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007211 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007212
7213 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007214 // Avoid null check if we know obj is not null.
7215 if (instruction->MustDoNullCheck()) {
7216 __ testl(obj, obj);
7217 __ j(kEqual, &zero);
7218 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007219
Roland Levillain7c1559a2015-12-15 10:55:36 +00007220 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007221 case TypeCheckKind::kExactCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007222 ReadBarrierOption read_barrier_option =
7223 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007224 // /* HeapReference<Class> */ out = obj->klass_
7225 GenerateReferenceLoadTwoRegisters(instruction,
7226 out_loc,
7227 obj_loc,
7228 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007229 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007230 if (cls.IsRegister()) {
7231 __ cmpl(out, cls.AsRegister<Register>());
7232 } else {
7233 DCHECK(cls.IsStackSlot()) << cls;
7234 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7235 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007236
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007237 // Classes must be equal for the instanceof to succeed.
7238 __ j(kNotEqual, &zero);
7239 __ movl(out, Immediate(1));
7240 __ jmp(&done);
7241 break;
7242 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007243
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007244 case TypeCheckKind::kAbstractClassCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007245 ReadBarrierOption read_barrier_option =
7246 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007247 // /* HeapReference<Class> */ out = obj->klass_
7248 GenerateReferenceLoadTwoRegisters(instruction,
7249 out_loc,
7250 obj_loc,
7251 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007252 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007253 // If the class is abstract, we eagerly fetch the super class of the
7254 // object to avoid doing a comparison we know will fail.
7255 NearLabel loop;
7256 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007257 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007258 GenerateReferenceLoadOneRegister(instruction,
7259 out_loc,
7260 super_offset,
7261 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007262 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007263 __ testl(out, out);
7264 // If `out` is null, we use it for the result, and jump to `done`.
7265 __ j(kEqual, &done);
7266 if (cls.IsRegister()) {
7267 __ cmpl(out, cls.AsRegister<Register>());
7268 } else {
7269 DCHECK(cls.IsStackSlot()) << cls;
7270 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7271 }
7272 __ j(kNotEqual, &loop);
7273 __ movl(out, Immediate(1));
7274 if (zero.IsLinked()) {
7275 __ jmp(&done);
7276 }
7277 break;
7278 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007279
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007280 case TypeCheckKind::kClassHierarchyCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007281 ReadBarrierOption read_barrier_option =
7282 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007283 // /* HeapReference<Class> */ out = obj->klass_
7284 GenerateReferenceLoadTwoRegisters(instruction,
7285 out_loc,
7286 obj_loc,
7287 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007288 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007289 // Walk over the class hierarchy to find a match.
7290 NearLabel loop, success;
7291 __ Bind(&loop);
7292 if (cls.IsRegister()) {
7293 __ cmpl(out, cls.AsRegister<Register>());
7294 } else {
7295 DCHECK(cls.IsStackSlot()) << cls;
7296 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7297 }
7298 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007299 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007300 GenerateReferenceLoadOneRegister(instruction,
7301 out_loc,
7302 super_offset,
7303 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007304 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007305 __ testl(out, out);
7306 __ j(kNotEqual, &loop);
7307 // If `out` is null, we use it for the result, and jump to `done`.
7308 __ jmp(&done);
7309 __ Bind(&success);
7310 __ movl(out, Immediate(1));
7311 if (zero.IsLinked()) {
7312 __ jmp(&done);
7313 }
7314 break;
7315 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007316
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007317 case TypeCheckKind::kArrayObjectCheck: {
Vladimir Marko87584542017-12-12 17:47:52 +00007318 ReadBarrierOption read_barrier_option =
7319 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007320 // /* HeapReference<Class> */ out = obj->klass_
7321 GenerateReferenceLoadTwoRegisters(instruction,
7322 out_loc,
7323 obj_loc,
7324 class_offset,
Vladimir Marko87584542017-12-12 17:47:52 +00007325 read_barrier_option);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007326 // Do an exact check.
7327 NearLabel exact_check;
7328 if (cls.IsRegister()) {
7329 __ cmpl(out, cls.AsRegister<Register>());
7330 } else {
7331 DCHECK(cls.IsStackSlot()) << cls;
7332 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7333 }
7334 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007335 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007336 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007337 GenerateReferenceLoadOneRegister(instruction,
7338 out_loc,
7339 component_offset,
7340 maybe_temp_loc,
Vladimir Marko87584542017-12-12 17:47:52 +00007341 read_barrier_option);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007342 __ testl(out, out);
7343 // If `out` is null, we use it for the result, and jump to `done`.
7344 __ j(kEqual, &done);
7345 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
7346 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007347 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007348 __ movl(out, Immediate(1));
7349 __ jmp(&done);
7350 break;
7351 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007352
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007353 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007354 // No read barrier since the slow path will retry upon failure.
7355 // /* HeapReference<Class> */ out = obj->klass_
7356 GenerateReferenceLoadTwoRegisters(instruction,
7357 out_loc,
7358 obj_loc,
7359 class_offset,
7360 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007361 if (cls.IsRegister()) {
7362 __ cmpl(out, cls.AsRegister<Register>());
7363 } else {
7364 DCHECK(cls.IsStackSlot()) << cls;
7365 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
7366 }
7367 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007368 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007369 instruction, /* is_fatal= */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007370 codegen_->AddSlowPath(slow_path);
7371 __ j(kNotEqual, slow_path->GetEntryLabel());
7372 __ movl(out, Immediate(1));
7373 if (zero.IsLinked()) {
7374 __ jmp(&done);
7375 }
7376 break;
7377 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007378
Calin Juravle98893e12015-10-02 21:05:03 +01007379 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00007380 case TypeCheckKind::kInterfaceCheck: {
7381 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007382 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00007383 // cases.
7384 //
7385 // We cannot directly call the InstanceofNonTrivial runtime
7386 // entry point without resorting to a type checking slow path
7387 // here (i.e. by calling InvokeRuntime directly), as it would
7388 // require to assign fixed registers for the inputs of this
7389 // HInstanceOf instruction (following the runtime calling
7390 // convention), which might be cluttered by the potential first
7391 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007392 //
7393 // TODO: Introduce a new runtime entry point taking the object
7394 // to test (instead of its class) as argument, and let it deal
7395 // with the read barrier issues. This will let us refactor this
7396 // case of the `switch` code as it was previously (with a direct
7397 // call to the runtime not using a type checking slow path).
7398 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007399 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007400 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007401 instruction, /* is_fatal= */ false);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007402 codegen_->AddSlowPath(slow_path);
7403 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007404 if (zero.IsLinked()) {
7405 __ jmp(&done);
7406 }
7407 break;
7408 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007409
7410 case TypeCheckKind::kBitstringCheck: {
7411 // /* HeapReference<Class> */ temp = obj->klass_
7412 GenerateReferenceLoadTwoRegisters(instruction,
7413 out_loc,
7414 obj_loc,
7415 class_offset,
7416 kWithoutReadBarrier);
7417
7418 GenerateBitstringTypeCheckCompare(instruction, out);
7419 __ j(kNotEqual, &zero);
7420 __ movl(out, Immediate(1));
7421 __ jmp(&done);
7422 break;
7423 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007424 }
7425
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007426 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007427 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007428 __ xorl(out, out);
7429 }
7430
7431 if (done.IsLinked()) {
7432 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007433 }
7434
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007435 if (slow_path != nullptr) {
7436 __ Bind(slow_path->GetExitLabel());
7437 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007438}
7439
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007440void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007441 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko87584542017-12-12 17:47:52 +00007442 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007443 LocationSummary* locations =
7444 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007445 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007446 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7447 // Require a register for the interface check since there is a loop that compares the class to
7448 // a memory address.
7449 locations->SetInAt(1, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007450 } else if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7451 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7452 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7453 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007454 } else {
7455 locations->SetInAt(1, Location::Any());
7456 }
Vladimir Marko9f8d3122018-04-06 13:47:59 +01007457 // Add temps for read barriers and other uses. One is used by TypeCheckSlowPathX86.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007458 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
7459}
7460
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007461void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007462 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007463 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00007464 Location obj_loc = locations->InAt(0);
7465 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007466 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007467 Location temp_loc = locations->GetTemp(0);
7468 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007469 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7470 DCHECK_GE(num_temps, 1u);
7471 DCHECK_LE(num_temps, 2u);
7472 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7473 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7474 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7475 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7476 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7477 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7478 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7479 const uint32_t object_array_data_offset =
7480 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007481
Vladimir Marko87584542017-12-12 17:47:52 +00007482 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007483 SlowPathCode* type_check_slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007484 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathX86(
7485 instruction, is_type_check_slow_path_fatal);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007486 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007487
Roland Levillain0d5a2812015-11-13 10:07:31 +00007488 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007489 // Avoid null check if we know obj is not null.
7490 if (instruction->MustDoNullCheck()) {
7491 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007492 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007493 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007494
Roland Levillain0d5a2812015-11-13 10:07:31 +00007495 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007496 case TypeCheckKind::kExactCheck:
7497 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007498 // /* HeapReference<Class> */ temp = obj->klass_
7499 GenerateReferenceLoadTwoRegisters(instruction,
7500 temp_loc,
7501 obj_loc,
7502 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007503 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007504
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007505 if (cls.IsRegister()) {
7506 __ cmpl(temp, cls.AsRegister<Register>());
7507 } else {
7508 DCHECK(cls.IsStackSlot()) << cls;
7509 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7510 }
7511 // Jump to slow path for throwing the exception or doing a
7512 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007513 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007514 break;
7515 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007516
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007517 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007518 // /* HeapReference<Class> */ temp = obj->klass_
7519 GenerateReferenceLoadTwoRegisters(instruction,
7520 temp_loc,
7521 obj_loc,
7522 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007523 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007524
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007525 // If the class is abstract, we eagerly fetch the super class of the
7526 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007527 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007528 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007529 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007530 GenerateReferenceLoadOneRegister(instruction,
7531 temp_loc,
7532 super_offset,
7533 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007534 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007535
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007536 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7537 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007538 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007539 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007540
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007541 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007542 if (cls.IsRegister()) {
7543 __ cmpl(temp, cls.AsRegister<Register>());
7544 } else {
7545 DCHECK(cls.IsStackSlot()) << cls;
7546 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7547 }
7548 __ j(kNotEqual, &loop);
7549 break;
7550 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007551
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007552 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007553 // /* HeapReference<Class> */ temp = obj->klass_
7554 GenerateReferenceLoadTwoRegisters(instruction,
7555 temp_loc,
7556 obj_loc,
7557 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007558 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007559
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007560 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007561 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007562 __ Bind(&loop);
7563 if (cls.IsRegister()) {
7564 __ cmpl(temp, cls.AsRegister<Register>());
7565 } else {
7566 DCHECK(cls.IsStackSlot()) << cls;
7567 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7568 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007569 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007570
Roland Levillain0d5a2812015-11-13 10:07:31 +00007571 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007572 GenerateReferenceLoadOneRegister(instruction,
7573 temp_loc,
7574 super_offset,
7575 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007576 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007577
7578 // If the class reference currently in `temp` is not null, jump
7579 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007580 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007581 __ j(kNotZero, &loop);
7582 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00007583 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007584 break;
7585 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007586
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007587 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007588 // /* HeapReference<Class> */ temp = obj->klass_
7589 GenerateReferenceLoadTwoRegisters(instruction,
7590 temp_loc,
7591 obj_loc,
7592 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007593 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007594
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007595 // Do an exact check.
7596 if (cls.IsRegister()) {
7597 __ cmpl(temp, cls.AsRegister<Register>());
7598 } else {
7599 DCHECK(cls.IsStackSlot()) << cls;
7600 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
7601 }
7602 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007603
7604 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007605 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007606 GenerateReferenceLoadOneRegister(instruction,
7607 temp_loc,
7608 component_offset,
7609 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007610 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007611
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007612 // If the component type is null (i.e. the object not an array), jump to the slow path to
7613 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007614 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007615 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00007616
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007617 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007618 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007619 break;
7620 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00007621
Calin Juravle98893e12015-10-02 21:05:03 +01007622 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007623 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00007624 // We cannot directly call the CheckCast runtime entry point
7625 // without resorting to a type checking slow path here (i.e. by
7626 // calling InvokeRuntime directly), as it would require to
7627 // assign fixed registers for the inputs of this HInstanceOf
7628 // instruction (following the runtime calling convention), which
7629 // might be cluttered by the potential first read barrier
7630 // emission at the beginning of this method.
7631 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007632 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007633
7634 case TypeCheckKind::kInterfaceCheck: {
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007635 // Fast path for the interface check. Try to avoid read barriers to improve the fast path.
7636 // We can not get false positives by doing this.
7637 // /* HeapReference<Class> */ temp = obj->klass_
7638 GenerateReferenceLoadTwoRegisters(instruction,
7639 temp_loc,
7640 obj_loc,
7641 class_offset,
7642 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007643
Vladimir Markoe619f6c2017-12-12 16:00:01 +00007644 // /* HeapReference<Class> */ temp = temp->iftable_
7645 GenerateReferenceLoadTwoRegisters(instruction,
7646 temp_loc,
7647 temp_loc,
7648 iftable_offset,
7649 kWithoutReadBarrier);
7650 // Iftable is never null.
7651 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
7652 // Maybe poison the `cls` for direct comparison with memory.
7653 __ MaybePoisonHeapReference(cls.AsRegister<Register>());
7654 // Loop through the iftable and check if any class matches.
7655 NearLabel start_loop;
7656 __ Bind(&start_loop);
7657 // Need to subtract first to handle the empty array case.
7658 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
7659 __ j(kNegative, type_check_slow_path->GetEntryLabel());
7660 // Go to next interface if the classes do not match.
7661 __ cmpl(cls.AsRegister<Register>(),
7662 CodeGeneratorX86::ArrayAddress(temp,
7663 maybe_temp2_loc,
7664 TIMES_4,
7665 object_array_data_offset));
7666 __ j(kNotEqual, &start_loop);
7667 // If `cls` was poisoned above, unpoison it.
7668 __ MaybeUnpoisonHeapReference(cls.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007669 break;
7670 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007671
7672 case TypeCheckKind::kBitstringCheck: {
7673 // /* HeapReference<Class> */ temp = obj->klass_
7674 GenerateReferenceLoadTwoRegisters(instruction,
7675 temp_loc,
7676 obj_loc,
7677 class_offset,
7678 kWithoutReadBarrier);
7679
7680 GenerateBitstringTypeCheckCompare(instruction, temp);
7681 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
7682 break;
7683 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007684 }
7685 __ Bind(&done);
7686
Roland Levillain0d5a2812015-11-13 10:07:31 +00007687 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007688}
7689
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007690void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007691 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
7692 instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007693 InvokeRuntimeCallingConvention calling_convention;
7694 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7695}
7696
7697void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01007698 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
7699 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01007700 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01007701 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007702 if (instruction->IsEnter()) {
7703 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7704 } else {
7705 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7706 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007707}
7708
Shalini Salomi Bodapatidd121f62018-10-26 15:03:53 +05307709void LocationsBuilderX86::VisitX86AndNot(HX86AndNot* instruction) {
7710 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7711 DCHECK(DataType::IsIntOrLongType(instruction->GetType())) << instruction->GetType();
7712 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7713 locations->SetInAt(0, Location::RequiresRegister());
7714 locations->SetInAt(1, Location::RequiresRegister());
7715 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7716}
7717
7718void InstructionCodeGeneratorX86::VisitX86AndNot(HX86AndNot* instruction) {
7719 LocationSummary* locations = instruction->GetLocations();
7720 Location first = locations->InAt(0);
7721 Location second = locations->InAt(1);
7722 Location dest = locations->Out();
7723 if (instruction->GetResultType() == DataType::Type::kInt32) {
7724 __ andn(dest.AsRegister<Register>(),
7725 first.AsRegister<Register>(),
7726 second.AsRegister<Register>());
7727 } else {
7728 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
7729 __ andn(dest.AsRegisterPairLow<Register>(),
7730 first.AsRegisterPairLow<Register>(),
7731 second.AsRegisterPairLow<Register>());
7732 __ andn(dest.AsRegisterPairHigh<Register>(),
7733 first.AsRegisterPairHigh<Register>(),
7734 second.AsRegisterPairHigh<Register>());
7735 }
7736}
7737
7738void LocationsBuilderX86::VisitX86MaskOrResetLeastSetBit(HX86MaskOrResetLeastSetBit* instruction) {
7739 DCHECK(codegen_->GetInstructionSetFeatures().HasAVX2());
7740 DCHECK(instruction->GetType() == DataType::Type::kInt32) << instruction->GetType();
7741 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
7742 locations->SetInAt(0, Location::RequiresRegister());
7743 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7744}
7745
7746void InstructionCodeGeneratorX86::VisitX86MaskOrResetLeastSetBit(
7747 HX86MaskOrResetLeastSetBit* instruction) {
7748 LocationSummary* locations = instruction->GetLocations();
7749 Location src = locations->InAt(0);
7750 Location dest = locations->Out();
7751 DCHECK(instruction->GetResultType() == DataType::Type::kInt32);
7752 switch (instruction->GetOpKind()) {
7753 case HInstruction::kAnd:
7754 __ blsr(dest.AsRegister<Register>(), src.AsRegister<Register>());
7755 break;
7756 case HInstruction::kXor:
7757 __ blsmsk(dest.AsRegister<Register>(), src.AsRegister<Register>());
7758 break;
7759 default:
7760 LOG(FATAL) << "Unreachable";
7761 }
7762}
7763
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007764void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
7765void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
7766void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
7767
7768void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7769 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007770 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007771 DCHECK(instruction->GetResultType() == DataType::Type::kInt32
7772 || instruction->GetResultType() == DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007773 locations->SetInAt(0, Location::RequiresRegister());
7774 locations->SetInAt(1, Location::Any());
7775 locations->SetOut(Location::SameAsFirstInput());
7776}
7777
7778void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
7779 HandleBitwiseOperation(instruction);
7780}
7781
7782void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
7783 HandleBitwiseOperation(instruction);
7784}
7785
7786void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
7787 HandleBitwiseOperation(instruction);
7788}
7789
7790void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
7791 LocationSummary* locations = instruction->GetLocations();
7792 Location first = locations->InAt(0);
7793 Location second = locations->InAt(1);
7794 DCHECK(first.Equals(locations->Out()));
7795
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007796 if (instruction->GetResultType() == DataType::Type::kInt32) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007797 if (second.IsRegister()) {
7798 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007799 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007800 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007801 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007802 } else {
7803 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007804 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007805 }
7806 } else if (second.IsConstant()) {
7807 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007808 __ andl(first.AsRegister<Register>(),
7809 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007810 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00007811 __ orl(first.AsRegister<Register>(),
7812 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007813 } else {
7814 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00007815 __ xorl(first.AsRegister<Register>(),
7816 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007817 }
7818 } else {
7819 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007820 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007821 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007822 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007823 } else {
7824 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00007825 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007826 }
7827 }
7828 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01007829 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007830 if (second.IsRegisterPair()) {
7831 if (instruction->IsAnd()) {
7832 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7833 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7834 } else if (instruction->IsOr()) {
7835 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7836 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7837 } else {
7838 DCHECK(instruction->IsXor());
7839 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7840 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7841 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007842 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007843 if (instruction->IsAnd()) {
7844 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7845 __ andl(first.AsRegisterPairHigh<Register>(),
7846 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7847 } else if (instruction->IsOr()) {
7848 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7849 __ orl(first.AsRegisterPairHigh<Register>(),
7850 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7851 } else {
7852 DCHECK(instruction->IsXor());
7853 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7854 __ xorl(first.AsRegisterPairHigh<Register>(),
7855 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7856 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007857 } else {
7858 DCHECK(second.IsConstant()) << second;
7859 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007860 int32_t low_value = Low32Bits(value);
7861 int32_t high_value = High32Bits(value);
7862 Immediate low(low_value);
7863 Immediate high(high_value);
7864 Register first_low = first.AsRegisterPairLow<Register>();
7865 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007866 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007867 if (low_value == 0) {
7868 __ xorl(first_low, first_low);
7869 } else if (low_value != -1) {
7870 __ andl(first_low, low);
7871 }
7872 if (high_value == 0) {
7873 __ xorl(first_high, first_high);
7874 } else if (high_value != -1) {
7875 __ andl(first_high, high);
7876 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007877 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007878 if (low_value != 0) {
7879 __ orl(first_low, low);
7880 }
7881 if (high_value != 0) {
7882 __ orl(first_high, high);
7883 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007884 } else {
7885 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007886 if (low_value != 0) {
7887 __ xorl(first_low, low);
7888 }
7889 if (high_value != 0) {
7890 __ xorl(first_high, high);
7891 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007892 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007893 }
7894 }
7895}
7896
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007897void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7898 HInstruction* instruction,
7899 Location out,
7900 uint32_t offset,
7901 Location maybe_temp,
7902 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007903 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007904 if (read_barrier_option == kWithReadBarrier) {
7905 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007906 if (kUseBakerReadBarrier) {
7907 // Load with fast path based Baker's read barrier.
7908 // /* HeapReference<Object> */ out = *(out + offset)
7909 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08007910 instruction, out, out_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007911 } else {
7912 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007913 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007914 // in the following move operation, as we will need it for the
7915 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007916 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007917 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007918 // /* HeapReference<Object> */ out = *(out + offset)
7919 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007920 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007921 }
7922 } else {
7923 // Plain load with no read barrier.
7924 // /* HeapReference<Object> */ out = *(out + offset)
7925 __ movl(out_reg, Address(out_reg, offset));
7926 __ MaybeUnpoisonHeapReference(out_reg);
7927 }
7928}
7929
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007930void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7931 HInstruction* instruction,
7932 Location out,
7933 Location obj,
7934 uint32_t offset,
7935 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007936 Register out_reg = out.AsRegister<Register>();
7937 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007938 if (read_barrier_option == kWithReadBarrier) {
7939 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007940 if (kUseBakerReadBarrier) {
7941 // Load with fast path based Baker's read barrier.
7942 // /* HeapReference<Object> */ out = *(obj + offset)
7943 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Andreas Gampe3db70682018-12-26 15:12:03 -08007944 instruction, out, obj_reg, offset, /* needs_null_check= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007945 } else {
7946 // Load with slow path based read barrier.
7947 // /* HeapReference<Object> */ out = *(obj + offset)
7948 __ movl(out_reg, Address(obj_reg, offset));
7949 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7950 }
7951 } else {
7952 // Plain load with no read barrier.
7953 // /* HeapReference<Object> */ out = *(obj + offset)
7954 __ movl(out_reg, Address(obj_reg, offset));
7955 __ MaybeUnpoisonHeapReference(out_reg);
7956 }
7957}
7958
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007959void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7960 HInstruction* instruction,
7961 Location root,
7962 const Address& address,
7963 Label* fixup_label,
7964 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007965 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007966 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007967 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007968 if (kUseBakerReadBarrier) {
7969 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7970 // Baker's read barrier are used:
7971 //
Roland Levillaind966ce72017-02-09 16:20:14 +00007972 // root = obj.field;
7973 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7974 // if (temp != null) {
7975 // root = temp(root)
Roland Levillain7c1559a2015-12-15 10:55:36 +00007976 // }
7977
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007978 // /* GcRoot<mirror::Object> */ root = *address
7979 __ movl(root_reg, address);
7980 if (fixup_label != nullptr) {
7981 __ Bind(fixup_label);
7982 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007983 static_assert(
7984 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7985 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7986 "have different sizes.");
7987 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7988 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7989 "have different sizes.");
7990
Vladimir Marko953437b2016-08-24 08:30:46 +00007991 // Slow path marking the GC root `root`.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007992 SlowPathCode* slow_path = new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08007993 instruction, root, /* unpoison_ref_before_marking= */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007994 codegen_->AddSlowPath(slow_path);
7995
Roland Levillaind966ce72017-02-09 16:20:14 +00007996 // Test the entrypoint (`Thread::Current()->pReadBarrierMarkReg ## root.reg()`).
7997 const int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +01007998 Thread::ReadBarrierMarkEntryPointsOffset<kX86PointerSize>(root.reg());
Roland Levillaind966ce72017-02-09 16:20:14 +00007999 __ fs()->cmpl(Address::Absolute(entry_point_offset), Immediate(0));
8000 // The entrypoint is null when the GC is not marking.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008001 __ j(kNotEqual, slow_path->GetEntryLabel());
8002 __ Bind(slow_path->GetExitLabel());
8003 } else {
8004 // GC root loaded through a slow path for read barriers other
8005 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008006 // /* GcRoot<mirror::Object>* */ root = address
8007 __ leal(root_reg, address);
8008 if (fixup_label != nullptr) {
8009 __ Bind(fixup_label);
8010 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008011 // /* mirror::Object* */ root = root->Read()
8012 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8013 }
8014 } else {
8015 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008016 // /* GcRoot<mirror::Object> */ root = *address
8017 __ movl(root_reg, address);
8018 if (fixup_label != nullptr) {
8019 __ Bind(fixup_label);
8020 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00008021 // Note that GC roots are not affected by heap poisoning, thus we
8022 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008023 }
8024}
8025
8026void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8027 Location ref,
8028 Register obj,
8029 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00008030 bool needs_null_check) {
8031 DCHECK(kEmitCompilerReadBarrier);
8032 DCHECK(kUseBakerReadBarrier);
8033
8034 // /* HeapReference<Object> */ ref = *(obj + offset)
8035 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00008036 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008037}
8038
8039void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8040 Location ref,
8041 Register obj,
8042 uint32_t data_offset,
8043 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00008044 bool needs_null_check) {
8045 DCHECK(kEmitCompilerReadBarrier);
8046 DCHECK(kUseBakerReadBarrier);
8047
Roland Levillain3d312422016-06-23 13:53:42 +01008048 static_assert(
8049 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8050 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00008051 // /* HeapReference<Object> */ ref =
8052 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008053 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00008054 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008055}
8056
8057void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8058 Location ref,
8059 Register obj,
8060 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008061 bool needs_null_check,
8062 bool always_update_field,
8063 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008064 DCHECK(kEmitCompilerReadBarrier);
8065 DCHECK(kUseBakerReadBarrier);
8066
8067 // In slow path based read barriers, the read barrier call is
8068 // inserted after the original load. However, in fast path based
8069 // Baker's read barriers, we need to perform the load of
8070 // mirror::Object::monitor_ *before* the original reference load.
8071 // This load-load ordering is required by the read barrier.
8072 // The fast path/slow path (for Baker's algorithm) should look like:
8073 //
8074 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8075 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8076 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008077 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00008078 // if (is_gray) {
8079 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
8080 // }
8081 //
8082 // Note: the original implementation in ReadBarrier::Barrier is
8083 // slightly more complex as:
8084 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00008085 // the high-bits of rb_state, which are expected to be all zeroes
8086 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
8087 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00008088 // - it performs additional checks that we do not do here for
8089 // performance reasons.
8090
8091 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00008092 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
8093
Vladimir Marko953437b2016-08-24 08:30:46 +00008094 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Roland Levillain14e5a292018-06-28 12:00:56 +01008095 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008096 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00008097 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
8098 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
8099 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
8100
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07008101 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00008102 // ref = ReadBarrier::Mark(ref);
8103 // At this point, just do the "if" and make sure that flags are preserved until the branch.
8104 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00008105 if (needs_null_check) {
8106 MaybeRecordImplicitNullCheck(instruction);
8107 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00008108
8109 // Load fence to prevent load-load reordering.
8110 // Note that this is a no-op, thanks to the x86 memory model.
8111 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
8112
8113 // The actual reference load.
8114 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00008115 __ movl(ref_reg, src); // Flags are unaffected.
8116
8117 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
8118 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008119 SlowPathCode* slow_path;
8120 if (always_update_field) {
8121 DCHECK(temp != nullptr);
Vladimir Marko174b2e22017-10-12 13:34:49 +01008122 slow_path = new (GetScopedAllocator()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008123 instruction, ref, obj, src, /* unpoison_ref_before_marking= */ true, *temp);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008124 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008125 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathX86(
Andreas Gampe3db70682018-12-26 15:12:03 -08008126 instruction, ref, /* unpoison_ref_before_marking= */ true);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008127 }
Vladimir Marko953437b2016-08-24 08:30:46 +00008128 AddSlowPath(slow_path);
8129
8130 // We have done the "if" of the gray bit check above, now branch based on the flags.
8131 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00008132
8133 // Object* ref = ref_addr->AsMirrorPtr()
8134 __ MaybeUnpoisonHeapReference(ref_reg);
8135
Roland Levillain7c1559a2015-12-15 10:55:36 +00008136 __ Bind(slow_path->GetExitLabel());
8137}
8138
8139void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
8140 Location out,
8141 Location ref,
8142 Location obj,
8143 uint32_t offset,
8144 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008145 DCHECK(kEmitCompilerReadBarrier);
8146
Roland Levillain7c1559a2015-12-15 10:55:36 +00008147 // Insert a slow path based read barrier *after* the reference load.
8148 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008149 // If heap poisoning is enabled, the unpoisoning of the loaded
8150 // reference will be carried out by the runtime within the slow
8151 // path.
8152 //
8153 // Note that `ref` currently does not get unpoisoned (when heap
8154 // poisoning is enabled), which is alright as the `ref` argument is
8155 // not used by the artReadBarrierSlow entry point.
8156 //
8157 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01008158 SlowPathCode* slow_path = new (GetScopedAllocator())
Roland Levillain0d5a2812015-11-13 10:07:31 +00008159 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
8160 AddSlowPath(slow_path);
8161
Roland Levillain0d5a2812015-11-13 10:07:31 +00008162 __ jmp(slow_path->GetEntryLabel());
8163 __ Bind(slow_path->GetExitLabel());
8164}
8165
Roland Levillain7c1559a2015-12-15 10:55:36 +00008166void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
8167 Location out,
8168 Location ref,
8169 Location obj,
8170 uint32_t offset,
8171 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008172 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00008173 // Baker's read barriers shall be handled by the fast path
8174 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
8175 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008176 // If heap poisoning is enabled, unpoisoning will be taken care of
8177 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00008178 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008179 } else if (kPoisonHeapReferences) {
8180 __ UnpoisonHeapReference(out.AsRegister<Register>());
8181 }
8182}
8183
Roland Levillain7c1559a2015-12-15 10:55:36 +00008184void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8185 Location out,
8186 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00008187 DCHECK(kEmitCompilerReadBarrier);
8188
Roland Levillain7c1559a2015-12-15 10:55:36 +00008189 // Insert a slow path based read barrier *after* the GC root load.
8190 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00008191 // Note that GC roots are not affected by heap poisoning, so we do
8192 // not need to do anything special for this here.
8193 SlowPathCode* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01008194 new (GetScopedAllocator()) ReadBarrierForRootSlowPathX86(instruction, out, root);
Roland Levillain0d5a2812015-11-13 10:07:31 +00008195 AddSlowPath(slow_path);
8196
Roland Levillain0d5a2812015-11-13 10:07:31 +00008197 __ jmp(slow_path->GetEntryLabel());
8198 __ Bind(slow_path->GetExitLabel());
8199}
8200
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008201void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008202 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008203 LOG(FATAL) << "Unreachable";
8204}
8205
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008206void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008207 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008208 LOG(FATAL) << "Unreachable";
8209}
8210
Mark Mendellfe57faa2015-09-18 09:26:15 -04008211// Simple implementation of packed switch - generate cascaded compare/jumps.
8212void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8213 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008214 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendellfe57faa2015-09-18 09:26:15 -04008215 locations->SetInAt(0, Location::RequiresRegister());
8216}
8217
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008218void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
8219 int32_t lower_bound,
8220 uint32_t num_entries,
8221 HBasicBlock* switch_block,
8222 HBasicBlock* default_block) {
8223 // Figure out the correct compare values and jump conditions.
8224 // Handle the first compare/branch as a special case because it might
8225 // jump to the default case.
8226 DCHECK_GT(num_entries, 2u);
8227 Condition first_condition;
8228 uint32_t index;
8229 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
8230 if (lower_bound != 0) {
8231 first_condition = kLess;
8232 __ cmpl(value_reg, Immediate(lower_bound));
8233 __ j(first_condition, codegen_->GetLabelOf(default_block));
8234 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008235
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008236 index = 1;
8237 } else {
8238 // Handle all the compare/jumps below.
8239 first_condition = kBelow;
8240 index = 0;
8241 }
8242
8243 // Handle the rest of the compare/jumps.
8244 for (; index + 1 < num_entries; index += 2) {
8245 int32_t compare_to_value = lower_bound + index + 1;
8246 __ cmpl(value_reg, Immediate(compare_to_value));
8247 // Jump to successors[index] if value < case_value[index].
8248 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
8249 // Jump to successors[index + 1] if value == case_value[index + 1].
8250 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
8251 }
8252
8253 if (index != num_entries) {
8254 // There are an odd number of entries. Handle the last one.
8255 DCHECK_EQ(index + 1, num_entries);
8256 __ cmpl(value_reg, Immediate(lower_bound + index));
8257 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008258 }
8259
8260 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008261 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
8262 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04008263 }
8264}
8265
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008266void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8267 int32_t lower_bound = switch_instr->GetStartValue();
8268 uint32_t num_entries = switch_instr->GetNumEntries();
8269 LocationSummary* locations = switch_instr->GetLocations();
8270 Register value_reg = locations->InAt(0).AsRegister<Register>();
8271
8272 GenPackedSwitchWithCompares(value_reg,
8273 lower_bound,
8274 num_entries,
8275 switch_instr->GetBlock(),
8276 switch_instr->GetDefaultBlock());
8277}
8278
Mark Mendell805b3b52015-09-18 14:10:29 -04008279void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8280 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008281 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Mark Mendell805b3b52015-09-18 14:10:29 -04008282 locations->SetInAt(0, Location::RequiresRegister());
8283
8284 // Constant area pointer.
8285 locations->SetInAt(1, Location::RequiresRegister());
8286
8287 // And the temporary we need.
8288 locations->AddTemp(Location::RequiresRegister());
8289}
8290
8291void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
8292 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008293 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04008294 LocationSummary* locations = switch_instr->GetLocations();
8295 Register value_reg = locations->InAt(0).AsRegister<Register>();
8296 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8297
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008298 if (num_entries <= kPackedSwitchJumpTableThreshold) {
8299 GenPackedSwitchWithCompares(value_reg,
8300 lower_bound,
8301 num_entries,
8302 switch_instr->GetBlock(),
8303 default_block);
8304 return;
8305 }
8306
Mark Mendell805b3b52015-09-18 14:10:29 -04008307 // Optimizing has a jump area.
8308 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
8309 Register constant_area = locations->InAt(1).AsRegister<Register>();
8310
8311 // Remove the bias, if needed.
8312 if (lower_bound != 0) {
8313 __ leal(temp_reg, Address(value_reg, -lower_bound));
8314 value_reg = temp_reg;
8315 }
8316
8317 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008318 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04008319 __ cmpl(value_reg, Immediate(num_entries - 1));
8320 __ j(kAbove, codegen_->GetLabelOf(default_block));
8321
8322 // We are in the range of the table.
8323 // Load (target-constant_area) from the jump table, indexing by the value.
8324 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
8325
8326 // Compute the actual target address by adding in constant_area.
8327 __ addl(temp_reg, constant_area);
8328
8329 // And jump.
8330 __ jmp(temp_reg);
8331}
8332
Mark Mendell0616ae02015-04-17 12:49:27 -04008333void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
8334 HX86ComputeBaseMethodAddress* insn) {
8335 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008336 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008337 locations->SetOut(Location::RequiresRegister());
8338}
8339
8340void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
8341 HX86ComputeBaseMethodAddress* insn) {
8342 LocationSummary* locations = insn->GetLocations();
8343 Register reg = locations->Out().AsRegister<Register>();
8344
8345 // Generate call to next instruction.
8346 Label next_instruction;
8347 __ call(&next_instruction);
8348 __ Bind(&next_instruction);
8349
8350 // Remember this offset for later use with constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008351 codegen_->AddMethodAddressOffset(insn, GetAssembler()->CodeSize());
Mark Mendell0616ae02015-04-17 12:49:27 -04008352
8353 // Grab the return address off the stack.
8354 __ popl(reg);
8355}
8356
8357void LocationsBuilderX86::VisitX86LoadFromConstantTable(
8358 HX86LoadFromConstantTable* insn) {
8359 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008360 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Mark Mendell0616ae02015-04-17 12:49:27 -04008361
8362 locations->SetInAt(0, Location::RequiresRegister());
8363 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
8364
8365 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00008366 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008367 return;
8368 }
8369
8370 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008371 case DataType::Type::kFloat32:
8372 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008373 locations->SetOut(Location::RequiresFpuRegister());
8374 break;
8375
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008376 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008377 locations->SetOut(Location::RequiresRegister());
8378 break;
8379
8380 default:
8381 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8382 }
8383}
8384
8385void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00008386 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04008387 return;
8388 }
8389
8390 LocationSummary* locations = insn->GetLocations();
8391 Location out = locations->Out();
8392 Register const_area = locations->InAt(0).AsRegister<Register>();
8393 HConstant *value = insn->GetConstant();
8394
8395 switch (insn->GetType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008396 case DataType::Type::kFloat32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008397 __ movss(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008398 codegen_->LiteralFloatAddress(
8399 value->AsFloatConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008400 break;
8401
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008402 case DataType::Type::kFloat64:
Mark Mendell0616ae02015-04-17 12:49:27 -04008403 __ movsd(out.AsFpuRegister<XmmRegister>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008404 codegen_->LiteralDoubleAddress(
8405 value->AsDoubleConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008406 break;
8407
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008408 case DataType::Type::kInt32:
Mark Mendell0616ae02015-04-17 12:49:27 -04008409 __ movl(out.AsRegister<Register>(),
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008410 codegen_->LiteralInt32Address(
8411 value->AsIntConstant()->GetValue(), insn->GetBaseMethodAddress(), const_area));
Mark Mendell0616ae02015-04-17 12:49:27 -04008412 break;
8413
8414 default:
8415 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
8416 }
8417}
8418
Mark Mendell0616ae02015-04-17 12:49:27 -04008419/**
8420 * Class to handle late fixup of offsets into constant area.
8421 */
Vladimir Marko5233f932015-09-29 19:01:15 +01008422class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04008423 public:
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008424 RIPFixup(CodeGeneratorX86& codegen,
8425 HX86ComputeBaseMethodAddress* base_method_address,
8426 size_t offset)
8427 : codegen_(&codegen),
8428 base_method_address_(base_method_address),
8429 offset_into_constant_area_(offset) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008430
8431 protected:
8432 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
8433
8434 CodeGeneratorX86* codegen_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008435 HX86ComputeBaseMethodAddress* base_method_address_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008436
8437 private:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +01008438 void Process(const MemoryRegion& region, int pos) override {
Mark Mendell0616ae02015-04-17 12:49:27 -04008439 // Patch the correct offset for the instruction. The place to patch is the
8440 // last 4 bytes of the instruction.
8441 // The value to patch is the distance from the offset in the constant area
8442 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04008443 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008444 int32_t relative_position =
8445 constant_offset - codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell0616ae02015-04-17 12:49:27 -04008446
8447 // Patch in the right value.
8448 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
8449 }
8450
Mark Mendell0616ae02015-04-17 12:49:27 -04008451 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04008452 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04008453};
8454
Mark Mendell805b3b52015-09-18 14:10:29 -04008455/**
8456 * Class to handle late fixup of offsets to a jump table that will be created in the
8457 * constant area.
8458 */
8459class JumpTableRIPFixup : public RIPFixup {
8460 public:
8461 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008462 : RIPFixup(codegen, switch_instr->GetBaseMethodAddress(), static_cast<size_t>(-1)),
8463 switch_instr_(switch_instr) {}
Mark Mendell805b3b52015-09-18 14:10:29 -04008464
8465 void CreateJumpTable() {
8466 X86Assembler* assembler = codegen_->GetAssembler();
8467
8468 // Ensure that the reference to the jump table has the correct offset.
8469 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
8470 SetOffset(offset_in_constant_table);
8471
8472 // The label values in the jump table are computed relative to the
8473 // instruction addressing the constant area.
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008474 const int32_t relative_offset = codegen_->GetMethodAddressOffset(base_method_address_);
Mark Mendell805b3b52015-09-18 14:10:29 -04008475
8476 // Populate the jump table with the correct values for the jump table.
8477 int32_t num_entries = switch_instr_->GetNumEntries();
8478 HBasicBlock* block = switch_instr_->GetBlock();
8479 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
8480 // The value that we want is the target offset - the position of the table.
8481 for (int32_t i = 0; i < num_entries; i++) {
8482 HBasicBlock* b = successors[i];
8483 Label* l = codegen_->GetLabelOf(b);
8484 DCHECK(l->IsBound());
8485 int32_t offset_to_block = l->Position() - relative_offset;
8486 assembler->AppendInt32(offset_to_block);
8487 }
8488 }
8489
8490 private:
8491 const HX86PackedSwitch* switch_instr_;
8492};
8493
8494void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
8495 // Generate the constant area if needed.
8496 X86Assembler* assembler = GetAssembler();
jaishank20d1c942019-03-08 15:08:17 +05308497
Mark Mendell805b3b52015-09-18 14:10:29 -04008498 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
8499 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
8500 // byte values.
8501 assembler->Align(4, 0);
8502 constant_area_start_ = assembler->CodeSize();
8503
8504 // Populate any jump tables.
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008505 for (JumpTableRIPFixup* jump_table : fixups_to_jump_tables_) {
Mark Mendell805b3b52015-09-18 14:10:29 -04008506 jump_table->CreateJumpTable();
8507 }
8508
8509 // And now add the constant area to the generated code.
8510 assembler->AddConstantArea();
8511 }
8512
8513 // And finish up.
8514 CodeGenerator::Finalize(allocator);
8515}
8516
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008517Address CodeGeneratorX86::LiteralDoubleAddress(double v,
8518 HX86ComputeBaseMethodAddress* method_base,
8519 Register reg) {
8520 AssemblerFixup* fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008521 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddDouble(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008522 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008523}
8524
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008525Address CodeGeneratorX86::LiteralFloatAddress(float v,
8526 HX86ComputeBaseMethodAddress* method_base,
8527 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008528 AssemblerFixup* fixup =
8529 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddFloat(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008530 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008531}
8532
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008533Address CodeGeneratorX86::LiteralInt32Address(int32_t v,
8534 HX86ComputeBaseMethodAddress* method_base,
8535 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008536 AssemblerFixup* fixup =
8537 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt32(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008538 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008539}
8540
Nicolas Geoffray133719e2017-01-22 15:44:39 +00008541Address CodeGeneratorX86::LiteralInt64Address(int64_t v,
8542 HX86ComputeBaseMethodAddress* method_base,
8543 Register reg) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008544 AssemblerFixup* fixup =
8545 new (GetGraph()->GetAllocator()) RIPFixup(*this, method_base, __ AddInt64(v));
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008546 return Address(reg, kPlaceholder32BitOffset, fixup);
Mark Mendell0616ae02015-04-17 12:49:27 -04008547}
8548
Aart Bika19616e2016-02-01 18:57:58 -08008549void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
8550 if (value == 0) {
8551 __ xorl(dest, dest);
8552 } else {
8553 __ movl(dest, Immediate(value));
8554 }
8555}
8556
8557void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
8558 if (value == 0) {
8559 __ testl(dest, dest);
8560 } else {
8561 __ cmpl(dest, Immediate(value));
8562 }
8563}
8564
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008565void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
8566 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07008567 GenerateIntCompare(lhs_reg, rhs);
8568}
8569
8570void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008571 if (rhs.IsConstant()) {
8572 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07008573 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008574 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07008575 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008576 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07008577 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01008578 }
8579}
8580
8581Address CodeGeneratorX86::ArrayAddress(Register obj,
8582 Location index,
8583 ScaleFactor scale,
8584 uint32_t data_offset) {
8585 return index.IsConstant() ?
8586 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
8587 Address(obj, index.AsRegister<Register>(), scale, data_offset);
8588}
8589
Mark Mendell805b3b52015-09-18 14:10:29 -04008590Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
8591 Register reg,
8592 Register value) {
8593 // Create a fixup to be used to create and address the jump table.
8594 JumpTableRIPFixup* table_fixup =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008595 new (GetGraph()->GetAllocator()) JumpTableRIPFixup(*this, switch_instr);
Mark Mendell805b3b52015-09-18 14:10:29 -04008596
8597 // We have to populate the jump tables.
8598 fixups_to_jump_tables_.push_back(table_fixup);
8599
8600 // We want a scaled address, as we are extracting the correct offset from the table.
Vladimir Marko4ef451a2020-07-23 09:54:27 +00008601 return Address(reg, value, TIMES_4, kPlaceholder32BitOffset, table_fixup);
Mark Mendell805b3b52015-09-18 14:10:29 -04008602}
8603
Andreas Gampe85b62f22015-09-09 13:15:38 -07008604// TODO: target as memory.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008605void CodeGeneratorX86::MoveFromReturnRegister(Location target, DataType::Type type) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07008606 if (!target.IsValid()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008607 DCHECK_EQ(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008608 return;
8609 }
8610
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008611 DCHECK_NE(type, DataType::Type::kVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008612
8613 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
8614 if (target.Equals(return_loc)) {
8615 return;
8616 }
8617
8618 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8619 // with the else branch.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008620 if (type == DataType::Type::kInt64) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008621 HParallelMove parallel_move(GetGraph()->GetAllocator());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008622 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), DataType::Type::kInt32, nullptr);
8623 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), DataType::Type::kInt32, nullptr);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008624 GetMoveResolver()->EmitNativeCode(&parallel_move);
8625 } else {
8626 // Let the parallel move resolver take care of all of this.
Vladimir Markoca6fff82017-10-03 14:49:14 +01008627 HParallelMove parallel_move(GetGraph()->GetAllocator());
Andreas Gampe85b62f22015-09-09 13:15:38 -07008628 parallel_move.AddMove(return_loc, target, type, nullptr);
8629 GetMoveResolver()->EmitNativeCode(&parallel_move);
8630 }
8631}
8632
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008633void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
8634 const uint8_t* roots_data,
8635 const PatchInfo<Label>& info,
8636 uint64_t index_in_table) const {
8637 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
8638 uintptr_t address =
8639 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
Andreas Gampec55bb392018-09-21 00:02:02 +00008640 using unaligned_uint32_t __attribute__((__aligned__(1))) = uint32_t;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008641 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
8642 dchecked_integral_cast<uint32_t>(address);
8643}
8644
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008645void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8646 for (const PatchInfo<Label>& info : jit_string_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008647 StringReference string_reference(info.target_dex_file, dex::StringIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008648 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008649 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008650 }
8651
8652 for (const PatchInfo<Label>& info : jit_class_patches_) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008653 TypeReference type_reference(info.target_dex_file, dex::TypeIndex(info.offset_or_index));
Vladimir Marko174b2e22017-10-12 13:34:49 +01008654 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01008655 PatchJitRootUse(code, roots_data, info, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008656 }
8657}
8658
xueliang.zhonge0eb4832017-10-30 13:43:14 +00008659void LocationsBuilderX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8660 ATTRIBUTE_UNUSED) {
8661 LOG(FATAL) << "Unreachable";
8662}
8663
8664void InstructionCodeGeneratorX86::VisitIntermediateAddress(HIntermediateAddress* instruction
8665 ATTRIBUTE_UNUSED) {
8666 LOG(FATAL) << "Unreachable";
8667}
8668
Shalini Salomi Bodapatib45a4352019-07-10 16:09:41 +05308669bool LocationsBuilderX86::CpuHasAvxFeatureFlag() {
8670 return codegen_->GetInstructionSetFeatures().HasAVX();
8671}
8672bool LocationsBuilderX86::CpuHasAvx2FeatureFlag() {
8673 return codegen_->GetInstructionSetFeatures().HasAVX2();
8674}
8675bool InstructionCodeGeneratorX86::CpuHasAvxFeatureFlag() {
8676 return codegen_->GetInstructionSetFeatures().HasAVX();
8677}
8678bool InstructionCodeGeneratorX86::CpuHasAvx2FeatureFlag() {
8679 return codegen_->GetInstructionSetFeatures().HasAVX2();
8680}
8681
Roland Levillain4d027112015-07-01 15:41:14 +01008682#undef __
8683
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008684} // namespace x86
8685} // namespace art